UNPKG

3.92 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 path_1 = require("path");
22var webpack_1 = require("webpack");
23var helpers_1 = require("./helpers");
24var pluginName = 'PiletWebpackPlugin';
25var PiletWebpackPlugin = /** @class */ (function () {
26 function PiletWebpackPlugin(piletPackage, options) {
27 if (options === void 0) { options = {}; }
28 this.piletPackage = piletPackage;
29 this.options = options;
30 }
31 PiletWebpackPlugin.prototype.apply = function (compiler) {
32 var _a, _b, _c, _d;
33 var piletPkg = this.piletPackage;
34 var shellPkg = require(piletPkg.piral.name + "/package.json");
35 var shortName = piletPkg.name.replace(/\W/gi, '');
36 var jsonpFunction = "pr_" + shortName;
37 var environment = process.env.NODE_ENV || 'development';
38 var piralExternals = (_b = (_a = shellPkg.pilets) === null || _a === void 0 ? void 0 : _a.externals) !== null && _b !== void 0 ? _b : [];
39 var piletExternals = (_c = piletPkg.externals) !== null && _c !== void 0 ? _c : [];
40 var peerDependencies = Object.keys((_d = piletPkg.peerDependencies) !== null && _d !== void 0 ? _d : {});
41 var externals = __spreadArrays(piralExternals, piletExternals, peerDependencies);
42 var variables = __assign(__assign({}, helpers_1.getVariables(piletPkg, environment)), this.options.variables);
43 var plugins = [
44 new webpack_1.BannerPlugin({
45 banner: "//@pilet v:1(" + jsonpFunction + ")",
46 entryOnly: true,
47 raw: true,
48 }),
49 new webpack_1.DefinePlugin(helpers_1.getDefineVariables(variables)),
50 ];
51 helpers_1.setEnvironment(variables);
52 plugins.forEach(function (plugin) { return plugin.apply(compiler); });
53 compiler.hooks.done.tap(pluginName, function (statsData) {
54 if (!statsData.hasErrors()) {
55 var _a = compiler.options.output, path = _a.path, filename = _a.filename;
56 if (typeof filename === 'string') {
57 var file = path_1.resolve(path, filename);
58 helpers_1.wrapPilet(file, jsonpFunction);
59 }
60 else {
61 var main = statsData.compilation.chunks.filter(function (m) { return m.entryModule; }).map(function (m) { return m.files[0]; })[0];
62 var file = path_1.resolve(compiler.outputPath, main);
63 helpers_1.wrapPilet(file, jsonpFunction);
64 }
65 }
66 });
67 compiler.hooks.afterEnvironment.tap(pluginName, function () {
68 var current = compiler.options.externals;
69 compiler.options.output.jsonpFunction = jsonpFunction;
70 compiler.options.output.libraryTarget = 'umd';
71 compiler.options.output.library = piletPkg.name;
72 compiler.options.externals = Array.isArray(current)
73 ? __spreadArrays(current, externals) : current
74 ? __spreadArrays([current], externals) : externals;
75 });
76 };
77 return PiletWebpackPlugin;
78}());
79exports.PiletWebpackPlugin = PiletWebpackPlugin;
80//# sourceMappingURL=index.js.map
\No newline at end of file