Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support npm resolution for file imports #4135

Merged
merged 15 commits into from Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/long-starfishes-mate.md
@@ -0,0 +1,19 @@
---
"wrangler": patch
Cherry marked this conversation as resolved.
Show resolved Hide resolved
---

feat: resolve npm exports for file imports

Previously, when using wasm (or other static files) from an npm package, you would have to import the file like so:

```js
import wasm from "../../node_modules/svg2png-wasm/svg2png_wasm_bg.wasm";
```

This update now allows you to import the file like so, assuming it's exposed and available in the package's `exports` field:

```js
import wasm from "svg2png-wasm/svg2png_wasm_bg.wasm";
```

This will look at the package's `exports` field in `package.json` and resolve the file using [`resolve.exports`](https://www.npmjs.com/package/resolve.exports).
1 change: 1 addition & 0 deletions packages/wrangler/package.json
Expand Up @@ -110,6 +110,7 @@
"miniflare": "3.20231002.1",
"nanoid": "^3.3.3",
"path-to-regexp": "^6.2.0",
"resolve.exports": "^2.0.2",
"selfsigned": "^2.0.1",
"source-map": "0.6.1",
"source-map-support": "0.5.21",
Expand Down
39 changes: 38 additions & 1 deletion packages/wrangler/src/deployment-bundle/module-collection.ts
Expand Up @@ -3,6 +3,7 @@
import { readFile } from "node:fs/promises";
import path from "node:path";
import globToRegExp from "glob-to-regexp";
import { exports as resolveExports } from "resolve.exports";
import { logger } from "../logger";
import {
findAdditionalModules,
Expand Down Expand Up @@ -237,7 +238,7 @@
// take the file and massage it to a
// transportable/manageable format

const filePath = path.join(args.resolveDir, args.path);
let filePath = path.join(args.resolveDir, args.path);

// If this was a found additional module, mark it as external.
// Note, there's no need to watch the file here as we already
Expand All @@ -251,6 +252,42 @@
// it to `esbuild` to bundle it.
if (isJavaScriptModuleRule(rule)) return;

// Check if this file is possibly from an npm package
// and if so, validate the import against the package.json exports
// and resolve the file path to the correct file.
if (args.path.includes("/") && !args.path.startsWith("./")) {
Skye-31 marked this conversation as resolved.
Show resolved Hide resolved
const dirName = path.dirname(args.path);
const packageJsonPath = path.join(

Check warning on line 260 in packages/wrangler/src/deployment-bundle/module-collection.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/deployment-bundle/module-collection.ts#L259-L260

Added lines #L259 - L260 were not covered by tests
args.resolveDir,
"node_modules",
dirName,
"package.json"
);
// Try and read the npm package's package.json
// and then resolve the import against the package's exports
// and then finally override filePath if we find a match.
try {
const packageJson = JSON.parse(

Check warning on line 270 in packages/wrangler/src/deployment-bundle/module-collection.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/deployment-bundle/module-collection.ts#L269-L270

Added lines #L269 - L270 were not covered by tests
await readFile(packageJsonPath, "utf8")
);
const testResolved = resolveExports(

Check warning on line 273 in packages/wrangler/src/deployment-bundle/module-collection.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/deployment-bundle/module-collection.ts#L273

Added line #L273 was not covered by tests
packageJson,
args.path.replace(`${dirName}/`, "")
);
if (testResolved) {
filePath = path.join(

Check warning on line 278 in packages/wrangler/src/deployment-bundle/module-collection.ts

View check run for this annotation

Codecov / codecov/patch

packages/wrangler/src/deployment-bundle/module-collection.ts#L278

Added line #L278 was not covered by tests
args.resolveDir,
"node_modules",
dirName,
testResolved[0]
);
}
} catch (e) {
// We tried, now it'll just fall-through to the previous behaviour
// and ENOENT if the absolute file path doesn't exist.
}
}

const fileContent = await readFile(filePath);
const fileHash = crypto
.createHash("sha1")
Expand Down
24 changes: 13 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.