vite-plugin-cross-origin-storage 1.5.1 → 1.6.0

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
@@ -1743,9 +1743,14 @@ function cosPlugin(options = {}) {
1743
1743
  }
1744
1744
  }
1745
1745
  }
1746
+ const specifierToCode = {};
1746
1747
  for (const specifier of discoveredSpecifiers) {
1747
1748
  try {
1748
1749
  const pkgPath = require2.resolve(specifier);
1750
+ const pkgName = Array.from(magicPackages).find((p) => specifier === p || specifier.startsWith(`${p}/`));
1751
+ const otherSpecifiersFromSamePkg = Array.from(discoveredSpecifiers).filter(
1752
+ (s) => s !== specifier && (s === pkgName || s.startsWith(`${pkgName}/`))
1753
+ );
1749
1754
  const esbuildRequire = createRequire(import.meta.url);
1750
1755
  const esbuild = esbuildRequire("esbuild");
1751
1756
  const buildResult = await esbuild.build({
@@ -1755,14 +1760,26 @@ function cosPlugin(options = {}) {
1755
1760
  minify: true,
1756
1761
  platform: "browser",
1757
1762
  write: false,
1758
- // Don't write to disk
1759
1763
  target: "esnext",
1760
- // Neutralize environment
1764
+ // Mark other components of the same library as external to avoid duplication
1765
+ external: otherSpecifiersFromSamePkg,
1761
1766
  define: {
1762
1767
  "process.env.NODE_ENV": '"production"'
1763
1768
  }
1764
1769
  });
1765
- const content = buildResult.outputFiles[0].contents;
1770
+ let code = buildResult.outputFiles[0].text;
1771
+ for (const otherSpec of otherSpecifiersFromSamePkg) {
1772
+ const escapedOtherSpec = otherSpec.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1773
+ const bareSpecifier = `coschunk-${otherSpec.replace(/[/@]/g, "-")}`;
1774
+ const staticRegex = new RegExp(`(import|export)\\b\\s*((?:(?!\\bimport\\b|\\bexport\\b)[\\s\\S])*?\\bfrom\\b\\s*)?['"]${escapedOtherSpec}['"]\\s*;?`, "g");
1775
+ code = code.replace(staticRegex, (match, keyword, fromPart) => {
1776
+ return `${keyword}${fromPart ? " " + fromPart : " "}"${bareSpecifier}";`;
1777
+ });
1778
+ const dynamicRegex = new RegExp(`import\\s*\\(\\s*['"]${escapedOtherSpec}['"]\\s*\\)`, "g");
1779
+ code = code.replace(dynamicRegex, () => `import("${bareSpecifier}")`);
1780
+ }
1781
+ specifierToCode[specifier] = code;
1782
+ const content = Buffer.from(code);
1766
1783
  const hash = crypto.createHash("sha256").update(content).digest("hex");
1767
1784
  const ext = ".js";
1768
1785
  const safeSpecifier = specifier.replace(/[/@]/g, "-").replace(/\.js$/, "");
@@ -1778,7 +1795,7 @@ function cosPlugin(options = {}) {
1778
1795
  managedChunks[fileName] = {
1779
1796
  type: "chunk",
1780
1797
  fileName,
1781
- code: content.toString(),
1798
+ code,
1782
1799
  name: specifier
1783
1800
  };
1784
1801
  externalToFileName[specifier] = fileName;
@@ -1787,10 +1804,18 @@ function cosPlugin(options = {}) {
1787
1804
  }
1788
1805
  }
1789
1806
  if (mainChunk) {
1807
+ const magicMapping = {};
1808
+ for (const specifier in externalToFileName) {
1809
+ const bareSpecifier = `coschunk-${specifier.replace(/[/@]/g, "-")}`;
1810
+ magicMapping[bareSpecifier] = externalToFileName[specifier];
1811
+ }
1812
+ const allChunks2 = Object.values(bundle).filter(
1813
+ (c) => c.type === "chunk"
1814
+ );
1790
1815
  const managedChunkNames = new Set(Object.keys(managedChunks));
1791
1816
  const unmanagedDependencies = /* @__PURE__ */ new Set();
1792
1817
  const base = config.base.endsWith("/") ? config.base : config.base + "/";
1793
- for (const targetChunk of allChunks) {
1818
+ for (const targetChunk of allChunks2) {
1794
1819
  const importerDir = path.dirname(targetChunk.fileName);
1795
1820
  const deps = [...targetChunk.imports, ...targetChunk.dynamicImports];
1796
1821
  for (const depFileName of deps) {
@@ -1823,8 +1848,7 @@ function cosPlugin(options = {}) {
1823
1848
  }
1824
1849
  }
1825
1850
  for (const pkgName in externalToFileName) {
1826
- const fileName = externalToFileName[pkgName];
1827
- const bareSpecifier = `coschunk-${fileName.replace(/\//g, "-")}`;
1851
+ const bareSpecifier = `coschunk-${pkgName.replace(/[/@]/g, "-")}`;
1828
1852
  const staticPattern = `(import|export)\\b\\s*((?:(?!\\bimport\\b|\\bexport\\b)[\\s\\S])*?\\bfrom\\b\\s*)?['"]${pkgName}['"]\\s*;?`;
1829
1853
  const staticRegex = new RegExp(staticPattern, "g");
1830
1854
  targetChunk.code = targetChunk.code.replace(
@@ -1844,7 +1868,8 @@ function cosPlugin(options = {}) {
1844
1868
  const manifest = {
1845
1869
  base,
1846
1870
  entry: mainChunk.fileName,
1847
- chunks: {}
1871
+ chunks: {},
1872
+ magic: magicMapping
1848
1873
  };
1849
1874
  for (const fileName in managedChunks) {
1850
1875
  const chunk = managedChunks[fileName];
package/dist/loader.js CHANGED
@@ -111,6 +111,18 @@
111
111
  // Inject Import Map
112
112
  const script = document.createElement('script');
113
113
  script.type = 'importmap';
114
+
115
+ // Add magic specifier mappings (e.g. coschunk-three -> assets/three-*.js)
116
+ if (manifest.magic) {
117
+ for (const [specifier, fileName] of Object.entries(manifest.magic)) {
118
+ const targetSpecifier = `coschunk-${fileName.replace(/\//g, '-')}`;
119
+ const targetUrl = importMap.imports[targetSpecifier];
120
+ if (targetUrl) {
121
+ importMap.imports[specifier] = targetUrl;
122
+ }
123
+ }
124
+ }
125
+
114
126
  script.textContent = JSON.stringify(importMap, null, 2);
115
127
  document.head.appendChild(script);
116
128
 
package/loader.js CHANGED
@@ -111,6 +111,18 @@
111
111
  // Inject Import Map
112
112
  const script = document.createElement('script');
113
113
  script.type = 'importmap';
114
+
115
+ // Add magic specifier mappings (e.g. coschunk-three -> assets/three-*.js)
116
+ if (manifest.magic) {
117
+ for (const [specifier, fileName] of Object.entries(manifest.magic)) {
118
+ const targetSpecifier = `coschunk-${fileName.replace(/\//g, '-')}`;
119
+ const targetUrl = importMap.imports[targetSpecifier];
120
+ if (targetUrl) {
121
+ importMap.imports[specifier] = targetUrl;
122
+ }
123
+ }
124
+ }
125
+
114
126
  script.textContent = JSON.stringify(importMap, null, 2);
115
127
  document.head.appendChild(script);
116
128
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-cross-origin-storage",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Vite plugin to load chunks from Cross-Origin Storage",
5
5
  "keywords": [
6
6
  "vite",