-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwebpack.config.js
executable file
·82 lines (75 loc) · 2.2 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
var path = require('path');
var globEntries = require('webpack-glob-entries');
var packageJson = require('./package.json');
var cwd = process.cwd();
const typeMapping = {
js: '/cartridge/client/default/js',
scss: '/cartridge/client/default/scss'
};
/**
* @param {Object} data aliases
* @returns {Object} combined aliases
*/
function getAliases(data) {
var cartridgeAliases = {};
Object.keys(packageJson.paths || {}).map((key) => {
cartridgeAliases[key] = path.join(cwd, packageJson.paths[key], typeMapping[data.type]);
});
var aliases = data.alias || {};
Object.keys(aliases).forEach(key => {
aliases[key] = path.join(cwd, aliases[key]);
});
return Object.assign({}, cartridgeAliases, aliases);
}
/**
* @param {Object} data config data
* @param {Object} env environment
* @returns {Object} configuration Object
*/
function getConfig(data, env = {}) {
var name = data.type || 'default';
var entryPath = path.resolve('cartridges', data.cartridge, 'cartridge', data.src, data.entry);
var includePaths = data.includePaths || [];
var aliases = getAliases(data);
const isProduction = env && env.production || false;
return {
name: name,
mode: isProduction ? 'production' : 'development',
devtool: 'source-map',
optimization: {
minimize: isProduction
},
entry: globEntries(entryPath),
output: {
path: path.resolve('cartridges', data.cartridge, 'cartridge', data.dest),
filename: '[name].js',
chunkFilename: '[name].bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
resolve: {
alias: aliases
}
};
}
/**
* @param {Object} env environment
* @returns {Array} configurations
*/
function generateConfig(env) {
var configs = packageJson.config.webpack || [];
return configs.map((data) => {
return getConfig(data, env);
});
}
module.exports = generateConfig();