-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·110 lines (92 loc) · 3 KB
/
index.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
const {
PHASE_DEVELOPMENT_SERVER,
PHASE_PRODUCTION_SERVER,
PHASE_PRODUCTION_BUILD,
} = require('next/constants');
const defaultOptions = {
staticPrefix: 'NEXT_STATIC_',
publicPrefix: 'NEXT_PUBLIC_',
};
const MAIN_BUNDLE = 'main.js';
function keysWithPrefix(prefix) {
return Object.keys(process.env).reduce(
(acc, key) => (key.indexOf(prefix) === 0 ? acc.concat([key]) : acc),
[],
);
}
function pick(keys, obj) {
return keys.reduce((acc, key) => {
acc[key] = obj[key];
return acc;
}, {});
}
module.exports = options => {
const opts = {
...defaultOptions,
...options,
};
if (!opts.staticPrefix || !opts.publicPrefix) {
throw new TypeError('The `staticPrefix` and `publicPrefix` options can not be empty');
}
return (nextConfig = {}, composePlugins = {}) => {
const { nextComposePlugins, phase } = composePlugins;
const nextConfigMethod = (phase, args) => {
if (typeof nextConfig === 'function') {
nextConfig = nextConfig(phase, args);
}
const newConfig = {
...nextConfig,
};
const publicKeys = keysWithPrefix(opts.publicPrefix);
const staticKeys = keysWithPrefix(opts.staticPrefix);
if (
(publicKeys.length || staticKeys.length) &&
(phase === PHASE_PRODUCTION_BUILD || phase === PHASE_DEVELOPMENT_SERVER)
) {
Object.assign(newConfig, {
webpack(config, options) {
if (!options.defaultLoaders) {
throw new Error(
'This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade',
);
}
if (!options.isServer && publicKeys.length) {
const path = require('path');
const originalEntry = config.entry;
config.entry = async () => {
const entries = await originalEntry();
const filePath = path.resolve(__dirname, 'src', 'injectVars.js');
if (entries[MAIN_BUNDLE] && !entries[MAIN_BUNDLE].includes(filePath)) {
entries[MAIN_BUNDLE].unshift(filePath);
}
return entries;
};
}
// include static keys
if (staticKeys.length) {
const webpack = require('webpack');
config.plugins.push(new webpack.EnvironmentPlugin(staticKeys));
}
if (typeof nextConfig.webpack === 'function') {
return nextConfig.webpack(config, options);
}
return config;
},
});
}
if (
publicKeys.length &&
(phase === PHASE_PRODUCTION_SERVER || phase === PHASE_DEVELOPMENT_SERVER)
) {
Object.assign(newConfig, {
publicRuntimeConfig: {
...nextConfig.publicRuntimeConfig,
...pick(publicKeys, process.env),
},
});
}
return newConfig;
};
return nextComposePlugins ? nextConfigMethod(phase) : nextConfigMethod;
};
};