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

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 +43 -35
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1654,12 +1654,14 @@ function cosPlugin(options = {}) {
1654
1654
  if (pkgName) {
1655
1655
  try {
1656
1656
  require2.resolve(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);
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
+ }
1663
1665
  }
1664
1666
  } catch (e) {
1665
1667
  }
@@ -1717,35 +1719,40 @@ function cosPlugin(options = {}) {
1717
1719
  }
1718
1720
  return null;
1719
1721
  }
1720
- const magicPackages = /* @__PURE__ */ new Set();
1721
1722
  for (const item of includeArray) {
1722
1723
  const pkgName = getPackageName(item);
1723
- if (pkgName) {
1724
+ if (!pkgName) continue;
1725
+ try {
1726
+ let pkgPath = "";
1724
1727
  try {
1725
- require2.resolve(pkgName);
1726
- magicPackages.add(pkgName);
1727
- } catch (e) {
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;
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
+ }
1742
1749
  }
1750
+ if (!pkgPath) {
1751
+ pkgPath = mainPath;
1752
+ }
1753
+ } catch (e) {
1754
+ pkgPath = require2.resolve(pkgName);
1743
1755
  }
1744
- }
1745
- }
1746
- for (const specifier of discoveredSpecifiers) {
1747
- try {
1748
- const pkgPath = require2.resolve(specifier);
1749
1756
  const esbuildRequire = createRequire(import.meta.url);
1750
1757
  const esbuild = esbuildRequire("esbuild");
1751
1758
  const buildResult = await esbuild.build({
@@ -1765,10 +1772,9 @@ function cosPlugin(options = {}) {
1765
1772
  const content = buildResult.outputFiles[0].contents;
1766
1773
  const hash = crypto.createHash("sha256").update(content).digest("hex");
1767
1774
  const ext = ".js";
1768
- const safeSpecifier = specifier.replace(/[/@]/g, "-").replace(/\.js$/, "");
1769
1775
  const fileName = path.join(
1770
1776
  config.build.assetsDir,
1771
- `${safeSpecifier}-${hash.slice(0, 8)}${ext}`
1777
+ `${pkgName}-${hash.slice(0, 8)}${ext}`
1772
1778
  );
1773
1779
  this.emitFile({
1774
1780
  type: "asset",
@@ -1779,14 +1785,16 @@ function cosPlugin(options = {}) {
1779
1785
  type: "chunk",
1780
1786
  fileName,
1781
1787
  code: content.toString(),
1782
- name: specifier
1788
+ name: item
1783
1789
  };
1784
- externalToFileName[specifier] = fileName;
1790
+ externalToFileName[pkgName] = fileName;
1785
1791
  } catch (e) {
1786
- console.error(`COS Plugin: Failed to bundle magic specifier "${specifier}"`, e);
1787
1792
  }
1788
1793
  }
1789
1794
  if (mainChunk) {
1795
+ const allChunks = Object.values(bundle).filter(
1796
+ (c) => c.type === "chunk"
1797
+ );
1790
1798
  const managedChunkNames = new Set(Object.keys(managedChunks));
1791
1799
  const unmanagedDependencies = /* @__PURE__ */ new Set();
1792
1800
  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.1",
3
+ "version": "1.7.0",
4
4
  "description": "Vite plugin to load chunks from Cross-Origin Storage",
5
5
  "keywords": [
6
6
  "vite",