-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
61 lines (55 loc) · 1.61 KB
/
Copy pathutil.js
File metadata and controls
61 lines (55 loc) · 1.61 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
const compilerDom = require("@vue/compiler-dom");
// 重新导入,变成相对地址
function rewriteImport(content) {
return content.replace(/ from ['|"](.*)['|"]/g, function (s0, s1) {
if (s1.startsWith(".") || s1.startsWith("/") || s1.startsWith("../")) {
return s0;
} else {
return ` from '/@modules/${s1}'`;
}
});
}
// 处理转换脚本
function handleScript(script, styles, url) {
const importCss = styles.reduce((prev, curr, index) => {
curr = prev + `import '${url}?type=style&index=${index}&lang=${curr.lang}'\n`;
return curr;
}, "");
const scriptContent = script.content.replace("export default ", "const __script = ");
body = `
${rewriteImport(scriptContent)}
import { render as __render } from '${url}?type=template'
${importCss}
__script.render = __render
export default __script
`;
return body;
}
// 编译为包含render模块的文件
function handleTemplate(template) {
const render = compilerDom.compile(template.content, {
mode: "module",
}).code;
body = rewriteImport(render);
return body;
}
// 获取styles
function handleStyle(styles, query) {
// 获取styles内容
const { index, lang } = query;
body = `
const css = "${styles[index].content.replace(/[\n\r]/g, "")}"
let link = document.createElement('style')
link.setAttribute('type', 'text/css')
document.head.appendChild(link)
link.innerHTML = css
export default css
`;
return body;
}
module.exports = {
rewriteImport,
handleScript,
handleTemplate,
handleStyle,
};