-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
222 lines (215 loc) · 7.35 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
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
const fse = require("fs-extra");
const { parse } = require("url");
const { extname, basename } = require("path");
const MagicString = require("magic-string").default;
function group(arr, pred) {
const positives = [];
const negatives = [];
for (let item of arr) {
if (pred(item)) {
positives.push(item);
} else {
negatives.push(item);
}
}
return [positives, negatives];
}
const defaultOpts = {
keepSource: false, // replaced modules source write to output
cacheDir: "./dist_cdn",
output: "./dist_cdn",
uploader: (content, ext) => {
return Promise.resolve("");
},
};
function cdnUploadPlugin(options) {
let uploadedChunkMap = new Map();
let assetsList;
const opts = { ...defaultOpts, ...options };
const cacheLocalDir = `${opts.cacheDir}/.cdn_cache`;
const reIgnorePath = /^(?:(https?:)?\/\/)|(?:data:)/;
const upload = async (content, name) => {
// 文件名作为key
const originName = name;
name = basename(name);
const ext = extname(name);
// memory cache
if (uploadedChunkMap.get(name)) {
return Promise.resolve(uploadedChunkMap.get(name));
}
return new Promise((resolve, reject) => {
opts
.uploader(content, ext)
.then((url) => {
uploadedChunkMap.set(name, url);
if (opts.keepSource) {
fse.outputFileSync(`${opts.output}/${originName}`, content);
}
resolve(url);
})
.catch((e) => {
reject(
`[rollup-plugin-upload-cdn]:CDN_UPLOAD_ERROR: a error occur in upload :${e},please check your cdn uploader`
);
});
});
};
const replaceCssAssets = async (cssAssets) => {
const re = /url\((?!['"]?(?:data:|https?:|\/\/))(['"]?)([^'")]*)\1\)/g;
for (let file of cssAssets) {
let content = file.source;
content = content.replace(re, (match, _, media) => {
if (reIgnorePath.test(media)) return match;
const uploadedUrl = uploadedChunkMap.get(basename(media));
return match.replace(media, uploadedUrl);
});
await upload(content, file.fileName);
}
};
const findByName = (name, bundles) => {
return bundles.filter((v) => v.fileName === name)[0];
};
const getBundleAst = (_bundle) => {
// entry may have multi
const entryChunks = _bundle.filter(
(output) => output.type === "chunk" && output.isEntry
);
const output = [];
for (const entryChunk of entryChunks) {
const ast = entryChunk;
(function walk(chunk) {
let deps = [...chunk.dynamicImports, ...chunk.imports].map((name) =>
findByName(name, _bundle)
);
chunk.children = deps;
for (const chunk of deps) {
walk(chunk);
}
})(entryChunk);
output.push(ast);
}
return output;
};
const replaceCssAssetsLegacy = (content) => {
const re = /url\((?!['"]?(?:data:|https?:|\/\/))(['"]?)([^'")]*)\1\)/g;
content = content.replace(re, (match, _, media) => {
if (reIgnorePath.test(media)) return match;
const uploadedUrl = uploadedChunkMap.get(basename(media));
return match.replace(media, uploadedUrl);
});
return content;
};
const replaceChunks = async (chunkDeps) => {
const currentCall = async (curChunk) => {
const dynamicImports = curChunk.dynamicImports.map((fileName) =>
basename(fileName)
);
const imports = curChunk.imports.map((fileName) => basename(fileName));
let replaceList = [...assetsList, ...dynamicImports, ...imports];
let content = curChunk.code;
for (const name of replaceList) {
const reg = new RegExp(`\\.?\/?((([a-z]{1,})\/){1,})?${name}`, "g");
let url = uploadedChunkMap.get(name);
if (!url) {
console.error(
`the plugin error: in replace ${curChunk.fileName},${name} has no uploaded url`
);
}
// bug:plugin-legacy把css写入到legacy.js文件中,文件中css url()的路径未被修改
content = content.replace(reg, url);
}
const childrenName = chunkDeps.children.map((v) => v.fileName);
// console.log(`${curChunk.fileName} has upload to cdn,triggered by ${JSON.stringify(childrenName)}`)
await upload(content, curChunk.fileName);
};
if (chunkDeps.children?.length) {
let uploadPromises = chunkDeps.children.map((chunk) =>
replaceChunks(chunk)
);
await Promise.all(uploadPromises);
await currentCall(chunkDeps);
} else {
await upload(chunkDeps.code, chunkDeps.fileName);
}
// _uploadDepsToCdn(chunkDeps)
};
const replaceHtml = (html, bundles) => {
const reImport =
/(?:<(?:link|script|img)[^>]+(?:src|href)\s*=\s*)(['"]?)([^'"\s>]+)\1/g;
let content = html.source;
html.source = content.replace(reImport, (match, _, path) => {
if (reIgnorePath.test(path)) return match;
const url = uploadedChunkMap.get(basename(path));
if (!url) {
console.error(
`${html.fileName} has a module [${path}] not upload,please upload by manual`
);
}
return url ? match.replace(path, url) : match;
});
return html;
};
return {
name: "rollup-plugin-upload-cdn",
async buildStart(options) {
// local cache
const local_cache_exist = await fse.pathExists(cacheLocalDir);
if (local_cache_exist) {
let localCache = fse.readFileSync(cacheLocalDir);
localCache = localCache ? JSON.parse(localCache) : false;
if (localCache) {
const localMap = new Map(Object.entries(localCache));
uploadedChunkMap = new Map([...uploadedChunkMap, ...localMap]);
}
}
},
async writeBundle(options, bundle) {
// staticAssets => css => js(entry) => html
let bundles = Object.values(bundle);
const assets = bundles.filter((output) => output.type === "asset");
const chunks = bundles.filter((output) => output.type === "chunk");
const [cssOrHtml, staticAssets] = group(assets, (asset) =>
/\.(css|html)$/.test(asset.fileName)
);
const [cssAssets, htmlAssets] = group(cssOrHtml, (asset) =>
asset.fileName.endsWith(".css")
);
assetsList = assets
.filter((output) => !output.fileName.endsWith(".html"))
.map((output) => basename(output.fileName));
try {
// staticAssets upload
await Promise.all(
staticAssets.map((asset) => upload(asset.source, asset.fileName))
);
// replace css url
await replaceCssAssets(cssAssets);
// replace chunks url
const chunkDeps_array = getBundleAst(chunks);
for (const chunkDeps of chunkDeps_array) {
await replaceChunks(chunkDeps);
}
// const dist = options.dir
// replace html url
const html_cdn = htmlAssets.map((html) => replaceHtml(html, bundle));
for (const html of html_cdn) {
let fileName = html.fileName;
// fileName = fileName.replace(/(.+)\.(.+)/, '$1-cdn.$2')
fse.outputFileSync(`${opts.output}/${fileName}`, html.source);
}
} catch (e) {
console.error("error:", e);
}
},
closeBundle() {
// cache to local
if (uploadedChunkMap.size) {
fse.outputFileSync(
cacheLocalDir,
JSON.stringify(Object.fromEntries(uploadedChunkMap.entries()))
);
}
},
};
}
module.exports = cdnUploadPlugin;