fix(js): libraries should be buildable by default (#15533)
This commit is contained in:
parent
eebadef0e6
commit
16ad3086be
@ -114,7 +114,7 @@
|
||||
"description": "The bundler to use. Choosing 'none' means this library is not buildable.",
|
||||
"type": "string",
|
||||
"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-priority": "important"
|
||||
},
|
||||
@ -130,7 +130,7 @@
|
||||
}
|
||||
},
|
||||
"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": []
|
||||
},
|
||||
"aliases": ["lib"],
|
||||
|
||||
@ -102,7 +102,7 @@ describe('inlining', () => {
|
||||
|
||||
it('should inline nesting libraries', async () => {
|
||||
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');
|
||||
runCLI(`generate @nrwl/js:lib ${child} --bundler=none --no-interactive`);
|
||||
|
||||
@ -35,8 +35,8 @@ describe('Nx Affected and Graph Tests', () => {
|
||||
const mypublishablelib = uniq('mypublishablelib');
|
||||
runCLI(`generate @nrwl/web:app ${myapp}`);
|
||||
runCLI(`generate @nrwl/web:app ${myapp2}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib2} --bundler=tsc`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib2}`);
|
||||
runCLI(
|
||||
`generate @nrwl/js:lib ${mypublishablelib} --publishable --importPath=@${proj}/${mypublishablelib}`
|
||||
);
|
||||
@ -195,7 +195,7 @@ describe('Nx Affected and Graph Tests', () => {
|
||||
function generateAll() {
|
||||
runCLI(`generate @nrwl/web:app ${myapp}`);
|
||||
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"`);
|
||||
}
|
||||
|
||||
@ -211,7 +211,7 @@ describe('Nx Affected and Graph Tests', () => {
|
||||
expect(runCLI('affected:apps')).toContain(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(myapp2);
|
||||
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 ${myapp2}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib2} --bundler=tsc`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib2}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mypublishablelib}`);
|
||||
|
||||
const app1ElementSpec = readFile(
|
||||
@ -413,8 +413,8 @@ describe('Nx Affected and Graph Tests', () => {
|
||||
runCLI(`generate @nrwl/web:app ${myapp}`);
|
||||
runCLI(`generate @nrwl/web:app ${myapp2}`);
|
||||
runCLI(`generate @nrwl/web:app ${myapp3}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib} --bundler=tsc`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib2} --bundler=tsc`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib}`);
|
||||
runCLI(`generate @nrwl/js:lib ${mylib2}`);
|
||||
|
||||
runCommand(`git init`);
|
||||
runCommand(`git config user.email "test@test.com"`);
|
||||
|
||||
@ -123,4 +123,14 @@ npx nx g lib mylib --directory=myapp
|
||||
|
||||
{% /tab %}
|
||||
|
||||
{% tab label="Non-buildable library" %}
|
||||
|
||||
Generate a non-buildable library.
|
||||
|
||||
```bash
|
||||
npx nx g @nrwl/js:lib mylib --bundler=none
|
||||
```
|
||||
|
||||
{% /tab %}
|
||||
|
||||
{% /tabs %}
|
||||
|
||||
@ -212,7 +212,7 @@ describe('lib', () => {
|
||||
).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/package.json`)).toBeFalsy();
|
||||
expect(tree.exists(`libs/my-dir/my-lib/package.json`)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should update project configuration', async () => {
|
||||
@ -739,14 +739,14 @@ describe('lib', () => {
|
||||
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, {
|
||||
...defaultOptions,
|
||||
name: 'myLib',
|
||||
});
|
||||
|
||||
const config = readProjectConfiguration(tree, 'my-lib');
|
||||
expect(config.targets.build).not.toBeDefined();
|
||||
expect(config.targets.build).toBeDefined();
|
||||
});
|
||||
|
||||
it('should generate the build target', async () => {
|
||||
|
||||
@ -384,17 +384,18 @@ function normalizeOptions(
|
||||
* and no compiler was provided.
|
||||
*
|
||||
* 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
|
||||
* we default to `tsc`. In the previous version of this, it would set `buildable` to true
|
||||
* 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.compiler ??
|
||||
(options.buildable || options.publishable ? 'tsc' : 'none');
|
||||
options.bundler = options.bundler ?? options.compiler ?? 'tsc';
|
||||
|
||||
if (options.publishable) {
|
||||
if (!options.importPath) {
|
||||
@ -404,11 +405,15 @@ function normalizeOptions(
|
||||
}
|
||||
|
||||
if (options.bundler === 'none') {
|
||||
// It must be buildable, so default to 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');
|
||||
if (options.config === 'npm-scripts') {
|
||||
options.unitTestRunner = 'none';
|
||||
|
||||
@ -114,7 +114,7 @@
|
||||
"description": "The bundler to use. Choosing 'none' means this library is not buildable.",
|
||||
"type": "string",
|
||||
"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-priority": "important"
|
||||
},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user