pilet-webpack-plugin 0.6.8 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/helpers.d.ts CHANGED
@@ -1,3 +1,8 @@
1
+ import { Configuration } from 'webpack';
2
+ import { SharedDependency } from './types';
1
3
  export declare function getVariables(name: string, version: string, env: string): Record<string, string>;
2
4
  export declare function setEnvironment(variables: Record<string, string>): void;
3
5
  export declare function getDefineVariables(variables: Record<string, string>): {};
6
+ export declare function getExternals(piral: string): any[];
7
+ export declare function getDependencies(importmap: Array<SharedDependency>, compilerOptions: Configuration): {};
8
+ export declare function withExternals(compilerOptions: Configuration, externals: Array<string>): void;
package/lib/helpers.js CHANGED
@@ -1,6 +1,15 @@
1
1
  "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDefineVariables = exports.setEnvironment = exports.getVariables = void 0;
12
+ exports.withExternals = exports.getDependencies = exports.getExternals = exports.getDefineVariables = exports.setEnvironment = exports.getVariables = void 0;
4
13
  function getVariables(name, version, env) {
5
14
  return {
6
15
  NODE_ENV: env,
@@ -23,4 +32,56 @@ function getDefineVariables(variables) {
23
32
  }, {});
24
33
  }
25
34
  exports.getDefineVariables = getDefineVariables;
