-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathnext.config.js
130 lines (123 loc) · 4.02 KB
/
next.config.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
/* eslint-disable */
const withLess = require('@zeit/next-less');
const lessToJS = require('less-vars-to-js');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const TerserPlugin = require('terser-webpack-plugin');
const fs = require('fs');
const path = require('path');
const withCSS = require('@zeit/next-css');
const { DefinePlugin } = require('webpack');
const withTypescript = require('@zeit/next-typescript');
const { parsed } = require('dotenv').config();
const { BASE_URL } = parsed;
// Where your antd-custom.less file lives
const themeVariables = lessToJS(
fs.readFileSync(path.resolve(__dirname, './assets/antd-custom.less'), 'utf8'),
);
const isDev = process.env.NODE_ENV !== 'production';
// fix antd bug in dev development
const devAntd = '@import "~antd/dist/antd.less";\n';
const stylesData = fs.readFileSync(
path.resolve(__dirname, './assets/_styles.less'),
'utf-8',
);
fs.writeFileSync(
path.resolve(__dirname, './assets/self-styles.less'),
isDev ? `${devAntd}${stylesData}` : stylesData,
'utf-8',
);
// fix: prevents error when .css files are required by node
if (typeof require !== 'undefined') {
require.extensions['.less'] = file => {};
}
module.exports = withTypescript(
withLess(
withCSS({
lessLoaderOptions: {
javascriptEnabled: true,
modifyVars: themeVariables,
localIdentName: '[local]___[hash:base64:5]',
},
webpack: (config, { buildId, dev, isServer, defaultLoaders }) => {
if (!dev) {
config.plugins.push(
...[
new BundleAnalyzerPlugin({
analyzerMode: 'disabled',
// For all options see https://github.com/th0r/webpack-bundle-analyzer#as-plugin
generateStatsFile: true,
// Will be available at `.next/stats.json`
statsFilename: 'stats.json',
}),
// 代替uglyJsPlugin
new TerserPlugin({
terserOptions: {
ecma: 6,
warnings: false,
extractComments: false, // remove comment
compress: {
drop_console: true, // remove console
},
ie8: false,
},
}),
new DefinePlugin({
'process.env': {
BASE_URL: JSON.stringify(BASE_URL),
},
}),
],
);
config.devtool = 'source-map';
} else {
config.module.rules.push({
test: /\.js$/,
enforce: 'pre',
include: [
path.resolve('components'),
path.resolve('pages'),
path.resolve('utils'),
path.resolve('constants'),
path.resolve('redux'),
path.resolve('containers'),
],
options: {
configFile: path.resolve('.eslintrc'),
eslint: {
configFile: path.resolve(__dirname, '.eslintrc'),
},
},
loader: 'eslint-loader',
});
config.plugins.push(
...[
new DefinePlugin({
'process.env': {
BASE_URL: JSON.stringify('http://localhost:3001'),
},
}),
],
);
config.devtool = 'cheap-module-inline-source-map';
}
return config;
},
webpackDevMiddleware: config => {
// Perform customizations to webpack dev middleware config
// console.log(config, '@@')
// Important: return the modified config
return config;
},
serverRuntimeConfig: {
// Will only be available on the server side
rootDir: path.join(__dirname, './'),
PORT: isDev ? 3006 : process.env.PORT || 3006,
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static',
isDev, // Pass through env variables
},
}),
),
);