fix(js): libraries should be buildable by default (#15533)

This commit is contained in:
Katerina Skroumpelou 2023-03-09 21:20:19 +02:00 committed by GitHub
parent eebadef0e6
commit 16ad3086be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 21 deletions

View File

@ -114,7 +114,7 @@
"description": "The bundler to use. Choosing 'none' means this library is not buildable.", "description": "The bundler to use. Choosing 'none' means this library is not buildable.",
"type": "string", "type": "string",
"enum": ["swc", "tsc", "rollup", "vite", "esbuild", "none"], "enum": ["swc", "tsc", "rollup", "vite", "esbuild", "none"],
"default": "none", "default": "tsc",
"x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup.", "x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup.",
"x-priority": "important" "x-priority": "important"
}, },
@ -130,7 +130,7 @@
} }
}, },
"required": ["name"], "required": ["name"],
"examplesFile": "---\ntitle: JS library generator examples\ndescription: This page contains examples for the @nrwl/js:lib generator.\n---\n\nThe `@nrwl/js:lib` generator will generate a library for you, and it will configure it according to the options you provide.\n\n```bash\nnpx nx g @nrwl/js:lib mylib\n```\n\nBy default, the library that is generated when you use this executor without passing any options, like the example above, will be a buildable library, using the `@nrwl/js:tsc` executor as a builder.\n\nYou may configure the tools you want to use to build your library, or bundle it too, by passing the `--bundler` flag. The `--bundler` flag controls the compiler and/or the bundler that will be used to build your library. If you choose `tsc` or `swc`, the result will be a buildable library using either `tsc` or `swc` as the compiler. If you choose `rollup` or `vite`, the result will be a buildable library using `rollup` or `vite` as the bundler. In the case of `rollup`, it will default to the `tsc` compiler. If you choose `esbuild`, you may use the [`esbuildOptions` property](https://esbuild.github.io/api/) in your `project.json` under the `build` target options to specify whether you wish to bundle your library or not.\n\n## Examples\n\n{% tabs %}\n\n{% tab label=\"Buildable with default compiler (tsc)\" %}\n\nGenerate a buildable library using the `@nrwl/js:tsc` executor. This uses `tsc` as the compiler.\n\n```bash\nnpx nx g @nrwl/js:lib mylib\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable with SWC compiler\" %}\n\nGenerate a buildable library using [SWC](https://swc.rs) as the compiler. This will use the `@nrwl/js:swc` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=swc\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable with tsc\" %}\n\nGenerate a buildable library using tsc as the compiler. This will use the `@nrwl/js:tsc` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=tsc\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable, with Rollup as a bundler\" %}\n\nGenerate a buildable library using [Rollup](https://rollupjs.org) as the bundler. This will use the `@nrwl/rollup:rollup` executor. It will also use [SWC](https://swc.rs) as the compiler.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=rollup\n```\n\nIf you do not want to use `swc` as the compiler, and want to use the default `babel` compiler, you can do so in your `project.json` under the `build` target options, using the [`compiler` property](https://nx.dev/packages/rollup/executors/rollup#compiler):\n\n```jsonc {% fileName=\"libs/mylib/project.json\" %}\n\"build\": {\n \"executor\": \"@nrwl/rollup:rollup\",\n \"options\": {\n //...\n \"compiler\": \"babel\"\n }\n}\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable, with Vite as a bundler\" %}\n\nGenerate a buildable library using [Vite](https://vitejs.dev/) as the bundler. This will use the `@nrwl/vite:build` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=vite\n```\n\n{% /tab %}\n\n{% tab label=\"Using ESBuild\" %}\n\nGenerate a buildable library using [ESBuild](https://esbuild.github.io/) as the bundler. This will use the `@nrwl/esbuild:esbuild` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=esbuild\n```\n\nIf you want to specify whether you want to bundle your library or not, you can do so in your `project.json` under the `build` target options, using the [`esbuildOptions` property](https://esbuild.github.io/api/):\n\n```jsonc {% fileName=\"libs/mylib/project.json\" %}\n\"build\": {\n \"executor\": \"@nrwl/esbuild:esbuild\",\n \"options\": {\n //...\n \"esbuildOptions\": {\n \"bundle\": true\n }\n }\n}\n```\n\n{% /tab %}\n\n{% tab label=\"Minimal publishing target\" %}\n\nGenerate a **publishable** library with a minimal publishing target. The result will be a buildable library using the `@nrwl/js:tsc` executor, using `tsc` as the compiler. You can change the compiler or the bundler by passing the `--bundler` flag.\n\n```bash\nnpx nx g lib mylib --publishable\n```\n\n{% /tab %}\n\n{% tab label=\"Using directory flag\" %}\n\nGenerate a library named `mylib` and put it under a directory named `myapp` (`libs/myapp/mylib`)\n\n```shell\nnpx nx g lib mylib --directory=myapp\n```\n\n{% /tab %}\n\n{% /tabs %}\n", "examplesFile": "---\ntitle: JS library generator examples\ndescription: This page contains examples for the @nrwl/js:lib generator.\n---\n\nThe `@nrwl/js:lib` generator will generate a library for you, and it will configure it according to the options you provide.\n\n```bash\nnpx nx g @nrwl/js:lib mylib\n```\n\nBy default, the library that is generated when you use this executor without passing any options, like the example above, will be a buildable library, using the `@nrwl/js:tsc` executor as a builder.\n\nYou may configure the tools you want to use to build your library, or bundle it too, by passing the `--bundler` flag. The `--bundler` flag controls the compiler and/or the bundler that will be used to build your library. If you choose `tsc` or `swc`, the result will be a buildable library using either `tsc` or `swc` as the compiler. If you choose `rollup` or `vite`, the result will be a buildable library using `rollup` or `vite` as the bundler. In the case of `rollup`, it will default to the `tsc` compiler. If you choose `esbuild`, you may use the [`esbuildOptions` property](https://esbuild.github.io/api/) in your `project.json` under the `build` target options to specify whether you wish to bundle your library or not.\n\n## Examples\n\n{% tabs %}\n\n{% tab label=\"Buildable with default compiler (tsc)\" %}\n\nGenerate a buildable library using the `@nrwl/js:tsc` executor. This uses `tsc` as the compiler.\n\n```bash\nnpx nx g @nrwl/js:lib mylib\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable with SWC compiler\" %}\n\nGenerate a buildable library using [SWC](https://swc.rs) as the compiler. This will use the `@nrwl/js:swc` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=swc\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable with tsc\" %}\n\nGenerate a buildable library using tsc as the compiler. This will use the `@nrwl/js:tsc` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=tsc\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable, with Rollup as a bundler\" %}\n\nGenerate a buildable library using [Rollup](https://rollupjs.org) as the bundler. This will use the `@nrwl/rollup:rollup` executor. It will also use [SWC](https://swc.rs) as the compiler.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=rollup\n```\n\nIf you do not want to use `swc` as the compiler, and want to use the default `babel` compiler, you can do so in your `project.json` under the `build` target options, using the [`compiler` property](https://nx.dev/packages/rollup/executors/rollup#compiler):\n\n```jsonc {% fileName=\"libs/mylib/project.json\" %}\n\"build\": {\n \"executor\": \"@nrwl/rollup:rollup\",\n \"options\": {\n //...\n \"compiler\": \"babel\"\n }\n}\n```\n\n{% /tab %}\n\n{% tab label=\"Buildable, with Vite as a bundler\" %}\n\nGenerate a buildable library using [Vite](https://vitejs.dev/) as the bundler. This will use the `@nrwl/vite:build` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=vite\n```\n\n{% /tab %}\n\n{% tab label=\"Using ESBuild\" %}\n\nGenerate a buildable library using [ESBuild](https://esbuild.github.io/) as the bundler. This will use the `@nrwl/esbuild:esbuild` executor.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=esbuild\n```\n\nIf you want to specify whether you want to bundle your library or not, you can do so in your `project.json` under the `build` target options, using the [`esbuildOptions` property](https://esbuild.github.io/api/):\n\n```jsonc {% fileName=\"libs/mylib/project.json\" %}\n\"build\": {\n \"executor\": \"@nrwl/esbuild:esbuild\",\n \"options\": {\n //...\n \"esbuildOptions\": {\n \"bundle\": true\n }\n }\n}\n```\n\n{% /tab %}\n\n{% tab label=\"Minimal publishing target\" %}\n\nGenerate a **publishable** library with a minimal publishing target. The result will be a buildable library using the `@nrwl/js:tsc` executor, using `tsc` as the compiler. You can change the compiler or the bundler by passing the `--bundler` flag.\n\n```bash\nnpx nx g lib mylib --publishable\n```\n\n{% /tab %}\n\n{% tab label=\"Using directory flag\" %}\n\nGenerate a library named `mylib` and put it under a directory named `myapp` (`libs/myapp/mylib`)\n\n```shell\nnpx nx g lib mylib --directory=myapp\n```\n\n{% /tab %}\n\n{% tab label=\"Non-buildable library\" %}\n\nGenerate a non-buildable library.\n\n```bash\nnpx nx g @nrwl/js:lib mylib --bundler=none\n```\n\n{% /tab %}\n\n{% /tabs %}\n",
"presets": [] "presets": []
}, },
"aliases": ["lib"], "aliases": ["lib"],