35
+ function getExternals(piral) {
36
+ var _a, _b;
37
+ var shellPkg = require(piral + "/package.json");
38
+ var piralExternals = (_b = (_a = shellPkg.pilets) === null || _a === void 0 ? void 0 : _a.externals) !== null && _b !== void 0 ? _b : [];
39
+ return __spreadArray(__spreadArray([], piralExternals, true), [
40
+ '@dbeining/react-atom',
41
+ '@libre/atom',
42
+ 'history',
43
+ 'react',
44
+ 'react-dom',
45
+ 'react-router',
46
+ 'react-router-dom',
47
+ 'tslib',
48
+ 'path-to-regexp',
49
+ ], false);
50
+ }
51
+ exports.getExternals = getExternals;
52
+ function getDependencies(importmap, compilerOptions) {
53
+ var dependencies = {};
54
+ var entry = compilerOptions.entry, externals = compilerOptions.externals;
55
+ if (typeof entry === 'object' && entry && Array.isArray(externals) && typeof externals[0] === 'object') {
56
+ for (var _i = 0, importmap_1 = importmap; _i < importmap_1.length; _i++) {
57
+ var dep = importmap_1[_i];
58
+ dependencies[dep.id] = dep.ref;
59
+ externals[0][dep.name] = dep.id;
60
+ if (dep.type === 'local') {
61
+ entry[dep.ref.replace(/\.js$/, '')] = dep.entry;
62
+ }
63
+ }
64
+ }
65
+ return dependencies;
66
+ }
67
+ exports.getDependencies = getDependencies;
68
+ function withExternals(compilerOptions, externals) {
69
+ var current = compilerOptions.externals;
70
+ var arrayExternals = Array.isArray(current) ? current : [current];
71
+ var objectExternal = externals.reduce(function (external, dep) {
72
+ external[dep] = dep;
73
+ return external;
74
+ }, {});
75
+ var newExternals = arrayExternals.filter(function (external) {
76
+ if (typeof external === 'object' && Object.keys(external).length) {
77
+ for (var dep in external) {
78
+ objectExternal[dep] = external[dep];
79
+ }
80
+ return false;
81
+ }
82
+ return true;
83
+ });
84
+ compilerOptions.externals = __spreadArray([objectExternal], newExternals, true);
85
+ }
86
+ exports.withExternals = withExternals;
26
87
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":";;;AAAA,SAAgB,YAAY,CAAC,IAAY,EAAE,OAAe,EAAE,GAAW;IACrE,OAAO;QACL,QAAQ,EAAE,GAAG;QACb,UAAU,EAAE,IAAI,IAAI,EAAE,CAAC,YAAY,EAAE;QACrC,eAAe,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;QACzC,kBAAkB,EAAE,OA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,CAAC,EAAE,CAAC;YAEhC,IAAI,GAAG,CAAC,IAAI,KAAK,OAAO,EAAE;gBACxB,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC;aACjD;SACF;KACF;IAED,OAAO,YAAY,CAAC;AACtB,CAAC;AAhBD,0CAgBC;AAED,SAAgB,aAAa,CAAC,eAA8B,EAAE,SAAwB;IACpF,IAAM,OAAO,GAAG,eAAe,CAAC,SAAS,CAAC;IAC1C,IAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;IAEpE,IAAM,cAAc,GAAG,SAAS,CAAC,MAAM,CAAC,UAAC,QAAQ,EAAE,GAAG;QACpD,QAAQ,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;QACpB,OAAO,QAAQ,CAAC;IAClB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAM,YAAY,GAAG,cAAc,CAAC,MAAM,CAAC,UAAA,QAAQ;QACjD,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,MAAM,EAAE;YAChE,KAAK,IAAM,GAAG,IAAI,QAAQ,EAAE;gBAC1B,cAAc,CAAC,GAAG,CAAC,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;aACrC;YACD,OAAO,KAAK,CAAC;SACd;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;IAEH,eAAe,CAAC,SAAS,kBAAI,cAAc,GAAK,YAAY,OAAC,CAAC;AAChE,CAAC;AApBD,sCAoBC"}
package/lib/index.d.ts CHANGED
@@ -1,33 +1,14 @@
1
- import { Plugin, Compiler } from 'webpack';
2
- export interface PiletWebpackPluginOptions {
3
- /**
4
- * The name of the pilet.
5
- */
6
- name: string;
7
- /**
8
- * The version of the pilet.
9
- */
10
- version: string;
11
- /**
12
- * The name of the Piral instance / app shell.
13
- */
14
- piral: string;
15
- /**
16
- * The schema version. By default, v1 is used.
17
- */
18
- schema?: 'v0' | 'v1' | 'none';
19
- /**
20
- * The shared dependencies. By default, these are read from the
21
- * Piral instance.
22
- */
23
- externals?: Array<string>;
24
- /**
25
- * Additional environment variables to define.
26
- */
27
- variables?: Record<string, string>;
28
- }
1
+ import { Plugin, Compiler, BannerPlugin, DefinePlugin } from 'webpack';
2
+ import { PiletWebpackPluginOptions } from './types';
29
3
  export declare class PiletWebpackPlugin implements Plugin {
30
4
  private options;
5
+ private variables;
6
+ private externals;
31
7
  constructor(options: PiletWebpackPluginOptions);
8
+ piletVxWebpackConfigEnhancer(compiler: Compiler): DefinePlugin[];
9
+ piletV0WebpackConfigEnhancer(compiler: Compiler): (DefinePlugin | BannerPlugin)[];
10
+ piletV1WebpackConfigEnhancer(compiler: Compiler): (DefinePlugin | BannerPlugin)[];
11
+ piletV2WebpackConfigEnhancer(compiler: Compiler): (DefinePlugin | BannerPlugin)[];
12
+ setup(compiler: Compiler): DefinePlugin[];
32
13
  apply(compiler: Compiler): void;
33
14
  }
package/lib/index.js CHANGED
@@ -10,75 +10,119 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __spreadArrays = (this && this.__spreadArrays) || function () {
14
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
15
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
16
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
17
- r[k] = a[j];
18
- return r;
19
- };
20
13
  Object.defineProperty(exports, "__esModule", { value: true });
21
14
  exports.PiletWebpackPlugin = void 0;
22
15
  var webpack_1 = require("webpack");
23
16
  var helpers_1 = require("./helpers");
24
17
  var pluginName = 'PiletWebpackPlugin';
