diff --git a/declarations.d.ts b/declarations.d.ts index 53e514e47..ea7c1ab61 100644 --- a/declarations.d.ts +++ b/declarations.d.ts @@ -371,6 +371,13 @@ declare module "browserslist" { export = browserslist; } +// TODO remove that when @types/estree is updated +interface ImportAttributeNode { + type: "ImportAttribute"; + key: import("estree").Identifier | import("estree").Literal; + value: import("estree").Literal; +} + type TODO = any; type RecursiveArrayOrRecord = diff --git a/lib/NormalModule.js b/lib/NormalModule.js index fca226e1b..586977483 100644 --- a/lib/NormalModule.js +++ b/lib/NormalModule.js @@ -293,9 +293,13 @@ class NormalModule extends Module { */ identifier() { if (this.layer === null) { - return this.request; + if (this.type === "javascript/auto") { + return this.request; + } else { + return `${this.type}|${this.request}`; + } } else { - return `${this.request}|${this.layer}`; + return `${this.type}|${this.request}|${this.layer}`; } } diff --git a/lib/NormalModuleFactory.js b/lib/NormalModuleFactory.js index 3eebe51e7..40b5d123c 100644 --- a/lib/NormalModuleFactory.js +++ b/lib/NormalModuleFactory.js @@ -20,7 +20,7 @@ const ModuleGraph = require("./ModuleGraph"); const NormalModule = require("./NormalModule"); const BasicEffectRulePlugin = require("./rules/BasicEffectRulePlugin"); const BasicMatcherRulePlugin = require("./rules/BasicMatcherRulePlugin"); -const DescriptionDataMatcherRulePlugin = require("./rules/DescriptionDataMatcherRulePlugin"); +const ObjectMatcherRulePlugin = require("./rules/ObjectMatcherRulePlugin"); const RuleSetCompiler = require("./rules/RuleSetCompiler"); const UseEffectRulePlugin = require("./rules/UseEffectRulePlugin"); const LazySet = require("./util/LazySet"); @@ -44,7 +44,7 @@ const { parseResource } = require("./util/identifier"); * @property {ModuleFactoryCreateData["resolveOptions"]} resolveOptions * @property {string} context * @property {string} request - * @property {Map} assertions + * @property {Record | undefined} assertions * @property {ModuleDependency[]} dependencies * @property {Object} createData * @property {LazySet} fileDependencies @@ -183,14 +183,14 @@ const ruleSetCompiler = new RuleSetCompiler([ new BasicMatcherRulePlugin("issuer"), new BasicMatcherRulePlugin("compiler"), new BasicMatcherRulePlugin("issuerLayer"), - new DescriptionDataMatcherRulePlugin(), + new ObjectMatcherRulePlugin("assert", "assertions"), + new ObjectMatcherRulePlugin("descriptionData"), new BasicEffectRulePlugin("type"), new BasicEffectRulePlugin("sideEffects"), new BasicEffectRulePlugin("parser"), new BasicEffectRulePlugin("resolve"), new BasicEffectRulePlugin("generator"), new BasicEffectRulePlugin("layer"), - new BasicEffectRulePlugin("assert"), new UseEffectRulePlugin() ]); @@ -341,7 +341,7 @@ class NormalModuleFactory extends ModuleFactory { context, dependencies, request, - assertions = new Map(), + assertions, resolveOptions, fileDependencies, missingDependencies, @@ -450,6 +450,7 @@ class NormalModuleFactory extends ModuleFactory { resourceQuery: resourceDataForRules.query, resourceFragment: resourceDataForRules.fragment, scheme, + assertions, mimetype: matchResourceData ? "" : resourceData.data.mimetype || "", dependency: dependencyType, descriptionData: matchResourceData @@ -517,13 +518,6 @@ class NormalModuleFactory extends ModuleFactory { } else { type = "javascript/auto"; } - if (assertions.has("type") && type !== assertions.get("type")) { - throw new Error( - `type mismatch; requested type ${assertions.get( - "type" - )} but got ${type}` - ); - } } const resolveOptions = settings.resolve; const layer = settings.layer; diff --git a/lib/config/defaults.js b/lib/config/defaults.js index 3cd437a10..a3800d7cb 100644 --- a/lib/config/defaults.js +++ b/lib/config/defaults.js @@ -449,13 +449,11 @@ const applyModuleDefaults = ( }, { test: /\.json$/i, - type: "json", - assert: { type: "json" } + type: "json" }, { mimetype: "application/json", - type: "json", - assert: { type: "json" } + type: "json" }, { test: /\.mjs$/i, @@ -484,18 +482,6 @@ const applyModuleDefaults = ( or: ["text/javascript", "application/javascript"] }, ...esm - }, - { - dependency: "url", - oneOf: [ - { - scheme: /^data$/, - type: "asset/inline" - }, - { - type: "asset/resource" - } - ] } ]; if (asyncWebAssembly) { @@ -543,6 +529,24 @@ const applyModuleDefaults = ( ...wasm }); } + rules.push( + { + dependency: "url", + oneOf: [ + { + scheme: /^data$/, + type: "asset/inline" + }, + { + type: "asset/resource" + } + ] + }, + { + assert: { type: "json" }, + type: "json" + } + ); return rules; }); }; diff --git a/lib/dependencies/HarmonyExportDependencyParserPlugin.js b/lib/dependencies/HarmonyExportDependencyParserPlugin.js index c341c1adc..5af870209 100644 --- a/lib/dependencies/HarmonyExportDependencyParserPlugin.js +++ b/lib/dependencies/HarmonyExportDependencyParserPlugin.js @@ -12,7 +12,8 @@ const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency") const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency"); const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency"); const { - harmonySpecifierTag + harmonySpecifierTag, + getAssertions } = require("./HarmonyImportDependencyParserPlugin"); const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency"); @@ -48,7 +49,8 @@ module.exports = class HarmonyExportDependencyParserPlugin { parser.state.module.addPresentationalDependency(clearDep); const sideEffectDep = new HarmonyImportSideEffectDependency( source, - parser.state.lastHarmonyImportOrder + parser.state.lastHarmonyImportOrder, + getAssertions(statement) ); sideEffectDep.loc = Object.create(statement.loc); sideEffectDep.loc.index = -1; @@ -127,7 +129,8 @@ module.exports = class HarmonyExportDependencyParserPlugin { harmonyNamedExports, null, this.strictExportPresence, - null + null, + settings.assertions ); } else { dep = new HarmonyExportSpecifierDependency(id, name); diff --git a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js index 501f64255..793ea25b5 100644 --- a/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +++ b/lib/dependencies/HarmonyExportImportedSpecifierDependency.js @@ -159,6 +159,7 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency { * @param {ReadonlyArray | Iterable} otherStarExports other star exports in the module before this import * @param {boolean} strictExportPresence when true, missing exports in the imported module lead to errors instead of warnings * @param {HarmonyStarExportsList} allStarExports all star exports in the module + * @param {Record=} assertions import assertions */ constructor( request, @@ -168,9 +169,10 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency { activeExports, otherStarExports, strictExportPresence, - allStarExports + allStarExports, + assertions ) { - super(request, sourceOrder); + super(request, sourceOrder, assertions); this.ids = ids; this.name = name; diff --git a/lib/dependencies/HarmonyImportDependency.js b/lib/dependencies/HarmonyImportDependency.js index 9bf8d311a..def765c9a 100644 --- a/lib/dependencies/HarmonyImportDependency.js +++ b/lib/dependencies/HarmonyImportDependency.js @@ -32,11 +32,12 @@ class HarmonyImportDependency extends ModuleDependency { * * @param {string} request request string * @param {number} sourceOrder source order - * @param {Map} assertions import assertions + * @param {Record=} assertions import assertions */ - constructor(request, sourceOrder, assertions = new Map()) { + constructor(request, sourceOrder, assertions) { super(request); this.sourceOrder = sourceOrder; + this.assertions = assertions; } get category() { @@ -202,12 +203,14 @@ class HarmonyImportDependency extends ModuleDependency { serialize(context) { const { write } = context; write(this.sourceOrder); + write(this.assertions); super.serialize(context); } deserialize(context) { const { read } = context; this.sourceOrder = read(); + this.assertions = read(); super.deserialize(context); } } diff --git a/lib/dependencies/HarmonyImportDependencyParserPlugin.js b/lib/dependencies/HarmonyImportDependencyParserPlugin.js index 0872b2cbd..de2466df4 100644 --- a/lib/dependencies/HarmonyImportDependencyParserPlugin.js +++ b/lib/dependencies/HarmonyImportDependencyParserPlugin.js @@ -14,7 +14,11 @@ const HarmonyExports = require("./HarmonyExports"); const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency"); const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency"); +/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclaration */ +/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclaration */ /** @typedef {import("estree").Identifier} Identifier */ +/** @typedef {import("estree").ImportDeclaration} ImportDeclaration */ +/** @typedef {import("estree").ImportExpression} ImportExpression */ /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ /** @typedef {import("../optimize/InnerGraph").InnerGraph} InnerGraph */ /** @typedef {import("../optimize/InnerGraph").TopLevelSymbol} TopLevelSymbol */ @@ -29,16 +33,30 @@ const harmonySpecifierTag = Symbol("harmony import"); * @property {number} sourceOrder * @property {string} name * @property {boolean} await + * @property {Record | undefined} assertions */ -function getAssertionMap(node) { - if (node.assertions === undefined) { - return new Map(); +/** + * @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions + * @returns {Record | undefined} assertions + */ +function getAssertions(node) { + // TODO remove cast when @type/estree has been updated to import assertions + const assertions = /** @type {{ assertions?: ImportAttributeNode[] }} */ ( + node + ).assertions; + if (assertions === undefined) { + return undefined; } - return node.assertions.reduce((map, assert) => { - map.set(assert.key.name, assert.value.value); - return map; - }, new Map()); + const result = {}; + for (const assertion of assertions) { + const key = + assertion.key.type === "Identifier" + ? assertion.key.name + : assertion.key.value; + result[key] = assertion.value.value; + } + return result; } module.exports = class HarmonyImportDependencyParserPlugin { @@ -75,11 +93,11 @@ module.exports = class HarmonyImportDependencyParserPlugin { clearDep.loc = statement.loc; parser.state.module.addPresentationalDependency(clearDep); parser.unsetAsiPosition(statement.range[1]); - const asserts = getAssertionMap(statement); + const assertions = getAssertions(statement); const sideEffectDep = new HarmonyImportSideEffectDependency( source, parser.state.lastHarmonyImportOrder, - asserts + assertions ); sideEffectDep.loc = statement.loc; parser.state.module.addDependency(sideEffectDep); @@ -94,7 +112,8 @@ module.exports = class HarmonyImportDependencyParserPlugin { name, source, ids, - sourceOrder: parser.state.lastHarmonyImportOrder + sourceOrder: parser.state.lastHarmonyImportOrder, + assertions: getAssertions(statement) }); return true; } @@ -109,7 +128,8 @@ module.exports = class HarmonyImportDependencyParserPlugin { settings.ids, settings.name, expr.range, - this.strictExportPresence + this.strictExportPresence, + settings.assertions ); dep.shorthand = parser.scope.inShorthand; dep.directImport = true; @@ -130,7 +150,8 @@ module.exports = class HarmonyImportDependencyParserPlugin { ids, settings.name, expr.range, - this.strictExportPresence + this.strictExportPresence, + settings.assertions ); dep.asiSafe = !parser.isAsiPosition(expr.range[0]); dep.loc = expr.loc; @@ -150,7 +171,8 @@ module.exports = class HarmonyImportDependencyParserPlugin { ids, settings.name, callee.range, - this.strictExportPresence + this.strictExportPresence, + settings.assertions ); dep.directImport = members.length === 0; dep.call = true; @@ -218,3 +240,4 @@ module.exports = class HarmonyImportDependencyParserPlugin { }; module.exports.harmonySpecifierTag = harmonySpecifierTag; +module.exports.getAssertions = getAssertions; diff --git a/lib/dependencies/HarmonyImportSpecifierDependency.js b/lib/dependencies/HarmonyImportSpecifierDependency.js index 0e18d4244..63c2416a1 100644 --- a/lib/dependencies/HarmonyImportSpecifierDependency.js +++ b/lib/dependencies/HarmonyImportSpecifierDependency.js @@ -29,8 +29,16 @@ const HarmonyImportDependency = require("./HarmonyImportDependency"); const idsSymbol = Symbol("HarmonyImportSpecifierDependency.ids"); class HarmonyImportSpecifierDependency extends HarmonyImportDependency { - constructor(request, sourceOrder, ids, name, range, strictExportPresence) { - super(request, sourceOrder); + constructor( + request, + sourceOrder, + ids, + name, + range, + strictExportPresence, + assertions + ) { + super(request, sourceOrder, assertions); this.ids = ids; this.name = name; this.range = range; diff --git a/lib/dependencies/ModuleDependency.js b/lib/dependencies/ModuleDependency.js index abbb34694..45e1a9c11 100644 --- a/lib/dependencies/ModuleDependency.js +++ b/lib/dependencies/ModuleDependency.js @@ -22,14 +22,20 @@ class ModuleDependency extends Dependency { this.request = request; this.userRequest = request; this.range = undefined; - this.assertions = new Map(); + // assertions must be serialized by subclasses that use it + /** @type {Record | undefined} */ + this.assertions = undefined; } /** * @returns {string | null} an identifier to merge equal requests */ getResourceIdentifier() { - return `module${this.request}`; + let str = `module${this.request}`; + if (this.assertions !== undefined) { + str += JSON.stringify(this.assertions); + } + return str; } /** diff --git a/lib/javascript/JavascriptParser.js b/lib/javascript/JavascriptParser.js index e50147954..7d94e499f 100644 --- a/lib/javascript/JavascriptParser.js +++ b/lib/javascript/JavascriptParser.js @@ -43,6 +43,10 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression"); /** @typedef {import("estree").Node} AnyNode */ /** @typedef {import("estree").Program} ProgramNode */ /** @typedef {import("estree").Statement} StatementNode */ +/** @typedef {import("estree").ImportDeclaration} ImportDeclarationNode */ +/** @typedef {import("estree").ExportNamedDeclaration} ExportNamedDeclarationNode */ +/** @typedef {import("estree").ExportDefaultDeclaration} ExportDefaultDeclarationNode */ +/** @typedef {import("estree").ExportAllDeclaration} ExportAllDeclarationNode */ /** @typedef {import("estree").Super} SuperNode */ /** @typedef {import("estree").TaggedTemplateExpression} TaggedTemplateExpressionNode */ /** @typedef {import("estree").TemplateLiteral} TemplateLiteralNode */ @@ -191,31 +195,31 @@ class JavascriptParser extends Parser { ]), /** @type {HookMap>} */ label: new HookMap(() => new SyncBailHook(["statement"])), - /** @type {SyncBailHook<[StatementNode, ImportSource], boolean | void>} */ + /** @type {SyncBailHook<[ImportDeclarationNode, ImportSource], boolean | void>} */ import: new SyncBailHook(["statement", "source"]), - /** @type {SyncBailHook<[StatementNode, ImportSource, string, string], boolean | void>} */ + /** @type {SyncBailHook<[ImportDeclarationNode, ImportSource, string, string], boolean | void>} */ importSpecifier: new SyncBailHook([ "statement", "source", "exportName", "identifierName" ]), - /** @type {SyncBailHook<[StatementNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode], boolean | void>} */ export: new SyncBailHook(["statement"]), - /** @type {SyncBailHook<[StatementNode, ImportSource], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, ImportSource], boolean | void>} */ exportImport: new SyncBailHook(["statement", "source"]), - /** @type {SyncBailHook<[StatementNode, DeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, DeclarationNode], boolean | void>} */ exportDeclaration: new SyncBailHook(["statement", "declaration"]), - /** @type {SyncBailHook<[StatementNode, DeclarationNode], boolean | void>} */ + /** @type {SyncBailHook<[ExportDefaultDeclarationNode, DeclarationNode], boolean | void>} */ exportExpression: new SyncBailHook(["statement", "declaration"]), - /** @type {SyncBailHook<[StatementNode, string, string, number | undefined], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, string, string, number | undefined], boolean | void>} */ exportSpecifier: new SyncBailHook([ "statement", "identifierName", "exportName", "index" ]), - /** @type {SyncBailHook<[StatementNode, ImportSource, string, string, number | undefined], boolean | void>} */ + /** @type {SyncBailHook<[ExportNamedDeclarationNode | ExportAllDeclarationNode, ImportSource, string, string, number | undefined], boolean | void>} */ exportImportSpecifier: new SyncBailHook([ "statement", "source", diff --git a/lib/rules/DescriptionDataMatcherRulePlugin.js b/lib/rules/ObjectMatcherRulePlugin.js similarity index 56% rename from lib/rules/DescriptionDataMatcherRulePlugin.js rename to lib/rules/ObjectMatcherRulePlugin.js index c4c703f86..613429e8c 100644 --- a/lib/rules/DescriptionDataMatcherRulePlugin.js +++ b/lib/rules/ObjectMatcherRulePlugin.js @@ -8,28 +8,32 @@ /** @typedef {import("./RuleSetCompiler")} RuleSetCompiler */ /** @typedef {import("./RuleSetCompiler").RuleCondition} RuleCondition */ -const RULE_PROPERTY = "descriptionData"; +class ObjectMatcherRulePlugin { + constructor(ruleProperty, dataProperty) { + this.ruleProperty = ruleProperty; + this.dataProperty = dataProperty || ruleProperty; + } -class DescriptionDataMatcherRulePlugin { /** * @param {RuleSetCompiler} ruleSetCompiler the rule set compiler * @returns {void} */ apply(ruleSetCompiler) { + const { ruleProperty, dataProperty } = this; ruleSetCompiler.hooks.rule.tap( - "DescriptionDataMatcherRulePlugin", + "ObjectMatcherRulePlugin", (path, rule, unhandledProperties, result) => { - if (unhandledProperties.has(RULE_PROPERTY)) { - unhandledProperties.delete(RULE_PROPERTY); - const value = rule[RULE_PROPERTY]; + if (unhandledProperties.has(ruleProperty)) { + unhandledProperties.delete(ruleProperty); + const value = rule[ruleProperty]; for (const property of Object.keys(value)) { - const dataProperty = property.split("."); + const nestedDataProperties = property.split("."); const condition = ruleSetCompiler.compileCondition( - `${path}.${RULE_PROPERTY}.${property}`, + `${path}.${ruleProperty}.${property}`, value[property] ); result.conditions.push({ - property: ["descriptionData", ...dataProperty], + property: [dataProperty, ...nestedDataProperties], matchWhenEmpty: condition.matchWhenEmpty, fn: condition.fn }); @@ -40,4 +44,4 @@ class DescriptionDataMatcherRulePlugin { } } -module.exports = DescriptionDataMatcherRulePlugin; +module.exports = ObjectMatcherRulePlugin; diff --git a/test/Defaults.unittest.js b/test/Defaults.unittest.js index ddd574d97..a1efef59d 100644 --- a/test/Defaults.unittest.js +++ b/test/Defaults.unittest.js @@ -122,16 +122,16 @@ describe("Defaults", () => { Object { "test": /\\\\\\.json\\$/i, "type": "json", - "assert": Object { + }, + Object { + "assert": Object { "type": "json", }, + "type": "json", }, Object { "mimetype": "application/json", "type": "json", - "assert": Object { - "type": "json", - }, }, Object { "resolve": Object { diff --git a/test/__snapshots__/StatsTestCases.basictest.js.snap b/test/__snapshots__/StatsTestCases.basictest.js.snap index aed94bbe4..fbebfd57e 100644 --- a/test/__snapshots__/StatsTestCases.basictest.js.snap +++ b/test/__snapshots__/StatsTestCases.basictest.js.snap @@ -2441,15 +2441,15 @@ LOG from webpack.FileSystemInfo exports[`StatsTestCases should print correct stats for real-content-hash 1`] = ` "a-normal: assets by path *.js 3.22 KiB - asset 92b100c9dd880fb6fedd-92b100.js 2.7 KiB [emitted] [immutable] [minimized] (name: runtime) - asset a639a9edc4557744bf94-a639a9.js 288 bytes [emitted] [immutable] [minimized] (name: lazy) - asset e00b58ce2785691cd374-e00b58.js 225 bytes [emitted] [immutable] [minimized] (name: index) + asset 10bfdcacb3051293cdb8-10bfdc.js 2.7 KiB [emitted] [immutable] [minimized] (name: runtime) + asset dafabd2c0a256cd3ba09-dafabd.js 288 bytes [emitted] [immutable] [minimized] (name: lazy) + asset 58b1760470afdc121e5b-58b176.js 225 bytes [emitted] [immutable] [minimized] (name: index) asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b) assets by chunk 20.4 KiB (auxiliary name: lazy) asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index) - Entrypoint index 2.92 KiB (5.89 KiB) = 92b100c9dd880fb6fedd-92b100.js 2.7 KiB e00b58ce2785691cd374-e00b58.js 225 bytes 1 auxiliary asset + Entrypoint index 2.92 KiB (5.89 KiB) = 10bfdcacb3051293cdb8-10bfdc.js 2.7 KiB 58b1760470afdc121e5b-58b176.js 225 bytes 1 auxiliary asset Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js runtime modules 7.19 KiB 9 modules @@ -2468,15 +2468,15 @@ exports[`StatsTestCases should print correct stats for real-content-hash 1`] = ` b-normal: assets by path *.js 3.22 KiB - asset b02624dcf8d618f12ba1-b02624.js 2.7 KiB [emitted] [immutable] [minimized] (name: runtime) - asset a639a9edc4557744bf94-a639a9.js 288 bytes [emitted] [immutable] [minimized] (name: lazy) - asset e00b58ce2785691cd374-e00b58.js 225 bytes [emitted] [immutable] [minimized] (name: index) + asset 51a734c984da3108483e-51a734.js 2.7 KiB [emitted] [immutable] [minimized] (name: runtime) + asset dafabd2c0a256cd3ba09-dafabd.js 288 bytes [emitted] [immutable] [minimized] (name: lazy) + asset 58b1760470afdc121e5b-58b176.js 225 bytes [emitted] [immutable] [minimized] (name: index) asset 666f2b8847021ccc7608-666f2b.js 21 bytes [emitted] [immutable] [minimized] (name: a, b) assets by chunk 20.4 KiB (auxiliary name: lazy) asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index) - Entrypoint index 2.92 KiB (5.89 KiB) = b02624dcf8d618f12ba1-b02624.js 2.7 KiB e00b58ce2785691cd374-e00b58.js 225 bytes 1 auxiliary asset + Entrypoint index 2.92 KiB (5.89 KiB) = 51a734c984da3108483e-51a734.js 2.7 KiB 58b1760470afdc121e5b-58b176.js 225 bytes 1 auxiliary asset Entrypoint a 21 bytes = 666f2b8847021ccc7608-666f2b.js Entrypoint b 21 bytes = 666f2b8847021ccc7608-666f2b.js runtime modules 7.19 KiB 9 modules @@ -2495,19 +2495,19 @@ b-normal: a-source-map: assets by path *.js 3.44 KiB - asset 64f4e187a137fbe6d037-64f4e1.js 2.76 KiB [emitted] [immutable] [minimized] (name: runtime) - sourceMap 64f4e187a137fbe6d037-64f4e1.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime) - asset 05c637d15dff2d0dc5fd-05c637.js 344 bytes [emitted] [immutable] [minimized] (name: lazy) - sourceMap 05c637d15dff2d0dc5fd-05c637.js.map 399 bytes [emitted] [dev] (auxiliary name: lazy) - asset c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes [emitted] [immutable] [minimized] (name: index) - sourceMap c41aff3dd03dbe1d8aa3-c41aff.js.map 366 bytes [emitted] [dev] (auxiliary name: index) + asset 53b1502f730643477466-53b150.js 2.76 KiB [emitted] [immutable] [minimized] (name: runtime) + sourceMap 53b1502f730643477466-53b150.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime) + asset 6a0b20739c3374bc8404-6a0b20.js 344 bytes [emitted] [immutable] [minimized] (name: lazy) + sourceMap 6a0b20739c3374bc8404-6a0b20.js.map 401 bytes [emitted] [dev] (auxiliary name: lazy) + asset 20918c3d6f37be1833a3-20918c.js 281 bytes [emitted] [immutable] [minimized] (name: index) + sourceMap 20918c3d6f37be1833a3-20918c.js.map 366 bytes [emitted] [dev] (auxiliary name: index) asset 222c2acc68675174e6b2-222c2a.js 77 bytes [emitted] [immutable] [minimized] (name: a, b) sourceMap 222c2acc68675174e6b2-222c2a.js.map 254 bytes [emitted] [dev] (auxiliary name: a, b) assets by chunk 20.4 KiB (auxiliary name: lazy) asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index) - Entrypoint index 3.03 KiB (20.5 KiB) = 64f4e187a137fbe6d037-64f4e1.js 2.76 KiB c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes 3 auxiliary assets + Entrypoint index 3.03 KiB (20.5 KiB) = 53b1502f730643477466-53b150.js 2.76 KiB 20918c3d6f37be1833a3-20918c.js 281 bytes 3 auxiliary assets Entrypoint a 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset Entrypoint b 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset runtime modules 7.19 KiB 9 modules @@ -2526,19 +2526,19 @@ a-source-map: b-source-map: assets by path *.js 3.44 KiB - asset 3e79f6f89bbfaf1c154c-3e79f6.js 2.76 KiB [emitted] [immutable] [minimized] (name: runtime) - sourceMap 3e79f6f89bbfaf1c154c-3e79f6.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime) - asset 05c637d15dff2d0dc5fd-05c637.js 344 bytes [emitted] [immutable] [minimized] (name: lazy) - sourceMap 05c637d15dff2d0dc5fd-05c637.js.map 395 bytes [emitted] [dev] (auxiliary name: lazy) - asset c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes [emitted] [immutable] [minimized] (name: index) - sourceMap c41aff3dd03dbe1d8aa3-c41aff.js.map 323 bytes [emitted] [dev] (auxiliary name: index) + asset 4421f63e38fac939b46f-4421f6.js 2.76 KiB [emitted] [immutable] [minimized] (name: runtime) + sourceMap 4421f63e38fac939b46f-4421f6.js.map 14.2 KiB [emitted] [dev] (auxiliary name: runtime) + asset 6a0b20739c3374bc8404-6a0b20.js 344 bytes [emitted] [immutable] [minimized] (name: lazy) + sourceMap 6a0b20739c3374bc8404-6a0b20.js.map 397 bytes [emitted] [dev] (auxiliary name: lazy) + asset 20918c3d6f37be1833a3-20918c.js 281 bytes [emitted] [immutable] [minimized] (name: index) + sourceMap 20918c3d6f37be1833a3-20918c.js.map 323 bytes [emitted] [dev] (auxiliary name: index) asset 222c2acc68675174e6b2-222c2a.js 77 bytes [emitted] [immutable] [minimized] (name: a, b) sourceMap 222c2acc68675174e6b2-222c2a.js.map 254 bytes [emitted] [dev] (auxiliary name: a, b) assets by chunk 20.4 KiB (auxiliary name: lazy) asset 89a353e9c515885abd8e.png 14.6 KiB [emitted] [immutable] [from: file.png] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg?query 5.89 KiB [cached] [immutable] [from: file.jpg?query] (auxiliary name: lazy) asset 7382fad5b015914e0811.jpg 5.89 KiB [emitted] [immutable] [from: file.jpg] (auxiliary name: index) - Entrypoint index 3.03 KiB (20.4 KiB) = 3e79f6f89bbfaf1c154c-3e79f6.js 2.76 KiB c41aff3dd03dbe1d8aa3-c41aff.js 281 bytes 3 auxiliary assets + Entrypoint index 3.03 KiB (20.4 KiB) = 4421f63e38fac939b46f-4421f6.js 2.76 KiB 20918c3d6f37be1833a3-20918c.js 281 bytes 3 auxiliary assets Entrypoint a 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset Entrypoint b 77 bytes (254 bytes) = 222c2acc68675174e6b2-222c2a.js 1 auxiliary asset runtime modules 7.19 KiB 9 modules @@ -4382,34 +4382,36 @@ webpack x.x.x compiled with 1 warning in X ms" `; exports[`StatsTestCases should print correct stats for wasm-explorer-examples-sync 1`] = ` -"assets by path *.js 21.5 KiB - asset bundle.js 16.1 KiB [emitted] (name: main) - asset 325.bundle.js 3.98 KiB [emitted] - asset 780.bundle.js 571 bytes [emitted] +"assets by path *.js 21.7 KiB + asset bundle.js 16.3 KiB [emitted] (name: main) + asset 325.bundle.js 3.81 KiB [emitted] + asset 795.bundle.js 571 bytes [emitted] asset 526.bundle.js 366 bytes [emitted] (id hint: vendors) - asset 230.bundle.js 243 bytes [emitted] - asset 99.bundle.js 241 bytes [emitted] + asset 189.bundle.js 243 bytes [emitted] + asset 517.bundle.js 243 bytes [emitted] + asset 20.bundle.js 241 bytes [emitted] assets by path *.wasm 1.37 KiB - asset 4098dd6bcb43bd54baf6.module.wasm 531 bytes [emitted] [immutable] - asset a0bbe50b118b7ff0648b.module.wasm 290 bytes [emitted] [immutable] - asset 86fec7665231a0198df8.module.wasm 156 bytes [emitted] [immutable] - asset 1da1d844c1509ed4d4c3.module.wasm 154 bytes [emitted] [immutable] - asset 2be4d707a05b1f7313ca.module.wasm 154 bytes [emitted] [immutable] - asset d1fdb94978c775bdc12a.module.wasm 120 bytes [emitted] [immutable] -chunk (runtime: main) 99.bundle.js 50 bytes (javascript) 531 bytes (webassembly) [rendered] + asset e3f145b183228cc640d7.module.wasm 531 bytes [emitted] [immutable] + asset 82d524821ee70d495948.module.wasm 290 bytes [emitted] [immutable] + asset ea450800640f54975338.module.wasm 156 bytes [emitted] [immutable] + asset ebbf27083d239c1ad5e3.module.wasm 154 bytes [emitted] [immutable] + asset ee97efb6a05a4e504238.module.wasm 154 bytes [emitted] [immutable] + asset 0301cb3f9f4151b567f5.module.wasm 120 bytes [emitted] [immutable] +chunk (runtime: main) 20.bundle.js 50 bytes (javascript) 531 bytes (webassembly) [rendered] ./duff.wasm 50 bytes (javascript) 531 bytes (webassembly) [built] [code generated] chunk (runtime: main) bundle.js (main) 586 bytes (javascript) 9.18 KiB (runtime) [entry] [rendered] runtime modules 9.18 KiB 11 modules ./index.js 586 bytes [built] [code generated] -chunk (runtime: main) 230.bundle.js 50 bytes (javascript) 156 bytes (webassembly) [rendered] +chunk (runtime: main) 189.bundle.js 50 bytes (javascript) 156 bytes (webassembly) [rendered] ./Q_rsqrt.wasm 50 bytes (javascript) 156 bytes (webassembly) [built] [code generated] -chunk (runtime: main) 325.bundle.js 1.5 KiB (javascript) 274 bytes (webassembly) [rendered] - ./popcnt.wasm 50 bytes (javascript) 120 bytes (webassembly) [dependent] [built] [code generated] +chunk (runtime: main) 325.bundle.js 1.45 KiB (javascript) 154 bytes (webassembly) [rendered] ./testFunction.wasm 50 bytes (javascript) 154 bytes (webassembly) [dependent] [built] [code generated] ./tests.js 1.4 KiB [built] [code generated] +chunk (runtime: main) 517.bundle.js 50 bytes (javascript) 120 bytes (webassembly) [rendered] + ./popcnt.wasm 50 bytes (javascript) 120 bytes (webassembly) [built] [code generated] chunk (runtime: main) 526.bundle.js (id hint: vendors) 34 bytes [rendered] split chunk (cache group: defaultVendors) ./node_modules/env.js 34 bytes [built] [code generated] -chunk (runtime: main) 780.bundle.js 110 bytes (javascript) 444 bytes (webassembly) [rendered] +chunk (runtime: main) 795.bundle.js 110 bytes (javascript) 444 bytes (webassembly) [rendered] ./fact.wasm 50 bytes (javascript) 154 bytes (webassembly) [built] [code generated] ./fast-math.wasm 60 bytes (javascript) 290 bytes (webassembly) [built] [code generated] runtime modules 9.18 KiB 11 modules diff --git a/test/cases/json/data/poison.js b/test/cases/json/data/poison similarity index 100% rename from test/cases/json/data/poison.js rename to test/cases/json/data/poison diff --git a/test/cases/json/data/unknown b/test/cases/json/data/unknown new file mode 100644 index 000000000..12bae17cf --- /dev/null +++ b/test/cases/json/data/unknown @@ -0,0 +1 @@ +[1, 2, 3, 4] diff --git a/test/cases/json/import-assertions-type-json/errors.js b/test/cases/json/import-assertions-type-json/errors.js new file mode 100644 index 000000000..bcc2cae77 --- /dev/null +++ b/test/cases/json/import-assertions-type-json/errors.js @@ -0,0 +1,3 @@ +module.exports = [ + [{ moduleName: /data.poison/, message: /Unexpected token .+ in JSON/ }] +]; diff --git a/test/cases/json/import-assertions-type-json/import-poison.js b/test/cases/json/import-assertions-type-json/import-poison.js new file mode 100644 index 000000000..0c1cc934c --- /dev/null +++ b/test/cases/json/import-assertions-type-json/import-poison.js @@ -0,0 +1,3 @@ +import poison from "../data/poison" assert { type: "json" }; + +export default poison; diff --git a/test/cases/json/import-assertions-type-json/index.js b/test/cases/json/import-assertions-type-json/index.js index 9dd4e8c1f..d757fbaf4 100644 --- a/test/cases/json/import-assertions-type-json/index.js +++ b/test/cases/json/import-assertions-type-json/index.js @@ -1,5 +1,21 @@ import c from "../data/c.json" assert { type: "json" }; +import unknownJson from "../data/unknown" assert { type: "json" }; +import unknownJs from "../data/unknown"; -it("should be possible to import json data with import assertion", function() { - expect(c[2]).toBe(3); +it("should be possible to import json data with import assertion", function () { + expect(c).toEqual([1, 2, 3, 4]); +}); + +it("should be possible to import json data without extension with import assertion", function () { + expect(unknownJson).toEqual([1, 2, 3, 4]); +}); + +it("should be possible to import js without extension without import assertion in the same file", function () { + expect(unknownJs).toEqual({}); +}); + +it("should not be possible to import js with import assertion", function () { + expect(() => { + require("./import-poison.js"); + }).toThrowError(); }); diff --git a/types.d.ts b/types.d.ts index b296b864e..17d178a58 100644 --- a/types.d.ts +++ b/types.d.ts @@ -4743,21 +4743,44 @@ declare class JavascriptParser extends Parser { boolean | void >; label: HookMap>; - import: SyncBailHook<[Statement, ImportSource], boolean | void>; + import: SyncBailHook<[ImportDeclaration, ImportSource], boolean | void>; importSpecifier: SyncBailHook< - [Statement, ImportSource, string, string], + [ImportDeclaration, ImportSource, string, string], + boolean | void + >; + export: SyncBailHook< + [ExportNamedDeclaration | ExportAllDeclaration], + boolean | void + >; + exportImport: SyncBailHook< + [ExportNamedDeclaration | ExportAllDeclaration, ImportSource], + boolean | void + >; + exportDeclaration: SyncBailHook< + [ExportNamedDeclaration | ExportAllDeclaration, Declaration], + boolean | void + >; + exportExpression: SyncBailHook< + [ExportDefaultDeclaration, Declaration], boolean | void >; - export: SyncBailHook<[Statement], boolean | void>; - exportImport: SyncBailHook<[Statement, ImportSource], boolean | void>; - exportDeclaration: SyncBailHook<[Statement, Declaration], boolean | void>; - exportExpression: SyncBailHook<[Statement, Declaration], boolean | void>; exportSpecifier: SyncBailHook< - [Statement, string, string, undefined | number], + [ + ExportNamedDeclaration | ExportAllDeclaration, + string, + string, + undefined | number + ], boolean | void >; exportImportSpecifier: SyncBailHook< - [Statement, ImportSource, string, string, undefined | number], + [ + ExportNamedDeclaration | ExportAllDeclaration, + ImportSource, + string, + string, + undefined | number + ], boolean | void >; preDeclarator: SyncBailHook< @@ -6382,7 +6405,7 @@ declare class ModuleDependency extends Dependency { request: string; userRequest: string; range: any; - assertions: Map; + assertions?: Record; static Template: typeof DependencyTemplate; static NO_EXPORTS_REFERENCED: string[][]; static EXPORTS_OBJECT_REFERENCED: string[][]; @@ -8939,7 +8962,7 @@ declare interface ResolveData { resolveOptions?: ResolveOptionsWebpackOptions; context: string; request: string; - assertions: Map; + assertions?: Record; dependencies: ModuleDependency[]; createData: Object; fileDependencies: LazySet;