test: refactor

This commit is contained in:
alexander.akait 2024-06-10 17:40:11 +03:00
parent dedbd69410
commit 08f87802f6
18 changed files with 146 additions and 43 deletions

View File

@ -171,7 +171,7 @@ const getSourceForImportExternal = (
}
const attributes =
dependencyMeta && dependencyMeta.attributes
? `, ${JSON.stringify({ assert: dependencyMeta.attributes })}`
? `, { assert: ${JSON.stringify(dependencyMeta.attributes)} }`
: "";
if (!Array.isArray(moduleAndSpecifiers)) {
return {

View File

@ -0,0 +1,3 @@
{
"foo": "eager"
}

View File

@ -0,0 +1,47 @@
import * as staticPkg from "./static-package.json" assert { type: "json" };
import * as staticPkgStr from "./static-package-str.json" assert { "type": "json" };
it("should allow async externals", async () => {
expect(staticPkg.default.foo).toBe("static");
expect(staticPkgStr.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", {
"assert": { "type": "json" }
})
expect(dynamicPkgStr.default.foo).toBe("dynamic-str");
const eagerPkg = await import(/* webpackMode: "eager" */ "./eager.json", {
assert: { type: "json" }
});
expect(eagerPkg.default.foo).toBe("eager");
await import("./weak.json", {
assert: { type: "json" }
});
const weakPkg = await import(/* webpackMode: "weak" */ "./weak.json", {
assert: { type: "json" }
});
expect(weakPkg.default.foo).toBe("weak");
const pkg = "pkg.json";
const nested = await import(`./nested/${pkg}`, {
assert: { type: "json" }
});
expect(nested.default.foo).toBe("context-dependency");
const reExportPkg = await import("./re-export.js");
expect(reExportPkg.foo).toBe("re-export");
});
export * from "./re-export-directly.json" assert { type: "json" }

View File

@ -0,0 +1,3 @@
{
"foo": "context-dependency"
}

View File

@ -0,0 +1 @@
export * from "./re-export.json" assert { type: "json" };

View File

@ -0,0 +1,5 @@
const supportsImportAttributes = require("../../../helpers/supportsImportAttributes");
module.exports = () => {
return supportsImportAttributes() && !/^v(2[2-9])/.test(process.version);
};

View File

@ -0,0 +1,3 @@
{
"foo": "weak"
}

View File

@ -0,0 +1,65 @@
const path = require("path");
const fs = require("fs");
const {
Compilation,
sources: { RawSource }
} = require("../../../../");
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
library: {
type: "module"
}
},
target: ["web", "es2020"],
experiments: {
outputModule: true
},
plugins: [
{
apply(compiler) {
compiler.hooks.compilation.tap("html-plugin", compilation => {
compilation.hooks.processAssets.tap(
{
name: "copy-plugin",
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
},
() => {
[
"static-package.json",
"static-package-str.json",
"dynamic-package.json",
"dynamic-package-str.json",
"eager.json",
"weak.json",
"./nested/pkg.json",
"re-export.json",
"re-export-directly.json"
].forEach(filename => {
const resolvedFilename = path.resolve(__dirname, filename);
const content = fs.readFileSync(resolvedFilename);
compilation.emitAsset(
filename.replace(/\.\/nested\//, ""),
new RawSource(content)
);
});
}
);
});
}
}
],
externals: {
"./static-package.json": "module ./static-package.json",
"./static-package-str.json": "module ./static-package-str.json",
"./dynamic-package.json": "import ./dynamic-package.json",
"./dynamic-package-str.json": "import ./dynamic-package-str.json",
"./eager.json": "import ./eager.json",
"./weak.json": "import ./weak.json",
"./pkg.json": "import ./pkg.json",
"./pkg": "import ./pkg",
"./re-export.json": "module ./re-export.json",
"./re-export-directly.json": "module ./re-export-directly.json"
}
};

View File

@ -1,58 +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" };
import * as staticPkg from "./static-package.json" with { type: "json" };
import * as staticPkgStr from "./static-package-str.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" }
with: { type: "json" }
})
expect(dynamicPkg.default.foo).toBe("dynamic");
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" }
const dynamicPkgStr = await import("./dynamic-package-str.json", {
"with": { "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" }
with: { type: "json" }
});
expect(eagerPkg.default.foo).toBe("eager");
await import("./weak.json", {
assert: { type: "json" }
with: { type: "json" }
});
const weakPkg = await import(/* webpackMode: "weak" */ "./weak.json", {
assert: { type: "json" }
with: { type: "json" }
});
expect(weakPkg.default.foo).toBe("weak");
const pkg = "pkg.json";
const nested = await import(`./nested/${pkg}`, {
assert: { type: "json" }
with: { type: "json" }
});
expect(nested.default.foo).toBe("context-dependency");
@ -62,5 +44,4 @@ 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" }
export * from "./re-export-directly.json" with { type: "json" }

View File

@ -0,0 +1,3 @@
{
"foo": "re-export"
}

View File

@ -1 +1 @@
export * from "./re-export.json" assert { type: "json" };
export * from "./re-export.json" with { type: "json" };

View File

@ -29,17 +29,13 @@ 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-assert.json",
"./re-export-with.json"
"re-export.json",
"re-export-directly.json"
].forEach(filename => {
const resolvedFilename = path.resolve(__dirname, filename);
const content = fs.readFileSync(resolvedFilename);
@ -57,17 +53,13 @@ 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-assert.json": "module ./re-export-assert.json",
"./re-export-with.json": "module ./re-export-with.json"
"./re-export.json": "module ./re-export.json",
"./re-export-directly": "module ./re-export-directly.json"
}
};