-
Notifications
You must be signed in to change notification settings - Fork 27
/
next-legacy.js
114 lines (100 loc) · 3.45 KB
/
next-legacy.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
const {
getClientStyleLoader
} = require('next/dist/build/webpack/config/blocks/css/loaders/client');
const { stringifyCssRequest } = require('./src/plugin-utils.js');
const Style9Plugin = require('./webpack/index.js');
const cssLoader = (() => {
try {
// v10 & v11
return require.resolve('next/dist/compiled/css-loader');
} catch (_) {
try {
// v12+
return require.resolve('next/dist/build/webpack/loaders/css-loader/src');
} catch (_) {
return 'css-loader';
}
}
})();
function getInlineLoader(options, MiniCssExtractPlugin) {
const outputLoaders = [{ loader: cssLoader }];
if (!options.isServer) {
outputLoaders.unshift({
// Logic adopted from https://git.io/JfD9r
...getClientStyleLoader({
// In development model Next.js uses style-loader, which inserts each
// CSS file as its own style tag, which means the CSS won't be sorted
// and causes issues with determinism when using media queries and
// pseudo selectors. Setting isDevelopment means MiniCssExtractPlugin is
// used instead.
isDevelopment: false,
assetPrefix: options.config.assetPrefix
}),
loader: MiniCssExtractPlugin.loader
});
}
return stringifyCssRequest(outputLoaders);
}
module.exports = (pluginOptions = {}) => (nextConfig = {}) => {
return {
...nextConfig,
webpack(config, options) {
const outputCSS = !options.isServer;
// The style9 compiler must run on source code, which means it must be
// configured as the last loader in webpack so that it runs before any
// other transformation.
if (typeof nextConfig.webpack === 'function') {
config = nextConfig.webpack(config, options);
}
// For some reason, Next 11.0.1 has `config.optimization.splitChunks`
// set to `false` when webpack 5 is enabled.
config.optimization.splitChunks = config.optimization.splitChunks || {
cacheGroups: {}
};
// Use own MiniCssExtractPlugin to ensure HMR works
// v9 has issues when using own plugin in production
// v10.2.1 has issues when using built-in plugin in development since it
// doesn't bundle HMR files
const MiniCssExtractPlugin = options.dev
? require('mini-css-extract-plugin')
: require('next/dist/build/webpack/plugins/mini-css-extract-plugin')
.default;
config.module.rules.push({
test: /\.(tsx|ts|js|mjs|jsx)$/,
use: [
{
loader: Style9Plugin.loader,
options: {
inlineLoader: getInlineLoader(options, MiniCssExtractPlugin),
outputCSS,
...pluginOptions
}
}
]
});
if (outputCSS) {
config.optimization.splitChunks.cacheGroups.styles = {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true
};
// HMR reloads the CSS file when the content changes but does not use
// the new file name, which means it can't contain a hash.
const filename = options.dev
? 'static/css/[name].css'
: 'static/css/[contenthash].css';
config.plugins.push(
// Logic adopted from https://git.io/JtdBy
new MiniCssExtractPlugin({
filename,
chunkFilename: filename,
ignoreOrder: true
}),
new Style9Plugin()
);
}
return config;
}
};
};