Skip to content

Commit

Permalink
Merge branch 'main' into fix/overwritten_webpack__exports_onChunksLoaded
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Jul 16, 2021
2 parents c469adc + e5570ab commit 3967571
Show file tree
Hide file tree
Showing 32 changed files with 201 additions and 306 deletions.
7 changes: 0 additions & 7 deletions declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,6 @@ 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<T> =
Expand Down
6 changes: 0 additions & 6 deletions declarations/WebpackOptions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,12 +1316,6 @@ export interface ModuleOptions {
* A rule description with conditions and effects for modules.
*/
export interface RuleSetRule {
/**
* Match on import assertions of the dependency.
*/
assert?: {
[k: string]: RuleSetConditionOrConditions;
};
/**
* Match the child compiler name.
*/
Expand Down
8 changes: 2 additions & 6 deletions lib/NormalModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,9 @@ class NormalModule extends Module {
*/
identifier() {
if (this.layer === null) {
if (this.type === "javascript/auto") {
return this.request;
} else {
return `${this.type}|${this.request}`;
}
return this.request;
} else {
return `${this.type}|${this.request}|${this.layer}`;
return `${this.request}|${this.layer}`;
}
}

Expand Down
10 changes: 2 additions & 8 deletions lib/NormalModuleFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const ModuleGraph = require("./ModuleGraph");
const NormalModule = require("./NormalModule");
const BasicEffectRulePlugin = require("./rules/BasicEffectRulePlugin");
const BasicMatcherRulePlugin = require("./rules/BasicMatcherRulePlugin");
const ObjectMatcherRulePlugin = require("./rules/ObjectMatcherRulePlugin");
const DescriptionDataMatcherRulePlugin = require("./rules/DescriptionDataMatcherRulePlugin");
const RuleSetCompiler = require("./rules/RuleSetCompiler");
const UseEffectRulePlugin = require("./rules/UseEffectRulePlugin");
const LazySet = require("./util/LazySet");
Expand All @@ -44,7 +44,6 @@ const { parseResource } = require("./util/identifier");
* @property {ModuleFactoryCreateData["resolveOptions"]} resolveOptions
* @property {string} context
* @property {string} request
* @property {Record<string, any> | undefined} assertions
* @property {ModuleDependency[]} dependencies
* @property {Object} createData
* @property {LazySet<string>} fileDependencies
Expand Down Expand Up @@ -183,8 +182,7 @@ const ruleSetCompiler = new RuleSetCompiler([
new BasicMatcherRulePlugin("issuer"),
new BasicMatcherRulePlugin("compiler"),
new BasicMatcherRulePlugin("issuerLayer"),
new ObjectMatcherRulePlugin("assert", "assertions"),
new ObjectMatcherRulePlugin("descriptionData"),
new DescriptionDataMatcherRulePlugin(),
new BasicEffectRulePlugin("type"),
new BasicEffectRulePlugin("sideEffects"),
new BasicEffectRulePlugin("parser"),
Expand Down Expand Up @@ -341,7 +339,6 @@ class NormalModuleFactory extends ModuleFactory {
context,
dependencies,
request,
assertions,
resolveOptions,
fileDependencies,
missingDependencies,
Expand Down Expand Up @@ -450,7 +447,6 @@ class NormalModuleFactory extends ModuleFactory {
resourceQuery: resourceDataForRules.query,
resourceFragment: resourceDataForRules.fragment,
scheme,
assertions,
mimetype: matchResourceData ? "" : resourceData.data.mimetype || "",
dependency: dependencyType,
descriptionData: matchResourceData
Expand Down Expand Up @@ -698,7 +694,6 @@ class NormalModuleFactory extends ModuleFactory {
const resolveOptions = data.resolveOptions || EMPTY_RESOLVE_OPTIONS;
const dependency = dependencies[0];
const request = dependency.request;
const assertions = dependency.assertions;
const contextInfo = data.contextInfo;
const fileDependencies = new LazySet();
const missingDependencies = new LazySet();
Expand All @@ -709,7 +704,6 @@ class NormalModuleFactory extends ModuleFactory {
resolveOptions,
context,
request,
assertions,
dependencies,
fileDependencies,
missingDependencies,
Expand Down
43 changes: 28 additions & 15 deletions lib/asset/AssetGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ class AssetGenerator extends Generator {
module.resourceResolveData &&
module.resourceResolveData.mimetype !== undefined
) {
mimeType = module.resourceResolveData.mimetype;
mimeType =
module.resourceResolveData.mimetype +
module.resourceResolveData.parameters;
} else if (ext) {
mimeType = mimeTypes.lookup(ext);
}
Expand All @@ -156,22 +158,33 @@ class AssetGenerator extends Generator {
}

let encodedContent;
switch (encoding) {
case "base64": {
encodedContent = originalSource.buffer().toString("base64");
break;
}
case false: {
const content = originalSource.source();
if (typeof content === "string") {
encodedContent = encodeURI(content);
} else {
encodedContent = encodeURI(content.toString("utf-8"));
if (
module.resourceResolveData &&
module.resourceResolveData.encoding === encoding
) {
encodedContent = module.resourceResolveData.encodedContent;
} else {
switch (encoding) {
case "base64": {
encodedContent = originalSource.buffer().toString("base64");
break;
}
case false: {
const content = originalSource.source();

if (typeof content !== "string") {
encodedContent = content.toString("utf-8");
}

encodedContent = encodeURIComponent(encodedContent).replace(
/[!'()*]/g,
character => "%" + character.codePointAt(0).toString(16)
);
break;
}
break;
default:
throw new Error(`Unsupported encoding '${encoding}'`);
}
default:
throw new Error(`Unsupported encoding '${encoding}'`);
}

encodedSource = `data:${mimeType}${
Expand Down
30 changes: 12 additions & 18 deletions lib/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ const applyModuleDefaults = (
or: ["text/javascript", "application/javascript"]
},
...esm
},
{
dependency: "url",
oneOf: [
{
scheme: /^data$/,
type: "asset/inline"
},
{
type: "asset/resource"
}
]
}
];
if (asyncWebAssembly) {
Expand Down Expand Up @@ -529,24 +541,6 @@ const applyModuleDefaults = (
...wasm
});
}
rules.push(
{
dependency: "url",
oneOf: [
{
scheme: /^data$/,
type: "asset/inline"
},
{
type: "asset/resource"
}
]
},
{
assert: { type: "json" },
type: "json"
}
);
return rules;
});
};
Expand Down
9 changes: 3 additions & 6 deletions lib/dependencies/HarmonyExportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency")
const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
const {
harmonySpecifierTag,
getAssertions
harmonySpecifierTag
} = require("./HarmonyImportDependencyParserPlugin");
const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");

Expand Down Expand Up @@ -49,8 +48,7 @@ module.exports = class HarmonyExportDependencyParserPlugin {
parser.state.module.addPresentationalDependency(clearDep);
const sideEffectDep = new HarmonyImportSideEffectDependency(
source,
parser.state.lastHarmonyImportOrder,
getAssertions(statement)
parser.state.lastHarmonyImportOrder
);
sideEffectDep.loc = Object.create(statement.loc);
sideEffectDep.loc.index = -1;
Expand Down Expand Up @@ -129,8 +127,7 @@ module.exports = class HarmonyExportDependencyParserPlugin {
harmonyNamedExports,
null,
this.strictExportPresence,
null,
settings.assertions
null
);
} else {
dep = new HarmonyExportSpecifierDependency(id, name);
Expand Down
6 changes: 2 additions & 4 deletions lib/dependencies/HarmonyExportImportedSpecifierDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
* @param {ReadonlyArray<HarmonyExportImportedSpecifierDependency> | Iterable<HarmonyExportImportedSpecifierDependency>} 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<string, any>=} assertions import assertions
*/
constructor(
request,
Expand All @@ -169,10 +168,9 @@ class HarmonyExportImportedSpecifierDependency extends HarmonyImportDependency {
activeExports,
otherStarExports,
strictExportPresence,
allStarExports,
assertions
allStarExports
) {
super(request, sourceOrder, assertions);
super(request, sourceOrder);

this.ids = ids;
this.name = name;
Expand Down
6 changes: 1 addition & 5 deletions lib/dependencies/HarmonyImportDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ class HarmonyImportDependency extends ModuleDependency {
*
* @param {string} request request string
* @param {number} sourceOrder source order
* @param {Record<string, any>=} assertions import assertions
*/
constructor(request, sourceOrder, assertions) {
constructor(request, sourceOrder) {
super(request);
this.sourceOrder = sourceOrder;
this.assertions = assertions;
}

get category() {
Expand Down Expand Up @@ -203,14 +201,12 @@ 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);
}
}
Expand Down
45 changes: 5 additions & 40 deletions lib/dependencies/HarmonyImportDependencyParserPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ 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 */
Expand All @@ -33,32 +29,8 @@ const harmonySpecifierTag = Symbol("harmony import");
* @property {number} sourceOrder
* @property {string} name
* @property {boolean} await
* @property {Record<string, any> | undefined} assertions
*/

/**
* @param {ImportDeclaration | ExportNamedDeclaration | ExportAllDeclaration | ImportExpression} node node with assertions
* @returns {Record<string, any> | undefined} assertions
*/
function getAssertions(node) {
// TODO remove cast when @types/estree has been updated to import assertions
const assertions = /** @type {{ assertions?: ImportAttributeNode[] }} */ (
node
).assertions;
if (assertions === undefined) {
return undefined;
}
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 {
constructor(options) {
this.strictExportPresence = options.strictExportPresence;
Expand Down Expand Up @@ -93,11 +65,9 @@ module.exports = class HarmonyImportDependencyParserPlugin {
clearDep.loc = statement.loc;
parser.state.module.addPresentationalDependency(clearDep);
parser.unsetAsiPosition(statement.range[1]);
const assertions = getAssertions(statement);
const sideEffectDep = new HarmonyImportSideEffectDependency(
source,
parser.state.lastHarmonyImportOrder,
assertions
parser.state.lastHarmonyImportOrder
);
sideEffectDep.loc = statement.loc;
parser.state.module.addDependency(sideEffectDep);
Expand All @@ -112,8 +82,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
name,
source,
ids,
sourceOrder: parser.state.lastHarmonyImportOrder,
assertions: getAssertions(statement)
sourceOrder: parser.state.lastHarmonyImportOrder
});
return true;
}
Expand All @@ -128,8 +97,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
settings.ids,
settings.name,
expr.range,
this.strictExportPresence,
settings.assertions
this.strictExportPresence
);
dep.shorthand = parser.scope.inShorthand;
dep.directImport = true;
Expand All @@ -150,8 +118,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
ids,
settings.name,
expr.range,
this.strictExportPresence,
settings.assertions
this.strictExportPresence
);
dep.asiSafe = !parser.isAsiPosition(expr.range[0]);
dep.loc = expr.loc;
Expand All @@ -171,8 +138,7 @@ module.exports = class HarmonyImportDependencyParserPlugin {
ids,
settings.name,
callee.range,
this.strictExportPresence,
settings.assertions
this.strictExportPresence
);
dep.directImport = members.length === 0;
dep.call = true;
Expand Down Expand Up @@ -240,4 +206,3 @@ module.exports = class HarmonyImportDependencyParserPlugin {
};

module.exports.harmonySpecifierTag = harmonySpecifierTag;
module.exports.getAssertions = getAssertions;
4 changes: 2 additions & 2 deletions lib/dependencies/HarmonyImportSideEffectDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const HarmonyImportDependency = require("./HarmonyImportDependency");
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */

class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
constructor(request, sourceOrder, assertions) {
super(request, sourceOrder, assertions);
constructor(request, sourceOrder) {
super(request, sourceOrder);
}

get type() {
Expand Down
Loading

0 comments on commit 3967571

Please sign in to comment.