-
Notifications
You must be signed in to change notification settings - Fork 60
/
webpack.config.babel.js
135 lines (130 loc) · 3.01 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { resolve } from 'path';
import {
DefinePlugin,
EnvironmentPlugin,
IgnorePlugin,
optimize,
} from 'webpack';
import WXAppWebpackPlugin, { Targets } from 'wxapp-webpack-plugin';
import StylelintPlugin from 'stylelint-webpack-plugin';
import MinifyPlugin from 'babel-minify-webpack-plugin';
import CopyPlugin from 'copy-webpack-plugin';
import pkg from './package.json';
const { NODE_ENV, LINT } = process.env;
const isDev = NODE_ENV !== 'production';
const shouldLint = !!LINT && LINT !== 'false';
const srcDir = resolve('src');
const copyPatterns = []
.concat(pkg.copyWebpack || [])
.map(
(pattern) =>
typeof pattern === 'string' ? { from: pattern, to: pattern } : pattern,
);
export default (env = {}) => {
const min = env.min;
const target = env.target || 'Wechat';
const isWechat = env.target !== 'Alipay';
const isAlipay = !isWechat;
const relativeFileLoader = (ext = '[ext]') => {
const namePrefix = isWechat ? '' : '[path]';
return {
loader: 'file-loader',
options: {
useRelativePath: isWechat,
name: `${namePrefix}[name].${ext}`,
context: srcDir,
},
};
};
return {
entry: {
app: './src/app.js',
},
output: {
filename: '[name].js',
publicPath: '/',
path: resolve('dist', isWechat ? 'wechat' : 'alipay'),
},
target: Targets[target],
module: {
rules: [
{
test: /\.js$/,
include: /src/,
exclude: /node_modules/,
use: ['babel-loader', shouldLint && 'eslint-loader'].filter(Boolean),
},
{
test: /\.wxs$/,
include: /src/,
exclude: /node_modules/,
use: [
relativeFileLoader(),
'babel-loader',
shouldLint && 'eslint-loader',
].filter(Boolean),
},
{
test: /\.(scss|wxss|acss)$/,
include: /src/,
use: [
relativeFileLoader(isWechat ? 'wxss' : 'acss'),
{
loader: 'sass-loader',
options: {
includePaths: [resolve('src', 'styles'), srcDir],
},
},
],
},
{
test: /\.(json|png|jpg|gif)$/,
include: /src/,
use: relativeFileLoader(),
},
{
test: /\.(wxml|axml)$/,
include: /src/,
use: [
relativeFileLoader(isWechat ? 'wxml' : 'axml'),
{
loader: 'wxml-loader',
options: {
root: srcDir,
enforceRelativePath: true,
},
},
],
},
],
},
plugins: [
new EnvironmentPlugin({
NODE_ENV: 'development',
}),
new DefinePlugin({
__DEV__: isDev,
__WECHAT__: isWechat,
__ALIPAY__: isAlipay,
wx: isWechat ? 'wx' : 'my',
my: isWechat ? 'wx' : 'my',
}),
new WXAppWebpackPlugin({
clear: !isDev,
}),
new optimize.ModuleConcatenationPlugin(),
new IgnorePlugin(/vertx/),
shouldLint && new StylelintPlugin(),
min && new MinifyPlugin(),
new CopyPlugin(copyPatterns, { context: srcDir }),
].filter(Boolean),
devtool: isDev ? 'source-map' : false,
resolve: {
modules: [resolve(__dirname, 'src'), 'node_modules'],
},
watchOptions: {
ignored: /dist|manifest/,
aggregateTimeout: 300,
},
};
};