Skip to content

Commit

Permalink
feat: allow to disable cjs/esm shims
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 7, 2021
1 parent ae30b68 commit edc92e4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/cli-main.ts
Expand Up @@ -84,6 +84,7 @@ export async function main(options: Options = {}) {
})
.option('--loader <ext=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, {
Expand Down
9 changes: 6 additions & 3 deletions src/esbuild/index.ts
Expand Up @@ -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<EsbuildPlugin | false | undefined> = [
format === 'cjs' && nodeProtocolPlugin(),
Expand Down Expand Up @@ -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',
}
Expand All @@ -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 || []),
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Expand Up @@ -122,4 +122,9 @@ export type Options = {
* Use a custom tsconfig
*/
tsconfig?: string
/**
* CJS and ESM shims
* @default `true`
*/
shims?: boolean
}

0 comments on commit edc92e4

Please sign in to comment.