1 | 'use strict';
|
2 |
|
3 | var _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; }; }();
|
4 |
|
5 | var _webpackSources = require('webpack-sources');
|
6 |
|
7 | var _webpack = require('webpack');
|
8 |
|
9 | var _ExternalModuleFactoryPlugin = require('webpack/lib/ExternalModuleFactoryPlugin');
|
10 |
|
11 | var _ExternalModuleFactoryPlugin2 = _interopRequireDefault(_ExternalModuleFactoryPlugin);
|
12 |
|
13 | var _RaxMainTemplatePlugin = require('./RaxMainTemplatePlugin');
|
14 |
|
15 | var _RaxMainTemplatePlugin2 = _interopRequireDefault(_RaxMainTemplatePlugin);
|
16 |
|
17 | var _BuiltinModules = require('./BuiltinModules');
|
18 |
|
19 | var _BuiltinModules2 = _interopRequireDefault(_BuiltinModules);
|
20 |
|
21 | var _MultiplePlatform = require('./MultiplePlatform');
|
22 |
|
23 | var _MultiplePlatform2 = _interopRequireDefault(_MultiplePlatform);
|
24 |
|
25 | var _DuplicateChecker = require('./DuplicateChecker');
|
26 |
|
27 | var _DuplicateChecker2 = _interopRequireDefault(_DuplicateChecker);
|
28 |
|
29 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
30 |
|
31 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
32 |
|
33 | var isProducation = process.env.NODE_ENV === 'production';
|
34 |
|
35 | var RaxWebpackPlugin = function () {
|
36 | function RaxWebpackPlugin(options) {
|
37 | _classCallCheck(this, RaxWebpackPlugin);
|
38 |
|
39 | this.options = Object.assign({
|
40 | builtinModules: _BuiltinModules2.default,
|
41 | externalBuiltinModules: false,
|
42 | frameworkComment: null,
|
43 | includePolyfills: false,
|
44 | platforms: [],
|
45 | polyfillModules: [],
|
46 | runModule: false,
|
47 | bundle: 'compatible',
|
48 | target: 'umd',
|
49 | duplicateCheck: ['rax']
|
50 | }, options);
|
51 | }
|
52 |
|
53 | _createClass(RaxWebpackPlugin, [{
|
54 | key: 'apply',
|
55 | value: function apply(compiler) {
|
56 | var _this = this;
|
57 |
|
58 | compiler.apply(new _webpack.DefinePlugin({
|
59 | '__DEV__': isProducation ? false : true
|
60 | }));
|
61 |
|
62 | compiler.apply(new _DuplicateChecker2.default({
|
63 | modulesToCheck: this.options.duplicateCheck
|
64 | }));
|
65 |
|
66 | compiler.plugin('this-compilation', function (compilation) {
|
67 | compilation.apply(new _RaxMainTemplatePlugin2.default(_this.options));
|
68 | });
|
69 |
|
70 | compiler.plugin('compile', function (params) {
|
71 | params.normalModuleFactory.apply(new _ExternalModuleFactoryPlugin2.default('amd', function (context, request, callback) {
|
72 |
|
73 | if (/^@weex\-module\//.test(request)) {
|
74 | return callback(null, request, 'commonjs');
|
75 | }
|
76 |
|
77 | var builtinModuleName = _this.options.builtinModules[request];
|
78 | if (_this.options.externalBuiltinModules && builtinModuleName) {
|
79 | if (Array.isArray(builtinModuleName)) {
|
80 | var customRequest = '(function(){ var mod;';
|
81 |
|
82 | builtinModuleName.forEach(function (name) {
|
83 | customRequest += 'if (!mod) { try { mod = require("' + name + '") } catch(e) {} }';
|
84 | });
|
85 |
|
86 | customRequest += 'return mod;})()';
|
87 |
|
88 | return callback(null, customRequest, 'custom-format');
|
89 | } else {
|
90 | return callback(null, builtinModuleName, 'commonjs');
|
91 | }
|
92 | }
|
93 |
|
94 | callback();
|
95 | }));
|
96 | });
|
97 |
|
98 | if (this.options.target === 'bundle' || this.options.frameworkComment) {
|
99 | var defaultFrameworkComment = '// {"framework" : "Rax"}';
|
100 | var frameworkComment = typeof this.options.frameworkComment === 'string' ? this.options.frameworkComment : defaultFrameworkComment;
|
101 |
|
102 | compiler.plugin('compilation', function (compilation) {
|
103 |
|
104 |
|
105 | compilation.plugin('after-optimize-chunk-assets', function (chunks) {
|
106 | chunks.forEach(function (chunk) {
|
107 |
|
108 | try {
|
109 | if (!chunk.initial) return;
|
110 | } catch (e) {
|
111 | if (!chunk.isInitial()) return;
|
112 | }
|
113 |
|
114 | chunk.files.forEach(function (file) {
|
115 | compilation.assets[file] = new _webpackSources.ConcatSource(frameworkComment, '\n', compilation.assets[file]);
|
116 | });
|
117 | });
|
118 | });
|
119 | });
|
120 | }
|
121 | }
|
122 | }]);
|
123 |
|
124 | return RaxWebpackPlugin;
|
125 | }();
|
126 |
|
127 | RaxWebpackPlugin.BuiltinModules = _BuiltinModules2.default;
|
128 | RaxWebpackPlugin.MultiplePlatform = _MultiplePlatform2.default;
|
129 |
|
130 | module.exports = RaxWebpackPlugin; |
\ | No newline at end of file |