UNPKG

4.85 kBJavaScriptView Raw
1'use strict';
2
3var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /**
4 * Convert single config to multiple platform configs into webpack MultipleCompiler
5 * Added platform-loader to module.preLoader
6 */
7
8var _lodash = require('lodash.clonedeep');
9
10var _lodash2 = _interopRequireDefault(_lodash);
11
12function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
14var platformLoader = require.resolve('./PlatformLoader');
15
16module.exports = function MultiplePlatform(config) {
17 var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
18
19 if (Object.prototype.toString.call(config) !== '[object Object]') {
20 throw new TypeError('Invalid argument: config, must be an object');
21 }
22
23 if (Object.prototype.toString.call(options) !== '[object Object]') {
24 throw new TypeError('Invalid argument: options, must be an object');
25 }
26
27 var platforms = options.platforms || config.platforms;
28
29 if (typeof platforms === 'undefined' || platforms.length === 0) {
30 console.log('');
31 console.warn('The \`platforms\` field is not specified!');
32 console.log('');
33 return config;
34 }
35
36 var platformWihteList = ['web', 'node', 'weex', 'reactnative'];
37
38 // filter platforms by platformWihteList
39 platforms = platforms.filter(function (platform) {
40 var p = platform.toLowerCase();
41 if (platformWihteList.indexOf(p) !== -1) {
42 return true;
43 }
44 return false;
45 });
46
47 if (platforms.length === 0) {
48 console.log('');
49 console.warn('The options.platforms is no available platform!');
50 console.warn('Accept platform list:', JSON.stringify(platformWihteList));
51 console.log('');
52 return config;
53 }
54
55 var multiplePlatformConfigs = [];
56 var entry = config.entry;
57
58 if (Array.isArray(entry) || typeof entry === 'string') {
59 // TODO: support entry pass array/string ?
60 } else if ((typeof entry === 'undefined' ? 'undefined' : _typeof(entry)) === 'object') {
61 var entries = Object.keys(entry);
62
63 platforms.forEach(function (platform) {
64 var platformType = platform.toLowerCase();
65 var platformConfig = (0, _lodash2.default)(config);
66 var platformEntry = {};
67 var nameQuery = '';
68 // append platform entry
69 entries.forEach(function (name) {
70 if (Array.isArray(entry[name])) {
71 platformEntry[name + '.' + platformType] = entry[name].map(function (ev) {
72 return '' + ev;
73 });
74 } else if (typeof entry[name] === 'string') {
75 platformEntry[name + '.' + platformType] = '' + entry[name];
76 }
77 });
78
79 platformConfig.entry = platformEntry;
80 platformConfig.name = platformType;
81
82 // support chunkFilename config according to platformType
83 if (typeof options.chunkFilename === 'function') {
84 platformConfig.output.chunkFilename = options.chunkFilename(platformType);
85 }
86
87 if (Array.isArray(options.name)) {
88 options.name.map(function (name) {
89 nameQuery += '&name[]=' + name;
90 });
91 }
92
93 if (Array.isArray(platformConfig.module.preLoaders)) {
94 platformConfig.module.preLoaders.push({
95 test: /\.jsx?$/,
96 exclude: options.exclude ? options.exclude : /(node_modules|bower_components)/,
97 loader: platformLoader + '?platform=' + platformType + nameQuery
98 });
99 } else if (typeof platformConfig.module.preLoaders === 'undefined') {
100 platformConfig.module.preLoaders = [{
101 test: /\.jsx?$/,
102 exclude: options.exclude ? options.exclude : /(node_modules|bower_components)/,
103 loader: platformLoader + '?platform=' + platformType + nameQuery
104 }];
105 }
106
107 multiplePlatformConfigs.push(platformConfig);
108 });
109 }
110
111 multiplePlatformConfigs.unshift(config);
112
113 return multiplePlatformConfigs;
114};
\No newline at end of file