UNPKG

3.63 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 });
21var webpack_1 = require("webpack");
22var helpers_1 = require("./helpers");
23var pluginName = 'PiletWebpackPlugin';
24function getExternals(piral) {
25 var _a, _b;
26 var shellPkg = require(piral + "/package.json");
27 var piralExternals = (_b = (_a = shellPkg.pilets) === null || _a === void 0 ? void 0 : _a.externals) !== null && _b !== void 0 ? _b : [];
28 return __spreadArrays(piralExternals, [
29 '@dbeining/react-atom',
30 '@libre/atom',
31 'history',
32 'react',
33 'react-dom',
34 'react-router',
35 'react-router-dom',
36 'tslib',
37 'path-to-regexp',
38 ]);
39}
40var PiletWebpackPlugin = /** @class */ (function () {
41 function PiletWebpackPlugin(options) {
42 this.options = options;
43 }
44 PiletWebpackPlugin.prototype.apply = function (compiler) {
45 var environment = process.env.NODE_ENV || 'development';
46 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;
47 var shortName = name.replace(/\W/gi, '');
48 var jsonpFunction = "pr_" + shortName;
49 var variables = __assign(__assign({}, helpers_1.getVariables(name, version, environment)), this.options.variables);
50 var plugins = [
51 new webpack_1.DefinePlugin(helpers_1.getDefineVariables(variables)),
52 ];
53 if (schema !== 'none') {
54 var bannerSuffix = schema ? "1(" + jsonpFunction + ")" : "0";
55 plugins.push(new webpack_1.BannerPlugin({
56 banner: "//@pilet v:" + bannerSuffix,
57 entryOnly: true,
58 raw: true,
59 }));
60 }
61 helpers_1.setEnvironment(variables);
62 plugins.forEach(function (plugin) { return plugin.apply(compiler); });
63 compiler.hooks.afterEnvironment.tap(pluginName, function () {
64 var current = compiler.options.externals;
65 compiler.options.output.jsonpFunction = jsonpFunction + "_chunks";
66 compiler.options.output.library = name;
67 if (schema !== 'none') {
68 compiler.options.output.libraryTarget = 'umd';
69 }
70 if (schema === 'v1') {
71 compiler.options.output.auxiliaryComment = {
72 commonjs2: "\nfunction define(d,k){(typeof document!=='undefined')&&(document.currentScript.app=k.apply(null,d.map(window." + jsonpFunction + ")));}define.amd=!0;",
73 };
74 }
75 compiler.options.externals = Array.isArray(current)
76 ? __spreadArrays(current, externals) : current
77 ? __spreadArrays([current], externals) : externals;
78 });
79 };
80 return PiletWebpackPlugin;
81}());
82exports.PiletWebpackPlugin = PiletWebpackPlugin;
83//# sourceMappingURL=index.js.map
\No newline at end of file