Skip to content

Commit

Permalink
pass resolveable extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Sep 5, 2024
1 parent 5b30cba commit bc8ae0f
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 111 deletions.
3 changes: 1 addition & 2 deletions packages/vite/src/esbuild-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Preprocessor } from 'content-tag';
const templateOnlyComponent =
`import templateOnly from '@ember/component/template-only';\n` + `export default templateOnly();\n`;

export function esBuildResolver(): EsBuildPlugin {
export function esBuildResolver(extensions: string[]): EsBuildPlugin {
let resolverLoader = new ResolverLoader(process.cwd());
let preprocessor = new Preprocessor();

Expand Down Expand Up @@ -95,7 +95,6 @@ export function esBuildResolver(): EsBuildPlugin {
}

// vite resolver does not take part when optimizing deps
const extensions = ['', '.hbs'];
for (const extension of extensions) {
let result = await build.resolve(path + extension, {
namespace,
Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/optimize-deps.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { DepOptimizationConfig } from 'vite';
import { esBuildResolver } from './esbuild-resolver';

export interface OptimizeDeps {
exclude?: string[];
[key: string]: unknown;
}

export function optimizeDeps(): OptimizeDeps {
export function optimizeDeps({ appExtensions }: DepOptimizationConfig & { appExtensions: string[] }): OptimizeDeps {
return {
exclude: ['@embroider/macros'],
extensions: ['.hbs', '.gjs', '.gts'],
esbuildOptions: {
plugins: [esBuildResolver()],
plugins: [esBuildResolver(appExtensions)],
},
};
}
36 changes: 17 additions & 19 deletions tests/addon-template/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ import { resolve } from "path";
import { babel } from "@rollup/plugin-babel";

const root = "tmp/rewritten-app";
const extensions = [
".mjs",
".gjs",
".js",
".mts",
".gts",
".ts",
".hbs",
".json",
];

export default defineConfig(({ mode }) => {
return {
root,
// esbuild in vite does not support decorators
esbuild: false,
cacheDir: resolve("node_modules", ".vite"),
resolve: {
extensions: [
".mjs",
".gjs",
".js",
".mts",
".gts",
".ts",
".hbs",
".json",
],
extensions
},
plugins: [
hbs(),
Expand All @@ -48,20 +47,19 @@ export default defineConfig(({ mode }) => {
// javascript but the javascript still also needs babel, but we don't want
// to rename them because vite isn't great about knowing how to hot-reload
// them if we resolve them to made-up names.
extensions: [".gjs", ".js", ".hbs", ".ts", ".gts"],
extensions,
}),
],
optimizeDeps: optimizeDeps(),
optimizeDeps: optimizeDeps({
appExtensions: extensions
}),
publicDir: resolve(process.cwd(), "public"),
server: {
port: 4200,
watch: {
ignored: ["!**/tmp/rewritten-app/**"],
ignored: ["!**/tmp/**"],
},
},
// If the "app" is a classic addon dummy app, the public directory is tests/dummy/public,
// any public directory at the root would rather contain the assets provided by the addon,
// which are managed by the assets plugin.
publicDir: resolve(process.cwd(), "tests/dummy/public"),
build: {
outDir: resolve(process.cwd(), "dist"),
rollupOptions: {
Expand Down
27 changes: 15 additions & 12 deletions tests/app-template/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ import { resolve } from "path";
import { babel } from "@rollup/plugin-babel";

const root = "tmp/rewritten-app";
const extensions = [
".mjs",
".gjs",
".js",
".mts",
".gts",
".ts",
".hbs",
".json",
];

export default defineConfig(({ mode }) => {
return {
root,
cacheDir: resolve("node_modules", ".vite"),
resolve: {
extensions: [
".mjs",
".gjs",
".js",
".mts",
".gts",
".ts",
".hbs",
".json",
],
extensions,
},
plugins: [
hbs(),
Expand All @@ -46,10 +47,12 @@ export default defineConfig(({ mode }) => {
// javascript but the javascript still also needs babel, but we don't want
// to rename them because vite isn't great about knowing how to hot-reload
// them if we resolve them to made-up names.
extensions: [".gjs", ".js", ".hbs", ".ts", ".gts"],
extensions,
}),
],
optimizeDeps: optimizeDeps(),
optimizeDeps: optimizeDeps({
appExtensions: extensions,
}),
publicDir: resolve(process.cwd(), "public"),
server: {
port: 4200,
Expand Down
84 changes: 46 additions & 38 deletions tests/ts-app-template-classic/vite.config.mjs
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
import { defineConfig } from 'vite';
import { resolver, hbs, scripts, templateTag, optimizeDeps, compatPrebuild, contentFor } from '@embroider/vite';
import { resolver, hbs, scripts, templateTag, optimizeDeps, compatPrebuild, assets, contentFor } from '@embroider/vite';
import { resolve } from 'path';
import { babel } from '@rollup/plugin-babel';

const root = 'tmp/rewritten-app';
const extensions = ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'];

export default defineConfig({
root,
// esbuild in vite does not support decorators
esbuild: false,
cacheDir: resolve('node_modules', '.vite'),
resolve: {
extensions: ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'],
},

plugins: [
hbs(),
templateTag(),
scripts(),
resolver(),
compatPrebuild(),
contentFor(),
export default defineConfig(({ mode }) => {
return {
root,
cacheDir: resolve('node_modules', '.vite'),
resolve: {
extensions,
},
plugins: [
hbs(),
templateTag(),
scripts(),
resolver(),
compatPrebuild(),
assets(),
contentFor(),

babel({
babelHelpers: 'runtime',
babel({
babelHelpers: 'runtime',

// this needs .hbs because our hbs() plugin above converts them to
// javascript but the javascript still also needs babel, but we don't want
// to rename them because vite isn't great about knowing how to hot-reload
// them if we resolve them to made-up names.
extensions: ['.gjs', '.js', '.hbs', '.ts', '.gts'],
// this needs .hbs because our hbs() plugin above converts them to
// javascript but the javascript still also needs babel, but we don't want
// to rename them because vite isn't great about knowing how to hot-reload
// them if we resolve them to made-up names.
extensions,
}),
],
optimizeDeps: optimizeDeps({
appExtensions: extensions,
}),
],
optimizeDeps: optimizeDeps(),
server: {
port: 4200,
watch: {
ignored: ['!**/tmp/rewritten-app/**'],
publicDir: resolve(process.cwd(), 'public'),
server: {
port: 4200,
watch: {
ignored: ['!**/tmp/**'],
},
},
},
build: {
outDir: resolve(process.cwd(), 'dist'),
rollupOptions: {
input: {
main: resolve(root, 'index.html'),
tests: resolve(root, 'tests/index.html'),
build: {
outDir: resolve(process.cwd(), 'dist'),
rollupOptions: {
input: {
main: resolve(root, 'index.html'),
...(shouldBuildTests(mode) ? { tests: resolve(root, 'tests/index.html') } : undefined),
},
},
},
},
};
});

function shouldBuildTests(mode) {
return mode !== 'production' || process.env.FORCE_BUILD_TESTS;
}
84 changes: 46 additions & 38 deletions tests/ts-app-template/vite.config.mjs
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
import { defineConfig } from 'vite';
import { resolver, hbs, scripts, templateTag, optimizeDeps, compatPrebuild, contentFor } from '@embroider/vite';
import { resolver, hbs, scripts, templateTag, optimizeDeps, compatPrebuild, assets, contentFor } from '@embroider/vite';
import { resolve } from 'path';
import { babel } from '@rollup/plugin-babel';

const root = 'tmp/rewritten-app';
const extensions = ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'];

export default defineConfig({
root,
// esbuild in vite does not support decorators
esbuild: false,
cacheDir: resolve('node_modules', '.vite'),
resolve: {
extensions: ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'],
},

plugins: [
hbs(),
templateTag(),
scripts(),
resolver(),
compatPrebuild(),
contentFor(),
export default defineConfig(({ mode }) => {
return {
root,
cacheDir: resolve('node_modules', '.vite'),
resolve: {
extensions,
},
plugins: [
hbs(),
templateTag(),
scripts(),
resolver(),
compatPrebuild(),
assets(),
contentFor(),

babel({
babelHelpers: 'runtime',
babel({
babelHelpers: 'runtime',

// this needs .hbs because our hbs() plugin above converts them to
// javascript but the javascript still also needs babel, but we don't want
// to rename them because vite isn't great about knowing how to hot-reload
// them if we resolve them to made-up names.
extensions: ['.gjs', '.js', '.hbs', '.ts', '.gts'],
// this needs .hbs because our hbs() plugin above converts them to
// javascript but the javascript still also needs babel, but we don't want
// to rename them because vite isn't great about knowing how to hot-reload
// them if we resolve them to made-up names.
extensions,
}),
],
optimizeDeps: optimizeDeps({
appExtensions: extensions,
}),
],
optimizeDeps: optimizeDeps(),
server: {
port: 4200,
watch: {
ignored: ['!**/tmp/rewritten-app/**'],
publicDir: resolve(process.cwd(), 'public'),
server: {
port: 4200,
watch: {
ignored: ['!**/tmp/**'],
},
},
},
build: {
outDir: resolve(process.cwd(), 'dist'),
rollupOptions: {
input: {
main: resolve(root, 'index.html'),
tests: resolve(root, 'tests/index.html'),
build: {
outDir: resolve(process.cwd(), 'dist'),
rollupOptions: {
input: {
main: resolve(root, 'index.html'),
...(shouldBuildTests(mode) ? { tests: resolve(root, 'tests/index.html') } : undefined),
},
},
},
},
};
});

function shouldBuildTests(mode) {
return mode !== 'production' || process.env.FORCE_BUILD_TESTS;
}

0 comments on commit bc8ae0f

Please sign in to comment.