vite-plugin-cross-origin-storage 1.5.0 → 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 +36 -44
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1654,14 +1654,12 @@ function cosPlugin(options = {}) {
1654
1654
  if (pkgName) {
1655
1655
  try {
1656
1656
  require2.resolve(pkgName);
1657
- const externalPatterns = [pkgName, `${pkgName}/*`];
1658
- for (const pattern of externalPatterns) {
1659
- if (!externalArray.includes(pattern)) {
1660
- console.log(
1661
- `COS Plugin: [MAGIC] Externalizing pattern "${pattern}" (matched from ${item})`
1662
- );
1663
- externalArray.push(pattern);
1664
- }
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);
1665
1663
  }
1666
1664
  } catch (e) {
1667
1665
  }
@@ -1719,40 +1717,35 @@ function cosPlugin(options = {}) {
1719
1717
  }
1720
1718
  return null;
1721
1719
  }
1720
+ const magicPackages = /* @__PURE__ */ new Set();
1722
1721
  for (const item of includeArray) {
1723
1722
  const pkgName = getPackageName(item);
1724
- if (!pkgName) continue;
1725
- try {
1726
- let pkgPath = "";
1723
+ if (pkgName) {
1727
1724
  try {
1728
- const mainPath = require2.resolve(pkgName);
1729
- let currentDir = path.dirname(mainPath);
1730
- let pkgJsonPath = "";
1731
- while (currentDir !== path.parse(currentDir).root) {
1732
- const candidate = path.join(currentDir, "package.json");
1733
- if (fs.existsSync(candidate)) {
1734
- pkgJsonPath = candidate;
1735
- break;
1736
- }
1737
- currentDir = path.dirname(currentDir);
1738
- }
1739
- if (pkgJsonPath) {
1740
- const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
1741
- const pkgDir = path.dirname(pkgJsonPath);
1742
- if (pkgJson.module) {
1743
- pkgPath = path.resolve(pkgDir, pkgJson.module);
1744
- } else if (pkgJson.exports?.["."]?.import) {
1745
- pkgPath = path.resolve(pkgDir, pkgJson.exports["."].import);
1746
- } else if (pkgJson.exports?.import) {
1747
- pkgPath = path.resolve(pkgDir, pkgJson.exports.import);
1748
- }
1749
- }
1750
- if (!pkgPath) {
1751
- pkgPath = mainPath;
1752
- }
1725
+ require2.resolve(pkgName);
1726
+ magicPackages.add(pkgName);
1753
1727
  } catch (e) {
1754
- pkgPath = require2.resolve(pkgName);
1755
1728
  }
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);
1756
1749
  const esbuildRequire = createRequire(import.meta.url);
1757
1750
  const esbuild = esbuildRequire("esbuild");
1758
1751
  const buildResult = await esbuild.build({
@@ -1771,10 +1764,11 @@ function cosPlugin(options = {}) {
1771
1764
  });
1772
1765
  const content = buildResult.outputFiles[0].contents;
1773
1766
  const hash = crypto.createHash("sha256").update(content).digest("hex");
1774
- const ext = path.extname(pkgPath);
1767
+ const ext = ".js";
1768
+ const safeSpecifier = specifier.replace(/[/@]/g, "-").replace(/\.js$/, "");
1775
1769
  const fileName = path.join(
1776
1770
  config.build.assetsDir,
1777
- `${pkgName}-${hash.slice(0, 8)}${ext}`
1771
+ `${safeSpecifier}-${hash.slice(0, 8)}${ext}`
1778
1772
  );
1779
1773
  this.emitFile({
1780
1774
  type: "asset",
@@ -1785,16 +1779,14 @@ function cosPlugin(options = {}) {
1785
1779
  type: "chunk",
1786
1780
  fileName,
1787
1781
  code: content.toString(),
1788
- name: item
1782
+ name: specifier
1789
1783
  };
1790
- externalToFileName[pkgName] = fileName;
1784
+ externalToFileName[specifier] = fileName;
1791
1785
  } catch (e) {
1786
+ console.error(`COS Plugin: Failed to bundle magic specifier "${specifier}"`, e);
1792
1787
  }
1793
1788
  }
1794
1789
  if (mainChunk) {
1795
- const allChunks = Object.values(bundle).filter(
1796
- (c) => c.type === "chunk"
1797
- );
1798
1790
  const managedChunkNames = new Set(Object.keys(managedChunks));
1799
1791
  const unmanagedDependencies = /* @__PURE__ */ new Set();
1800
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.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Vite plugin to load chunks from Cross-Origin Storage",
5
5
  "keywords": [
6
6
  "vite",