diff --git a/src/cli-main.ts b/src/cli-main.ts index 6beb664e..d9a0e17e 100644 --- a/src/cli-main.ts +++ b/src/cli-main.ts @@ -84,6 +84,7 @@ export async function main(options: Options = {}) { }) .option('--loader ', 'Specify the loader for a file extension') .option('--no-config', 'Disable config file') + .option('--no-shims', 'Disable cjs and esm shims') .action(async (files: string[], flags) => { const { build } = await import('.') Object.assign(options, { diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 82fbca58..e9963325 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -87,6 +87,7 @@ export async function runEsbuild( const platform = options.platform || 'node' const loader = options.loader || {} + const injectShims = options.shims !== false const esbuildPlugins: Array = [ format === 'cjs' && nodeProtocolPlugin(), @@ -155,7 +156,7 @@ export async function runEsbuild( : ['browser', 'module', 'main'], plugins: esbuildPlugins.filter(truthy), define: { - ...(format === 'cjs' + ...(format === 'cjs' && injectShims ? { 'import.meta.url': 'importMetaUrlShim', } @@ -171,8 +172,10 @@ export async function runEsbuild( }, {}), }, inject: [ - format === 'cjs' ? path.join(__dirname, '../assets/cjs_shims.js') : '', - format === 'esm' && platform === 'node' + format === 'cjs' && injectShims + ? path.join(__dirname, '../assets/cjs_shims.js') + : '', + format === 'esm' && injectShims && platform === 'node' ? path.join(__dirname, '../assets/esm_shims.js') : '', ...(options.inject || []), diff --git a/src/options.ts b/src/options.ts index 415fceba..5a7cb9f5 100644 --- a/src/options.ts +++ b/src/options.ts @@ -122,4 +122,9 @@ export type Options = { * Use a custom tsconfig */ tsconfig?: string + /** + * CJS and ESM shims + * @default `true` + */ + shims?: boolean }