-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node): use webpack compilation hooks to emit package.json
Use the webpack plugin system to create the package.json, this allows for developers to use plugins like CleanWebpackPlugin which clears the dist before the compilation starts. If this plugin is used in combination with the old method of generating the package.json, then it is deleted on compilation start.
- Loading branch information
1 parent
00ee173
commit 26922df
Showing
6 changed files
with
119 additions
and
36 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
42 changes: 42 additions & 0 deletions
42
packages/node/src/utils/generate-package-json-webpack-plugin.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,42 @@ | ||
import { type Compiler, sources, type WebpackPluginInstance } from 'webpack'; | ||
import { type ProjectGraph, serializeJson } from '@nrwl/devkit'; | ||
import { createPackageJson } from '@nrwl/workspace/src/utilities/create-package-json'; | ||
|
||
import type { BuildNodeBuilderOptions } from './types'; | ||
|
||
export class GeneratePackageJsonWebpackPlugin implements WebpackPluginInstance { | ||
constructor( | ||
private readonly projectName: string, | ||
private readonly projectGraph: ProjectGraph, | ||
private readonly options: BuildNodeBuilderOptions | ||
) {} | ||
|
||
apply(compiler: Compiler): void { | ||
const pluginName = this.constructor.name; | ||
|
||
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: 'nx-generate-package-json-plugin', | ||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, | ||
}, | ||
() => { | ||
const packageJson = createPackageJson( | ||
this.projectName, | ||
this.projectGraph, | ||
this.options | ||
); | ||
|
||
packageJson.main = packageJson.main ?? this.options.outputFileName; | ||
|
||
delete packageJson.devDependencies; | ||
|
||
compilation.emitAsset( | ||
'package.json', | ||
new sources.RawSource(serializeJson(packageJson)) | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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