1 | const webpack = require('webpack');
|
2 | const path = require('path');
|
3 | const HtmlWebpackPlugin = require('html-webpack-plugin');
|
4 | const CleanWebpackPlugin = require('clean-webpack-plugin');
|
5 | const OpenBrowserPlugin = require('open-browser-webpack-plugin');
|
6 | const fs = require('fs');
|
7 | const LocalServer = require('./tools/localServer');
|
8 |
|
9 | process.env.NODE_ENV = 'production';
|
10 |
|
11 | const getEntry = () => {
|
12 | const basePath = `${__dirname}/src/page/`;
|
13 | return {
|
14 | jsEntry: fs.readdirSync(basePath).reduce((map, dirName) => {
|
15 | map[dirName] = `${basePath}${dirName}/index.js`;
|
16 | return map;
|
17 | }, {
|
18 | zaro: ['react', 'react-dom', 'zaro']
|
19 | }),
|
20 | HtmlWebpackPlugin: fs.readdirSync(basePath).map(dirName => new HtmlWebpackPlugin({
|
21 | filename: `${dirName}.html`,
|
22 | chunks: [dirName, 'zaro'],
|
23 | title: dirName,
|
24 | template: './src/template/basic.html'
|
25 | }))
|
26 | };
|
27 | };
|
28 |
|
29 | const plugins = [
|
30 | new CleanWebpackPlugin('build'),
|
31 | new webpack.HotModuleReplacementPlugin(),
|
32 | new webpack.optimize.CommonsChunkPlugin({
|
33 | name: 'zaro',
|
34 | minChunks: Infinity,
|
35 | filename: 'zaro.js'
|
36 | }),
|
37 | new OpenBrowserPlugin({
|
38 | url: 'http://localhost:8080/sample.html'
|
39 | }),
|
40 | new LocalServer({
|
41 | port: 9090
|
42 | })
|
43 | ].concat(getEntry().HtmlWebpackPlugin);
|
44 |
|
45 | module.exports = {
|
46 | entry: getEntry().jsEntry,
|
47 | output: {
|
48 | filename: '[name].[chunkhash].js',
|
49 | path: path.resolve(__dirname, 'build')
|
50 | },
|
51 | plugins,
|
52 | module: {
|
53 | rules: [{
|
54 | test: /\.(js|jsx)$/,
|
55 | exclude: /node_modules/,
|
56 | use: [
|
57 | 'babel-loader'
|
58 | ]
|
59 | }, {
|
60 | test: /\.css$/,
|
61 | exclude: /node_modules/,
|
62 | use: [
|
63 | 'style-loader',
|
64 | 'css-loader'
|
65 | ]
|
66 | }, {
|
67 | test: /(\.jp(e)g|\.png|\.gif|\.svg)$/,
|
68 | exclude: /node_modules/,
|
69 | use: {
|
70 | loader: 'file-loader',
|
71 | query: {
|
72 | name: '[name].[ext]'
|
73 | },
|
74 | }
|
75 | }]
|
76 | },
|
77 | devtool: 'source-map',
|
78 | devServer: {
|
79 | proxy: {
|
80 | '/api/**': 'http://127.0.0.1:9090'
|
81 | }
|
82 | }
|
83 | };
|