-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (138 loc) · 3.3 KB
/
index.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
const cheerio = require('cheerio');
const preloadHelperId = 'vite/preload-helper';
const preloadMethod = `__vitePreload`;
function preload(baseModule, deps){
if (!deps || deps.length === 0) {
return baseModule()
}
var rdeps=[];
var links = document.getElementsByTagName("LINK");
var ps=[];
deps.forEach(function(dep) {
// @ts-ignore
dep = base + dep;
// @ts-ignore
if (Object.prototype.hasOwnProperty.call(seen,dep)) return;
// @ts-ignore
seen[dep] = true;
var isCss = dep.endsWith('.css');
// @ts-ignore check if the file is already preloaded by SSR markup
var link;
var i = links.length;
while(i-->0){
link = links[i];
if(link.href == dep && link.rel=="stylesheet"){
return ;
}
}
if (isCss) {
// @ts-ignore
link = document.createElement('link');
// @ts-ignore
link.rel = 'stylesheet';
link.href = dep;
// @ts-ignore
// document.head.appendChild(link);
// ps.push( new Promise(function(res, rej) {
// link.onload = function(){
// res(this);
// };
// link.onerror = function(message){
// rej(new Error(message));
// };
// }));
}else{
rdeps.push(dep);
}
});
if(rdeps){
ps.push(new Promise(function(resolve,reject){
require(rdeps,resolve,reject);
}));
}
return Promise.all(rdeps).then(baseModule);
};
function amd(options) {
if (!options) options = {};
var requirejs = options.requirejs;
let config;
return {
name: 'amd',
enforce: 'pre',
apply: 'build',
config(config, { command }) {
if (command === 'build') {
//console.log(config);
var build = config.build;
if (!build) {
build = config.build = {};
}
var rollupOptions = build.rollupOptions;
if (!rollupOptions) {
rollupOptions = build.rollupOptions = {};
}
var output = rollupOptions.output;
if (!output) {
output = rollupOptions.output = {};
}
build.polyfillModulePreload = false;
output.format = "amd";
output.freeze = options.freeze || false;
output.strict = options.strict || false;
output.interop = options.interop || false;
}
},
transformIndexHtml(html, { bundle }) {
//console.log(bundle);
const $ = cheerio.load(html);
var entries = [];
$('script[type=module]').each(function () {
var url = $(this).attr('src');
if (!url.match(/^\w+:/.test(url))) {
entries.push(url);
$(this).remove();
}
});
$('link[rel=modulepreload]').each(function () {
var url = $(this).attr('href');
if (!url.match(/^\w+:/.test(url))) {
entries.push(url);
$(this).remove();
}
});
if(requirejs){
$('head').append(
$('<script></script>').attr('src', requirejs)
);
}
var requirejsConfig=options.config;
if(requirejsConfig){
if(typeof requirejsConfig!=="string"){
requirejsConfig=JSON.stringify(requirejsConfig)
}
$('head').append(
$('<script></script>').html(`require.config(${requirejsConfig});`)
);
}
$('head').append(
$('<script></script>').html(`require(${JSON.stringify(entries)});`)
);
return $.html();
},
configResolved(_config) {
config = _config;
},
load(id) {
Promise.resolve();
if (id === preloadHelperId) {
return `
var seen = {};
var base = '${config.base}';
export var ${preloadMethod}=${preload.toString()};
`;
}
}
}
}
amd.default = amd;
module.exports = amd;