2020-05-06 20:45:18 +08:00
|
|
|
const cdnDependencies = require("./dependencies-cdn");
|
|
|
|
|
|
|
|
// 引入文件的 cdn 链接
|
|
|
|
const cdn = {
|
|
|
|
css: cdnDependencies.map((e) => e.css).filter((e) => e),
|
|
|
|
js: cdnDependencies.map((e) => e.js).filter((e) => e),
|
|
|
|
};
|
|
|
|
|
|
|
|
class BuildAppJSPlugin {
|
|
|
|
apply(compiler) {
|
|
|
|
compiler.hooks.emit.tapAsync(
|
|
|
|
"BuildAppJSPlugin",
|
|
|
|
(compilation, callback) => {
|
2020-06-29 22:45:58 +08:00
|
|
|
const isProd = process.env.NODE_ENV === "production"
|
2020-05-06 20:45:18 +08:00
|
|
|
let cssarr = [];
|
|
|
|
let jsarr = [];
|
2020-06-29 22:45:58 +08:00
|
|
|
let reg = "";
|
|
|
|
if (isProd) {
|
|
|
|
reg = "(app|chunk-vendors)\\.";
|
|
|
|
}
|
|
|
|
// 遍历所有编译过的资源文件
|
2020-05-06 20:45:18 +08:00
|
|
|
for (let filename in compilation.assets) {
|
2020-06-29 22:45:58 +08:00
|
|
|
if (filename.match(reg + ".*\\.js$")) {
|
|
|
|
if (isProd) {
|
2020-05-22 02:11:32 +08:00
|
|
|
filename = (process.env.VUE_APP_CDN_PATH || "/") + filename;
|
2020-05-19 18:10:44 +08:00
|
|
|
} else {
|
|
|
|
filename = "/" + filename;
|
|
|
|
}
|
2020-05-06 20:45:18 +08:00
|
|
|
jsarr.push(filename);
|
|
|
|
}
|
2020-06-29 22:45:58 +08:00
|
|
|
if (filename.match(reg + ".*\\.css$")) {
|
2020-05-06 20:45:18 +08:00
|
|
|
cssarr.push(filename);
|
|
|
|
}
|
|
|
|
}
|
2020-05-31 01:02:42 +08:00
|
|
|
cssarr = cssarr.sort(function (a) {
|
2020-05-06 20:45:18 +08:00
|
|
|
return a.indexOf("app.");
|
|
|
|
});
|
2020-05-31 01:02:42 +08:00
|
|
|
var cdnjs = "";
|
2020-06-29 22:45:58 +08:00
|
|
|
if (isProd) {
|
2020-05-06 20:45:18 +08:00
|
|
|
cssarr = cdn.css.concat(cssarr);
|
|
|
|
cdnjs = `var cdnjs = ${JSON.stringify(cdn.js)};
|
|
|
|
cdnjs.forEach((item) => {
|
|
|
|
document.write('<script src="' + item + '"></script>');
|
2020-05-31 01:02:42 +08:00
|
|
|
});`;
|
2020-05-06 20:45:18 +08:00
|
|
|
} else {
|
2020-05-31 01:02:42 +08:00
|
|
|
cssarr = cssarr.concat(
|
|
|
|
cdnDependencies.filter((e) => e.name === "").map((e) => e.css)
|
|
|
|
);
|
2020-05-06 20:45:18 +08:00
|
|
|
}
|
|
|
|
let content = `
|
|
|
|
var scripts = ${JSON.stringify(jsarr)};
|
|
|
|
${cdnjs}
|
|
|
|
scripts.forEach((item) => {
|
|
|
|
document.write('<script src="' + item + '"></script>');
|
|
|
|
});
|
|
|
|
`;
|
2020-05-31 01:02:42 +08:00
|
|
|
let cssContent = "";
|
|
|
|
cssarr.forEach((item) => {
|
|
|
|
cssContent += `@import url(${item});\n`;
|
|
|
|
});
|
2020-05-06 20:45:18 +08:00
|
|
|
// 将这个列表作为一个新的文件资源,插入到 webpack 构建中:
|
|
|
|
compilation.assets["app.js"] = {
|
2020-05-31 01:02:42 +08:00
|
|
|
source: function () {
|
2020-05-06 20:45:18 +08:00
|
|
|
return content;
|
|
|
|
},
|
2020-05-31 01:02:42 +08:00
|
|
|
size: function () {
|
2020-05-19 18:10:44 +08:00
|
|
|
return jsarr.length;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
compilation.assets["style.css"] = {
|
2020-05-31 01:02:42 +08:00
|
|
|
source: function () {
|
2020-05-19 18:10:44 +08:00
|
|
|
return cssContent;
|
|
|
|
},
|
2020-05-31 01:02:42 +08:00
|
|
|
size: function () {
|
2020-05-19 18:10:44 +08:00
|
|
|
return cssarr.length;
|
2020-05-06 20:45:18 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = BuildAppJSPlugin;
|