25
- function getExternals(piral) {
26
- var _a, _b;
27
- var shellPkg = require(piral + "/package.json");
28
- var piralExternals = (_b = (_a = shellPkg.pilets) === null || _a === void 0 ? void 0 : _a.externals) !== null && _b !== void 0 ? _b : [];
29
- return __spreadArrays(piralExternals, [
30
- '@dbeining/react-atom',
31
- '@libre/atom',
32
- 'history',
33
- 'react',
34
- 'react-dom',
35
- 'react-router',
36
- 'react-router-dom',
37
- 'tslib',
38
- 'path-to-regexp',
39
- ]);
40
- }
41
18
  var PiletWebpackPlugin = /** @class */ (function () {
42
19
  function PiletWebpackPlugin(options) {
43
20
  this.options = options;
44
21
  }
45
- PiletWebpackPlugin.prototype.apply = function (compiler) {
46
- var environment = process.env.NODE_ENV || 'development';
47
- var _a = this.options, name = _a.name, version = _a.version, piral = _a.piral, _b = _a.externals, externals = _b === void 0 ? getExternals(piral) : _b, schema = _a.schema;
22
+ PiletWebpackPlugin.prototype.piletVxWebpackConfigEnhancer = function (compiler) {
23
+ var config = compiler.options;
24
+ (0, helpers_1.setEnvironment)(this.variables);
25
+ (0, helpers_1.withExternals)(config, this.externals);
26
+ var plugins = [new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables))];
27
+ compiler.hooks.afterEnvironment.tap(pluginName, function () { });
28
+ return plugins;
29
+ };
30
+ PiletWebpackPlugin.prototype.piletV0WebpackConfigEnhancer = function (compiler) {
31
+ var name = this.options.name;
32
+ var config = compiler.options;
48
33
  var shortName = name.replace(/\W/gi, '');
49
- var jsonpFunction = "pr_" + shortName;
50
- var variables = __assign(__assign({}, helpers_1.getVariables(name, version, environment)), this.options.variables);
34
+ var prName = "wp4Chunkpr_" + shortName;
35
+ var mainEntry = Object.keys(config.entry)[0];
36
+ (0, helpers_1.setEnvironment)(this.variables);
37
+ (0, helpers_1.withExternals)(config, this.externals);
51
38
  var plugins = [
52
- new webpack_1.DefinePlugin(helpers_1.getDefineVariables(variables)),
39
+ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)),
40
+ new webpack_1.BannerPlugin({
41
+ banner: "//@pilet v:0",
42
+ entryOnly: true,
43
+ include: mainEntry + ".js",
44
+ raw: true,
45
+ }),
53
46
  ];
