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 wrangler 1.x module specifiers with a deprecation warning #596

Merged
merged 1 commit into from
Mar 15, 2022

Conversation

threepointone
Copy link
Contributor

@threepointone threepointone commented Mar 14, 2022

This implements wrangler 1.x style module specifiers, but also logs a deprecation warning for every usage.

Consider a project like so:

  project
  ├── index.js
  └── some-dependency.js

where the content of index.js is:

import SomeDependency from "some-dependency.js";
addEventListener("fetch", (event) => {
  // ...
});

wrangler 1.x would resolve import SomeDependency from "some-dependency.js"; to the file some-dependency.js. This will work in wrangler v2, but it will log a deprecation warning. Instead, you should rewrite the import to specify that it's a relative path, like so:

- import SomeDependency from "some-dependency.js";
+ import SomeDependency from "./some-dependency.js";

In a near future version, this will become a breaking deprecation and throw an error.

(This also updates workers-chat-demo to use the older style specifier, since that's how it currently is at https://github.com/cloudflare/workers-chat-demo)

Known issue: This might not work as expected with .js/.cjs/.mjs files as expected, but that's something to be fixed overall with the module system.

Closes #586

@changeset-bot
Copy link

changeset-bot bot commented Mar 14, 2022

🦋 Changeset detected

Latest commit: 4728593

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 14, 2022

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/1985472671/npm-package-wrangler-596

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/596/npm-package-wrangler-596

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/1985472671/npm-package-wrangler-596 dev path/to/script.js

@threepointone threepointone force-pushed the deprecate-legacy-module-specifiers branch from 1c872cd to ff24697 Compare March 14, 2022 17:29
@threepointone threepointone removed the request for review from petebacondarwin March 14, 2022 18:13
@threepointone threepointone marked this pull request as draft March 14, 2022 18:13
@threepointone threepointone changed the title feat: support wrangler 1.x module specifiers with a deprecation warning WIP feat: support wrangler 1.x module specifiers with a deprecation warning Mar 14, 2022
@threepointone threepointone force-pushed the deprecate-legacy-module-specifiers branch 2 times, most recently from 4c03f34 to 1cdda06 Compare March 15, 2022 06:32
@threepointone threepointone changed the title WIP feat: support wrangler 1.x module specifiers with a deprecation warning feat: support wrangler 1.x module specifiers with a deprecation warning Mar 15, 2022
@threepointone threepointone marked this pull request as ready for review March 15, 2022 06:33
This implements wrangler 1.x style module specifiers, but also logs a deprecation warning for every usage.

Consider a project like so:

```
  project
  ├── index.js
  └── some-dependency.js
```

where the content of `index.js` is:

```jsx
import SomeDependency from "some-dependency.js";
addEventListener("fetch", (event) => {
  // ...
});
```

`wrangler` 1.x would resolve `import SomeDependency from "some-dependency.js";` to the file `some-dependency.js`. This will work in `wrangler` v2, but it will log a deprecation warning. Instead, you should rewrite the import to specify that it's a relative path, like so:

```diff
- import SomeDependency from "some-dependency.js";
+ import SomeDependency from "./some-dependency.js";
```

In a near future version, this will become a breaking deprecation and throw an error.

(This also updates `workers-chat-demo` to use the older style specifier, since that's how it currently is at https://github.com/cloudflare/workers-chat-demo)

Known issue: This might not work as expected with `.js`/`.cjs`/`.mjs` files as expected, but that's something to be fixed overall with the module system.

Closes #586
@threepointone threepointone force-pushed the deprecate-legacy-module-specifiers branch from 1cdda06 to 4728593 Compare March 15, 2022 08:20
{
filter: new RegExp(
[...props.wrangler1xlegacyModuleReferences.fileNames]
.map((fileName) => `^${fileName}$`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for deeper paths to be imported in this way in Wrangler 1?
e.g import HTML from "views/chat.html";

Is it possible for modules in subdirectories to use this kind of import? And if so, should the import be relative to the current module or to the root?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this regex simply match any import that does not start with a . and then see if it can resolve it to a real file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I checked, and it's not possible for deeper paths to be here. That was part of the quirks of wrangler 1. (and why you couldn't have wasm inside npm modules, and why there was a special webpack type, etc etc)
  2. I don't want to change the regex to be any non-. file because then there's the whole possible clashes with node modules etc etc. But this should cover the usecases (happy to revisit if I'm wrong).

external: props.format === "modules", // mark it as external in the bundle
namespace: `wrangler-module-${rule.type}`, // just a tag, this isn't strictly necessary
watchFiles: [filePath], // we also add the file to esbuild's watch list
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot of duplication here with the actual module-collection plugin handlers.
I appreciate that you are trying to keep this bit separate so that it is easy to remove later.
But I think there is still room for creating some helper function or two to remove that duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, and you're right I did try to keep it separate so we could just delete at one go. Lemme spend a little time tonight and try to function-ify some bits.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly, it doesn't seem worth it. there's just enough differences interspersed in the body where the abstraction will be a weird argument signature, and it'll only get removed by 2.1. I'll give it a go when I try to implement bundle splitting, that might tease it apart some more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Thanks for taking a look.

@threepointone threepointone merged commit 187264d into main Mar 15, 2022
@threepointone threepointone deleted the deprecate-legacy-module-specifiers branch March 15, 2022 16:52
@github-actions github-actions bot mentioned this pull request Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support/deprecate wrangler1 style module specifiers
2 participants