UNPKG

5.94 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _webpackSources = require('webpack-sources');
10
11var _fs = require('fs');
12
13var _fs2 = _interopRequireDefault(_fs);
14
15var _path = require('path');
16
17var _path2 = _interopRequireDefault(_path);
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23var CustomUmdMainTemplatePlugin = function () {
24 function CustomUmdMainTemplatePlugin(options) {
25 _classCallCheck(this, CustomUmdMainTemplatePlugin);
26
27 this.name = '[name]';
28 this.options = options;
29 }
30
31 _createClass(CustomUmdMainTemplatePlugin, [{
32 key: 'apply',
33 value: function apply(compilation) {
34 var _this = this;
35
36 var mainTemplate = compilation.mainTemplate;
37
38 compilation.templatesPlugin('render-with-entry', function (source, chunk, hash) {
39 var requireCall = '';
40 var polyfills = [];
41 var name = mainTemplate.applyPluginsWaterfall('asset-path', _this.name, {
42 hash: hash,
43 chunk: chunk
44 });
45
46 if (_this.options.includePolyfills) {
47 var polyfillModules = _this.options.polyfillModules;
48 polyfills = polyfillModules.map(function (fp) {
49 return _fs2.default.readFileSync(fp, 'utf8');
50 });
51 }
52
53 var moduleName = _this.options.moduleName || name;
54 var globalName = _this.options.globalName || name;
55 var target = _this.options.target;
56 var sourcePrefix = '';
57 var sourceSuffix = '';
58
59 if (typeof _this.options.sourcePrefix === 'function' && typeof _this.options.sourceSuffix === 'function') {
60 sourcePrefix = _this.options.sourcePrefix(source, chunk, hash);
61 sourceSuffix = _this.options.sourceSuffix(source, chunk, hash);
62 } else {
63 // module, function is private, only use in rax internal
64 if (chunk.name.endsWith('.module') || target === 'module') {
65 sourcePrefix = 'module.exports = ';
66 sourceSuffix = ';';
67 } else if (chunk.name.endsWith('.function') || target === 'function') {
68 sourcePrefix = 'module.exports = function() {\n return ';
69 sourceSuffix = '};';
70 } else if (chunk.name.endsWith('.bundle') || target === 'bundle') {
71 // Build page bundle use this mode.
72 if (_this.options.bundle === 'compatible') {
73 sourcePrefix = 'define("' + chunk.name + '", function(require) {';
74 sourceSuffix = '}); require("' + chunk.name + '");';
75 } else {
76 sourcePrefix = '';
77 sourceSuffix = '';
78 }
79 } else if (chunk.name.endsWith('.factory') || target === 'factory') {
80 // Build weex builtin modules use this mode.
81 // NOTE: globals should sync logic in weex-rax-framework
82 if (_this.options.factoryGlobals) {
83 var globalsCodes = _this.options.factoryGlobals.map(function (name) {
84 return 'var ' + name + ' = this["' + name + '"];';
85 });
86 sourcePrefix = 'module.exports = function(require, exports, module) {\n ' + globalsCodes.join('\n') + '\n module.exports = ';
87 sourceSuffix = '};';
88 } else {
89 sourcePrefix = 'module.exports = function(require, exports, module) {\n with(this) { module.exports = ';
90 sourceSuffix = '}};';
91 }
92 } else if (chunk.name.endsWith('.cmd') || target === 'cmd') {
93 sourcePrefix = 'define(' + JSON.stringify(moduleName) + ', function(require, exports, module){\n module.exports = ';
94 sourceSuffix = '});';
95 } else if (chunk.name.endsWith('.umd') || target === 'umd') {
96 // CommonJS first that could rename module name by wrap another define in air
97 sourcePrefix = '\n ;(function(fn) {\n if (typeof exports === "object" && typeof module !== "undefined") {\n module.exports = fn();\n } else if (typeof define === "function") {\n define(' + JSON.stringify(moduleName) + ', function(require, exports, module){\n module.exports = fn();\n });\n } else {\n var root;\n if (typeof window !== "undefined") {\n root = window;\n } else if (typeof self !== "undefined") {\n root = self;\n } else if (typeof global !== "undefined") {\n root = global;\n } else {\n // NOTICE: In JavaScript strict mode, this is null\n root = this;\n }\n root["' + globalName + '"] = fn();\n }\n })(function(){\n return ';
98
99 sourceSuffix = '});';
100 }
101 }
102
103 return new _webpackSources.ConcatSource(polyfills.join('\n'), sourcePrefix, source, sourceSuffix, requireCall);
104 });
105
106 mainTemplate.plugin('global-hash-paths', function (paths) {
107 if (_this.name) paths = paths.concat(_this.name);
108 return paths;
109 });
110
111 mainTemplate.plugin('hash', function (hash) {
112 hash.update('custom-umd');
113 hash.update(String(_this.name));
114 });
115 }
116 }]);
117
118 return CustomUmdMainTemplatePlugin;
119}();
120
121exports.default = CustomUmdMainTemplatePlugin;
122module.exports = exports['default'];
\No newline at end of file