Skip to content

Commit

Permalink
feat(bundling): add option to esbuild to not bundle (#14145)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jan 4, 2023
1 parent b8d0ba6 commit bd14b24
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 6 deletions.
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

1 comment on commit bd14b24

@vercel
Copy link

@vercel vercel bot commented on bd14b24 Jan 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.