UNPKG

3.81 kBJavaScriptView Raw
1"use strict";
2var __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};
13var __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};
20Object.defineProperty(exports, "__esModule", { value: true });
21exports.PiletWebpackPlugin = void 0;
22var webpack_1 = require("webpack");
23var helpers_1 = require("./helpers");
24var pluginName = 'PiletWebpackPlugin';
25function 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}
41var PiletWebpackPlugin = /** @class */ (function () {
42 function PiletWebpackPlugin(options) {
43 this.options = options;
44 }
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;
48 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);
51 var plugins = [
52 new webpack_1.DefinePlugin(helpers_1.getDefineVariables(variables)),
53 ];
54 if (schema !== 'none') {
55 var bannerSuffix = schema ? "1(" + jsonpFunction + ")" : "0";
56 plugins.push(new webpack_1.BannerPlugin({
57 banner: "//@pilet v:" + bannerSuffix,
58 entryOnly: true,
59 include: /\.js$/,
60 raw: true,
61 }));
62 }
63 helpers_1.setEnvironment(variables);
64 plugins.forEach(function (plugin) { return plugin.apply(compiler); });
65 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;
81 });
82 };
83 return PiletWebpackPlugin;
84}());
85exports.PiletWebpackPlugin = PiletWebpackPlugin;
86//# sourceMappingURL=index.js.map
\No newline at end of file