-
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
838170e
commit de59f0d
Showing
5 changed files
with
139 additions
and
48 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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
import { type Compiler, sources, type WebpackPluginInstance } from 'webpack'; | ||
import { | ||
ExecutorContext, | ||
type ProjectGraph, | ||
serializeJson, | ||
} from '@nrwl/devkit'; | ||
import { createPackageJson } from '@nrwl/workspace/src/utilities/create-package-json'; | ||
|
||
import type { BuildNodeBuilderOptions } from './types'; | ||
import { getHelperDependenciesFromProjectGraph } from '@nrwl/js/src/utils/compiler-helper-dependency'; | ||
|
||
export class GeneratePackageJsonWebpackPlugin implements WebpackPluginInstance { | ||
constructor( | ||
private readonly context: ExecutorContext, | ||
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 helperDependencies = getHelperDependenciesFromProjectGraph( | ||
this.context.root, | ||
this.context.projectName | ||
); | ||
|
||
if (helperDependencies.length > 0) { | ||
this.projectGraph.dependencies[this.context.projectName] = | ||
this.projectGraph.dependencies[this.context.projectName].concat( | ||
helperDependencies | ||
); | ||
} | ||
|
||
const packageJson = createPackageJson( | ||
this.context.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