Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(bundling): correct main field in package.json when using esbuild
- Loading branch information
Showing
7 changed files
with
356 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
packages/esbuild/src/executors/esbuild/lib/build-esbuild-options.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { buildEsbuildOptions } from './build-esbuild-options'; | ||
import { ExecutorContext } from 'nx/src/config/misc-interfaces'; | ||
|
||
describe('buildEsbuildOptions', () => { | ||
const context: ExecutorContext = { | ||
workspace: { | ||
version: 2, | ||
projects: { | ||
myapp: { | ||
root: 'apps/myapp', | ||
}, | ||
}, | ||
}, | ||
isVerbose: false, | ||
root: '/', | ||
cwd: '/', | ||
target: { | ||
executor: '@nrwl/esbuild:esbuild', | ||
options: { | ||
outputPath: 'dist/apps/myapp', | ||
}, | ||
}, | ||
}; | ||
|
||
it('should include environment variables for platform === browser', () => { | ||
expect( | ||
buildEsbuildOptions( | ||
'esm', | ||
{ | ||
platform: 'browser', | ||
main: 'apps/myapp/src/index.ts', | ||
outputPath: 'dist/apps/myapp', | ||
tsConfig: 'apps/myapp/tsconfig.app.json', | ||
project: 'apps/myapp/package.json', | ||
assets: [], | ||
outputFileName: 'index.js', | ||
singleEntry: true, | ||
}, | ||
context | ||
) | ||
).toEqual({ | ||
bundle: true, | ||
define: expect.objectContaining({ | ||
'process.env.NODE_ENV': '"test"', | ||
}), | ||
entryNames: '[dir]/[name]', | ||
entryPoints: ['apps/myapp/src/index.ts'], | ||
format: 'esm', | ||
platform: 'browser', | ||
outfile: 'dist/apps/myapp/index.js', | ||
tsconfig: 'apps/myapp/tsconfig.app.json', | ||
outExtension: { | ||
'.js': '.js', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should support multiple entry points', () => { | ||
expect( | ||
buildEsbuildOptions( | ||
'esm', | ||
{ | ||
platform: 'browser', | ||
main: 'apps/myapp/src/index.ts', | ||
additionalEntryPoints: ['apps/myapp/src/extra-entry.ts'], | ||
outputPath: 'dist/apps/myapp', | ||
tsConfig: 'apps/myapp/tsconfig.app.json', | ||
project: 'apps/myapp/package.json', | ||
assets: [], | ||
outputFileName: 'index.js', | ||
singleEntry: false, | ||
}, | ||
context | ||
) | ||
).toEqual({ | ||
bundle: true, | ||
define: expect.objectContaining({ | ||
'process.env.NODE_ENV': '"test"', | ||
}), | ||
entryNames: '[dir]/[name]', | ||
entryPoints: ['apps/myapp/src/index.ts', 'apps/myapp/src/extra-entry.ts'], | ||
format: 'esm', | ||
platform: 'browser', | ||
outdir: 'dist/apps/myapp', | ||
tsconfig: 'apps/myapp/tsconfig.app.json', | ||
outExtension: { | ||
'.js': '.js', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should support cjs format', () => { | ||
expect( | ||
buildEsbuildOptions( | ||
'cjs', | ||
{ | ||
platform: 'browser', | ||
main: 'apps/myapp/src/index.ts', | ||
outputPath: 'dist/apps/myapp', | ||
tsConfig: 'apps/myapp/tsconfig.app.json', | ||
project: 'apps/myapp/package.json', | ||
assets: [], | ||
outputFileName: 'index.js', | ||
singleEntry: true, | ||
}, | ||
context | ||
) | ||
).toEqual({ | ||
bundle: true, | ||
define: expect.objectContaining({ | ||
'process.env.NODE_ENV': '"test"', | ||
}), | ||
entryNames: '[dir]/[name]', | ||
entryPoints: ['apps/myapp/src/index.ts'], | ||
format: 'cjs', | ||
platform: 'browser', | ||
outfile: 'dist/apps/myapp/index.cjs', | ||
tsconfig: 'apps/myapp/tsconfig.app.json', | ||
outExtension: { | ||
'.js': '.cjs', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should not define environment variables for node', () => { | ||
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', | ||
assets: [], | ||
outputFileName: 'index.js', | ||
singleEntry: true, | ||
}, | ||
context | ||
) | ||
).toEqual({ | ||
bundle: true, | ||
entryNames: '[dir]/[name]', | ||
entryPoints: ['apps/myapp/src/index.ts'], | ||
format: 'cjs', | ||
platform: 'node', | ||
outfile: 'dist/apps/myapp/index.cjs', | ||
tsconfig: 'apps/myapp/tsconfig.app.json', | ||
outExtension: { | ||
'.js': '.cjs', | ||
}, | ||
}); | ||
}); | ||
}); |
66 changes: 66 additions & 0 deletions
66
packages/esbuild/src/executors/esbuild/lib/build-esbuild-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as esbuild from 'esbuild'; | ||
import { getClientEnvironment } from '../../../utils/environment-variables'; | ||
import { | ||
EsBuildExecutorOptions, | ||
NormalizedEsBuildExecutorOptions, | ||
} from '../schema'; | ||
import { ExecutorContext } from 'nx/src/config/misc-interfaces'; | ||
import { joinPathFragments } from 'nx/src/utils/path'; | ||
import { parse } from 'path'; | ||
|
||
const ESM_FILE_EXTENSION = '.js'; | ||
const CJS_FILE_EXTENSION = '.cjs'; | ||
|
||
export function buildEsbuildOptions( | ||
format: 'cjs' | 'esm', | ||
options: NormalizedEsBuildExecutorOptions, | ||
context: ExecutorContext | ||
): esbuild.BuildOptions { | ||
const esbuildOptions: esbuild.BuildOptions = { | ||
...options.esbuildOptions, | ||
entryPoints: options.additionalEntryPoints | ||
? [options.main, ...options.additionalEntryPoints] | ||
: [options.main], | ||
entryNames: | ||
options.outputHashing === 'all' ? '[dir]/[name].[hash]' : '[dir]/[name]', | ||
bundle: true, // TODO(jack): support non-bundled builds | ||
external: options.external, | ||
minify: options.minify, | ||
platform: options.platform, | ||
target: options.target, | ||
metafile: options.metafile, | ||
tsconfig: options.tsConfig, | ||
format, | ||
outExtension: { '.js': getOutExtension(format) }, | ||
}; | ||
|
||
if (options.platform === 'browser') { | ||
esbuildOptions.define = getClientEnvironment(); | ||
} | ||
|
||
if (options.singleEntry) { | ||
esbuildOptions.outfile = getOutfile(format, options, context); | ||
} else { | ||
esbuildOptions.outdir = options.outputPath; | ||
} | ||
|
||
return esbuildOptions; | ||
} | ||
|
||
function getOutExtension(format: 'cjs' | 'esm') { | ||
return format === 'esm' ? ESM_FILE_EXTENSION : CJS_FILE_EXTENSION; | ||
} | ||
|
||
function getOutfile( | ||
format: 'cjs' | 'esm', | ||
options: EsBuildExecutorOptions, | ||
context: ExecutorContext | ||
) { | ||
const candidate = joinPathFragments( | ||
context.target.options.outputPath, | ||
options.outputFileName | ||
); | ||
const ext = getOutExtension(format); | ||
const { dir, name } = parse(candidate); | ||
return `${dir}/${name}${ext}`; | ||
} |
Oops, something went wrong.