-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.babel.js
103 lines (97 loc) · 2.18 KB
/
webpack.config.babel.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const { resolve } = require('path');
const webpack = require('webpack');
const { name } = require('./package.json');
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
const isDemo = process.env.REACT_DEMO && process.env.REACT_DEMO !== 'false';
const PROJECT_PATH = __dirname;
const inProject = (...args) => resolve(PROJECT_PATH, ...args);
const inSrc = inProject.bind(null, 'src');
const inTest = inProject.bind(null, 'test');
const inDemo = inProject.bind(null, 'demo');
const srcDir = inSrc();
const testDir = inTest();
const demoDir = inDemo();
module.exports = (webpackEnv = {}) => {
const { minify } = webpackEnv;
const config = {
devtool: 'source-map',
module: {
rules: [
{
test: /\.js$/,
include: [srcDir, testDir, demoDir],
loader: 'babel-loader',
options: {
presets: [['es2015', { modules: false }], 'react', 'stage-0'],
plugins: ['transform-decorators-legacy'],
cacheDirectory: true,
babelrc: false,
},
},
{
test: /\.css$/,
include: [demoDir],
loaders: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
],
resolve: {
modules: [srcDir, 'node_modules'],
extensions: ['.js'],
alias: {
'react-form-mobx': srcDir,
},
},
resolveLoader: {
moduleExtensions: ['-loader'],
},
mode: process.env.NODE_ENV,
optimization: {
minimize: !!minify,
},
devServer: {
contentBase: demoDir,
port: 3000,
},
};
if (isDemo) {
config.entry = './demo';
config.output = {
filename: 'bundle.js',
path: resolve(__dirname, 'demo'),
};
config.module.rules.push(
{
test: /\.scss$/,
include: demoDir,
use: ['style', 'css', 'postcss', 'sass'],
},
{
test: /\.es$/,
include: demoDir,
use: ['raw'],
},
);
}
else {
config.entry = './src';
config.output = {
filename: `${name}${minify ? '.min' : ''}.js`,
path: resolve(__dirname, 'dist'),
library: name,
libraryTarget: 'umd',
};
config.externals = {
react: 'React',
'react-dom': 'ReactDom',
mobx: 'mobx',
'mobx-react': 'mobxReact',
};
}
return config;
};