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

refactor: migrate on acorn-import-attributes #18251

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/ExternalModule.js
Expand Up @@ -167,7 +167,7 @@ const getSourceForImportExternal = (
}
const attributes =
dependencyMeta && dependencyMeta.attributes
? `, ${JSON.stringify(dependencyMeta.attributes)}`
? `, ${JSON.stringify({ assert: dependencyMeta.attributes })}`
: "";
if (!Array.isArray(moduleAndSpecifiers)) {
return {
Expand Down
4 changes: 2 additions & 2 deletions lib/javascript/JavascriptParser.js
Expand Up @@ -6,7 +6,7 @@
"use strict";

const { Parser: AcornParser } = require("acorn");
const { importAssertions } = require("acorn-import-assertions");
const { importAttributesOrAssertions } = require("acorn-import-attributes");
const { SyncBailHook, HookMap } = require("tapable");
const vm = require("vm");
const Parser = require("../Parser");
Expand Down Expand Up @@ -108,7 +108,7 @@ const ALLOWED_MEMBER_TYPES_ALL = 0b11;

// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API

const parser = AcornParser.extend(importAssertions);
const parser = AcornParser.extend(importAttributesOrAssertions);

class VariableInfo {
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -11,7 +11,7 @@
"@webassemblyjs/wasm-edit": "^1.12.1",
"@webassemblyjs/wasm-parser": "^1.12.1",
"acorn": "^8.7.1",
"acorn-import-assertions": "^1.9.0",
"acorn-import-attributes": "^1.9.5",
"browserslist": "^4.21.10",
"chrome-trace-event": "^1.0.2",
"enhanced-resolve": "^5.16.0",
Expand Down
@@ -0,0 +1,3 @@
{
"foo": "dynamic-str"
}
@@ -0,0 +1,3 @@
{
"foo": "dynamic"
}
23 changes: 22 additions & 1 deletion test/configCases/externals/import-attributes/index.js
@@ -1,22 +1,40 @@
import * as staticPkg from "./static-package.json" assert { type: "json" };
import * as staticPkgStr from "./static-package-str.json" assert { "type": "json" };

import * as staticPkgWith from "./static-package-with.json" with { type: "json" };
import * as staticPkgStrWith from "./static-package-str-with.json" with { "type": "json" };

it("should allow async externals", async () => {
expect(staticPkg.default.foo).toBe("static");
expect(staticPkgStr.default.foo).toBe("static-str");

expect(staticPkgWith.default.foo).toBe("static");
expect(staticPkgStrWith.default.foo).toBe("static-str");

const dynamicPkg = await import("./dynamic-package.json", {
assert: { type: "json" }
})

expect(dynamicPkg.default.foo).toBe("dynamic");

const dynamicPkgStr = await import("./dynamic-package-str.json", {
const dynamicPkgWith = await import("./dynamic-package-with.json", {
with: { type: "json" }
})

expect(dynamicPkgWith.default.foo).toBe("dynamic");

const dynamicPkgStr = await import("./dynamic-package-str-with.json", {
"assert": { "type": "json" }
})

expect(dynamicPkgStr.default.foo).toBe("dynamic-str");

const dynamicPkgStrWith = await import("./dynamic-package-str.json", {
"assert": { "type": "json" }
})

expect(dynamicPkgStrWith.default.foo).toBe("dynamic-str");

const eagerPkg = await import(/* webpackMode: "eager" */ "./eager.json", {
assert: { type: "json" }
});
Expand All @@ -43,3 +61,6 @@ it("should allow async externals", async () => {

expect(reExportPkg.foo).toBe("re-export");
});

export * from "./re-export-assert.json" assert { type: "json" }
export * from "./re-export-with.json" with { type: "json" }
@@ -0,0 +1,3 @@
{
"foo": "re-export"
}
@@ -0,0 +1,3 @@
{
"foo": "re-export"
}
@@ -0,0 +1,3 @@
{
"foo": "static-str"
}
@@ -0,0 +1,3 @@
{
"foo": "static"
}
14 changes: 12 additions & 2 deletions test/configCases/externals/import-attributes/webpack.config.js
Expand Up @@ -29,12 +29,17 @@ module.exports = {
[
"static-package.json",
"static-package-str.json",
"static-package-with.json",
"static-package-str-with.json",
"dynamic-package.json",
"dynamic-package-str.json",
"dynamic-package-with.json",
"dynamic-package-str-with.json",
"eager.json",
"weak.json",
"./nested/pkg.json",
"./re-export.json"
"./re-export-assert.json",
"./re-export-with.json"
].forEach(filename => {
const resolvedFilename = path.resolve(__dirname, filename);
const content = fs.readFileSync(resolvedFilename);
Expand All @@ -52,12 +57,17 @@ module.exports = {
externals: {
"./static-package.json": "module ./static-package.json",
"./static-package-str.json": "module ./static-package-str.json",
"./static-package-with.json": "module ./static-package-with.json",
"./static-package-str-with.json": "module ./static-package-str-with.json",
"./dynamic-package.json": "import ./dynamic-package.json",
"./dynamic-package-str.json": "import ./dynamic-package-str.json",
"./dynamic-package-with.json": "import ./dynamic-package-with.json",
"./dynamic-package-str-with.json": "import ./dynamic-package-str-with.json",
"./eager.json": "import ./eager.json",
"./weak.json": "import ./weak.json",
"./pkg.json": "import ./pkg.json",
"./pkg": "import ./pkg",
"./re-export.json": "import ./re-export.json"
"./re-export-assert.json": "module ./re-export-assert.json",
"./re-export-with.json": "module ./re-export-with.json"
}
};
8 changes: 4 additions & 4 deletions yarn.lock
Expand Up @@ -1433,10 +1433,10 @@ abbrev@1.0.x:
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135"
integrity sha512-LEyx4aLEC3x6T0UguF6YILf+ntvmOaWsVfENmIW0E9H09vKlLDGelMjjSm0jkDHALj8A8quZ/HapKNigzwge+Q==

acorn-import-assertions@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/acorn-import-assertions/-/acorn-import-assertions-1.9.0.tgz#507276249d684797c84e0734ef84860334cfb1ac"
integrity sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==
acorn-import-attributes@^1.9.5:
version "1.9.5"
resolved "https://registry.yarnpkg.com/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz#7eb1557b1ba05ef18b5ed0ec67591bfab04688ef"
integrity sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==

acorn-jsx@^5.3.2:
version "5.3.2"
Expand Down