View File

@ -102,7 +102,7 @@ describe('inlining', () => {
it('should inline nesting libraries', async () => { it('should inline nesting libraries', async () => {
const parent = uniq('parent'); const parent = uniq('parent');
runCLI(`generate @nrwl/js:lib ${parent} --bundler=tsc --no-interactive`); runCLI(`generate @nrwl/js:lib ${parent} --no-interactive`);
const child = uniq('child'); const child = uniq('child');
runCLI(`generate @nrwl/js:lib ${child} --bundler=none --no-interactive`); runCLI(`generate @nrwl/js:lib ${child} --bundler=none --no-interactive`);

View File

@ -35,8 +35,8 @@ describe('Nx Affected and Graph Tests', () => {
const mypublishablelib = uniq('mypublishablelib'); const mypublishablelib = uniq('mypublishablelib');
runCLI(`generate @nrwl/web:app ${myapp}`); runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`generate @nrwl/web:app ${myapp2}`); runCLI(`generate @nrwl/web:app ${myapp2}`);
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib}`);
runCLI(`generate @nrwl/js:lib ${mylib2} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib2}`);
runCLI( runCLI(
`generate @nrwl/js:lib ${mypublishablelib} --publishable --importPath=@${proj}/${mypublishablelib}` `generate @nrwl/js:lib ${mypublishablelib} --publishable --importPath=@${proj}/${mypublishablelib}`
); );
@ -195,7 +195,7 @@ describe('Nx Affected and Graph Tests', () => {
function generateAll() { function generateAll() {
runCLI(`generate @nrwl/web:app ${myapp}`); runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`generate @nrwl/web:app ${myapp2}`); runCLI(`generate @nrwl/web:app ${myapp2}`);
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib}`);
runCommand(`git add . && git commit -am "add all"`); runCommand(`git add . && git commit -am "add all"`);
} }
@ -211,7 +211,7 @@ describe('Nx Affected and Graph Tests', () => {
expect(runCLI('affected:apps')).toContain(myapp2); expect(runCLI('affected:apps')).toContain(myapp2);
runCommand(`git add . && git commit -am "add ${myapp2}"`); runCommand(`git add . && git commit -am "add ${myapp2}"`);
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib}`);
expect(runCLI('affected:apps')).not.toContain(myapp); expect(runCLI('affected:apps')).not.toContain(myapp);
expect(runCLI('affected:apps')).not.toContain(myapp2); expect(runCLI('affected:apps')).not.toContain(myapp2);
expect(runCLI('affected:libs')).toContain(mylib); expect(runCLI('affected:libs')).toContain(mylib);
@ -294,8 +294,8 @@ describe('Nx Affected and Graph Tests', () => {
runCLI(`generate @nrwl/web:app ${myapp}`); runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`generate @nrwl/web:app ${myapp2}`); runCLI(`generate @nrwl/web:app ${myapp2}`);
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib}`);
runCLI(`generate @nrwl/js:lib ${mylib2} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib2}`);
runCLI(`generate @nrwl/js:lib ${mypublishablelib}`); runCLI(`generate @nrwl/js:lib ${mypublishablelib}`);
const app1ElementSpec = readFile( const app1ElementSpec = readFile(
@ -413,8 +413,8 @@ describe('Nx Affected and Graph Tests', () => {
runCLI(`generate @nrwl/web:app ${myapp}`); runCLI(`generate @nrwl/web:app ${myapp}`);
runCLI(`generate @nrwl/web:app ${myapp2}`); runCLI(`generate @nrwl/web:app ${myapp2}`);
runCLI(`generate @nrwl/web:app ${myapp3}`); runCLI(`generate @nrwl/web:app ${myapp3}`);
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib}`);
runCLI(`generate @nrwl/js:lib ${mylib2} --bundler=tsc`); runCLI(`generate @nrwl/js:lib ${mylib2}`);
runCommand(`git init`); runCommand(`git init`);
runCommand(`git config user.email "test@test.com"`); runCommand(`git config user.email "test@test.com"`);

View File

@ -123,4 +123,14 @@ npx nx g lib mylib --directory=myapp
{% /tab %} {% /tab %}
{% tab label="Non-buildable library" %}
Generate a non-buildable library.
```bash
npx nx g @nrwl/js:lib mylib --bundler=none
```
{% /tab %}
{% /tabs %} {% /tabs %}

View File

@ -212,7 +212,7 @@ describe('lib', () => {
).toBeTruthy(); ).toBeTruthy();
expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy(); expect(tree.exists('libs/my-dir/my-lib/src/index.ts')).toBeTruthy();
expect(tree.exists(`libs/my-dir/my-lib/.eslintrc.json`)).toBeTruthy(); expect(tree.exists(`libs/my-dir/my-lib/.eslintrc.json`)).toBeTruthy();
expect(tree.exists(`libs/my-dir/my-lib/package.json`)).toBeFalsy(); expect(tree.exists(`libs/my-dir/my-lib/package.json`)).toBeTruthy();
}); });
it('should update project configuration', async () => { it('should update project configuration', async () => {
@ -739,14 +739,14 @@ describe('lib', () => {
expect(config.targets.build).not.toBeDefined(); expect(config.targets.build).not.toBeDefined();
}); });
it('should NOT generate the build target if bundler is undefined', async () => { it('should still generate the build target if bundler is undefined', async () => {
await libraryGenerator(tree, { await libraryGenerator(tree, {
...defaultOptions, ...defaultOptions,
name: 'myLib', name: 'myLib',
}); });
const config = readProjectConfiguration(tree, 'my-lib'); const config = readProjectConfiguration(tree, 'my-lib');
expect(config.targets.build).not.toBeDefined(); expect(config.targets.build).toBeDefined();
}); });
it('should generate the build target', async () => { it('should generate the build target', async () => {

View File

@ -384,17 +384,18 @@ function normalizeOptions(
* and no compiler was provided. * and no compiler was provided.
* *
* If the user has not provided a bundler and no compiler, and has not set buildable to true, then * If the user has not provided a bundler and no compiler, and has not set buildable to true, then
* set the bundler to none. * set the bundler to tsc, to preserve old default behaviour (buildable: true by default).
* *
* If it's publishable, we need to build the code before publishing it, so again * If it's publishable, we need to build the code before publishing it, so again
* we default to `tsc`. In the previous version of this, it would set `buildable` to true * we default to `tsc`. In the previous version of this, it would set `buildable` to true
* and that would default to `tsc`. * and that would default to `tsc`.
*
* In the past, the only way to get a non-buildable library was to set buildable to false.
* Now, the only way to get a non-buildble library is to set bundler to none.
* By default, with nothing provided, libraries are buildable with `@nrwl/js:tsc`.
*/ */
options.bundler = options.bundler = options.bundler ?? options.compiler ?? 'tsc';
options.bundler ??
options.compiler ??
(options.buildable || options.publishable ? 'tsc' : 'none');
if (options.publishable) { if (options.publishable) {
if (!options.importPath) { if (!options.importPath) {
@ -404,11 +405,15 @@ function normalizeOptions(
} }
if (options.bundler === 'none') { if (options.bundler === 'none') {
// It must be buildable, so default to tsc
options.bundler = 'tsc'; options.bundler = 'tsc';
} }
} }
// This is to preserve old behaviour, buildable: false
if (options.publishable === false && options.buildable === false) {
options.bundler = 'none';
}
const { Linter } = require('@nrwl/linter'); const { Linter } = require('@nrwl/linter');
if (options.config === 'npm-scripts') { if (options.config === 'npm-scripts') {
options.unitTestRunner = 'none'; options.unitTestRunner = 'none';

View File

@ -114,7 +114,7 @@
"description": "The bundler to use. Choosing 'none' means this library is not buildable.", "description": "The bundler to use. Choosing 'none' means this library is not buildable.",
"type": "string", "type": "string",
"enum": ["swc", "tsc", "rollup", "vite", "esbuild", "none"], "enum": ["swc", "tsc", "rollup", "vite", "esbuild", "none"],
"default": "none", "default": "tsc",
"x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup.", "x-prompt": "Which bundler would you like to use to build the library? Choose 'none' to skip build setup.",
"x-priority": "important" "x-priority": "important"
}, },