-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvue.config.js
More file actions
243 lines (216 loc) · 7 KB
/
vue.config.js
File metadata and controls
243 lines (216 loc) · 7 KB
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
const path = require('path');
const { spawn } = require("child_process");
const JX3BOX = require("@jx3box/jx3box-common/data/jx3box.json");
const CMS_PROXY_TARGET = (process.env.VUE_APP_CMS || JX3BOX.__cms || "https://cms.jx3box.com").replace(/\/$/, "");
const { __cms, __node, __team, __next } = JX3BOX;
const pages = {
index: {
title: "导航",
entry: "src/pages/index.js",
template: "public/index.html",
filename: "index.html",
},
article: {
title: "Article渲染",
entry: "src/pages/article.js",
template: "public/index.html",
filename: "article/index.html",
},
tinymce: {
title: "Tinymce编辑器",
entry: "src/pages/tinymce.js",
template: "public/tinymce.html",
filename: "tinymce/index.html",
},
markdown: {
title: 'Markdown',
entry: "src/pages/markdown.js",
template: 'public/index.html',
filename: 'markdown/index.html',
},
upload: {
title: '文件上传',
entry: "src/pages/upload.js",
template: 'public/index.html',
filename: 'upload/index.html',
}
}
let chokidar = null;
try {
chokidar = require("chokidar");
} catch (e) {
chokidar = null;
}
// const pkg = require("./package.json");
// const { JX3BOX } = require("@jx3box/jx3box-common");
// const webpack = require('webpack')
class RunBuildOnCssChangePlugin {
constructor(options = {}) {
this.paths = options.paths || [];
this.debounceMs = Number.isFinite(options.debounceMs) ? options.debounceMs : 400;
this.command = options.command || ["npm", ["run", "build"]];
this.enabled = options.enabled !== false;
}
apply() {
if (!this.enabled) return;
// Only run in `vue-cli-service serve`
if (!process.env.WEBPACK_SERVE) return;
if (!chokidar) return;
const watchTargets = (this.paths || []).filter(Boolean);
if (!watchTargets.length) return;
let timer = null;
let running = false;
let queued = false;
const run = () => {
if (running) {
queued = true;
return;
}
running = true;
queued = false;
const npmBin = process.platform === "win32" ? "npm.cmd" : "npm";
const [cmd, args] = this.command;
const realCmd = cmd === "npm" ? npmBin : cmd;
const child = spawn(realCmd, args, {
stdio: "inherit",
env: process.env,
});
child.on("exit", () => {
running = false;
// build 完成后,触发 dev 页面强制 reload
if (typeof global.__JX3BOX_DEV_TRIGGER_RELOAD__ === "function") {
global.__JX3BOX_DEV_TRIGGER_RELOAD__("css-build");
}
if (queued) run();
});
};
const schedule = () => {
if (timer) clearTimeout(timer);
timer = setTimeout(run, this.debounceMs);
};
const watcher = chokidar.watch(watchTargets, {
ignoreInitial: true,
});
watcher.on("add", schedule);
watcher.on("change", schedule);
watcher.on("unlink", schedule);
}
}
module.exports = {
// 项目不需要 eslint,关闭 lintOnSave 以避免启动/编译时报错
lintOnSave: false,
configureWebpack: {
plugins: [
new RunBuildOnCssChangePlugin({
paths: [
path.resolve(__dirname, "src/assets/css"),
],
command: ["npm", ["run", "build"]],
debounceMs: 500,
}),
],
},
//❤️ Multiple pages ~
pages: pages,
//❤️ Porxy ~
devServer: {
proxy: {
"^/api/cms": {
target: CMS_PROXY_TARGET,
changeOrigin: true,
secure: true,
},
"^/api/node": {
target: __node,
changeOrigin: true,
secure: true,
},
"^/__proxy/cms": {
target: __cms,
changeOrigin: true,
secure: true,
pathRewrite: {
"^/__proxy/cms": "",
},
},
"^/__proxy/node": {
target: __node,
changeOrigin: true,
secure: true,
pathRewrite: {
"^/__proxy/node": "",
},
},
"^/__proxy/team": {
target: __team,
changeOrigin: true,
secure: true,
pathRewrite: {
"^/__proxy/team": "",
},
},
"^/__proxy/next": {
target: __next,
changeOrigin: true,
secure: true,
pathRewrite: {
"^/__proxy/next": "",
},
},
},
setupMiddlewares(middlewares, devServer) {
// 暴露一个全局方法,给上面的 watcher/build 插件用来强制刷新当前打开的 dev 页面
global.__JX3BOX_DEV_TRIGGER_RELOAD__ = (file = "manual") => {
try {
if (devServer && devServer.webSocketServer && devServer.webSocketServer.clients) {
// devServer.sendMessage(devServer.webSocketServer.clients, "static-changed", file);
}
} catch (e) {
// ignore
}
};
return middlewares;
},
client: {
overlay: {
warnings: false,
errors: false,
},
},
},
//❤️ 配置需要被 Babel 转译的 node_modules 依赖 ~
// transpileDependencies: [
// 'htmlparser2',
// 'cheerio',
// 'dom-serializer',
// 'domelementtype',
// 'domhandler',
// 'domutils',
// 'entities',
// 'parse5',
// 'parse5-htmlparser2-tree-adapter'
// ],
chainWebpack: config => {
//💝 in-line small imgs ~
config.module.rule("images").set("parser", {
dataUrlCondition: {
maxSize: 4 * 1024, // 4KiB
},
});
//💝 in-line svg imgs ~
config.module.rule("vue").use("vue-svg-inline-loader").loader("vue-svg-inline-loader");
//💖 import common less var * mixin ~
const types = ["vue-modules", "vue", "normal-modules", "normal"];
types.forEach((type) => addStyleResource(config.module.rule("less").oneOf(type)));
},
};
function addStyleResource(rule) {
var preload_styles = [];
preload_styles.push(
path.resolve(__dirname, "./node_modules/csslab/base.less"),
path.resolve(__dirname, "./assets/css/var.less")
);
rule.use("style-resource").loader("style-resources-loader").options({
patterns: preload_styles,
});
}