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

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 +53 -41
  2. package/package.json +6 -3
package/dist/index.js CHANGED
@@ -1654,9 +1654,12 @@ function cosPlugin(options = {}) {
1654
1654
  if (pkgName) {
1655
1655
  try {
1656
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);
1657
+ const pkgRegex = new RegExp(`^${pkgName}(?:/.*)?$`);
1658
+ if (!externalArray.some((e) => e instanceof RegExp && e.source === pkgRegex.source)) {
1659
+ console.log(
1660
+ `COS Plugin: [MAGIC] Externalizing package and subpaths: ${pkgRegex} (matched from ${item})`
1661
+ );
1662
+ externalArray.push(pkgRegex);
1660
1663
  }
1661
1664
  } catch (e) {
1662
1665
  }
@@ -1714,47 +1717,58 @@ function cosPlugin(options = {}) {
1714
1717
  }
1715
1718
  return null;
1716
1719
  }
1720
+ const magicPackages = /* @__PURE__ */ new Set();
1717
1721
  for (const item of includeArray) {
1718
1722
  const pkgName = getPackageName(item);
1719
- if (!pkgName) continue;
1720
- try {
1721
- let pkgPath = "";
1723
+ if (pkgName) {
1722
1724
  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
- }
1725
+ require2.resolve(pkgName);
1726
+ magicPackages.add(pkgName);
1748
1727
  } catch (e) {
1749
- pkgPath = require2.resolve(pkgName);
1750
1728
  }
1751
- console.log(`COS Plugin: [MAGIC] Resolved ${pkgName} to ${pkgPath}`);
1752
- const content = fs.readFileSync(pkgPath);
1729
+ }
1730
+ }
1731
+ const discoveredSpecifiers = /* @__PURE__ */ new Set();
1732
+ const allChunks = Object.values(bundle).filter(
1733
+ (c) => c.type === "chunk"
1734
+ );
1735
+ for (const chunk of allChunks) {
1736
+ const allImports = [...chunk.imports, ...chunk.dynamicImports];
1737
+ for (const specifier of allImports) {
1738
+ for (const pkgName of magicPackages) {
1739
+ if (specifier === pkgName || specifier.startsWith(`${pkgName}/`)) {
1740
+ discoveredSpecifiers.add(specifier);
1741
+ break;
1742
+ }
1743
+ }
1744
+ }
1745
+ }
1746
+ for (const specifier of discoveredSpecifiers) {
1747
+ try {
1748
+ const pkgPath = require2.resolve(specifier);
1749
+ const esbuildRequire = createRequire(import.meta.url);
1750
+ const esbuild = esbuildRequire("esbuild");
1751
+ const buildResult = await esbuild.build({
1752
+ entryPoints: [pkgPath],
1753
+ bundle: true,
1754
+ format: "esm",
1755
+ minify: true,
1756
+ platform: "browser",
1757
+ write: false,
1758
+ // Don't write to disk
1759
+ target: "esnext",
1760
+ // Neutralize environment
1761
+ define: {
1762
+ "process.env.NODE_ENV": '"production"'
1763
+ }
1764
+ });
1765
+ const content = buildResult.outputFiles[0].contents;
1753
1766
  const hash = crypto.createHash("sha256").update(content).digest("hex");
1754
- const ext = path.extname(pkgPath);
1767
+ const ext = ".js";
1768
+ const safeSpecifier = specifier.replace(/[/@]/g, "-").replace(/\.js$/, "");
1755
1769
  const fileName = path.join(
1756
1770
  config.build.assetsDir,
1757
- `${pkgName}-${hash.slice(0, 8)}${ext}`
1771
+ `${safeSpecifier}-${hash.slice(0, 8)}${ext}`
1758
1772
  );
1759
1773
  this.emitFile({
1760
1774
  type: "asset",
@@ -1765,16 +1779,14 @@ function cosPlugin(options = {}) {
1765
1779
  type: "chunk",
1766
1780
  fileName,
1767
1781
  code: content.toString(),
1768
- name: item
1782
+ name: specifier
1769
1783
  };
1770
- externalToFileName[pkgName] = fileName;
1784
+ externalToFileName[specifier] = fileName;
1771
1785
  } catch (e) {
1786
+ console.error(`COS Plugin: Failed to bundle magic specifier "${specifier}"`, e);
1772
1787
  }
1773
1788
  }
1774
1789
  if (mainChunk) {
1775
- const allChunks = Object.values(bundle).filter(
1776
- (c) => c.type === "chunk"
1777
- );
1778
1790
  const managedChunkNames = new Set(Object.keys(managedChunks));
1779
1791
  const unmanagedDependencies = /* @__PURE__ */ new Set();
1780
1792
  const base = config.base.endsWith("/") ? config.base : config.base + "/";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-cross-origin-storage",
3
- "version": "1.4.5",
3
+ "version": "1.5.1",
4
4
  "description": "Vite plugin to load chunks from Cross-Origin Storage",
5
5
  "keywords": [
6
6
  "vite",
@@ -48,9 +48,12 @@
48
48
  "@rollup/pluginutils": "^5.3.0",
49
49
  "@types/node": "^25.2.3",
50
50
  "rollup": "^4.57.1",
51
+ "three": "^0.183.0",
51
52
  "tsup": "^8.5.1",
52
53
  "typescript": "^5.9.3",
53
- "vite": "^7.3.1",
54
- "three": "^0.183.0"
54
+ "vite": "^7.3.1"
55
+ },
56
+ "dependencies": {
57
+ "esbuild": "^0.27.3"
55
58
  }
56
59
  }