You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1.0 KiB
33 lines
1.0 KiB
import webpack, { type Compiler } from 'webpack'; |
|
|
|
const PLUGIN_NAME = 'BuildModeWebpack'; |
|
|
|
export class BuildModeWebpackPlugin { |
|
apply(compiler: webpack.Compiler) { |
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
|
compilation.hooks.processAssets.tap( |
|
{ |
|
name: PLUGIN_NAME, |
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS, |
|
}, |
|
async () => { |
|
const assets = compilation.getAssets(); |
|
for (const asset of assets) { |
|
if (asset.name.endsWith('plugin.json')) { |
|
const pluginJsonString = asset.source.source().toString(); |
|
const pluginJsonWithBuildMode = JSON.stringify( |
|
{ |
|
...JSON.parse(pluginJsonString), |
|
buildMode: compilation.options.mode, |
|
}, |
|
null, |
|
4 |
|
); |
|
compilation.updateAsset(asset.name, new webpack.sources.RawSource(pluginJsonWithBuildMode)); |
|
} |
|
} |
|
} |
|
); |
|
}); |
|
} |
|
}
|
|
|