vite-plugin-cross-origin-storage 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +0 -1
- package/dist/index.mjs +18 -11
- package/dist/loader.d.mts +9 -16
- package/dist/loader.entry.mjs +1 -3
- package/dist/loader.mjs +23 -15
- package/package.json +14 -12
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,6 @@ import { fileURLToPath } from "node:url";
|
|
|
4
4
|
import MagicString from "magic-string";
|
|
5
5
|
import { rolldown } from "rolldown";
|
|
6
6
|
import { parseAst } from "rolldown/parseAst";
|
|
7
|
-
|
|
8
7
|
//#region src/index.ts
|
|
9
8
|
const MANIFEST_PLACEHOLDER = "__COS_MANIFEST__";
|
|
10
9
|
/**
|
|
@@ -24,6 +23,14 @@ function defaultLoaderEntry() {
|
|
|
24
23
|
function contentSpecifier(hash) {
|
|
25
24
|
return `${RECIPE}:${hash}`;
|
|
26
25
|
}
|
|
26
|
+
/** Recover the npm package name from a resolved module id's `node_modules` segment. */
|
|
27
|
+
function packageNameFromId(id) {
|
|
28
|
+
const index = id.lastIndexOf("/node_modules/");
|
|
29
|
+
if (index === -1) return;
|
|
30
|
+
const [first, second] = id.slice(index + 14).split("/");
|
|
31
|
+
if (!first) return;
|
|
32
|
+
return first.startsWith("@") && second ? `${first}/${second}` : first;
|
|
33
|
+
}
|
|
27
34
|
function toMatchers(packages) {
|
|
28
35
|
return packages.map((p) => typeof p === "string" ? new RegExp(`^${p}$`) : p);
|
|
29
36
|
}
|
|
@@ -63,6 +70,11 @@ function collectImportSources(code) {
|
|
|
63
70
|
start: source.start,
|
|
64
71
|
end: source.end
|
|
65
72
|
});
|
|
73
|
+
else if (source?.type === "TemplateLiteral" && source.expressions?.length === 0 && source.quasis?.length === 1 && typeof source.quasis[0]?.value?.cooked === "string" && typeof source.start === "number" && typeof source.end === "number") sources.push({
|
|
74
|
+
value: source.quasis[0].value.cooked,
|
|
75
|
+
start: source.start,
|
|
76
|
+
end: source.end
|
|
77
|
+
});
|
|
66
78
|
}
|
|
67
79
|
for (const key in record) if (key !== "type") visit(record[key]);
|
|
68
80
|
};
|
|
@@ -190,18 +202,14 @@ function cosPlugin(options) {
|
|
|
190
202
|
hashes.set(id, hash);
|
|
191
203
|
managed[contentSpecifier(hash)] = {
|
|
192
204
|
file: `${hash}.js`,
|
|
193
|
-
hash
|
|
205
|
+
hash,
|
|
206
|
+
name: packageNameFromId(id)
|
|
194
207
|
};
|
|
195
|
-
|
|
208
|
+
this.emitFile({
|
|
196
209
|
type: "asset",
|
|
197
210
|
fileName,
|
|
198
|
-
name: hash,
|
|
199
|
-
names: [hash],
|
|
200
|
-
originalFileName: null,
|
|
201
|
-
originalFileNames: [],
|
|
202
|
-
needsCodeReference: false,
|
|
203
211
|
source: resolved
|
|
204
|
-
};
|
|
212
|
+
});
|
|
205
213
|
return hash;
|
|
206
214
|
};
|
|
207
215
|
for (const id of raw.keys()) visit(id, []);
|
|
@@ -237,6 +245,5 @@ function cosPlugin(options) {
|
|
|
237
245
|
}
|
|
238
246
|
};
|
|
239
247
|
}
|
|
240
|
-
|
|
241
248
|
//#endregion
|
|
242
|
-
export { cosPlugin, cosPlugin as default };
|
|
249
|
+
export { cosPlugin, cosPlugin as default };
|
package/dist/loader.d.mts
CHANGED
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
//#region src/loader.d.ts
|
|
2
2
|
declare global {
|
|
3
|
-
interface
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
algorithm: string;
|
|
7
|
-
value: string;
|
|
8
|
-
}>, options?: {
|
|
9
|
-
create?: boolean;
|
|
10
|
-
}) => Promise<Array<{
|
|
11
|
-
getFile: () => Promise<File>;
|
|
12
|
-
createWritable: () => Promise<{
|
|
13
|
-
write: (data: Blob) => Promise<void>;
|
|
14
|
-
close: () => Promise<void>;
|
|
15
|
-
}>;
|
|
16
|
-
}>>;
|
|
17
|
-
};
|
|
3
|
+
interface Window {
|
|
4
|
+
/** The manifest the loader ran with, exposed for introspection (e.g. devtools, demo UIs). */
|
|
5
|
+
__cosManifest?: CosManifest;
|
|
18
6
|
}
|
|
19
7
|
}
|
|
20
8
|
interface CosManifest {
|
|
@@ -28,10 +16,15 @@ interface CosManifest {
|
|
|
28
16
|
specifier: string;
|
|
29
17
|
file: string;
|
|
30
18
|
};
|
|
31
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Map of content-addressed specifier to `{ file, hash, name }` for every COS-managed chunk.
|
|
21
|
+
* `name` is the npm package the chunk was bundled from (e.g. `vue`, `@vue/reactivity`), when
|
|
22
|
+
* it could be derived from the resolved module path; absent for chunks it couldn't be.
|
|
23
|
+
*/
|
|
32
24
|
chunks: Record<string, {
|
|
33
25
|
file: string;
|
|
34
26
|
hash: string;
|
|
27
|
+
name?: string;
|
|
35
28
|
}>;
|
|
36
29
|
}
|
|
37
30
|
//#endregion
|
package/dist/loader.entry.mjs
CHANGED
package/dist/loader.mjs
CHANGED
|
@@ -1,32 +1,41 @@
|
|
|
1
1
|
//#region src/loader.ts
|
|
2
2
|
async function runCosLoader(manifest) {
|
|
3
|
+
window.__cosManifest = manifest;
|
|
3
4
|
const cos = navigator.crossOriginStorage;
|
|
4
5
|
const imports = {};
|
|
6
|
+
let cosQueue = Promise.resolve();
|
|
7
|
+
const enqueue = (task) => {
|
|
8
|
+
const result = cosQueue.then(task, task);
|
|
9
|
+
cosQueue = result.catch(() => {});
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
5
12
|
async function resolveChunk(hash, file) {
|
|
6
13
|
if (cos) try {
|
|
7
|
-
const
|
|
14
|
+
const blob = await (await enqueue(() => cos.requestFileHandle({
|
|
8
15
|
algorithm: "SHA-256",
|
|
9
16
|
value: hash
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
const blob = await handle.getFile();
|
|
13
|
-
return URL.createObjectURL(new Blob([blob], { type: "text/javascript" }));
|
|
14
|
-
}
|
|
17
|
+
}))).getFile();
|
|
18
|
+
return URL.createObjectURL(new Blob([blob], { type: "text/javascript" }));
|
|
15
19
|
} catch (error) {
|
|
16
20
|
if (error?.name !== "NotFoundError") console.error("[cos] lookup failed", error);
|
|
17
21
|
}
|
|
18
22
|
const response = await fetch(file);
|
|
23
|
+
if (!response.ok) throw new Error(`[cos] failed to fetch chunk ${file}: ${response.status} ${response.statusText}`);
|
|
24
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
25
|
+
if (contentType && !/javascript|ecmascript/i.test(contentType)) throw new Error(`[cos] chunk ${file} served as ${contentType}, expected JavaScript`);
|
|
19
26
|
const blob = new Blob([await response.blob()], { type: "text/javascript" });
|
|
20
27
|
if (cos) try {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
await enqueue(async () => {
|
|
29
|
+
const writable = await (await cos.requestFileHandle({
|
|
30
|
+
algorithm: "SHA-256",
|
|
31
|
+
value: hash
|
|
32
|
+
}, {
|
|
33
|
+
create: true,
|
|
34
|
+
origins: "*"
|
|
35
|
+
})).createWritable();
|
|
27
36
|
await writable.write(blob);
|
|
28
37
|
await writable.close();
|
|
29
|
-
}
|
|
38
|
+
});
|
|
30
39
|
} catch (error) {
|
|
31
40
|
console.error("[cos] store failed", error);
|
|
32
41
|
}
|
|
@@ -46,6 +55,5 @@ async function runCosLoader(manifest) {
|
|
|
46
55
|
manifest.entry.specifier
|
|
47
56
|
);
|
|
48
57
|
}
|
|
49
|
-
|
|
50
58
|
//#endregion
|
|
51
|
-
export { runCosLoader };
|
|
59
|
+
export { runCosLoader };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-cross-origin-storage",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.3",
|
|
5
5
|
"description": "Vite plugin to extract shared dependencies into content-addressed chunks loaded from Cross-Origin Storage",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Daniel Roe",
|
|
@@ -43,23 +43,25 @@
|
|
|
43
43
|
"node": ">=20.19.0"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0"
|
|
46
|
+
"vite": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"magic-string": "^0.30.21",
|
|
50
|
-
"rolldown": "1.
|
|
50
|
+
"rolldown": "1.2.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/node": "latest",
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
54
|
+
"@types/wicg-cross-origin-storage": "2026.7.0",
|
|
55
|
+
"eslint": "10.7.0",
|
|
56
|
+
"hookable": "6.1.1",
|
|
57
|
+
"playwright-core": "1.61.1",
|
|
58
|
+
"tsdown": "0.22.9",
|
|
59
|
+
"typescript": "6.0.3",
|
|
60
|
+
"unhead": "3.1.8",
|
|
61
|
+
"vite": "8.1.4",
|
|
62
|
+
"vite8": "npm:vite@8.1.4",
|
|
63
|
+
"vitest": "4.1.10",
|
|
64
|
+
"vue": "3.5.40"
|
|
63
65
|
},
|
|
64
66
|
"scripts": {
|
|
65
67
|
"build": "tsdown",
|