Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bundling): support custom outExtension for ESM and CJS (e.g. .mjs) #13985

Merged
merged 1 commit into from Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -159,4 +159,112 @@ describe('buildEsbuildOptions', () => {
},
});
});

it('should respect user defined outExtension', () => {
expect(
buildEsbuildOptions(
'esm',
{
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
tsConfig: 'apps/myapp/tsconfig.app.json',
project: 'apps/myapp/package.json',
outputFileName: 'index.js',
assets: [],
singleEntry: true,
external: [],
esbuildOptions: {
outExtension: {
'.js': '.mjs',
},
},
},
context
)
).toEqual({
bundle: true,
entryNames: '[dir]/[name]',
entryPoints: ['apps/myapp/src/index.ts'],
format: 'esm',
platform: 'node',
outfile: 'dist/apps/myapp/index.mjs',
tsconfig: 'apps/myapp/tsconfig.app.json',
external: [],
outExtension: {
'.js': '.mjs',
},
});

expect(
buildEsbuildOptions(
'cjs',
{
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
tsConfig: 'apps/myapp/tsconfig.app.json',
project: 'apps/myapp/package.json',
outputFileName: 'index.js',
assets: [],
singleEntry: true,
external: [],
esbuildOptions: {
outExtension: {
'.js': '.js',
},
},
},
context
)
).toEqual({
bundle: true,
entryNames: '[dir]/[name]',
entryPoints: ['apps/myapp/src/index.ts'],
format: 'cjs',
platform: 'node',
outfile: 'dist/apps/myapp/index.js',
tsconfig: 'apps/myapp/tsconfig.app.json',
external: [],
outExtension: {
'.js': '.js',
},
});

// ESM cannot be mapped to .cjs so ignore
expect(
buildEsbuildOptions(
'esm',
{
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
tsConfig: 'apps/myapp/tsconfig.app.json',
project: 'apps/myapp/package.json',
outputFileName: 'index.js',
assets: [],
singleEntry: true,
external: [],
esbuildOptions: {
outExtension: {
'.js': '.cjs',
},
},
},
context
)
).toEqual({
bundle: true,
entryNames: '[dir]/[name]',
entryPoints: ['apps/myapp/src/index.ts'],
format: 'esm',
platform: 'node',
outfile: 'dist/apps/myapp/index.js',
tsconfig: 'apps/myapp/tsconfig.app.json',
external: [],
outExtension: {
'.js': '.js',
},
});
});
});
Expand Up @@ -31,7 +31,9 @@ export function buildEsbuildOptions(
metafile: options.metafile,
tsconfig: options.tsConfig,
format,
outExtension: { '.js': getOutExtension(format) },
outExtension: {
'.js': getOutExtension(format, options),
},
};

if (options.platform === 'browser') {
Expand All @@ -47,20 +49,33 @@ export function buildEsbuildOptions(
return esbuildOptions;
}

function getOutExtension(format: 'cjs' | 'esm') {
return format === 'esm' ? ESM_FILE_EXTENSION : CJS_FILE_EXTENSION;
function getOutExtension(
format: 'cjs' | 'esm',
options: EsBuildExecutorOptions
): string {
const userDefinedExt = options.esbuildOptions?.outExtension?.['.js'];
// Allow users to change the output extensions from default CJS and ESM extensions.
// CJS -> .js
// ESM -> .mjs
return userDefinedExt === '.js' && format === 'cjs'
? '.js'
: userDefinedExt === '.mjs' && format === 'esm'
? '.mjs'
: format === 'esm'
? ESM_FILE_EXTENSION
: CJS_FILE_EXTENSION;
}

function getOutfile(
format: 'cjs' | 'esm',
options: EsBuildExecutorOptions,
context: ExecutorContext
) {
const ext = getOutExtension(format, options);
const candidate = joinPathFragments(
context.target.options.outputPath,
options.outputFileName
);
const ext = getOutExtension(format);
const { dir, name } = parse(candidate);
return `${dir}/${name}${ext}`;
}