-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(js): add watchIgnore and runBuildTargetDependencies options to sp…
…eed up build
- Loading branch information
Showing
8 changed files
with
316 additions
and
58 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
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,104 @@ | ||
--- | ||
title: JS Node executor examples | ||
description: This page contains examples for the @nx/js:node executor. | ||
--- | ||
|
||
The `@nx/js:node` executor runs the output of a build target. For example, an application uses esbuild ([`@nx/esbuild:esbuild`](/packages/esbuild/executors/esbuild)) to output the bundle to `dist/my-app` folder, which can then be executed by `@nx/js:node`. | ||
|
||
`project.json`: | ||
|
||
```json | ||
"my-app": { | ||
"targets": { | ||
"serve": { | ||
"executor": "@nx/js:node", | ||
"options": { | ||
"buildTarget": "my-app:build" | ||
} | ||
}, | ||
"build": { | ||
"executor": "@nx/esbuild:esbuild", | ||
"options": { | ||
"main": "my-app/src/main.ts", | ||
"output": ["dist/my-app"], | ||
//... | ||
} | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
```bash | ||
npx nx serve my-app | ||
``` | ||
|
||
## Examples | ||
|
||
{% tabs %} | ||
{% tab label="Pass extra Node CLI arguments" %} | ||
|
||
Using `runtimeArgs`, you can pass arguments to the underlying `node` command. For example, if you want to set [`--no-warnings`](https://nodejs.org/api/cli.html#--no-warnings) to silence all Node warnings, then add the following to the `project.json` file. | ||
|
||
```json | ||
"my-app": { | ||
"targets": { | ||
"serve": { | ||
"executor": "@nx/js:node", | ||
"options": { | ||
"runtimeArgs": ["--no-warnings"], | ||
//... | ||
}, | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
{% /tab %} | ||
|
||
{% tab label="Ignore files during watch" %} | ||
|
||
If you have project files that do not affect the application, you can set `watchIgnore` to avoid kicking off a rebuild and restart when the only file changes are the ones you ignore. For example, if you have a `README.md` files, you can safely ignore them with `**/README.md`. | ||
|
||
Note that the glob patterns are matched relative to the workspace root. | ||
|
||
```json | ||
"my-app": { | ||
"targets": { | ||
"serve": { | ||
"executor": "@nx/js:node", | ||
"options": { | ||
"watchIgnore": ["**/README.md"] | ||
//... | ||
}, | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
{% /tab %} | ||
|
||
{% tab label="Run all task dependencies" %} | ||
|
||
If your application build depends on other tasks, and you want those tasks to also be executed, then set the `watchRunBuildTargetDependencies` to `true`. For example, a library may have a task to generate GraphQL schemas, which is consume by the application. In this case, you want to run the generate task before building and running the application. | ||
|
||
This option is also useful when the build consumes a library from its output, not its source. For example, if an executor that supports `buildLibsFromSource` option has it set to `false` (e.g. [`@nx/webpack:webpack`](/packages/webpack/executors/webpack)). | ||
|
||
Note that this option will increase the build time, so use it only when necessary. | ||
|
||
```json | ||
"my-app": { | ||
"targets": { | ||
"serve": { | ||
"executor": "@nx/js:node", | ||
"options": { | ||
"watchRunBuildTargetDependencies": true, | ||
//... | ||
}, | ||
}, | ||
} | ||
} | ||
``` | ||
|
||
{% /tab %} | ||
|
||
{% /tabs %} |
10 changes: 10 additions & 0 deletions
10
packages/js/src/executors/node/lib/any-matching-files.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,10 @@ | ||
import { anyMatchingFiles } from './any-matching-files'; | ||
|
||
describe('anyMatchingFiles', () => { | ||
it('should return true if a file matches any of the patterns', () => { | ||
const fn = anyMatchingFiles(['**/*.txt']); | ||
|
||
expect(fn([{ path: 'a.ts' }, { path: 'b.ts' }])).toBe(false); | ||
expect(fn([{ path: 'a.txt' }, { path: 'b.ts' }])).toBe(true); | ||
}); | ||
}); |
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,14 @@ | ||
import * as minimatch from 'minimatch'; | ||
|
||
export function anyMatchingFiles(patterns: string[]) { | ||
const filters = patterns.map((p) => minimatch.filter(p)); | ||
return (files: { path: string }[]) => { | ||
// Worst-case is nested loop through both files and patterns, but neither should be large. | ||
for (const filter of filters) { | ||
for (const file of files) { | ||
if (filter(file.path)) return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
} |
Oops, something went wrong.