54
- if (schema !== 'none') {
55
- var bannerSuffix = schema === 'v1' ? "1(" + jsonpFunction + ")" : "0";
56
- plugins.push(new webpack_1.BannerPlugin({
57
- banner: "//@pilet v:" + bannerSuffix,
47
+ compiler.hooks.afterEnvironment.tap(pluginName, function () {
48
+ config.output.jsonpFunction = "" + prName;
49
+ config.output.library = name;
50
+ config.output.libraryTarget = 'umd';
51
+ });
52
+ return plugins;
53
+ };
54
+ PiletWebpackPlugin.prototype.piletV1WebpackConfigEnhancer = function (compiler) {
55
+ var name = this.options.name;
56
+ var config = compiler.options;
57
+ var shortName = name.replace(/\W/gi, '');
58
+ var prName = "wp4Chunkpr_" + shortName;
59
+ var mainEntry = Object.keys(config.entry)[0];
60
+ (0, helpers_1.setEnvironment)(this.variables);
61
+ (0, helpers_1.withExternals)(config, this.externals);
62
+ var plugins = [
63
+ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)),
64
+ new webpack_1.BannerPlugin({
65
+ banner: "//@pilet v:1(" + prName + ")",
58
66
  entryOnly: true,
59
- include: /\.js$/,
67
+ include: mainEntry + ".js",
60
68
  raw: true,
61
- }));
62
- }
63
- helpers_1.setEnvironment(variables);
64
- plugins.forEach(function (plugin) { return plugin.apply(compiler); });
69
+ }),
70
+ ];
65
71
  compiler.hooks.afterEnvironment.tap(pluginName, function () {
66
- var current = compiler.options.externals;
67
- compiler.options.output.jsonpFunction = jsonpFunction + "_chunks";
68
- compiler.options.output.library = name;
69
- if (schema !== 'none') {
70
- compiler.options.output.libraryTarget = 'umd';
71
- }
72
- if (schema === 'v1') {
73
- var reset = environment !== 'production' ? "delete " + jsonpFunction + "_chunks;" : '';
74
- compiler.options.output.auxiliaryComment = {
75
- commonjs2: "\nfunction define(d,k){" + reset + "(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window." + jsonpFunction + ")));}define.amd=!0;",
76
- };
77
- }
78
- compiler.options.externals = Array.isArray(current)
79
- ? __spreadArrays(current, externals) : current
80
- ? __spreadArrays([current], externals) : externals;
72
+ config.output.jsonpFunction = "" + prName;
73
+ config.output.library = name;
74
+ config.output.libraryTarget = 'umd';
75
+ config.output.auxiliaryComment = {
76
+ commonjs2: "\nfunction define(d,k){if(typeof document!=='undefined'){var _r=" + prName + ";delete " + prName + ";document.currentScript.app=k.apply(null,d.map(_r))}}define.amd=!0;",
77
+ };
81
78
  });
79
+ return plugins;
80
+ };
81
+ PiletWebpackPlugin.prototype.piletV2WebpackConfigEnhancer = function (compiler) {
82
+ var _a = this.options, name = _a.name, importmap = _a.importmap;
83
+ var config = compiler.options;
84
+ var shortName = name.replace(/\W/gi, '');
85
+ var prName = "wp4Chunkpr_" + shortName;
86
+ var mainEntry = Object.keys(config.entry)[0];
87
+ (0, helpers_1.withExternals)(config, this.externals);
88
+ (0, helpers_1.setEnvironment)(this.variables);
89
+ var dependencies = (0, helpers_1.getDependencies)(importmap, config);
90
+ var plugins = [
91
+ new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)),
92
+ new webpack_1.BannerPlugin({
93
+ banner: "//@pilet v:2(" + prName + "," + JSON.stringify(dependencies) + ")",
94
+ entryOnly: true,
95
+ include: mainEntry + ".js",
96
+ raw: true,
97
+ }),
98
+ ];
99
+ compiler.hooks.afterEnvironment.tap(pluginName, function () {
100
+ config.module.rules.push({ parser: { system: false } });
101
+ config.output.jsonpFunction = "" + prName;
102
+ config.output.libraryTarget = 'system';
103
+ });
104
+ return plugins;
105
+ };
106
+ PiletWebpackPlugin.prototype.setup = function (compiler) {
107
+ var _a = this.options, name = _a.name, version = _a.version, piral = _a.piral, _b = _a.externals, externals = _b === void 0 ? (0, helpers_1.getExternals)(piral) : _b, schema = _a.schema;
108
+ var environment = process.env.NODE_ENV || 'development';
109
+ this.variables = __assign(__assign({}, (0, helpers_1.getVariables)(name, version, environment)), this.options.variables);
110
+ this.externals = externals;
111
+ switch (schema) {
112
+ case 'v0':
113
+ return this.piletV0WebpackConfigEnhancer(compiler);
114
+ case 'v1':
115
+ return this.piletV1WebpackConfigEnhancer(compiler);
116
+ case 'v2':
117
+ return this.piletV2WebpackConfigEnhancer(compiler);
118
+ case 'none':
119
+ default:
120
+ return this.piletVxWebpackConfigEnhancer(compiler);
121
+ }
122
+ };
123
+ PiletWebpackPlugin.prototype.apply = function (compiler) {
124
+ var plugins = this.setup(compiler);
125
+ plugins.forEach(function (plugin) { return plugin.apply(compiler); });
82
126
  };
