1 | const path = require('path');
|
2 |
|
3 | const webpack = require('webpack');
|
4 | const LiveReloadPlugin = require('webpack-livereload-plugin');
|
5 | const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
|
6 | const getESLintOptions = require('./getESLintOptions');
|
7 | const getTSLintOptions = require('./getTSLintOptions');
|
8 |
|
9 | const getWebpackConfig = function ({
|
10 |
|
11 | // input
|
12 | base,
|
13 | entrys, demo, // if (!!entrys && !!demo) is true, it's in application mode
|
14 | entry, umdName, // otherwise if (!!entry && !!umdName) is true, it's in library mode
|
15 |
|
16 | // output
|
17 | dist, suffix,
|
18 | publicPath, // used only in appliaction mode
|
19 |
|
20 | // use babel by default
|
21 | babelOptions,
|
22 | babelPolyfill: useBabelPolyfill,
|
23 |
|
24 | // configs
|
25 | react,
|
26 | typescript,
|
27 | lint,
|
28 | commonsChunk,
|
29 | liveReload,
|
30 | minify,
|
31 |
|
32 | // custom loaders and plugins
|
33 | loaders: extraLoaders,
|
34 | plugins: extraPlugins,
|
35 |
|
36 | }) {
|
37 |
|
38 | const entryConfig = {};
|
39 | const outputConfig = {
|
40 | path: path.join(base, dist),
|
41 | publicPath: '/'
|
42 | };
|
43 |
|
44 | const plugins = [
|
45 | ...extraPlugins
|
46 | ];
|
47 |
|
48 | const loaders = [
|
49 | {
|
50 | test: /\.js$/,
|
51 | exclude: /(node_modules)/,
|
52 | use: [
|
53 | {
|
54 | loader: 'babel-loader',
|
55 | options: babelOptions
|
56 | }
|
57 | ]
|
58 | },
|
59 | {
|
60 | test: /\.ts$/,
|
61 | exclude: /(node_modules)/,
|
62 | use: [
|
63 | {
|
64 | loader: 'babel-loader',
|
65 | options: babelOptions
|
66 | },
|
67 | {
|
68 | loader: 'ts-loader',
|
69 | options: {
|
70 | context: base,
|
71 | configFile: path.join(__dirname, '../space/tsconfig.json')
|
72 | }
|
73 | }
|
74 | ]
|
75 | },
|
76 | ...extraLoaders
|
77 | ]
|
78 |
|
79 | if (entrys && demo) {
|
80 |
|
81 |
|
82 |
|
83 | entrys.forEach(file => {
|
84 | const res = [];
|
85 | if (useBabelPolyfill) {
|
86 | res.push('babel-polyfill');
|
87 | }
|
88 | res.push(demo + '/' + file + (typescript ? '.ts' : '.js'));
|
89 | entryConfig[file] = res;
|
90 | });
|
91 |
|
92 | outputConfig.filename = `[name].${suffix}.js`;
|
93 |
|
94 | if (publicPath) {
|
95 | outputConfig.publicPath = publicPath;
|
96 | }
|
97 |
|
98 | } else if (entry && umdName) {
|
99 |
|
100 |
|
101 |
|
102 | entryConfig[umdName.toLowerCase()] = useBabelPolyfill ? ['babel-polyfill', entry] : [entry];
|
103 |
|
104 | outputConfig.library = umdName;
|
105 | outputConfig.libraryTarget = 'umd';
|
106 | outputConfig.filename = minify ? `[name].${suffix}.js` : `[name].js`;
|
107 |
|
108 | } else if (!entry && !entrys) {
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | entryConfig.index = './src/index';
|
115 |
|
116 | } else {
|
117 |
|
118 | throw 'get webpack config input not valid';
|
119 |
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 | if (react) {
|
125 | loaders.push({
|
126 | test: /\.less$/,
|
127 | exclude: /(node_modules)/,
|
128 | use: [
|
129 | 'style-loader',
|
130 | 'css-loader',
|
131 | 'less-loader'
|
132 | ]
|
133 | })
|
134 | }
|
135 |
|
136 | if (lint) {
|
137 |
|
138 | loaders.push({
|
139 | test: /\.js$/,
|
140 | exclude: /(node_modules)/,
|
141 | enforce: 'pre',
|
142 | use: [
|
143 | {
|
144 | loader: 'eslint-loader',
|
145 | options: getESLintOptions()
|
146 | }
|
147 | ]
|
148 | });
|
149 |
|
150 | if (typescript) {
|
151 |
|
152 | loaders.push({
|
153 | test: /\.ts$/,
|
154 | exclude: /(node_modules)/,
|
155 | enforce: 'pre',
|
156 | use: [
|
157 | {
|
158 | loader: 'tslint-loader',
|
159 | options: getTSLintOptions()
|
160 | }
|
161 | ]
|
162 | })
|
163 |
|
164 | }
|
165 |
|
166 | }
|
167 |
|
168 |
|
169 |
|
170 | if (minify) {
|
171 | plugins.push(new UglifyJSPlugin());
|
172 | }
|
173 |
|
174 | if (liveReload) {
|
175 | plugins.push(new LiveReloadPlugin());
|
176 | }
|
177 |
|
178 | if (commonsChunk) {
|
179 | plugins.push(
|
180 | new webpack.optimize.CommonsChunkPlugin({
|
181 | name: 'commons',
|
182 | filename: `commons.${suffix}.js`,
|
183 | minChunks: 2
|
184 | })
|
185 | )
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 | var config = {
|
191 |
|
192 | entry: entryConfig,
|
193 |
|
194 | output: outputConfig,
|
195 |
|
196 | module: {
|
197 | rules: loaders
|
198 | },
|
199 |
|
200 | plugins,
|
201 |
|
202 | resolve: {
|
203 | extensions: [".ts", ".js"],
|
204 | },
|
205 |
|
206 | resolveLoader: {
|
207 | modules: ['node_modules', path.resolve(__dirname, '../node_modules')]
|
208 | },
|
209 |
|
210 | mode: 'none'
|
211 | }
|
212 |
|
213 | return config;
|
214 | };
|
215 |
|
216 | module.exports = getWebpackConfig; |
\ | No newline at end of file |