UNPKG

5.1 kBJavaScriptView Raw
1'use strict';
2
3var _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
5var _webpackSources = require('webpack-sources');
6
7var _webpack = require('webpack');
8
9var _ExternalModuleFactoryPlugin = require('webpack/lib/ExternalModuleFactoryPlugin');
10
11var _ExternalModuleFactoryPlugin2 = _interopRequireDefault(_ExternalModuleFactoryPlugin);
12
13var _RaxMainTemplatePlugin = require('./RaxMainTemplatePlugin');
14
15var _RaxMainTemplatePlugin2 = _interopRequireDefault(_RaxMainTemplatePlugin);
16
17var _BuiltinModules = require('./BuiltinModules');
18
19var _BuiltinModules2 = _interopRequireDefault(_BuiltinModules);
20
21var _MultiplePlatform = require('./MultiplePlatform');
22
23var _MultiplePlatform2 = _interopRequireDefault(_MultiplePlatform);
24
25var _DuplicateChecker = require('./DuplicateChecker');
26
27var _DuplicateChecker2 = _interopRequireDefault(_DuplicateChecker);
28
29function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33var isProducation = process.env.NODE_ENV === 'production';
34
35var 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: [], // web node weex reactnative
45 polyfillModules: [],
46 runModule: false,
47 bundle: 'compatible', // private
48 target: 'umd', // default 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 // @weex-module/* ignored
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 // Custom external format
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 // uglify-webpack-plugin will remove javascript's comments in
104 // optimize-chunk-assets, add frameworkComment after that.
105 compilation.plugin('after-optimize-chunk-assets', function (chunks) {
106 chunks.forEach(function (chunk) {
107 // In webpack2 chunk.initial was removed. Use isInitial()
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
127RaxWebpackPlugin.BuiltinModules = _BuiltinModules2.default;
128RaxWebpackPlugin.MultiplePlatform = _MultiplePlatform2.default;
129
130module.exports = RaxWebpackPlugin;
\No newline at end of file