forked from yarnpkg/berry
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webpack.config.hook.js
42 lines (36 loc) Β· 1.25 KB
/
webpack.config.hook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
require(`@yarnpkg/monorepo/scripts/setup-ts-execution`);
const {makeConfig} = require(`@yarnpkg/builder/sources/tools/makeConfig`);
const webpack = require(`webpack`);
const {brotliCompressSync} = require(`zlib`);
module.exports = makeConfig({
context: __dirname,
mode: `production`,
optimization: {
minimize: false,
},
entry: {
[`hook`]: `./sources/loader/_entryPoint.ts`,
},
output: {
filename: `[name].js`,
path: `${__dirname}/sources`,
libraryExport: `default`,
libraryTarget: `umd`,
library: `pnpHook`,
},
plugins: [
{apply: compiler => {
compiler.hooks.compilation.tap(`MyPlugin`, compilation => {
compilation.hooks.optimizeChunkAssets.tap(`MyPlugin`, chunks => {
for (const chunk of chunks) {
for (const file of chunk.files) {
compilation.assets[file] = new webpack.sources.RawSource(
`let hook;\n\nmodule.exports = () => {\n if (typeof hook === \`undefined\`)\n hook = require('zlib').brotliDecompressSync(Buffer.from('${brotliCompressSync(compilation.assets[file].source().replace(/\r\n/g, `\n`)).toString(`base64`)}', 'base64')).toString();\n\n return hook;\n};\n`
);
}
}
});
});
}},
],
});