vite-plugin-cross-origin-storage 1.4.2 → 1.4.4

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
@@ -1618,6 +1618,7 @@ forbiddenIdentifiers.add("");
1618
1618
  var hasStringIsWellFormed = "isWellFormed" in String.prototype;
1619
1619
 
1620
1620
  // index.ts
1621
+ import { createRequire } from "module";
1621
1622
  function cosPlugin(options = {}) {
1622
1623
  const filter = options.include || options.exclude ? createFilter(options.include || ["**/*"], options.exclude, {
1623
1624
  resolve: false
@@ -1629,6 +1630,32 @@ function cosPlugin(options = {}) {
1629
1630
  name: "vite-plugin-cos",
1630
1631
  apply: "build",
1631
1632
  enforce: "post",
1633
+ config(config2) {
1634
+ config2.build = config2.build || {};
1635
+ config2.build.rollupOptions = config2.build.rollupOptions || {};
1636
+ const external = config2.build.rollupOptions.external || [];
1637
+ const externalArray = Array.isArray(external) ? external : typeof external === "string" ? [external] : [];
1638
+ const include = options.include || ["**/*"];
1639
+ const includeArray = Array.isArray(include) ? include : [include];
1640
+ const require2 = createRequire(path.join(process.cwd(), "index.js"));
1641
+ for (const item of includeArray) {
1642
+ if (typeof item === "string") {
1643
+ let pkgName = item;
1644
+ if (item.startsWith("vendor-")) {
1645
+ pkgName = item.replace("vendor-", "");
1646
+ }
1647
+ try {
1648
+ require2.resolve(pkgName);
1649
+ if (!externalArray.includes(pkgName)) {
1650
+ console.log(`COS Plugin: [MAGIC] Externalizing package "${pkgName}"`);
1651
+ externalArray.push(pkgName);
1652
+ }
1653
+ } catch (e) {
1654
+ }
1655
+ }
1656
+ }
1657
+ config2.build.rollupOptions.external = externalArray;
1658
+ },
1632
1659
  configResolved(resolvedConfig) {
1633
1660
  config = resolvedConfig;
1634
1661
  },
@@ -1651,21 +1678,83 @@ function cosPlugin(options = {}) {
1651
1678
  if (chunk.isEntry) {
1652
1679
  console.log(`COS Plugin: [ENTRY] ${fileName}`);
1653
1680
  mainChunk = chunk;
1681
+ }
1682
+ const res = filter(fileName) || filter(chunk.name);
1683
+ console.log(
1684
+ `COS Plugin: [FILTER] ${fileName} (name: ${chunk.name}) -> ${res ? "INCLUDE" : "SKIP"}`
1685
+ );
1686
+ if (res) {
1654
1687
  managedChunks[fileName] = chunk;
1655
- } else {
1656
- const res = filter(fileName) || filter(chunk.name);
1657
- console.log(
1658
- `COS Plugin: [FILTER] ${fileName} (name: ${chunk.name}) -> ${res ? "INCLUDE" : "SKIP"}`
1659
- );
1660
- if (res) {
1661
- managedChunks[fileName] = chunk;
1662
- }
1663
1688
  }
1664
1689
  }
1665
1690
  if (fileName === "index.html" && chunk.type === "asset") {
1666
1691
  htmlAsset = chunk;
1667
1692
  }
1668
1693
  }
1694
+ const externalToFileName = {};
1695
+ const require2 = createRequire(path.join(process.cwd(), "index.js"));
1696
+ const include = options.include || ["**/*"];
1697
+ const includeArray = Array.isArray(include) ? include : [include];
1698
+ for (const item of includeArray) {
1699
+ if (typeof item !== "string") continue;
1700
+ let pkgName = item;
1701
+ if (item.startsWith("vendor-")) {
1702
+ pkgName = item.replace("vendor-", "");
1703
+ }
1704
+ try {
1705
+ let pkgPath = "";
1706
+ try {
1707
+ const mainPath = require2.resolve(pkgName);
1708
+ let currentDir = path.dirname(mainPath);
1709
+ let pkgJsonPath = "";
1710
+ while (currentDir !== path.parse(currentDir).root) {
1711
+ const candidate = path.join(currentDir, "package.json");
1712
+ if (fs.existsSync(candidate)) {
1713
+ pkgJsonPath = candidate;
1714
+ break;
1715
+ }
1716
+ currentDir = path.dirname(currentDir);
1717
+ }
1718
+ if (pkgJsonPath) {
1719
+ const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
1720
+ const pkgDir = path.dirname(pkgJsonPath);
1721
+ if (pkgJson.module) {
1722
+ pkgPath = path.resolve(pkgDir, pkgJson.module);
1723
+ } else if (pkgJson.exports?.["."]?.import) {
1724
+ pkgPath = path.resolve(pkgDir, pkgJson.exports["."].import);
1725
+ } else if (pkgJson.exports?.import) {
1726
+ pkgPath = path.resolve(pkgDir, pkgJson.exports.import);
1727
+ }
1728
+ }
1729
+ if (!pkgPath) {
1730
+ pkgPath = mainPath;
1731
+ }
1732
+ } catch (e) {
1733
+ pkgPath = require2.resolve(pkgName);
1734
+ }
1735
+ console.log(`COS Plugin: [MAGIC] Resolved ${pkgName} to ${pkgPath}`);
1736
+ const content = fs.readFileSync(pkgPath);
1737
+ const hash = crypto.createHash("sha256").update(content).digest("hex");
1738
+ const ext = path.extname(pkgPath);
1739
+ const fileName = path.join(
1740
+ config.build.assetsDir,
1741
+ `${pkgName}-${hash.slice(0, 8)}${ext}`
1742
+ );
1743
+ this.emitFile({
1744
+ type: "asset",
1745
+ fileName,
1746
+ source: content
1747
+ });
1748
+ managedChunks[fileName] = {
1749
+ type: "chunk",
1750
+ fileName,
1751
+ code: content.toString(),
1752
+ name: item
1753
+ };
1754
+ externalToFileName[pkgName] = fileName;
1755
+ } catch (e) {
1756
+ }
1757
+ }
1669
1758
  if (mainChunk) {
1670
1759
  const allChunks = Object.values(bundle).filter(
1671
1760
  (c) => c.type === "chunk"
@@ -1705,6 +1794,24 @@ function cosPlugin(options = {}) {
1705
1794
  unmanagedDependencies.add(depFileName);
1706
1795
  }
1707
1796
  }
1797
+ for (const pkgName in externalToFileName) {
1798
+ const fileName = externalToFileName[pkgName];
1799
+ const bareSpecifier = `coschunk-${fileName.replace(/\//g, "-")}`;
1800
+ const staticPattern = `(import|export)\\b\\s*((?:(?!\\bimport\\b|\\bexport\\b)[\\s\\S])*?\\bfrom\\b\\s*)?['"]${pkgName}['"]\\s*;?`;
1801
+ const staticRegex = new RegExp(staticPattern, "g");
1802
+ targetChunk.code = targetChunk.code.replace(
1803
+ staticRegex,
1804
+ (match, keyword, fromPart) => {
1805
+ return `${keyword}${fromPart ? " " + fromPart : " "}"${bareSpecifier}";`;
1806
+ }
1807
+ );
1808
+ const dynamicPattern = `import\\s*\\(\\s*['"]${pkgName}['"]\\s*\\)`;
1809
+ const dynamicRegex = new RegExp(dynamicPattern, "g");
1810
+ targetChunk.code = targetChunk.code.replace(
1811
+ dynamicRegex,
1812
+ () => `import("${bareSpecifier}")`
1813
+ );
1814
+ }
1708
1815
  }
1709
1816
  const manifest = {
1710
1817
  base,
@@ -1716,6 +1823,9 @@ function cosPlugin(options = {}) {
1716
1823
  const finalHash = crypto.createHash("sha256").update(chunk.code).digest("hex");
1717
1824
  manifest.chunks[fileName] = finalHash;
1718
1825
  }
1826
+ if (mainChunk && !managedChunkNames.has(mainChunk.fileName)) {
1827
+ unmanagedDependencies.add(mainChunk.fileName);
1828
+ }
1719
1829
  manifest.unmanaged = Array.from(unmanagedDependencies);
1720
1830
  if (htmlAsset) {
1721
1831
  try {
package/dist/loader.js CHANGED
@@ -55,7 +55,7 @@
55
55
  }
56
56
  }
57
57
 
58
- async function getChunkDataUrl(chunk) {
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,10 +85,10 @@
85
85
  try {
86
86
  console.log('COS Loader: Starting app...');
87
87
 
88
- // Resolve all chunks to Data URLs
88
+ // Resolve all chunks to Blob URLs
89
89
  const importMap = { imports: {} };
90
90
 
91
- // Set up unmanaged dependencies correctly so Data URLs can resolve them.
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
94
  importMap.imports[bareSpecifier] =
@@ -97,12 +97,12 @@
97
97
 
98
98
  console.log(`COS Loader: Loading ${chunksToLoad.length} chunks...`);
99
99
  const loadPromises = chunksToLoad.map(async (chunk) => {
100
- const dataUrl = await getChunkDataUrl(chunk);
101
- if (dataUrl) {
100
+ const blobUrl = await getChunkBlobUrl(chunk);
101
+ if (blobUrl) {
102
102
  // Use a hyphenated prefix and replace all slashes to ensure it's treated
103
103
  // as a truly bare specifier, bypassing hierarchical/protocol checks.
104
104
  const bareSpecifier = `coschunk-${chunk.fileName.replace(/\//g, '-')}`;
105
- importMap.imports[bareSpecifier] = dataUrl;
105
+ importMap.imports[bareSpecifier] = blobUrl;
106
106
  }
107
107
  });
108
108
 
package/loader.js CHANGED
@@ -55,7 +55,7 @@
55
55
  }
56
56
  }
57
57
 
58
- async function getChunkDataUrl(chunk) {
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,10 +85,10 @@
85
85
  try {
86
86
  console.log('COS Loader: Starting app...');
87
87
 
88
- // Resolve all chunks to Data URLs
88
+ // Resolve all chunks to Blob URLs
89
89
  const importMap = { imports: {} };
90
90
 
91
- // Set up unmanaged dependencies correctly so Data URLs can resolve them.
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
94
  importMap.imports[bareSpecifier] =
@@ -97,12 +97,12 @@
97
97
 
98
98
  console.log(`COS Loader: Loading ${chunksToLoad.length} chunks...`);
99
99
  const loadPromises = chunksToLoad.map(async (chunk) => {
100
- const dataUrl = await getChunkDataUrl(chunk);
101
- if (dataUrl) {
100
+ const blobUrl = await getChunkBlobUrl(chunk);
101
+ if (blobUrl) {
102
102
  // Use a hyphenated prefix and replace all slashes to ensure it's treated
103
103
  // as a truly bare specifier, bypassing hierarchical/protocol checks.
104
104
  const bareSpecifier = `coschunk-${chunk.fileName.replace(/\//g, '-')}`;
105
- importMap.imports[bareSpecifier] = dataUrl;
105
+ importMap.imports[bareSpecifier] = blobUrl;
106
106
  }
107
107
  });
108
108
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-cross-origin-storage",
3
- "version": "1.4.2",
3
+ "version": "1.4.4",
4
4
  "description": "Vite plugin to load chunks from Cross-Origin Storage",
5
5
  "keywords": [
6
6
  "vite",
@@ -46,10 +46,11 @@
46
46
  },
47
47
  "devDependencies": {
48
48
  "@rollup/pluginutils": "^5.3.0",
49
- "@types/node": "^25.2.0",
49
+ "@types/node": "^25.2.3",
50
50
  "rollup": "^4.57.1",
51
51
  "tsup": "^8.5.1",
52
52
  "typescript": "^5.9.3",
53
- "vite": "^7.3.1"
53
+ "vite": "^7.3.1",
54
+ "three": "^0.183.0"
54
55
  }
55
56
  }