83
127
  return PiletWebpackPlugin;
84
128
  }());
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA,mCAAuE;AACvE,qCAA6E;AAE7E,IAAM,UAAU,GAAG,oBAAoB,CAAC;AA8BxC,SAAS,YAAY,CAAC,KAAa;;IACjC,IAAM,QAAQ,GAAG,OAAO,CAAI,KAAK,kBAAe,CAAC,CAAC;IAClD,IAAM,cAAc,eAAG,QAAQ,CAAC,MAAM,0CAAE,SAAS,mCAAI,EAAE,CAAC;IACxD,sBACK,cAAc;QACjB,sBAAsB;QACtB,aAAa;QACb,SAAS;QACT,OAAO;QACP,WAAW;QACX,cAAc;QACd,kBAAkB;QAClB,OAAO;QACP,gBAAgB;OAChB;AACJ,CAAC;AAED;IACE,4BAAoB,OAAkC;QAAlC,YAAO,GAAP,OAAO,CAA2B;IAAI,CAAC;IAE3D,kCAAK,GAAL,UAAM,QAAkB;QACtB,IAAM,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,aAAa,CAAC;QACpD,IAAA,KAAoE,IAAI,CAAC,OAAO,EAA9E,IAAI,UAAA,EAAE,OAAO,aAAA,EAAE,KAAK,WAAA,EAAE,iBAA+B,EAA/B,SAAS,mBAAG,YAAY,CAAC,KAAK,CAAC,KAAA,EAAE,MAAM,YAAiB,CAAC;QACv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CAAC,QAAQ,IAAI,aAAa,CAAC;QAC1D,IAAI,CAAC,SAAS,yBACT,IAAA,sBAAY,EAAC,IAAI,EAAE,OAAO,EAAE,WAAW,CAAC,GACxC,IAAI,CAAC,OAAO,CAAC,SAAS,CAC1B,CAAC;QACF,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAE3B,QAAQ,MAAM,EAAE;YACd,KAAK,IAAI;gBACP,OAAO,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CAAC,CAAC;YACrD,KAAK,IAAI;gBACP,OAAO,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CAAC,CAAC;YACrD,KAAK,IAAI;gBACP,OAAO,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CAAC,CAAC;YACrD,KAAK,MAAM,CAAC;YACZ;gBACE,OAAO,IAAI,CAAC,4BAA4B,CAAC,QAAQ,CAAC,CAAC;SACtD;IACH,CAAC;IAED,kCAAK,GAAL,UAAM,QAAkB;QACtB,IAAM,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QACrC,OAAO,CAAC,OAAO,CAAC,UAAA,MAAM,IAAI,OAAA,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAtB,CAAsB,CAAC,CAAC;IACpD,CAAC;IACH,yBAAC;AAAD,CAAC,AAvID,IAuIC;AAvIY,gDAAkB"}
package/lib/types.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ export interface SharedDependency {
2
+ id: string;
3
+ name: string;
4
+ ref: string;
5
+ type: 'local' | 'remote';
6
+ entry: string;
7
+ }
8
+ export interface PiletWebpackPluginOptions {
9
+ /**
10
+ * The name of the pilet.
11
+ */
12
+ name: string;
13
+ /**
14
+ * The version of the pilet.
15
+ */
16
+ version: string;
17
+ /**
18
+ * The name of the Piral instance / app shell.
19
+ */
20
+ piral: string;
21
+ /**
22
+ * The schema version. By default, v1 is used.
23
+ */
24
+ schema?: 'v0' | 'v1' | 'v2' | 'none';
25
+ /**
26
+ * The shared dependencies. By default, these are read from the
27
+ * Piral instance.
28
+ */
29
+ externals?: Array<string>;
30
+ /**
31
+ * Additional environment variables to define.
32
+ */
33
+ variables?: Record<string, string>;
34
+ /**
35
+ * The shared dependencies to consider.
36
+ */
37
+ importmap?: Array<SharedDependency>;
38
+ }
package/lib/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pilet-webpack-plugin",
3
- "version": "0.6.8",
3
+ "version": "0.8.1",
4
4
  "description": "Webpack plugin for generating a valid pilet bundle.",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -31,5 +31,5 @@
