1 | import { Plugin, Compiler, BannerPlugin, DefinePlugin } from 'webpack';
|
2 | import { setEnvironment, getDefineVariables, getVariables } from './helpers';
|
3 |
|
4 | const pluginName = 'PiletWebpackPlugin';
|
5 |
|
6 | export interface PiletWebpackPluginOptions {
|
7 | |
8 |
|
9 |
|
10 | name: string;
|
11 | |
12 |
|
13 |
|
14 | version: string;
|
15 | |
16 |
|
17 |
|
18 | piral: string;
|
19 | |
20 |
|
21 |
|
22 | schema?: 'v0' | 'v1' | 'none';
|
23 | |
24 |
|
25 |
|
26 |
|
27 | externals?: Array<string>;
|
28 | |
29 |
|
30 |
|
31 | variables?: Record<string, string>;
|
32 | }
|
33 |
|
34 | function getExternals(piral: string) {
|
35 | const shellPkg = require(`${piral}/package.json`);
|
36 | const piralExternals = shellPkg.pilets?.externals ?? [];
|
37 | return [
|
38 | ...piralExternals,
|
39 | '@dbeining/react-atom',
|
40 | '@libre/atom',
|
41 | 'history',
|
42 | 'react',
|
43 | 'react-dom',
|
44 | 'react-router',
|
45 | 'react-router-dom',
|
46 | 'tslib',
|
47 | 'path-to-regexp',
|
48 | ];
|
49 | }
|
50 |
|
51 | export class PiletWebpackPlugin implements Plugin {
|
52 | constructor(private options: PiletWebpackPluginOptions) { }
|
53 |
|
54 | apply(compiler: Compiler) {
|
55 | const environment = process.env.NODE_ENV || 'development';
|
56 | const { name, version, piral, externals = getExternals(piral), schema } = this.options;
|
57 | const shortName = name.replace(/\W/gi, '');
|
58 | const jsonpFunction = `pr_${shortName}`;
|
59 | const variables = {
|
60 | ...getVariables(name, version, environment),
|
61 | ...this.options.variables,
|
62 | };
|
63 | const plugins = [
|
64 | new DefinePlugin(getDefineVariables(variables)),
|
65 | ];
|
66 |
|
67 | if (schema !== 'none') {
|
68 | const bannerSuffix = schema ? `1(${jsonpFunction})` : `0`;
|
69 |
|
70 | plugins.push(
|
71 | new BannerPlugin({
|
72 | banner: `//@pilet v:${bannerSuffix}`,
|
73 | entryOnly: true,
|
74 | include: /\.js$/,
|
75 | raw: true,
|
76 | }),
|
77 | );
|
78 | }
|
79 |
|
80 | setEnvironment(variables);
|
81 |
|
82 | plugins.forEach(plugin => plugin.apply(compiler));
|
83 |
|
84 | compiler.hooks.afterEnvironment.tap(pluginName, () => {
|
85 | const current = compiler.options.externals;
|
86 | compiler.options.output.jsonpFunction = `${jsonpFunction}_chunks`;
|
87 | compiler.options.output.library = name;
|
88 |
|
89 | if (schema !== 'none') {
|
90 | compiler.options.output.libraryTarget = 'umd';
|
91 | }
|
92 |
|
93 | if (schema === 'v1') {
|
94 | const reset = environment !== 'production' ? `delete ${jsonpFunction}_chunks;` : '';
|
95 | compiler.options.output.auxiliaryComment = {
|
96 | commonjs2: `\nfunction define(d,k){${reset}(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window.${jsonpFunction})));}define.amd=!0;`,
|
97 | } as any;
|
98 | }
|
99 |
|
100 | compiler.options.externals = Array.isArray(current)
|
101 | ? [...current, ...externals]
|
102 | : current
|
103 | ? [current, ...externals]
|
104 | : externals;
|
105 | });
|
106 | }
|
107 | }
|