-
Notifications
You must be signed in to change notification settings - Fork 734
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: implement rules
config field
#530
Conversation
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/1912742590/npm-package-wrangler-530 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/530/npm-package-wrangler-530 Or you can use npx https://prerelease-registry.developers.workers.dev/runs/1912742590/npm-package-wrangler-530 dev path/to/script.js |
Why didn't the changeset bot drop a comment here? 🤔 I'll investigate later, unless someone else has an idea |
22aa1a4
to
5ef8cf2
Compare
🦋 Changeset detectedLatest commit: 9cfaec8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
5ef8cf2
to
0d3c6c6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a bunch of comments, but no blockers.
{ type: "Text", globs: ["**/*.file"], fallthrough: true }, | ||
{ type: "Text", globs: ["**/*.other"], fallthrough: true }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, but I don't understand how this is different to:
{ type: "Text", globs: ["**/*.file", **/*.other"] },
and if so, what the point of fallthrough
is?
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it is not useful (and just adds unnecessary complexity), how about we just don't support fallthrough
as a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, OK. So it seems that this is used to allow custom rules to fallback to the DEFAULT_MODULE_RULES
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or any rule that follows. It's a weird one, but I have to preserve the behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, but apart from the default ones, the developer of the worker has full control over this list, so they could just put all the globs that they want to match into a single rule for a particular type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, but that would be a breaking change. I didn't want to introduce that in this PR. Happy to do so in a subsequent one.
let err: Error | undefined; | ||
try { | ||
await runWrangler("publish index.js"); | ||
} catch (e) { | ||
err = e as Error; | ||
} | ||
expect(err?.message).toMatch( | ||
`The file ./other.other matched a module rule in your configuration ({"type":"Text","globs":["**/*.other"]}), but was ignored because a previous rule with the same type had \`fallthrough = false\`.` | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use:
await expect(runWrangler("publish index.js")).rejects.toThrowError(...);
(or similar but with an inline snapshot)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the full error contains a file path specific to the machine (so in my case, it contained my home dir etc) and I needed to match only with part of the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. So I tried rejects.toThrowError()
with the same string you provide here and it worked; so I am not sure what the problem is.
To be clear I tried:
await expect(runWrangler("publish index.js")).rejects.toThrowError(
`The file ./other.other matched a module rule in your configuration ({"type":"Text","globs":["**/*.other"]}), but was ignored because a previous rule with the same type had \`fallthrough = false\`.`
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh damn you're right, I was trying toMatchInlineSnapshot
fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT"); | ||
|
||
// We throw an error when we come across a file that matched a rule | ||
// but was skipped because of faullthrough = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// but was skipped because of faullthrough = false | |
// but was skipped because of fallthrough = false |
fs.writeFileSync("./other.other", "SOME OTHER TEXT CONTENT"); | ||
|
||
// We throw an error when we come across a file that matched a rule | ||
// but was skipped because of faullthrough = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// but was skipped because of faullthrough = false | |
// but was skipped because of fallthrough = false |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
.concat(DEFAULT_MODULE_RULES!) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good reason for the Config
not to have unnecessary optional properties.
} else { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} else { | |
} | |
} |
index++; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😢
.update(fileContent) | ||
.digest("hex"); | ||
const fileName = `${fileHash}-${path.basename(args.path)}`; | ||
rules?.forEach((rule) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😭
// with an underscore. | ||
// TODO: what of "Data"? | ||
contents: `export default ${args.path.replace( | ||
/[^a-zA-Z0-9_$]/g, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to ensure that the first character is not a number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's always a relative path, so I didn't bother (unless I'm wrong?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean that these paths always start with ./
, which will be converted to __
?
args.path | ||
} matched a module rule in your configuration (${JSON.stringify( | ||
rule | ||
)}), but was ignored because a previous rule with the same type had \`fallthrough = false\`.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly, I think it is ignored because a previous rule did not have fallthrough = true
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, yes.
console.warn( | ||
`Deprecation notice: The \`build.upload.rules\` config field is no longer used, the rules should be specified via the \`rules\` config field. Delete the \`build.upload\` field from the configuration file, and add this: | ||
|
||
${TOML.stringify({ rules: config.build.upload.rules })}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
0d3c6c6
to
e9cbc40
Compare
This implements the top level `rules` configuration field. It lets you specify transport rules for non-js modules. For example, you can specify `*.md` files to be included as a text file with - ``` [[rules]] {type = "Text", globs = ["**/*.md"]} ``` We also include a default ruleset - ``` { type: "Text", globs: ["**/*.txt", "**/*.html"] }, { type: "Data", globs: ["**/*.bin"] }, { type: "CompiledWasm", globs: ["**/*.wasm"] }, ``` More info at https://developers.cloudflare.com/workers/cli-wrangler/configuration/#build. Known issues - - non-wasm module types do not work in `--local` mode - `Data` type does not work in service worker format, in either mode
e9cbc40
to
9cfaec8
Compare
This implements the top level
rules
configuration field. It lets you specify transport rules for non-js modules. For example, you can specify*.md
files to be included as a text file with -We also include a default ruleset -
More info at https://developers.cloudflare.com/workers/cli-wrangler/configuration/#build.
Known issues -
--local
modeData
type does not work in service worker format, in either mode