31
31
  "devDependencies": {
32
32
  "webpack": "4.42.0"
33
33
  },
34
- "gitHead": "b5f0d3ed7643c0232ab762d35155d5659d3a19ad"
34
+ "gitHead": "b03ec9ab419f7fdfdabd0c46558b1993229067c4"
35
35
  }
package/src/helpers.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { Configuration } from 'webpack';
2
+ import { SharedDependency } from './types';
3
+
1
4
  export function getVariables(name: string, version: string, env: string): Record<string, string> {
2
5
  return {
3
6
  NODE_ENV: env,
@@ -18,3 +21,60 @@ export function getDefineVariables(variables: Record<string, string>) {
18
21
  return obj;
19
22
  }, {});
20
23
  }
24
+
25
+ export function getExternals(piral: string) {
26
+ const shellPkg = require(`${piral}/package.json`);
27
+ const piralExternals = shellPkg.pilets?.externals ?? [];
28
+ return [
29
+ ...piralExternals,
30
+ '@dbeining/react-atom',
31
+ '@libre/atom',
32
+ 'history',
33
+ 'react',
34
+ 'react-dom',
35
+ 'react-router',
36
+ 'react-router-dom',
37
+ 'tslib',
38
+ 'path-to-regexp',
39
+ ];
40
+ }
41
+
42
+ export function getDependencies(importmap: Array<SharedDependency>, compilerOptions: Configuration) {
43
+ const dependencies = {};
44
+ const { entry, externals } = compilerOptions;
45
+
46
+ if (typeof entry === 'object' && entry && Array.isArray(externals) && typeof externals[0] === 'object') {
47
+ for (const dep of importmap) {
48
+ dependencies[dep.id] = dep.ref;
49
+ externals[0][dep.name] = dep.id;
50
+
51
+ if (dep.type === 'local') {
52
+ entry[dep.ref.replace(/\.js$/, '')] = dep.entry;
53
+ }
54
+ }
55
+ }
56
+
57
+ return dependencies;
58
+ }
59
+
60
+ export function withExternals(compilerOptions: Configuration, externals: Array<string>) {
61
+ const current = compilerOptions.externals;
62
+ const arrayExternals = Array.isArray(current) ? current : [current];
63
+
64
+ const objectExternal = externals.reduce((external, dep) => {
65
+ external[dep] = dep;
66
+ return external;
67
+ }, {});
68
+
69
+ const newExternals = arrayExternals.filter(external => {
70
+ if (typeof external === 'object' && Object.keys(external).length) {
71
+ for (const dep in external) {
72
+ objectExternal[dep] = external[dep];
73
+ }
74
+ return false;
75
+ }
76
+ return true;
77
+ });
78
+
79
+ compilerOptions.externals = [objectExternal, ...newExternals];
80
+ }
package/src/index.ts CHANGED
@@ -1,107 +1,149 @@
1
1
  import { Plugin, Compiler, BannerPlugin, DefinePlugin } from 'webpack';
2
- import { setEnvironment, getDefineVariables, getVariables } from './helpers';
2
+ import {
3
+ setEnvironment,
4
+ getDefineVariables,
5
+ getVariables,
6
+ getExternals,
7
+ withExternals,
8
+ getDependencies,
9
+ } from './helpers';
10
+ import { PiletWebpackPluginOptions } from './types';
3
11
 
4
12
  const pluginName = 'PiletWebpackPlugin';
5
13
 
