vite-plugin-cross-origin-storage 1.4.3 → 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.
Files changed (2) hide show
  1. package/dist/index.js +109 -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,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
  },
@@ -1664,6 +1691,70 @@ function cosPlugin(options = {}) {
1664
1691
  htmlAsset = chunk;
1665
1692
  }
1666
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
+ }
1667
1758
  if (mainChunk) {
1668
1759
  const allChunks = Object.values(bundle).filter(
1669
1760
  (c) => c.type === "chunk"
@@ -1703,6 +1794,24 @@ function cosPlugin(options = {}) {
1703
1794
  unmanagedDependencies.add(depFileName);
1704
1795
  }
1705
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
+ }
1706
1815
  }
1707
1816
  const manifest = {
1708
1817
  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.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
  }