Skip to content

Commit

Permalink
unify resolvable extensions config
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Sep 9, 2024
1 parent 8c1451c commit a3098ae
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 30 deletions.
14 changes: 6 additions & 8 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,12 @@ export class CompatAppBuilder {
}

private resolvableExtensions(): string[] {
// webpack's default is ['.wasm', '.mjs', '.js', '.json']. Keeping that
// subset in that order is sensible, since many third-party libraries will
// expect it to work that way.
//
// For TS, we defer to ember-cli-babel, and the setting for
// "enableTypescriptTransform" can be set with and without
// ember-cli-typescript
return ['.wasm', '.mjs', '.js', '.json', '.ts', '.hbs', '.hbs.js', '.gjs', '.gts'];
let fromEnv = process.env.EMBROIDER_RESOLVABLE_EXTENSIONS;
if (fromEnv) {
return fromEnv.split(',');
} else {
return ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.hbs.js', '.json'];
}
}

private modulePrefix(): string {
Expand Down
29 changes: 16 additions & 13 deletions packages/vite/src/build.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { fork } from 'child_process';
import type { Plugin } from 'vite';

export function emberBuild(command: string, mode: string): Promise<void> {
export function emberBuild(command: string, mode: string, resolvableExtensions: string[] | undefined): Promise<void> {
let env: Record<string, string> = {
...process.env,
EMBROIDER_PREBUILD: 'true',
};

if (resolvableExtensions) {
env['EMBROIDER_RESOLVABLE_EXTENSIONS'] = resolvableExtensions?.join(',');
}

if (command === 'build') {
return new Promise((resolve, reject) => {
const child = fork('./node_modules/ember-cli/bin/ember', ['build', '--environment', mode], {
env: {
...process.env,
EMBROIDER_PREBUILD: 'true',
},
});
const child = fork('./node_modules/ember-cli/bin/ember', ['build', '--environment', mode], { env });
child.on('exit', code => (code === 0 ? resolve() : reject()));
});
}
return new Promise((resolve, reject) => {
const child = fork('./node_modules/ember-cli/bin/ember', ['build', '--watch', '--environment', mode], {
silent: true,
env: {
...process.env,
EMBROIDER_PREBUILD: 'true',
},
env,
});
child.on('exit', code => (code === 0 ? resolve() : reject(new Error('ember build --watch failed'))));
child.on('spawn', () => {
Expand All @@ -39,13 +40,15 @@ export function emberBuild(command: string, mode: string): Promise<void> {
export function compatPrebuild(): Plugin {
let viteCommand: string | undefined;
let viteMode: string | undefined;
let resolvableExtensions: string[] | undefined;

return {
name: 'embroider-builder',
enforce: 'pre',
config(_config, { mode, command }) {
config(config, { mode, command }) {
viteCommand = command;
viteMode = mode;
resolvableExtensions = config.resolve?.extensions;
},
async buildStart() {
if (!viteCommand) {
Expand All @@ -54,7 +57,7 @@ export function compatPrebuild(): Plugin {
if (!viteMode) {
throw new Error(`bug: embroider compatPrebuild did not detect Vite's mode`);
}
await emberBuild(viteCommand, viteMode);
await emberBuild(viteCommand, viteMode, resolvableExtensions);
},
};
}
9 changes: 2 additions & 7 deletions packages/vite/src/esbuild-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function esBuildResolver(): EsBuildPlugin {

let firstResult: ResolveResult | undefined;

for (let requestName of extensionSearch(path)) {
for (let requestName of extensionSearch(path, resolverLoader.resolver.options.resolvableExtensions)) {
let result = await build.resolve(requestName, {
namespace,
resolveDir,
Expand Down Expand Up @@ -162,12 +162,7 @@ function detectPhase(build: PluginBuild): 'bundling' | 'scanning' {
}
}

// TODO: make this share with vite config. We may need to pass it directly as an
// argument to our esbuild plugin, or perhaps share it via embroider's
// resolver-config.json
const extensions = ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'];

function* extensionSearch(specifier: string): Generator<string> {
function* extensionSearch(specifier: string, extensions: string[]): Generator<string> {
yield specifier;
// when there's no explicit extension, we may do extension search
if (extname(specifier) === '') {
Expand Down
1 change: 1 addition & 0 deletions tests/addon-template/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default defineConfig(({ mode }) => {
".gts",
".ts",
".hbs",
".hbs.js",
".json",
],
},
Expand Down
1 change: 1 addition & 0 deletions tests/app-template/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default defineConfig(({ mode }) => {
".gts",
".ts",
".hbs",
".hbs.js",
".json",
],
},
Expand Down
2 changes: 1 addition & 1 deletion tests/ts-app-template-classic/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { babel } from '@rollup/plugin-babel';
export default defineConfig({
cacheDir: resolve('node_modules', '.vite'),
resolve: {
extensions: ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'],
extensions: ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.hbs.js', '.json'],
},

plugins: [
Expand Down
2 changes: 1 addition & 1 deletion tests/ts-app-template/vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { babel } from '@rollup/plugin-babel';
export default defineConfig({
cacheDir: resolve('node_modules', '.vite'),
resolve: {
extensions: ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.json'],
extensions: ['.mjs', '.gjs', '.js', '.mts', '.gts', '.ts', '.hbs', '.hbs.js', '.json'],
},

plugins: [
Expand Down

0 comments on commit a3098ae

Please sign in to comment.