UNPKG

3.03 kBJavaScriptView Raw
1const path = require('path');
2const webpack = require('webpack');
3const copyWebpackPlugin = require('copy-webpack-plugin');
4
5const config = {
6 entry: {
7 index: [
8 'babel-polyfill',
9 'react-hot-loader/patch',
10 'webpack-hot-middleware/client?noInfo=false',
11 path.resolve(__dirname, "./lib/entry.index.js")
12 ]
13 },
14 output: {
15 path: path.resolve(__dirname, "theme/static"),
16 filename: "[name].js"
17 },
18 resolve: {
19 extensions: [".js", ".json", ".jsx", ".css"],
20 },
21
22 module: {
23 rules: [
24 {
25 test: /\.jsx?$/,
26 exclude: [
27 path.resolve(process.cwd(), 'node_modules'),
28 ],
29 use: {
30 loader: 'babel-loader',
31 options: {
32 presets: [
33 ["es2015", {"modules": false}],
34 // webpack understands the native import syntax, and uses it for tree shaking
35 "stage-0",
36 "react"
37 // Transpile React components to JavaScript
38 ],
39 plugins: [
40 "react-hot-loader/babel",
41 // Enables React code to work with HMR.
42 ["import", { "libraryName": "antd", "style": "css" }] // `style: true` 会加载 less 文件
43
44 ]
45 }
46 }
47 },
48 {
49 test: /\.css$/,
50 use: [ 'style-loader', 'css-loader' ]
51 },
52 {
53 test: /\.md$/,
54 use: ['babel-loader', path.resolve(__dirname, 'lib/md-loader.js') ]
55 },
56 {
57 test: /\.less$/,
58 use: [{
59 loader: "style-loader" // creates style nodes from JS strings
60 }, {
61 loader: "css-loader" // translates CSS into CommonJS
62 }, {
63 loader: "less-loader" // compiles Less to CSS
64 }]
65 }
66 ],
67 },
68
69 devtool: "source-map",
70
71 plugins: [
72 new webpack.HotModuleReplacementPlugin()
73 // new webpack.DefinePlugin({
74 // 'process.env.NODE_ENV': '"production"',
75 // })
76 ]
77}
78
79module.exports = (isProduction = false) => {
80 if (isProduction) {
81 process.env.NODE_ENV = 'production';
82 delete config.devtool;
83
84 config.plugins = config.plugins.concat([
85 new webpack.optimize.UglifyJsPlugin({
86 compress: {
87 warnings: false
88 },
89 comments: false
90 }),
91 new copyWebpackPlugin([
92 {from: path.resolve(__dirname, './theme/static'), to: path.resolve(process.cwd(), './site')}
93 ])
94 ]);
95 }
96
97 return config;
98};