-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (137 loc) · 4.63 KB
/
index.js
File metadata and controls
150 lines (137 loc) · 4.63 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
import * as compiler from '@vue/compiler-sfc';
function toBase64(str) {
return btoa(unescape(encodeURIComponent(str)));
}
function generateID() {
return Math.random().toString(36).slice(2, 12);
}
function transformVueSFC(source, filename, mountname) {
const {descriptor, errors} = compiler.parse(source, {filename});
if(errors.length) throw new Error(errors.toString());
const id = generateID();
const hasScoped = descriptor.styles.some(e => e.scoped);
const scopeId = hasScoped ? `data-v-${id}` : undefined;
const templateOptions = {
id,
source: descriptor.template.content,
filename: descriptor.filename,
scoped: hasScoped,
slotted: descriptor.slotted,
compilerOptions: {
scopeId: hasScoped ? scopeId : undefined,
mode: 'module',
},
};
const script = compiler.compileScript(descriptor, {id, templateOptions, sourceMap:true});
if(script.map) {
script.content = `${script.content}\n//# sourceMappingURL=data:application/json;base64,${toBase64(JSON.stringify(script.map))}`;
}
const template = compiler.compileTemplate({
...templateOptions,
sourceMap: true,
compilerOptions: {
...templateOptions.compilerOptions,
// https://github.com/vuejs/repl/blob/2daac718a212e61d200cecdfc3623535bd0196a9/src/transform.ts#L167C7-L167C15
bindingMetadata: script.bindings,
}
});
if(template.map) {
template.map.sources[0] = `${template.map.sources[0]}?template`;
template.code = `${template.code}\n//# sourceMappingURL=data:application/json;base64,${toBase64(JSON.stringify(template.map))}`;
}
let cssInJS = '';
if(descriptor.styles) {
const styled = descriptor.styles.map((style) => {
return compiler.compileStyle({
id,
source: style.content,
scoped: style.scoped,
preprocessLang: style.lang,
});
});
if(styled.length) {
const cssCode = styled.map(s => `${mountname} ${s.code}`).join('\n');
cssInJS = `(function(){const el = document.createElement('style');
el.innerHTML = \`${cssCode}\`;
document.body.appendChild(el);}());`;
}
}
const moduleCode = `
import script from '${getBlobURL(script.content)}';
import {render} from '${getBlobURL(template.code)}';
script.render = render;
${filename ? `script.__file = '${filename}'` : ''};
${scopeId ? `script.__scopeId = '${scopeId}'` : ''};
${cssInJS}
export default script;
`;
return moduleCode;
}
function getBlobURL(jsCode) {
const blob = new Blob([jsCode], {type: 'text/javascript'});
const blobURL = URL.createObjectURL(blob);
return blobURL;
}
// https://github.com/WICG/import-maps
const map = {
imports: {
vue: 'https://unpkg.com/vue@3/dist/vue.esm-browser.js',
},
scopes: { },
};
function makeComponent(component) {
const module = component.getAttribute('component');
let moduleName = module;
if(!/\.vue$/.test(module)) {
moduleName += '.vue';
}
component.setAttribute('module', moduleName);
if(module) {
return [getBlobURL(transformVueSFC(component.innerHTML, moduleName, component.getAttribute('mount'))), module];
}
return [];
}
const currentScript = document.currentScript || document.querySelector('script');
function setup() {
const components = document.querySelectorAll('noscript[type="vue-sfc"]');
const importMap = {};
let mount = [];
[...components].forEach((component) => {
const [url, module] = makeComponent(component);
if(component.hasAttribute('mount')) {
mount.push([module, component.getAttribute('mount')]);
}
if(url) {
importMap[module] = url;
}
});
const importMapEl = document.querySelector('script[type="importmap"]');
if(importMapEl) {
// map = JSON.parse(mapEl.innerHTML);
throw new Error('Cannot setup after importmap is set. Use <script type="sfc-importmap"> instead.');
}
const externalMapEl = document.querySelector('script[type="sfc-importmap"]');
if(externalMapEl) {
const externalMap = JSON.parse(externalMapEl.textContent);
Object.assign(map.imports, externalMap.imports);
Object.assign(map.scopes, externalMap.scopes);
}
Object.assign(map.imports, importMap);
const mapEl = document.createElement('script');
mapEl.setAttribute('type', 'importmap');
mapEl.textContent = JSON.stringify(map);
currentScript.after(mapEl);
if(mount) {
const script = document.createElement('script');
script.setAttribute('type', 'module');
const apps = mount.map((item, index)=>`
import App${index} from '${item[0]}';
createApp(App${index}).mount('${item[1]}');`).join('');
script.innerHTML = `
import {createApp} from 'vue';
${apps}
`;
document.body.appendChild(script);
}
}
setup();