1 | "use strict";
|
2 | var __assign = (this && this.__assign) || function () {
|
3 | __assign = Object.assign || function(t) {
|
4 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
5 | s = arguments[i];
|
6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
7 | t[p] = s[p];
|
8 | }
|
9 | return t;
|
10 | };
|
11 | return __assign.apply(this, arguments);
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.PiletWebpackPlugin = void 0;
|
15 | var webpack_1 = require("webpack");
|
16 | var helpers_1 = require("./helpers");
|
17 | var pluginName = 'PiletWebpackPlugin';
|
18 | var PiletWebpackPlugin = (function () {
|
19 | function PiletWebpackPlugin(options) {
|
20 | this.options = options;
|
21 | }
|
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.piletV1WebpackConfigEnhancer = function (compiler) {
|
31 | var name = this.options.name;
|
32 | var config = compiler.options;
|
33 | var shortName = name.replace(/\W/gi, '');
|
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);
|
38 | var plugins = [
|
39 | new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)),
|
40 | new webpack_1.BannerPlugin({
|
41 | banner: "//@pilet v:1(" + prName + ")",
|
42 | entryOnly: true,
|
43 | include: mainEntry + ".js",
|
44 | raw: true,
|
45 | }),
|
46 | ];
|
47 | compiler.hooks.afterEnvironment.tap(pluginName, function () {
|
48 | config.output.jsonpFunction = "" + prName;
|
49 | config.output.library = name;
|
50 | config.output.libraryTarget = 'umd';
|
51 | config.output.auxiliaryComment = {
|
52 | 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;",
|
53 | };
|
54 | });
|
55 | return plugins;
|
56 | };
|
57 | PiletWebpackPlugin.prototype.piletV2WebpackConfigEnhancer = function (compiler) {
|
58 | var _a = this.options, name = _a.name, importmap = _a.importmap;
|
59 | var config = compiler.options;
|
60 | var shortName = name.replace(/\W/gi, '');
|
61 | var prName = "wp4Chunkpr_" + shortName;
|
62 | var mainEntry = Object.keys(config.entry)[0];
|
63 | (0, helpers_1.withExternals)(config, this.externals);
|
64 | (0, helpers_1.setEnvironment)(this.variables);
|
65 | var dependencies = (0, helpers_1.getDependencies)(importmap, config);
|
66 | var plugins = [
|
67 | new webpack_1.DefinePlugin((0, helpers_1.getDefineVariables)(this.variables)),
|
68 | new webpack_1.BannerPlugin({
|
69 | banner: "//@pilet v:2(" + prName + "," + JSON.stringify(dependencies) + ")",
|
70 | entryOnly: true,
|
71 | include: mainEntry + ".js",
|
72 | raw: true,
|
73 | }),
|
74 | ];
|
75 | compiler.hooks.afterEnvironment.tap(pluginName, function () {
|
76 | config.module.rules.push({ parser: { system: false } });
|
77 | config.output.jsonpFunction = "" + prName;
|
78 | config.output.libraryTarget = 'system';
|
79 | });
|
80 | return plugins;
|
81 | };
|
82 | PiletWebpackPlugin.prototype.setup = function (compiler) {
|
83 | 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;
|
84 | var environment = process.env.NODE_ENV || 'development';
|
85 | this.variables = __assign(__assign({}, (0, helpers_1.getVariables)(name, version, environment)), this.options.variables);
|
86 | this.externals = externals;
|
87 | switch (schema) {
|
88 | case 'v1':
|
89 | return this.piletV1WebpackConfigEnhancer(compiler);
|
90 | case 'v2':
|
91 | return this.piletV2WebpackConfigEnhancer(compiler);
|
92 | case 'v0':
|
93 | case 'none':
|
94 | default:
|
95 | return this.piletVxWebpackConfigEnhancer(compiler);
|
96 | }
|
97 | };
|
98 | PiletWebpackPlugin.prototype.apply = function (compiler) {
|
99 | var plugins = this.setup(compiler);
|
100 | plugins.forEach(function (plugin) { return plugin.apply(compiler); });
|
101 | };
|
102 | return PiletWebpackPlugin;
|
103 | }());
|
104 | exports.PiletWebpackPlugin = PiletWebpackPlugin;
|
105 |
|
\ | No newline at end of file |