-
Notifications
You must be signed in to change notification settings - Fork 0
/
next.config.js
98 lines (84 loc) · 2.49 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
/* eslint-disable import/no-extraneous-dependencies */
const path = require('path');
const withCss = require('@zeit/next-css');
const withImages = require('next-images');
const withBundleAnalyzer = require('@zeit/next-bundle-analyzer');
const webpack = require('webpack');
const withLess = require('./next-plugins/next-less');
let config = {
// 编译过后的文件夹名称
distDir: 'build',
// 只有服务器端才有的配置
serverRuntimeConfig: {
serverOnly: 'secret',
},
// 服务器和客户端都有的配置
publicRuntimeConfig: {
serverAndClient: 'public',
host: '127.0.0.1:3333',
// 可以用于 子域名 切换语言
localeSubpaths:
typeof process.env.LOCALE_SUBPATHS === 'string' ? process.env.LOCALE_SUBPATHS : 'none',
},
webpack: (webpackConifg, { isServer }) => {
webpackConifg.resolve.alias['@'] = path.join(__dirname, '');
webpackConifg.plugins.push(
new webpack.DefinePlugin({
'process.env.DEBUG': JSON.stringify(process.env.DEBUG),
}),
);
// 服务端antd样式
if (isServer) {
const antStyles = /antd\/.*?\/style.*?/;
const origExternals = [...webpackConifg.externals];
webpackConifg.externals = [
(context, request, callback) => {
if (request.match(antStyles)) return callback();
if (typeof origExternals[0] === 'function') {
origExternals[0](context, request, callback);
} else {
callback();
}
},
...(typeof origExternals[0] === 'function' ? [] : origExternals),
];
webpackConifg.module.rules.unshift({
test: antStyles,
use: 'null-loader',
});
}
return webpackConifg;
},
};
config = withLess({
...config,
// 修复less打包问题
lessLoaderOptions: {
javascriptEnabled: true
},
cssLoaderOptions: {
importLoaders: 1,
localIdentName: '[local]_[hash:base64:5]',
},
// postcss配置
postcssLoaderOptions: {},
});
config = withCss({ ...config });
config = withImages({ ...config });
config = withBundleAnalyzer({
...config,
// analyze
analyzeServer: ['server', 'both'].includes(process.env.BUNDLE_ANALYZE),
analyzeBrowser: ['browser', 'both'].includes(process.env.BUNDLE_ANALYZE),
bundleAnalyzerConfig: {
server: {
analyzerMode: 'static',
reportFilename: '../bundles/server.html',
},
browser: {
analyzerMode: 'static',
reportFilename: '../bundles/client.html',
},
},
});
module.exports = config;