Skip to content

JS: parse import attributes (with clause) in import and export statements - #138

Merged
tdewolff merged 1 commit into
tdewolff:masterfrom
bardrop-oss:import-attributes
Nov 12, 2026
Merged

tdewolff merged 1 commit into
tdewolff:masterfrom
bardrop-oss:import-attributes

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Fixes tdewolff/minify#786

Import attributes (ES2025) fail to parse:

import json from "./a.json" with { type: "json" };
// expected ( instead of { in with statement

This parses the WithClause after the module specifier of import and export ... from statements:

  • new ImportAttribute{Key, Value}; Key is an identifier name or a string literal, Value a string literal
  • new Attributes []ImportAttribute field on ImportStmt and ExportStmt; it is nil without a clause and an empty slice for with {}, so with {} is not lost
  • String() and JS() write the clause back
  • export {a} with {...} (no from) is still an error

Tests: five cases in TestParse, six in TestParseError and three in TestJS, failing before the change. go test ./... passes.

minify prints the statement field by field, so it needs a small change to write Attributes; otherwise it would now output the import without its clause. I have that change ready on bardrop/minify@import-attributes (import x from"./a.json"with{type:"json"}, tests included, checked in node) and will open it once this is merged, or you can pick it up directly if that is easier.

…ents

`import json from "./a.json" with { type: "json" }` failed with
"expected ( instead of { in with statement". Parse the WithClause after
the module specifier of import and export-from statements into a new
Attributes field (nil when there is no clause) and write it back in
String() and JS().

Fixes tdewolff/minify#786
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.

JS minify fails on import attributes

2 participants