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

feat(bundling): add option to esbuild to not bundle #14145

Merged
merged 1 commit into from
Jan 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/generated/packages/esbuild/executors/esbuild.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"items": { "type": "string" },
"default": []
},
"bundle": {
"type": "boolean",
"description": "Whether to bundle the main entry point and additional entry points. Set to false to keep individual output files.",
"default": true
},
"format": {
"type": "array",
"description": "List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).",
Expand Down
18 changes: 18 additions & 0 deletions e2e/esbuild/src/esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,22 @@ describe('EsBuild Plugin', () => {
expect(runResult).toMatch(/Hello world/);
expect(runResult).toMatch(/Hello from child lib/);
}, 300_000);

it('should support non-bundle builds', () => {
const myPkg = uniq('my-pkg');
runCLI(`generate @nrwl/js:lib ${myPkg} --bundler=esbuild`);
updateFile(`libs/${myPkg}/src/lib/${myPkg}.ts`, `console.log('Hello');\n`);
updateFile(`libs/${myPkg}/src/index.ts`, `import './lib/${myPkg}.js';\n`);

runCLI(`build ${myPkg} --bundle=false`);

checkFilesExist(
`dist/libs/${myPkg}/lib/${myPkg}.js`,
`dist/libs/${myPkg}/index.js`
);
// Test files are excluded in tsconfig (e.g. tsconfig.lib.json)
checkFilesDoNotExist(`dist/libs/${myPkg}/lib/${myPkg}.spec.js`);
// Can run package (package.json fields are correctly generated)
expect(runCommand(`node dist/libs/${myPkg}`)).toMatch(/Hello/);
}, 300_000);
});
1 change: 1 addition & 0 deletions packages/esbuild/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@nrwl/workspace": "file:../workspace",
"chalk": "4.1.0",
"dotenv": "~10.0.0",
"fast-glob": "3.2.7",
"fs-extra": "^10.1.0",
"tslib": "^2.3.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ExecutorContext } from 'nx/src/config/misc-interfaces';

describe('buildEsbuildOptions', () => {
const context: ExecutorContext = {
projectName: 'myapp',
workspace: {
version: 2,
projects: {
Expand All @@ -27,6 +28,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'esm',
{
bundle: true,
platform: 'browser',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
Expand Down Expand Up @@ -62,6 +64,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'esm',
{
bundle: true,
platform: 'browser',
main: 'apps/myapp/src/index.ts',
additionalEntryPoints: ['apps/myapp/src/extra-entry.ts'],
Expand Down Expand Up @@ -98,6 +101,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'cjs',
{
bundle: true,
platform: 'browser',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
Expand Down Expand Up @@ -133,6 +137,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'cjs',
{
bundle: true,
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
Expand Down Expand Up @@ -165,6 +170,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'esm',
{
bundle: true,
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
Expand Down Expand Up @@ -200,6 +206,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'cjs',
{
bundle: true,
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
Expand Down Expand Up @@ -236,6 +243,7 @@ describe('buildEsbuildOptions', () => {
buildEsbuildOptions(
'esm',
{
bundle: true,
platform: 'node',
main: 'apps/myapp/src/index.ts',
outputPath: 'dist/apps/myapp',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as esbuild from 'esbuild';
import * as path from 'path';
import { parse } from 'path';
import * as glob from 'fast-glob';
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';
import { readJsonFile } from 'nx/src/utils/fileutils';

const ESM_FILE_EXTENSION = '.js';
const CJS_FILE_EXTENSION = '.cjs';
Expand All @@ -18,12 +21,9 @@ export function buildEsbuildOptions(
): 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
bundle: options.bundle,
external: options.external,
minify: options.minify,
platform: options.platform,
Expand All @@ -40,12 +40,29 @@ export function buildEsbuildOptions(
esbuildOptions.define = getClientEnvironment();
}

if (options.singleEntry) {
if (options.singleEntry && options.bundle) {
esbuildOptions.outfile = getOutfile(format, options, context);
} else {
esbuildOptions.outdir = options.outputPath;
}

const entryPoints = options.additionalEntryPoints
? [options.main, ...options.additionalEntryPoints]
: [options.main];
if (!options.bundle) {
const projectRoot = context.workspace.projects[context.projectName].root;
const tsconfig = readJsonFile(path.join(context.root, options.tsConfig));
const matchedFiles = glob
.sync(tsconfig.include ?? [], {
cwd: projectRoot,
ignore: (tsconfig.exclude ?? []).concat([options.main]),
})
.map((f) => path.join(projectRoot, f))
.filter((f) => !entryPoints.includes(f));
entryPoints.push(...matchedFiles);
}
esbuildOptions.entryPoints = entryPoints;

return esbuildOptions;
}

Expand Down
1 change: 1 addition & 0 deletions packages/esbuild/src/executors/esbuild/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface EsBuildExecutorOptions {
additionalEntryPoints?: string[];
assets: AssetGlob[];
buildableProjectDepsInPackageJsonType?: 'dependencies' | 'peerDependencies';
bundle?: boolean;
deleteOutputPath?: boolean;
dependenciesFieldType?: boolean;
esbuildOptions?: Record<string, any>;
Expand Down
5 changes: 5 additions & 0 deletions packages/esbuild/src/executors/esbuild/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
},
"default": []
},
"bundle": {
"type": "boolean",
"description": "Whether to bundle the main entry point and additional entry points. Set to false to keep individual output files.",
"default": true
},
"format": {
"type": "array",
"description": "List of module formats to output. Defaults to matching format from tsconfig (e.g. CJS for CommonJS, and ESM otherwise).",
Expand Down