vite-plugin-cross-origin-storage 1.3.8 → 1.3.10

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 CHANGED
@@ -1659,57 +1659,41 @@ function cosPlugin(options = {}) {
1659
1659
  }
1660
1660
  }
1661
1661
  if (mainChunk) {
1662
- const chunkInfo = {};
1662
+ const allChunks = Object.values(bundle).filter((c) => c.type === "chunk");
1663
+ const managedChunkNames = new Set(Object.keys(managedChunks));
1664
+ const managedChunkInfo = {};
1663
1665
  for (const fileName in managedChunks) {
1664
1666
  const nameHash = crypto.createHash("sha256").update(fileName).digest("hex").substring(0, 8);
1665
- chunkInfo[fileName] = {
1667
+ managedChunkInfo[fileName] = {
1666
1668
  globalVar: `__COS_CHUNK_${nameHash}__`,
1667
1669
  chunk: managedChunks[fileName]
1668
1670
  };
1669
1671
  }
1670
- const allChunks = Object.values(bundle).filter((c) => c.type === "chunk");
1671
- const managedChunkNames = new Set(Object.keys(managedChunks));
1672
- const chunksNeededInImportMap = new Set(managedChunkNames);
1673
- const queue = Array.from(managedChunkNames);
1674
- while (queue.length > 0) {
1675
- const currentName = queue.shift();
1676
- const chunk = bundle[currentName];
1677
- if (!chunk) continue;
1678
- [...chunk.imports, ...chunk.dynamicImports].forEach((dep) => {
1679
- if (!chunksNeededInImportMap.has(dep)) {
1680
- chunksNeededInImportMap.add(dep);
1681
- const depChunk = bundle[dep];
1682
- if (depChunk && depChunk.type === "chunk" && !managedChunkNames.has(dep)) {
1683
- queue.push(dep);
1684
- }
1685
- }
1686
- });
1687
- }
1688
1672
  for (const targetChunk of allChunks) {
1673
+ const isTargetManaged = managedChunkNames.has(targetChunk.fileName);
1689
1674
  const importerDir = path.dirname(targetChunk.fileName);
1690
1675
  for (const depFileName in bundle) {
1691
1676
  const depChunk = bundle[depFileName];
1692
1677
  if (!depChunk || depChunk.type !== "chunk") continue;
1693
- let relPath = path.relative(importerDir, depFileName);
1694
- if (!relPath.startsWith(".")) relPath = "./" + relPath;
1695
- const escapedRelPath = relPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1696
- const staticPattern = `import\\s*(?:(?:\\{\\s*([^}]+)\\s*\\}|\\*\\s+as\\s+([^\\s]+)|([^\\s\\{\\}]+))\\s*from\\s*)?['"]${escapedRelPath}['"];?`;
1697
- const staticRegex = new RegExp(staticPattern, "g");
1698
- const dynamicPattern = `import\\s*\\(\\s*['"]${escapedRelPath}['"]\\s*\\)`;
1699
- const dynamicRegex = new RegExp(dynamicPattern, "g");
1700
- const exportPattern = `export\\s*(?:(?:\\{\\s*([^}]+)\\s*\\}|\\*\\s*(?:as\\s+([^\\s]+))?))\\s*from\\s*['"]${escapedRelPath}['"];?`;
1701
- const exportRegex = new RegExp(exportPattern, "g");
1702
1678
  const isDepManaged = managedChunkNames.has(depFileName);
1703
- const isTargetManaged = managedChunkNames.has(targetChunk.fileName);
1704
- if (isDepManaged || isTargetManaged) {
1679
+ if (isTargetManaged || isDepManaged) {
1680
+ let relPath = path.relative(importerDir, depFileName);
1681
+ if (!relPath.startsWith(".")) relPath = "./" + relPath;
1682
+ const escapedRelPath = relPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1705
1683
  const bareSpecifier = depFileName;
1684
+ const staticPattern = `import\\s*(?:(?:\\{\\s*([^}]+)\\s*\\}|\\*\\s+as\\s+([^\\s]+)|([^\\s\\{\\}]+))\\s*from\\s*)?['"]${escapedRelPath}['"];?`;
1685
+ const staticRegex = new RegExp(staticPattern, "g");
1706
1686
  targetChunk.code = targetChunk.code.replace(staticRegex, (_match, named, namespace, defaultImport) => {
1707
1687
  if (named) return `import {${named}} from "${bareSpecifier}";`;
1708
1688
  if (namespace) return `import * as ${namespace} from "${bareSpecifier}";`;
1709
1689
  if (defaultImport) return `import ${defaultImport} from "${bareSpecifier}";`;
1710
1690
  return `import "${bareSpecifier}";`;
1711
1691
  });
1692
+ const dynamicPattern = `import\\s*\\(\\s*['"]${escapedRelPath}['"]\\s*\\)`;
1693
+ const dynamicRegex = new RegExp(dynamicPattern, "g");
1712
1694
  targetChunk.code = targetChunk.code.replace(dynamicRegex, () => `import("${bareSpecifier}")`);
1695
+ const exportPattern = `export\\s*(?:(?:\\{\\s*([^}]+)\\s*\\}|\\*\\s*(?:as\\s+([^\\s]+))?))\\s*from\\s*['"]${escapedRelPath}['"];?`;
1696
+ const exportRegex = new RegExp(exportPattern, "g");
1713
1697
  targetChunk.code = targetChunk.code.replace(exportRegex, (_match, named, namespace) => {
1714
1698
  if (named) return `export {${named}} from "${bareSpecifier}";`;
1715
1699
  if (namespace) return `export * as ${namespace} from "${bareSpecifier}";`;
@@ -1720,29 +1704,17 @@ function cosPlugin(options = {}) {
1720
1704
  }
1721
1705
  const manifest = {};
1722
1706
  const base = config.base.endsWith("/") ? config.base : config.base + "/";
1723
- for (const fileName in bundle) {
1724
- const chunk = bundle[fileName];
1725
- if (chunk.type !== "chunk") continue;
1726
- if (chunksNeededInImportMap.has(fileName)) {
1727
- if (managedChunkNames.has(fileName)) {
1728
- const { globalVar } = chunkInfo[fileName];
1729
- const finalHash = crypto.createHash("sha256").update(chunk.code).digest("hex");
1730
- const hasDefault = chunk.exports.includes("default");
1731
- manifest[fileName] = {
1732
- fileName,
1733
- file: `${base}${fileName}`,
1734
- hash: finalHash,
1735
- globalVar,
1736
- hasDefault
1737
- };
1738
- } else {
1739
- manifest[fileName] = {
1740
- fileName,
1741
- file: `${base}${fileName}`,
1742
- unmanaged: true
1743
- };
1744
- }
1745
- }
1707
+ for (const fileName in managedChunkInfo) {
1708
+ const { chunk, globalVar } = managedChunkInfo[fileName];
1709
+ const finalHash = crypto.createHash("sha256").update(chunk.code).digest("hex");
1710
+ const hasDefault = chunk.exports.includes("default");
1711
+ manifest[fileName] = {
1712
+ fileName,
1713
+ file: `${base}${fileName}`,
1714
+ hash: finalHash,
1715
+ globalVar,
1716
+ hasDefault
1717
+ };
1746
1718
  }
1747
1719
  manifest["index"] = {
1748
1720
  file: `${config.base.endsWith("/") ? config.base : config.base + "/"}${mainChunk.fileName}`
package/dist/loader.js CHANGED
@@ -51,15 +51,17 @@
51
51
  }
52
52
 
53
53
  // Load all managed chunks in parallel
54
- if (Object.keys(manifest).length > 0) {
54
+ if (chunksToLoad.length > 0) {
55
55
  const importMap = { imports: {} };
56
56
 
57
- // Fill unmanaged chunks first
58
- for (const fileName in manifest) {
59
- const entry = manifest[fileName];
60
- if (fileName !== 'index' && !entry.hash) {
61
- importMap.imports[fileName] = entry.file;
62
- }
57
+ // Prefix mapping: Handles all unmanaged chunks automatically.
58
+ // Longest prefix wins in Import Maps, so specific managed entries below take precedence.
59
+ // We assume all chunks are in the same relative directory as the managed ones.
60
+ const firstChunk = chunksToLoad[0];
61
+ const assetsDir = firstChunk.fileName.substring(0, firstChunk.fileName.lastIndexOf('/') + 1);
62
+ const assetsUrl = firstChunk.file.substring(0, firstChunk.file.lastIndexOf('/') + 1);
63
+ if (assetsDir && assetsUrl) {
64
+ importMap.imports[assetsDir] = assetsUrl;
63
65
  }
64
66
 
65
67
  await Promise.all(chunksToLoad.map(async (chunk) => {
@@ -87,15 +89,10 @@
87
89
  }
88
90
 
89
91
  if (url) {
90
- // Create a Data URL shim that re-exports everything from the Blob URL.
91
- const shimSource = chunk.hasDefault
92
- ? `export * from "${url}"; export { default } from "${url}";`
93
- : `export * from "${url}";`;
94
- const shimUrl = `data:text/javascript;base64,${btoa(shimSource)}`;
95
-
96
- // Map the bare specifier (fileName) and full paths to this shim
97
- importMap.imports[chunk.fileName] = shimUrl; // bare specifier
98
- importMap.imports[chunk.file] = shimUrl; // absolute path (legacy/fallback)
92
+ // Map the bare specifier and absolute path directly to the Blob URL.
93
+ // This avoids the indirection of a Data URL shim and handles cycles naturally.
94
+ importMap.imports[chunk.fileName] = url;
95
+ importMap.imports[chunk.file] = url;
99
96
 
100
97
  // Also set global if anyone still needs it (legacy)
101
98
  if (chunk.globalVar) {
package/loader.js CHANGED
@@ -51,15 +51,17 @@
51
51
  }
52
52
 
53
53
  // Load all managed chunks in parallel
54
- if (Object.keys(manifest).length > 0) {
54
+ if (chunksToLoad.length > 0) {
55
55
  const importMap = { imports: {} };
56
56
 
57
- // Fill unmanaged chunks first
58
- for (const fileName in manifest) {
59
- const entry = manifest[fileName];
60
- if (fileName !== 'index' && !entry.hash) {
61
- importMap.imports[fileName] = entry.file;
62
- }
57
+ // Prefix mapping: Handles all unmanaged chunks automatically.
58
+ // Longest prefix wins in Import Maps, so specific managed entries below take precedence.
59
+ // We assume all chunks are in the same relative directory as the managed ones.
60
+ const firstChunk = chunksToLoad[0];
61
+ const assetsDir = firstChunk.fileName.substring(0, firstChunk.fileName.lastIndexOf('/') + 1);
62
+ const assetsUrl = firstChunk.file.substring(0, firstChunk.file.lastIndexOf('/') + 1);
63
+ if (assetsDir && assetsUrl) {
64
+ importMap.imports[assetsDir] = assetsUrl;
63
65
  }
64
66
 
65
67
  await Promise.all(chunksToLoad.map(async (chunk) => {
@@ -87,15 +89,10 @@
87
89
  }
88
90
 
89
91
  if (url) {
90
- // Create a Data URL shim that re-exports everything from the Blob URL.
91
- const shimSource = chunk.hasDefault
92
- ? `export * from "${url}"; export { default } from "${url}";`
93
- : `export * from "${url}";`;
94
- const shimUrl = `data:text/javascript;base64,${btoa(shimSource)}`;
95
-
96
- // Map the bare specifier (fileName) and full paths to this shim
97
- importMap.imports[chunk.fileName] = shimUrl; // bare specifier
98
- importMap.imports[chunk.file] = shimUrl; // absolute path (legacy/fallback)
92
+ // Map the bare specifier and absolute path directly to the Blob URL.
93
+ // This avoids the indirection of a Data URL shim and handles cycles naturally.
94
+ importMap.imports[chunk.fileName] = url;
95
+ importMap.imports[chunk.file] = url;
99
96
 
100
97
  // Also set global if anyone still needs it (legacy)
101
98
  if (chunk.globalVar) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-cross-origin-storage",
3
- "version": "1.3.8",
3
+ "version": "1.3.10",
4
4
  "description": "Vite plugin to load chunks from Cross-Origin Storage",
5
5
  "keywords": [
6
6
  "vite",