6
- export interface PiletWebpackPluginOptions {
7
- /**
8
- * The name of the pilet.
9
- */
10
- name: string;
11
- /**
12
- * The version of the pilet.
13
- */
14
- version: string;
15
- /**
16
- * The name of the Piral instance / app shell.
17
- */
18
- piral: string;
19
- /**
20
- * The schema version. By default, v1 is used.
21
- */
22
- schema?: 'v0' | 'v1' | 'none';
23
- /**
24
- * The shared dependencies. By default, these are read from the
25
- * Piral instance.
26
- */
27
- externals?: Array<string>;
28
- /**
29
- * Additional environment variables to define.
30
- */
31
- variables?: Record<string, string>;
32
- }
14
+ export class PiletWebpackPlugin implements Plugin {
15
+ private variables: Record<string, string>;
16
+ private externals: Array<string>;
33
17
 
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
- }
18
+ constructor(private options: PiletWebpackPluginOptions) {}
50
19
 
51
- export class PiletWebpackPlugin implements Plugin {
52
- constructor(private options: PiletWebpackPluginOptions) { }
20
+ piletVxWebpackConfigEnhancer(compiler: Compiler) {
21
+ const config = compiler.options;
53
22
 
54
- apply(compiler: Compiler) {
55
- const environment = process.env.NODE_ENV || 'development';
56
- const { name, version, piral, externals = getExternals(piral), schema } = this.options;
23
+ setEnvironment(this.variables);
24
+ withExternals(config, this.externals);
25
+
26
+ const plugins = [new DefinePlugin(getDefineVariables(this.variables))];
27
+ compiler.hooks.afterEnvironment.tap(pluginName, () => {});
28
+ return plugins;
29
+ }
30
+
31
+ piletV0WebpackConfigEnhancer(compiler: Compiler) {
32
+ const { name } = this.options;
33
+ const config = compiler.options;
57
34
  const shortName = name.replace(/\W/gi, '');
58
- const jsonpFunction = `pr_${shortName}`;
59
- const variables = {
60
- ...getVariables(name, version, environment),
61
- ...this.options.variables,
62
- };
35
+ const prName = `wp4Chunkpr_${shortName}`;
36
+ const [mainEntry] = Object.keys(config.entry);
37
+
38
+ setEnvironment(this.variables);
39
+ withExternals(config, this.externals);
40
+
63
41
  const plugins = [
64
- new DefinePlugin(getDefineVariables(variables)),
42
+ new DefinePlugin(getDefineVariables(this.variables)),
43
+ new BannerPlugin({
44
+ banner: `//@pilet v:0`,
45
+ entryOnly: true,
46
+ include: `${mainEntry}.js`,
47
+ raw: true,
48
+ }),
65
49
  ];
66
50
 
67
- if (schema !== 'none') {
68
- const bannerSuffix = schema === 'v1' ? `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
- }
51
+ compiler.hooks.afterEnvironment.tap(pluginName, () => {
52
+ config.output.jsonpFunction = `${prName}`;
53
+ config.output.library = name;
54
+ config.output.libraryTarget = 'umd';
55
+ });
79
56
 
80
- setEnvironment(variables);
57
+ return plugins;
58
+ }
81
59
 
82
- plugins.forEach(plugin => plugin.apply(compiler));
60
+ piletV1WebpackConfigEnhancer(compiler: Compiler) {
61
+ const { name } = this.options;
62
+ const config = compiler.options;
63
+ const shortName = name.replace(/\W/gi, '');
64
+ const prName = `wp4Chunkpr_${shortName}`;
65
+ const [mainEntry] = Object.keys(config.entry);
66
+
67
+ setEnvironment(this.variables);
68
+ withExternals(config, this.externals);
69
+
70
+ const plugins = [
71
+ new DefinePlugin(getDefineVariables(this.variables)),
72
+ new BannerPlugin({
73
+ banner: `//@pilet v:1(${prName})`,
74
+ entryOnly: true,
75
+ include: `${mainEntry}.js`,
76
+ raw: true,
77
+ }),
78
+ ];
83
79
 
84
80
  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;
81
+ config.output.jsonpFunction = `${prName}`;
82
+ config.output.library = name;
83
+ config.output.libraryTarget = 'umd';
84
+ config.output.auxiliaryComment = {
85
+ commonjs2: `\nfunction define(d,k){if(typeof document!=='undefined'){var _r=${prName};delete ${prName};document.currentScript.app=k.apply(null,d.map(_r))}}define.amd=!0;`,
86
+ } as any;
105
87
  });
