vite-plugin-cross-origin-storage 1.4.3 → 1.4.5

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.
Files changed (2) hide show
  1. package/dist/index.js +125 -0
  2. package/package.json +4 -3
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,40 @@ 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
+ function getPackageName(item) {
1642
+ if (typeof item === "string") {
1643
+ return item.startsWith("vendor-") ? item.replace("vendor-", "") : item;
1644
+ }
1645
+ if (item instanceof RegExp) {
1646
+ const source = item.source;
1647
+ const match = source.match(/vendor-([a-zA-Z0-9-@/]+?)(?:[-._]|\/|$)/);
1648
+ return match ? match[1] : null;
1649
+ }
1650
+ return null;
1651
+ }
1652
+ for (const item of includeArray) {
1653
+ const pkgName = getPackageName(item);
1654
+ if (pkgName) {
1655
+ try {
1656
+ require2.resolve(pkgName);
1657
+ if (!externalArray.includes(pkgName)) {
1658
+ console.log(`COS Plugin: [MAGIC] Externalizing package "${pkgName}" (matched from ${item})`);
1659
+ externalArray.push(pkgName);
1660
+ }
1661
+ } catch (e) {
1662
+ }
1663
+ }
1664
+ }
1665
+ config2.build.rollupOptions.external = externalArray;
1666
+ },
1632
1667
  configResolved(resolvedConfig) {
1633
1668
  config = resolvedConfig;
1634
1669
  },
@@ -1664,6 +1699,78 @@ function cosPlugin(options = {}) {
1664
1699
  htmlAsset = chunk;
1665
1700
  }
1666
1701
  }
1702
+ const externalToFileName = {};
1703
+ const require2 = createRequire(path.join(process.cwd(), "index.js"));
1704
+ const include = options.include || ["**/*"];
1705
+ const includeArray = Array.isArray(include) ? include : [include];
1706
+ function getPackageName(item) {
1707
+ if (typeof item === "string") {
1708
+ return item.startsWith("vendor-") ? item.replace("vendor-", "") : item;
1709
+ }
1710
+ if (item instanceof RegExp) {
1711
+ const source = item.source;
1712
+ const match = source.match(/vendor-([a-zA-Z0-9-@/]+?)(?:[-._]|\/|$)/);
1713
+ return match ? match[1] : null;
1714
+ }
1715
+ return null;
1716
+ }
1717
+ for (const item of includeArray) {
1718
+ const pkgName = getPackageName(item);
1719
+ if (!pkgName) continue;
1720
+ try {
1721
+ let pkgPath = "";
1722
+ try {
1723
+ const mainPath = require2.resolve(pkgName);
1724
+ let currentDir = path.dirname(mainPath);
1725
+ let pkgJsonPath = "";
1726
+ while (currentDir !== path.parse(currentDir).root) {
1727
+ const candidate = path.join(currentDir, "package.json");
1728
+ if (fs.existsSync(candidate)) {
1729
+ pkgJsonPath = candidate;
1730
+ break;
1731
+ }
1732
+ currentDir = path.dirname(currentDir);
1733
+ }
1734
+ if (pkgJsonPath) {
1735
+ const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
1736
+ const pkgDir = path.dirname(pkgJsonPath);
1737
+ if (pkgJson.module) {
1738
+ pkgPath = path.resolve(pkgDir, pkgJson.module);
1739
+ } else if (pkgJson.exports?.["."]?.import) {
1740
+ pkgPath = path.resolve(pkgDir, pkgJson.exports["."].import);
1741
+ } else if (pkgJson.exports?.import) {
1742
+ pkgPath = path.resolve(pkgDir, pkgJson.exports.import);
1743
+ }
1744
+ }
1745
+ if (!pkgPath) {
1746
+ pkgPath = mainPath;
1747
+ }
1748
+ } catch (e) {
1749
+ pkgPath = require2.resolve(pkgName);
1750
+ }
1751
+ console.log(`COS Plugin: [MAGIC] Resolved ${pkgName} to ${pkgPath}`);
1752
+ const content = fs.readFileSync(pkgPath);
1753
+ const hash = crypto.createHash("sha256").update(content).digest("hex");
1754
+ const ext = path.extname(pkgPath);
1755
+ const fileName = path.join(
1756
+ config.build.assetsDir,
1757
+ `${pkgName}-${hash.slice(0, 8)}${ext}`
1758
+ );
1759
+ this.emitFile({
1760
+ type: "asset",
1761
+ fileName,
1762
+ source: content
1763
+ });
1764
+ managedChunks[fileName] = {
1765
+ type: "chunk",
1766
+ fileName,
1767
+ code: content.toString(),
1768
+ name: item
1769
+ };
1770
+ externalToFileName[pkgName] = fileName;
1771
+ } catch (e) {
1772
+ }
1773
+ }
1667
1774
  if (mainChunk) {
1668
1775
  const allChunks = Object.values(bundle).filter(
1669
1776
  (c) => c.type === "chunk"
@@ -1703,6 +1810,24 @@ function cosPlugin(options = {}) {
1703
1810
  unmanagedDependencies.add(depFileName);
1704
1811
  }
1705
1812
  }
1813
+ for (const pkgName in externalToFileName) {
1814
+ const fileName = externalToFileName[pkgName];
1815
+ const bareSpecifier = `coschunk-${fileName.replace(/\//g, "-")}`;
1816
+ const staticPattern = `(import|export)\\b\\s*((?:(?!\\bimport\\b|\\bexport\\b)[\\s\\S])*?\\bfrom\\b\\s*)?['"]${pkgName}['"]\\s*;?`;
1817
+ const staticRegex = new RegExp(staticPattern, "g");
1818
+ targetChunk.code = targetChunk.code.replace(
1819
+ staticRegex,
1820
+ (match, keyword, fromPart) => {
1821
+ return `${keyword}${fromPart ? " " + fromPart : " "}"${bareSpecifier}";`;
1822
+ }
1823
+ );
1824
+ const dynamicPattern = `import\\s*\\(\\s*['"]${pkgName}['"]\\s*\\)`;
1825
+ const dynamicRegex = new RegExp(dynamicPattern, "g");
1826
+ targetChunk.code = targetChunk.code.replace(
1827
+ dynamicRegex,
1828
+ () => `import("${bareSpecifier}")`
1829
+ );
1830
+ }
1706
1831
  }
1707
1832
  const manifest = {
1708
1833
  base,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-cross-origin-storage",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
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
  }