-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue.config.js
358 lines (330 loc) · 10.4 KB
/
vue.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const path = require('path');
const fs = require('fs');
const emptyDir = require('empty-dir');
const webpack = require('webpack');
const copyPlugin = require('copy-webpack-plugin');
const esmRequire = require('esm')(module);
const { generateInjectConfig } = require('./build/inject/collect');
const InjectHtmlWebpackPlugin = require('./build/inject/plugin');
const merge = require('./build/utils/merge');
require('dotenv').config({
path: path.resolve(
process.cwd(),
process.env.NODE_ENV !== 'production' ? './.env.local' : './.env',
),
});
const IS_IN_NODE_MODULES = path.resolve(__dirname).includes('node_modules');
// user can speicify which config file should be loaded.
const { FRAGY_CONFIG: fragyConfigName } = process.env;
const configFileName = `fragy.config${fragyConfigName ? `.${fragyConfigName}` : ''}.js`;
const nodeModulesPath = IS_IN_NODE_MODULES
? path.resolve(__dirname, '../')
: path.resolve(__dirname, './node_modules');
const userProjectRoot = IS_IN_NODE_MODULES ? path.resolve(__dirname, '../../') : __dirname;
const userDataPath = IS_IN_NODE_MODULES
? path.resolve(userProjectRoot, './.fragy')
: path.resolve(__dirname, './.fragy');
const userConfigPath = IS_IN_NODE_MODULES
? path.resolve(userProjectRoot, `./${configFileName}`)
: path.resolve(__dirname, `./${configFileName}`);
const customComponentIndex = path.resolve(userDataPath, './components/fragy.entry.js');
const customPageIndex = path.resolve(userDataPath, './pages/fragy.entry.js');
const customInjectionPath = path.resolve(userDataPath, './inject');
// check user config path
if (!fs.existsSync(userConfigPath)) {
throw new Error(
`Cannot locate user configuration (${configFileName}), please check your project.`,
);
}
const { normalizeConfig } = esmRequire('./src/utils/config');
const fragyConfig = normalizeConfig(esmRequire(userConfigPath).default);
const themeFuncs = {};
let themeVueConfig = null;
const themePkgInfoPath = path.resolve(
nodeModulesPath,
`./${fragyConfig.theme.package}/package.json`,
);
// check compatibility
let themePkgInfo;
if (fs.existsSync(themePkgInfoPath)) {
themePkgInfo = JSON.parse(fs.readFileSync(themePkgInfoPath, { encoding: 'utf-8' }));
} else {
throw new Error('Cannot find package.json of theme.');
}
if (themePkgInfo.compatibility) {
if (
typeof themePkgInfo.compatibility.github !== 'undefined' &&
themePkgInfo.compatibility.github === false &&
fragyConfig.github
) {
throw new Error('The theme you are using now is not compatible with GitHub Mode.');
}
}
const context = {
frameworkRoot: __dirname,
siteTitle: fragyConfig.title,
themePkg: fragyConfig.theme.package,
themeRoot: path.resolve(nodeModulesPath, `./${fragyConfig.theme.package}`),
themeConfigPath: path.resolve(nodeModulesPath, `./${fragyConfig.theme.package}/config.js`),
themeEntryPath: path.resolve(nodeModulesPath, `./${fragyConfig.theme.package}/entry.vue`),
customComponentIndex: fs.existsSync(customComponentIndex) ? customComponentIndex : '',
customPageIndex: fs.existsSync(customPageIndex) ? customPageIndex : '',
// config objs
fragyConfig,
themePkgInfo,
};
const themeConfig = esmRequire(context.themeConfigPath).default;
const userThemeConfig = fragyConfig.theme.config;
if (userThemeConfig) {
merge(themeConfig, userThemeConfig);
}
context.themeConfig = themeConfig;
const chainWebpack = (config) => {
// theme flags
config.plugin('fragy-flags').use(webpack.DefinePlugin, [
{
__MARKVUE_ENABLED__: JSON.stringify(!!fragyConfig.markVue?.enable),
__FAVICON_URL__: JSON.stringify(fragyConfig.icon || ''),
__FRAGY_TITLE__: JSON.stringify(context.siteTitle || ''),
__FRAGY_LOCALE__: JSON.stringify(context.fragyConfig.locale || ''),
__FRAGY_THEME_PKG__: JSON.stringify(context.themePkg),
__FRAGY_USER_CONFIG_PATH__: JSON.stringify(userConfigPath),
__FRAGY_THEME_CONFIG_PATH__: JSON.stringify(context.themeConfigPath),
__FRAGY_THEME_ENTRY_PATH__: JSON.stringify(context.themeEntryPath),
__FRAGY_CUSTOM_COMPONENT_INDEX__: JSON.stringify(context.customComponentIndex),
__FRAGY_CUSTOM_PAGE_INDEX__: JSON.stringify(context.customPageIndex),
},
]);
// copy public files
const publicPath = path.resolve(userDataPath, './public');
if (fs.existsSync(publicPath)) {
config.plugin('public-files').use(copyPlugin, [
{
patterns: [
{
from: publicPath,
},
],
},
]);
}
// copy posts
if (fragyConfig.articles) {
const { feed: articleFeed } = fragyConfig.articles;
if (articleFeed && !/^https?\/\//.test(articleFeed)) {
const articlesSource = `${path.resolve(userDataPath, './posts').replace(/\\/g, '/')}/**/*.md`;
config.plugin('fragy-articles').use(copyPlugin, [
{
patterns: [
{
from: articlesSource,
to: `${articleFeed.substr(1)}/[name].md`,
},
],
},
]);
}
}
// copy feed data
['articleList', 'category', 'tag'].forEach((propertyName) => {
if (!fragyConfig[propertyName]) {
return;
}
const { output: sourcePath, feed: targetPath } = fragyConfig[propertyName];
if (!sourcePath || /^https?\/\//.test(sourcePath)) {
return;
}
const copySource = path.resolve(userDataPath, sourcePath);
if (emptyDir.sync(copySource)) {
return;
}
config.plugin(`fragy-feed-${propertyName}`).use(copyPlugin, [
{
patterns: [
{
from: copySource,
to: targetPath.replace(/^\//, ''),
},
],
},
]);
});
// copy manifest data
if (fragyConfig.category.manifestDetails || fragyConfig.tag.manifestDetails) {
config.plugin('fragy-manifest').use(copyPlugin, [
{
patterns: [
{
from: path.resolve(userDataPath, './manifest'),
to: 'data/manifest',
},
],
},
]);
}
const cacheGroups = {
vendors: {
name: `chunk-vendors`,
test(module) {
if (!module.resource) {
return false;
}
const filtered = [
'vue',
'@vue',
'vuex',
'@vue/compiler-sfc',
'marked',
'markvue',
'core-js',
'axios',
'yaml',
'mitt',
'lodash',
].reduce((res, curr) => {
if (res) return res;
return (
res || module.resource.includes(`/${curr}`) || module.resource.includes(`\\${curr}`)
);
}, false);
return /[\\/]node_modules[\\/]/.test(module.resource) && !filtered;
},
chunks: 'initial',
priority: -5,
},
base: {
name: 'chunk-base',
chunks: 'initial',
test(module) {
if (!module.resource) {
return false;
}
const included = [
'vue',
'@vue',
'vuex',
'marked',
'core-js',
'axios',
'yaml',
'mitt',
'lodash',
].reduce((res, curr) => {
if (res) return res;
return (
res || module.resource.includes(`/${curr}`) || module.resource.includes(`\\${curr}`)
);
}, false);
return (
/[\\/]node_modules[\\/]/.test(module.resource) &&
!/[\\/]node_modules[\\/](@vue[\\/]compiler-sfc)|markvue/.test(module.resource) &&
included
);
},
priority: 20,
enforce: true,
},
theme: {
name: 'theme',
chunks: 'async',
test: new RegExp(context.themePkg.replace(/\//g, '[\\\\/]')),
priority: 10,
},
themeVendors: {
name: 'theme-vendors',
chunks: 'async',
test: /[\\/]node_modules[\\/]/,
priority: 5,
reuseExistingChunk: true,
},
};
if (fragyConfig.markVue?.enable) {
Object.assign(cacheGroups, {
markVue: {
name: 'chunk-markvue',
chunks: 'all',
test: /[\\/]node_modules[\\/](markvue)|(@vue[\\/]compiler-sfc)/,
priority: 30,
enforce: true,
},
});
}
config.optimization.splitChunks({
cacheGroups,
});
// check theme entry override
if (themeVueConfig?.pages?.index) {
config.plugin('copy').tap((options) => {
options[0].patterns[0].globOptions.ignore.push('**/index.html');
return options;
});
}
const iconFormat = ['ico', 'png', 'jpg', 'jpeg', 'gif', 'webp'];
const favIconPaths = iconFormat.map((ext) =>
path.resolve(userDataPath, `./public/favicon.${ext}`),
);
const userFavIconExisted = favIconPaths.reduce((res, curr) => {
if (res) return res;
return res || fs.existsSync(curr);
}, false);
if (userFavIconExisted) {
config.plugin('copy').tap((options) => {
options[0].patterns[0].globOptions.ignore.push('**/favicon.ico');
return options;
});
}
themeFuncs.chainWebpack?.(config);
if (process.env.BUNDLE_ANALYZE === 'true') {
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
config.plugin('bundle-analyzer').use(
new BundleAnalyzerPlugin({
analyzerPort: 'auto',
}),
);
}
};
const configureWebpack = (config) => {
if (!Array.isArray(config.plugins)) {
config.plugins = [];
}
if (customInjectionPath && fs.existsSync(customInjectionPath)) {
config.plugins.push(new InjectHtmlWebpackPlugin(generateInjectConfig(customInjectionPath)));
}
themeFuncs.configureWebpack?.(config);
};
const vueConfig = {
outputDir: IS_IN_NODE_MODULES ? path.resolve(userProjectRoot, './dist') : 'dist',
productionSourceMap: false,
chainWebpack,
configureWebpack,
integrity: fragyConfig.build.integrity,
devServer: {
port: 9090,
server: 'https',
allowedHosts: 'all',
client: {
webSocketURL: 'auto://localhost:9090/ws',
},
},
};
// merge theme vue.config.js
const themeFilePath = path.resolve(nodeModulesPath, `./${fragyConfig.theme.package}/vue.config.js`);
if (fs.existsSync(themeFilePath)) {
let exported = require(themeFilePath);
// if exported item is a function, get the return.
if (typeof exported === 'function') {
exported = exported(context);
}
themeVueConfig = exported;
// merge webpack related functions
if (exported.chainWebpack) {
themeFuncs.chainWebpack = exported.chainWebpack;
}
if (exported.configureWebpack) {
themeFuncs.configureWebpack = exported.configureWebpack;
}
// assign static properties
const staticExported = JSON.parse(JSON.stringify(exported));
Object.assign(vueConfig, staticExported);
}
module.exports = vueConfig;