88
+
89
+ return plugins;
90
+ }
91
+
92
+ piletV2WebpackConfigEnhancer(compiler: Compiler) {
93
+ const { name, importmap } = this.options;
94
+ const config = compiler.options;
95
+ const shortName = name.replace(/\W/gi, '');
96
+ const prName = `wp4Chunkpr_${shortName}`;
97
+ const [mainEntry] = Object.keys(config.entry);
98
+
99
+ withExternals(config, this.externals);
100
+ setEnvironment(this.variables);
101
+
102
+ const dependencies = getDependencies(importmap, config);
103
+
104
+ const plugins = [
105
+ new DefinePlugin(getDefineVariables(this.variables)),
106
+ new BannerPlugin({
107
+ banner: `//@pilet v:2(${prName},${JSON.stringify(dependencies)})`,
108
+ entryOnly: true,
109
+ include: `${mainEntry}.js`,
110
+ raw: true,
111
+ }),
112
+ ];
113
+
114
+ compiler.hooks.afterEnvironment.tap(pluginName, () => {
115
+ config.module.rules.push({ parser: { system: false } });
116
+ config.output.jsonpFunction = `${prName}`;
117
+ config.output.libraryTarget = 'system';
118
+ });
119
+
120
+ return plugins;
121
+ }
122
+
123
+ setup(compiler: Compiler) {
124
+ const { name, version, piral, externals = getExternals(piral), schema } = this.options;
125
+ const environment = process.env.NODE_ENV || 'development';
126
+ this.variables = {
127
+ ...getVariables(name, version, environment),
128
+ ...this.options.variables,
129
+ };
130
+ this.externals = externals;
131
+
132
+ switch (schema) {
133
+ case 'v0':
134
+ return this.piletV0WebpackConfigEnhancer(compiler);
135
+ case 'v1':
136
+ return this.piletV1WebpackConfigEnhancer(compiler);
137
+ case 'v2':
138
+ return this.piletV2WebpackConfigEnhancer(compiler);
139
+ case 'none':
140
+ default:
141
+ return this.piletVxWebpackConfigEnhancer(compiler);
142
+ }
143
+ }
144
+
145
+ apply(compiler: Compiler) {
146
+ const plugins = this.setup(compiler);
147
+ plugins.forEach(plugin => plugin.apply(compiler));
106
148
  }
107
149
  }
package/src/types.ts ADDED
@@ -0,0 +1,39 @@
1
+ export interface SharedDependency {
2
+ id: string;
3
+ name: string;
4
+ ref: string;
5
+ type: 'local' | 'remote';
6
+ entry: string;
7
+ }
8
+
9
+ export interface PiletWebpackPluginOptions {
10
+ /**
11
+ * The name of the pilet.
12
+ */
13
+ name: string;
14
+ /**
15
+ * The version of the pilet.
16
+ */
17
+ version: string;
18
+ /**
19
+ * The name of the Piral instance / app shell.
20
+ */
21
+ piral: string;
22
+ /**
23
+ * The schema version. By default, v1 is used.
24
+ */
25
+ schema?: 'v0' | 'v1' | 'v2' | 'none';
26
+ /**
27
+ * The shared dependencies. By default, these are read from the
28
+ * Piral instance.
29
+ */
30
+ externals?: Array<string>;
31
+ /**
32
+ * Additional environment variables to define.
33
+ */
34
+ variables?: Record<string, string>;
35
+ /**
36
+ * The shared dependencies to consider.
37
+ */
38
+ importmap?: Array<SharedDependency>;
39
+ }