vite-plugin-cross-origin-storage 1.4.1 → 1.4.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.js +34 -30
- package/dist/loader.js +13 -11
- package/loader.js +13 -11
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1619,7 +1619,9 @@ var hasStringIsWellFormed = "isWellFormed" in String.prototype;
|
|
|
1619
1619
|
|
|
1620
1620
|
// index.ts
|
|
1621
1621
|
function cosPlugin(options = {}) {
|
|
1622
|
-
const filter = options.include || options.exclude ? createFilter(options.include || ["**/*"], options.exclude, {
|
|
1622
|
+
const filter = options.include || options.exclude ? createFilter(options.include || ["**/*"], options.exclude, {
|
|
1623
|
+
resolve: false
|
|
1624
|
+
}) : () => true;
|
|
1623
1625
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
1624
1626
|
const loaderPath = path.resolve(__dirname, "./loader.js");
|
|
1625
1627
|
let config;
|
|
@@ -1649,14 +1651,13 @@ function cosPlugin(options = {}) {
|
|
|
1649
1651
|
if (chunk.isEntry) {
|
|
1650
1652
|
console.log(`COS Plugin: [ENTRY] ${fileName}`);
|
|
1651
1653
|
mainChunk = chunk;
|
|
1652
|
-
}
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
}
|
|
1654
|
+
}
|
|
1655
|
+
const res = filter(fileName) || filter(chunk.name);
|
|
1656
|
+
console.log(
|
|
1657
|
+
`COS Plugin: [FILTER] ${fileName} (name: ${chunk.name}) -> ${res ? "INCLUDE" : "SKIP"}`
|
|
1658
|
+
);
|
|
1659
|
+
if (res) {
|
|
1660
|
+
managedChunks[fileName] = chunk;
|
|
1660
1661
|
}
|
|
1661
1662
|
}
|
|
1662
1663
|
if (fileName === "index.html" && chunk.type === "asset") {
|
|
@@ -1671,35 +1672,35 @@ function cosPlugin(options = {}) {
|
|
|
1671
1672
|
const unmanagedDependencies = /* @__PURE__ */ new Set();
|
|
1672
1673
|
const base = config.base.endsWith("/") ? config.base : config.base + "/";
|
|
1673
1674
|
for (const targetChunk of allChunks) {
|
|
1674
|
-
const isTargetManaged = managedChunkNames.has(targetChunk.fileName);
|
|
1675
1675
|
const importerDir = path.dirname(targetChunk.fileName);
|
|
1676
1676
|
const deps = [...targetChunk.imports, ...targetChunk.dynamicImports];
|
|
1677
1677
|
for (const depFileName of deps) {
|
|
1678
1678
|
const depChunk = bundle[depFileName];
|
|
1679
1679
|
if (!depChunk || depChunk.type !== "chunk") continue;
|
|
1680
1680
|
const isDepManaged = managedChunkNames.has(depFileName);
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1681
|
+
let relPath = path.relative(importerDir, depFileName);
|
|
1682
|
+
if (!relPath.startsWith(".")) relPath = "./" + relPath;
|
|
1683
|
+
const escapedRelPath = relPath.replace(
|
|
1684
|
+
/[.*+?^${}()|[\]\\]/g,
|
|
1685
|
+
"\\$&"
|
|
1686
|
+
);
|
|
1687
|
+
const bareSpecifier = `coschunk-${depFileName.replace(/\//g, "-")}`;
|
|
1688
|
+
const staticPattern = `(import|export)\\b\\s*((?:(?!\\bimport\\b|\\bexport\\b)[\\s\\S])*?\\bfrom\\b\\s*)?['"]${escapedRelPath}['"]\\s*;?`;
|
|
1689
|
+
const staticRegex = new RegExp(staticPattern, "g");
|
|
1690
|
+
targetChunk.code = targetChunk.code.replace(
|
|
1691
|
+
staticRegex,
|
|
1692
|
+
(match, keyword, fromPart) => {
|
|
1692
1693
|
return `${keyword}${fromPart ? " " + fromPart : " "}"${bareSpecifier}";`;
|
|
1693
|
-
});
|
|
1694
|
-
const dynamicPattern = `import\\s*\\(\\s*['"]${escapedRelPath}['"]\\s*\\)`;
|
|
1695
|
-
const dynamicRegex = new RegExp(dynamicPattern, "g");
|
|
1696
|
-
targetChunk.code = targetChunk.code.replace(
|
|
1697
|
-
dynamicRegex,
|
|
1698
|
-
() => `import("${bareSpecifier}")`
|
|
1699
|
-
);
|
|
1700
|
-
if (!isDepManaged) {
|
|
1701
|
-
unmanagedDependencies.add(depFileName);
|
|
1702
1694
|
}
|
|
1695
|
+
);
|
|
1696
|
+
const dynamicPattern = `import\\s*\\(\\s*['"]${escapedRelPath}['"]\\s*\\)`;
|
|
1697
|
+
const dynamicRegex = new RegExp(dynamicPattern, "g");
|
|
1698
|
+
targetChunk.code = targetChunk.code.replace(
|
|
1699
|
+
dynamicRegex,
|
|
1700
|
+
() => `import("${bareSpecifier}")`
|
|
1701
|
+
);
|
|
1702
|
+
if (!isDepManaged) {
|
|
1703
|
+
unmanagedDependencies.add(depFileName);
|
|
1703
1704
|
}
|
|
1704
1705
|
}
|
|
1705
1706
|
}
|
|
@@ -1713,6 +1714,9 @@ function cosPlugin(options = {}) {
|
|
|
1713
1714
|
const finalHash = crypto.createHash("sha256").update(chunk.code).digest("hex");
|
|
1714
1715
|
manifest.chunks[fileName] = finalHash;
|
|
1715
1716
|
}
|
|
1717
|
+
if (mainChunk && !managedChunkNames.has(mainChunk.fileName)) {
|
|
1718
|
+
unmanagedDependencies.add(mainChunk.fileName);
|
|
1719
|
+
}
|
|
1716
1720
|
manifest.unmanaged = Array.from(unmanagedDependencies);
|
|
1717
1721
|
if (htmlAsset) {
|
|
1718
1722
|
try {
|
package/dist/loader.js
CHANGED
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
async function
|
|
58
|
+
async function getChunkBlobUrl(chunk) {
|
|
59
59
|
let blob = await getBlobFromCOS(chunk.hash);
|
|
60
60
|
if (blob) {
|
|
61
61
|
console.log(`COS Loader: ${chunk.fileName} found in COS`);
|
|
@@ -85,23 +85,24 @@
|
|
|
85
85
|
try {
|
|
86
86
|
console.log('COS Loader: Starting app...');
|
|
87
87
|
|
|
88
|
-
// Resolve all chunks to
|
|
88
|
+
// Resolve all chunks to Blob URLs
|
|
89
89
|
const importMap = { imports: {} };
|
|
90
90
|
|
|
91
|
-
// Set up unmanaged dependencies correctly so
|
|
91
|
+
// Set up unmanaged dependencies correctly so Blob URLs can resolve them.
|
|
92
92
|
for (const fileName of manifest.unmanaged || []) {
|
|
93
93
|
const bareSpecifier = `coschunk-${fileName.replace(/\//g, '-')}`;
|
|
94
|
-
importMap.imports[bareSpecifier] =
|
|
94
|
+
importMap.imports[bareSpecifier] =
|
|
95
|
+
window.location.origin + base + fileName;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
console.log(`COS Loader: Loading ${chunksToLoad.length} chunks...`);
|
|
98
99
|
const loadPromises = chunksToLoad.map(async (chunk) => {
|
|
99
|
-
const
|
|
100
|
-
if (
|
|
100
|
+
const blobUrl = await getChunkBlobUrl(chunk);
|
|
101
|
+
if (blobUrl) {
|
|
101
102
|
// Use a hyphenated prefix and replace all slashes to ensure it's treated
|
|
102
103
|
// as a truly bare specifier, bypassing hierarchical/protocol checks.
|
|
103
104
|
const bareSpecifier = `coschunk-${chunk.fileName.replace(/\//g, '-')}`;
|
|
104
|
-
importMap.imports[bareSpecifier] =
|
|
105
|
+
importMap.imports[bareSpecifier] = blobUrl;
|
|
105
106
|
}
|
|
106
107
|
});
|
|
107
108
|
|
|
@@ -115,13 +116,14 @@
|
|
|
115
116
|
|
|
116
117
|
console.log('COS Loader: Import Map injected');
|
|
117
118
|
|
|
118
|
-
// Import the main entry
|
|
119
|
-
|
|
119
|
+
// Import the main entry via its bare specifier to ensure it resolves
|
|
120
|
+
// through the import map and can find other managed chunks.
|
|
121
|
+
const entrySpecifier = `coschunk-${mainEntry.replace(/\//g, '-')}`;
|
|
120
122
|
|
|
121
|
-
// Small delay to ensure the browser has fully registered the
|
|
123
|
+
// Small delay to ensure the browser has fully registered the
|
|
122
124
|
// import map before resolving the first module.
|
|
123
125
|
setTimeout(() => {
|
|
124
|
-
import(
|
|
126
|
+
import(entrySpecifier).catch((err) => {
|
|
125
127
|
console.error('COS Loader: Failed to start app', err);
|
|
126
128
|
});
|
|
127
129
|
}, 0);
|
package/loader.js
CHANGED
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
async function
|
|
58
|
+
async function getChunkBlobUrl(chunk) {
|
|
59
59
|
let blob = await getBlobFromCOS(chunk.hash);
|
|
60
60
|
if (blob) {
|
|
61
61
|
console.log(`COS Loader: ${chunk.fileName} found in COS`);
|
|
@@ -85,23 +85,24 @@
|
|
|
85
85
|
try {
|
|
86
86
|
console.log('COS Loader: Starting app...');
|
|
87
87
|
|
|
88
|
-
// Resolve all chunks to
|
|
88
|
+
// Resolve all chunks to Blob URLs
|
|
89
89
|
const importMap = { imports: {} };
|
|
90
90
|
|
|
91
|
-
// Set up unmanaged dependencies correctly so
|
|
91
|
+
// Set up unmanaged dependencies correctly so Blob URLs can resolve them.
|
|
92
92
|
for (const fileName of manifest.unmanaged || []) {
|
|
93
93
|
const bareSpecifier = `coschunk-${fileName.replace(/\//g, '-')}`;
|
|
94
|
-
importMap.imports[bareSpecifier] =
|
|
94
|
+
importMap.imports[bareSpecifier] =
|
|
95
|
+
window.location.origin + base + fileName;
|
|
95
96
|
}
|
|
96
97
|
|
|
97
98
|
console.log(`COS Loader: Loading ${chunksToLoad.length} chunks...`);
|
|
98
99
|
const loadPromises = chunksToLoad.map(async (chunk) => {
|
|
99
|
-
const
|
|
100
|
-
if (
|
|
100
|
+
const blobUrl = await getChunkBlobUrl(chunk);
|
|
101
|
+
if (blobUrl) {
|
|
101
102
|
// Use a hyphenated prefix and replace all slashes to ensure it's treated
|
|
102
103
|
// as a truly bare specifier, bypassing hierarchical/protocol checks.
|
|
103
104
|
const bareSpecifier = `coschunk-${chunk.fileName.replace(/\//g, '-')}`;
|
|
104
|
-
importMap.imports[bareSpecifier] =
|
|
105
|
+
importMap.imports[bareSpecifier] = blobUrl;
|
|
105
106
|
}
|
|
106
107
|
});
|
|
107
108
|
|
|
@@ -115,13 +116,14 @@
|
|
|
115
116
|
|
|
116
117
|
console.log('COS Loader: Import Map injected');
|
|
117
118
|
|
|
118
|
-
// Import the main entry
|
|
119
|
-
|
|
119
|
+
// Import the main entry via its bare specifier to ensure it resolves
|
|
120
|
+
// through the import map and can find other managed chunks.
|
|
121
|
+
const entrySpecifier = `coschunk-${mainEntry.replace(/\//g, '-')}`;
|
|
120
122
|
|
|
121
|
-
// Small delay to ensure the browser has fully registered the
|
|
123
|
+
// Small delay to ensure the browser has fully registered the
|
|
122
124
|
// import map before resolving the first module.
|
|
123
125
|
setTimeout(() => {
|
|
124
|
-
import(
|
|
126
|
+
import(entrySpecifier).catch((err) => {
|
|
125
127
|
console.error('COS Loader: Failed to start app', err);
|
|
126
128
|
});
|
|
127
129
|
}, 0);
|