-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rspack): add generatePackageJson plugin (#341)
Co-authored-by: Travis Tarr <[email protected]>
- Loading branch information
Showing
7 changed files
with
158 additions
and
6 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
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
87 changes: 87 additions & 0 deletions
87
packages/rspack/src/plugins/generate-package-json-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,87 @@ | ||
import { | ||
ExecutorContext, | ||
detectPackageManager, | ||
serializeJson, | ||
type ProjectGraph, | ||
} from '@nx/devkit'; | ||
import { | ||
HelperDependency, | ||
createLockFile, | ||
createPackageJson, | ||
getHelperDependenciesFromProjectGraph, | ||
getLockFileName, | ||
readTsConfig, | ||
} from '@nx/js'; | ||
import { type Compiler, type RspackPluginInstance } from '@rspack/core'; | ||
import { RawSource } from 'webpack-sources'; | ||
|
||
const pluginName = 'GeneratePackageJsonPlugin'; | ||
|
||
export class GeneratePackageJsonPlugin implements RspackPluginInstance { | ||
private readonly projectGraph: ProjectGraph; | ||
|
||
constructor( | ||
private readonly options: { tsConfig: string; outputFileName: string }, | ||
private readonly context: ExecutorContext | ||
) { | ||
this.projectGraph = context.projectGraph; | ||
} | ||
|
||
apply(compiler: Compiler): void { | ||
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: pluginName, | ||
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL, | ||
}, | ||
() => { | ||
const helperDependencies = getHelperDependenciesFromProjectGraph( | ||
this.context.root, | ||
this.context.projectName, | ||
this.projectGraph | ||
); | ||
|
||
const importHelpers = !!readTsConfig(this.options.tsConfig).options | ||
.importHelpers; | ||
const shouldAddHelperDependency = | ||
importHelpers && | ||
helperDependencies.every( | ||
(dep) => dep.target !== HelperDependency.tsc | ||
); | ||
|
||
if (shouldAddHelperDependency) { | ||
helperDependencies.push({ | ||
type: 'static', | ||
source: this.context.projectName, | ||
target: HelperDependency.tsc, | ||
}); | ||
} | ||
|
||
const packageJson = createPackageJson( | ||
this.context.projectName, | ||
this.projectGraph, | ||
{ | ||
target: this.context.targetName, | ||
root: this.context.root, | ||
isProduction: true, | ||
helperDependencies: helperDependencies.map((dep) => dep.target), | ||
} | ||
); | ||
packageJson.main = packageJson.main ?? this.options.outputFileName; | ||
|
||
compilation.emitAsset( | ||
'package.json', | ||
new RawSource(serializeJson(packageJson)) | ||
); | ||
const packageManager = detectPackageManager(this.context.root); | ||
compilation.emitAsset( | ||
getLockFileName(packageManager), | ||
new RawSource( | ||
createLockFile(packageJson, this.projectGraph, packageManager) | ||
) | ||
); | ||
} | ||
); | ||
}); | ||
} | ||
} |
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