GeezerVison is showing github.com — Merge pull request #8294 from ljqx/zhbliu/import-parser-plugin-fix-co… · webpack/webpack@eb68316
File treeExpand file treeCollapse file tree Open diff view settings configCases/parsing/issue-8293
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/BasicEvaluatedExpression.js +39-4Lines changed: 39 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,11 +29,13 @@ class BasicEvaluatedExpression { | 29 | 29 | this.regExp = null; | 30 | 30 | this.string = null; | 31 | 31 | this.quasis = null; | | 32 | + this.parts = null; | 32 | 33 | this.array = null; | 33 | 34 | this.items = null; | 34 | 35 | this.options = null; | 35 | 36 | this.prefix = null; | 36 | 37 | this.postfix = null; | | 38 | + this.expression = null; | 37 | 39 | } | 38 | 40 |
| 39 | 41 | isNull() { | @@ -105,10 +107,36 @@ class BasicEvaluatedExpression { | 105 | 107 | : undefined; | 106 | 108 | } | 107 | 109 | if (this.isTemplateString()) { | 108 | | - for (const quasi of this.quasis) { | 109 | | - if (quasi.asBool()) return true; | | 110 | + const str = this.asString(); | | 111 | + if (typeof str === "string") return str !== ""; | | 112 | + } | | 113 | + return undefined; | | 114 | + } | | 115 | + | | 116 | + asString() { | | 117 | + if (this.isBoolean()) return `${this.bool}`; | | 118 | + if (this.isNull()) return "null"; | | 119 | + if (this.isString()) return this.string; | | 120 | + if (this.isNumber()) return `${this.number}`; | | 121 | + if (this.isRegExp()) return `${this.regExp}`; | | 122 | + if (this.isArray()) { | | 123 | + let array = []; | | 124 | + for (const item of this.items) { | | 125 | + const itemStr = item.asString(); | | 126 | + if (itemStr === undefined) return undefined; | | 127 | + array.push(itemStr); | | 128 | + } | | 129 | + return `${array}`; | | 130 | + } | | 131 | + if (this.isConstArray()) return `${this.array}`; | | 132 | + if (this.isTemplateString()) { | | 133 | + let str = ""; | | 134 | + for (const part of this.parts) { | | 135 | + const partStr = part.asString(); | | 136 | + if (partStr === undefined) return undefined; | | 137 | + str += partStr; | 110 | 138 | } | 111 | | - // can't tell if string will be empty without executing | | 139 | + return str; | 112 | 140 | } | 113 | 141 | return undefined; | 114 | 142 | } | @@ -184,9 +212,11 @@ class BasicEvaluatedExpression { | 184 | 212 | return this; | 185 | 213 | } | 186 | 214 |
| 187 | | - setTemplateString(quasis) { | | 215 | + setTemplateString(quasis, parts, kind) { | 188 | 216 | this.type = TypeTemplateString; | 189 | 217 | this.quasis = quasis; | | 218 | + this.parts = parts; | | 219 | + this.templateStringKind = kind; | 190 | 220 | return this; | 191 | 221 | } | 192 | 222 |
| @@ -206,6 +236,11 @@ class BasicEvaluatedExpression { | 206 | 236 | this.range = range; | 207 | 237 | return this; | 208 | 238 | } | | 239 | + | | 240 | + setExpression(expression) { | | 241 | + this.expression = expression; | | 242 | + return this; | | 243 | + } | 209 | 244 | } | 210 | 245 |
| 211 | 246 | module.exports = BasicEvaluatedExpression; |
Collapse file Copy file name to clipboardExpand all lines: lib/Parser.js +49-38Lines changed: 49 additions & 38 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -602,62 +602,66 @@ class Parser extends Tapable { | 602 | 602 |
| 603 | 603 | /** | 604 | 604 | * @param {string} kind "cooked" | "raw" | 605 | | - * @param {TODO[]} quasis quasis | 606 | | - * @param {TODO[]} expressions expressions | 607 | | - * @returns {BasicEvaluatedExpression[]} Simplified template | | 605 | + * @param {TODO} templateLiteralExpr TemplateLiteral expr | | 606 | + * @returns {{quasis: BasicEvaluatedExpression[], parts: BasicEvaluatedExpression[]}} Simplified template | 608 | 607 | */ | 609 | | - const getSimplifiedTemplateResult = (kind, quasis, expressions) => { | | 608 | + const getSimplifiedTemplateResult = (kind, templateLiteralExpr) => { | | 609 | + const quasis = []; | 610 | 610 | const parts = []; | 611 | 611 |
| 612 | | - for (let i = 0; i < quasis.length; i++) { | 613 | | - parts.push( | 614 | | - new BasicEvaluatedExpression() | 615 | | - .setString(quasis[i].value[kind]) | 616 | | - .setRange(quasis[i].range) | 617 | | - ); | | 612 | + for (let i = 0; i < templateLiteralExpr.quasis.length; i++) { | | 613 | + const quasiExpr = templateLiteralExpr.quasis[i]; | | 614 | + const quasi = quasiExpr.value[kind]; | 618 | 615 |
| 619 | 616 | if (i > 0) { | 620 | | - const prevExpr = parts[parts.length - 2], | 621 | | - lastExpr = parts[parts.length - 1]; | 622 | | - const expr = this.evaluateExpression(expressions[i - 1]); | 623 | | - if (!(expr.isString() || expr.isNumber())) continue; | 624 | | - | 625 | | - prevExpr.setString( | 626 | | - prevExpr.string + | 627 | | - (expr.isString() ? expr.string : expr.number) + | 628 | | - lastExpr.string | | 617 | + const prevExpr = parts[parts.length - 1]; | | 618 | + const expr = this.evaluateExpression( | | 619 | + templateLiteralExpr.expressions[i - 1] | 629 | 620 | ); | 630 | | - prevExpr.setRange([prevExpr.range[0], lastExpr.range[1]]); | 631 | | - parts.pop(); | | 621 | + const exprAsString = expr.asString(); | | 622 | + if (typeof exprAsString === "string") { | | 623 | + // We can merge quasi + expr + quasi when expr | | 624 | + // is a const string | | 625 | + | | 626 | + prevExpr.setString(prevExpr.string + exprAsString + quasi); | | 627 | + prevExpr.setRange([prevExpr.range[0], quasiExpr.range[1]]); | | 628 | + // We unset the expression as it doesn't match to a single expression | | 629 | + prevExpr.setExpression(undefined); | | 630 | + continue; | | 631 | + } | | 632 | + parts.push(expr); | 632 | 633 | } | | 634 | + | | 635 | + const part = new BasicEvaluatedExpression() | | 636 | + .setString(quasi) | | 637 | + .setRange(quasiExpr.range) | | 638 | + .setExpression(quasiExpr); | | 639 | + quasis.push(part); | | 640 | + parts.push(part); | 633 | 641 | } | 634 | | - return parts; | | 642 | + return { | | 643 | + quasis, | | 644 | + parts | | 645 | + }; | 635 | 646 | }; | 636 | 647 |
| 637 | 648 | this.hooks.evaluate.for("TemplateLiteral").tap("Parser", node => { | 638 | | - const parts = getSimplifiedTemplateResult.call( | 639 | | - this, | 640 | | - "cooked", | 641 | | - node.quasis, | 642 | | - node.expressions | 643 | | - ); | | 649 | + const { quasis, parts } = getSimplifiedTemplateResult("cooked", node); | 644 | 650 | if (parts.length === 1) { | 645 | 651 | return parts[0].setRange(node.range); | 646 | 652 | } | 647 | 653 | return new BasicEvaluatedExpression() | 648 | | - .setTemplateString(parts) | | 654 | + .setTemplateString(quasis, parts, "cooked") | 649 | 655 | .setRange(node.range); | 650 | 656 | }); | 651 | 657 | this.hooks.evaluate.for("TaggedTemplateExpression").tap("Parser", node => { | 652 | 658 | if (this.evaluateExpression(node.tag).identifier !== "String.raw") return; | 653 | | - const parts = getSimplifiedTemplateResult.call( | 654 | | - this, | 655 | | - "raw", | 656 | | - node.quasi.quasis, | 657 | | - node.quasi.expressions | 658 | | - ); | | 659 | + const { quasis, parts } = getSimplifiedTemplateResult("raw", node.quasi); | | 660 | + if (parts.length === 1) { | | 661 | + return parts[0].setRange(node.range); | | 662 | + } | 659 | 663 | return new BasicEvaluatedExpression() | 660 | | - .setTemplateString(parts) | | 664 | + .setTemplateString(quasis, parts, "raw") | 661 | 665 | .setRange(node.range); | 662 | 666 | }); | 663 | 667 |
| @@ -1926,13 +1930,20 @@ class Parser extends Tapable { | 1926 | 1930 | const hook = this.hooks.evaluate.get(expression.type); | 1927 | 1931 | if (hook !== undefined) { | 1928 | 1932 | const result = hook.call(expression); | 1929 | | - if (result !== undefined) return result; | | 1933 | + if (result !== undefined) { | | 1934 | + if (result) { | | 1935 | + result.setExpression(expression); | | 1936 | + } | | 1937 | + return result; | | 1938 | + } | 1930 | 1939 | } | 1931 | 1940 | } catch (e) { | 1932 | 1941 | console.warn(e); | 1933 | 1942 | // ignore error | 1934 | 1943 | } | 1935 | | - return new BasicEvaluatedExpression().setRange(expression.range); | | 1944 | + return new BasicEvaluatedExpression() | | 1945 | + .setRange(expression.range) | | 1946 | + .setExpression(expression); | 1936 | 1947 | } | 1937 | 1948 |
| 1938 | 1949 | parseString(expression) { | |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/AMDDefineDependencyParserPlugin.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,7 +136,9 @@ class AMDDefineDependencyParserPlugin { | 136 | 136 | param.range, | 137 | 137 | param, | 138 | 138 | expr, | 139 | | - this.options | | 139 | + this.options, | | 140 | + {}, | | 141 | + parser | 140 | 142 | ); | 141 | 143 | if (!dep) return; | 142 | 144 | dep.loc = expr.loc; | |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -142,7 +142,9 @@ class AMDRequireDependenciesBlockParserPlugin { | 142 | 142 | param.range, | 143 | 143 | param, | 144 | 144 | expr, | 145 | | - this.options | | 145 | + this.options, | | 146 | + {}, | | 147 | + parser | 146 | 148 | ); | 147 | 149 | if (!dep) return; | 148 | 150 | dep.loc = expr.loc; | |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/CommonJsRequireDependencyParserPlugin.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,7 +35,9 @@ class CommonJsRequireDependencyParserPlugin { | 35 | 35 | expr.range, | 36 | 36 | param, | 37 | 37 | expr, | 38 | | - options | | 38 | + options, | | 39 | + {}, | | 40 | + parser | 39 | 41 | ); | 40 | 42 | if (!dep) return; | 41 | 43 | dep.loc = expr.loc; | |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/ContextDependencyHelpers.js +70-22Lines changed: 70 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,27 +47,40 @@ ContextDependencyHelpers.create = ( | 47 | 47 | param, | 48 | 48 | expr, | 49 | 49 | options, | 50 | | - contextOptions | | 50 | + contextOptions, | | 51 | + // when parser is not passed in, expressions won't be walked | | 52 | + parser = null | 51 | 53 | ) => { | 52 | 54 | if (param.isTemplateString()) { | 53 | 55 | let prefixRaw = param.quasis[0].string; | 54 | 56 | let postfixRaw = | 55 | 57 | param.quasis.length > 1 | 56 | 58 | ? param.quasis[param.quasis.length - 1].string | 57 | 59 | : ""; | 58 | | - const prefixRange = [param.quasis[0].range[0], param.quasis[0].range[1]]; | 59 | | - const postfixRange = | 60 | | - param.quasis.length > 1 | 61 | | - ? param.quasis[param.quasis.length - 1].range | 62 | | - : ""; | | 60 | + | 63 | 61 | const valueRange = param.range; | 64 | 62 | const { context, prefix } = splitContextFromPrefix(prefixRaw); | 65 | 63 | const { postfix, query } = splitQueryFromPostfix(postfixRaw); | 66 | | - // If there are more than two quasis, maybe the generated RegExp can be more precise? | | 64 | + | | 65 | + // When there are more than two quasis, the generated RegExp can be more precise | | 66 | + // We join the quasis with the expression regexp | | 67 | + const innerQuasis = param.quasis.slice(1, param.quasis.length - 1); | | 68 | + const innerRegExp = | | 69 | + options.wrappedContextRegExp.source + | | 70 | + innerQuasis | | 71 | + .map(q => quotemeta(q.string) + options.wrappedContextRegExp.source) | | 72 | + .join(""); | | 73 | + | | 74 | + // Example: `./context/pre${e}inner${e}inner2${e}post?query` | | 75 | + // context: "./context" | | 76 | + // prefix: "./pre" | | 77 | + // innerQuasis: [BEE("inner"), BEE("inner2")] | | 78 | + // (BEE = BasicEvaluatedExpression) | | 79 | + // postfix: "post" | | 80 | + // query: "?query" | | 81 | + // regExp: /^\.\/pre.*inner.*inner2.*post$/ | 67 | 82 | const regExp = new RegExp( | 68 | | - `^${quotemeta(prefix)}${options.wrappedContextRegExp.source}${quotemeta( | 69 | | - postfix | 70 | | - )}$` | | 83 | + `^${quotemeta(prefix)}${innerRegExp}${quotemeta(postfix)}$` | 71 | 84 | ); | 72 | 85 | const dep = new Dep( | 73 | 86 | Object.assign( | @@ -84,18 +97,48 @@ ContextDependencyHelpers.create = ( | 84 | 97 | ); | 85 | 98 | dep.loc = expr.loc; | 86 | 99 | const replaces = []; | 87 | | - if (prefixRange && prefix !== prefixRaw) { | 88 | | - replaces.push({ | 89 | | - range: prefixRange, | 90 | | - value: prefix | 91 | | - }); | 92 | | - } | 93 | | - if (postfixRange && postfix !== postfixRaw) { | 94 | | - replaces.push({ | 95 | | - range: postfixRange, | 96 | | - value: postfix | 97 | | - }); | 98 | | - } | | 100 | + | | 101 | + param.parts.forEach((part, i) => { | | 102 | + if (i % 2 === 0) { | | 103 | + // Quasis or merged quasi | | 104 | + let range = part.range; | | 105 | + let value = part.string; | | 106 | + if (param.templateStringKind === "cooked") { | | 107 | + value = JSON.stringify(value); | | 108 | + value = value.slice(1, value.length - 1); | | 109 | + } | | 110 | + if (i === 0) { | | 111 | + // prefix | | 112 | + value = prefix; | | 113 | + range = [param.range[0], part.range[1]]; | | 114 | + value = | | 115 | + (param.templateStringKind === "cooked" ? "`" : "String.raw`") + | | 116 | + value; | | 117 | + } else if (i === param.parts.length - 1) { | | 118 | + // postfix | | 119 | + value = postfix; | | 120 | + range = [part.range[0], param.range[1]]; | | 121 | + value = value + "`"; | | 122 | + } else if ( | | 123 | + part.expression && | | 124 | + part.expression.type === "TemplateElement" && | | 125 | + part.expression.value.raw === value | | 126 | + ) { | | 127 | + // Shortcut when it's a single quasi and doesn't need to be replaced | | 128 | + return; | | 129 | + } | | 130 | + replaces.push({ | | 131 | + range, | | 132 | + value | | 133 | + }); | | 134 | + } else { | | 135 | + // Expression | | 136 | + if (parser) { | | 137 | + parser.walkExpression(part.expression); | | 138 | + } | | 139 | + } | | 140 | + }); | | 141 | + | 99 | 142 | dep.replaces = replaces; | 100 | 143 | dep.critical = | 101 | 144 | options.wrappedContextCritical && | @@ -172,6 +215,11 @@ ContextDependencyHelpers.create = ( | 172 | 215 | dep.critical = | 173 | 216 | options.exprContextCritical && | 174 | 217 | "the request of a dependency is an expression"; | | 218 | + | | 219 | + if (parser) { | | 220 | + parser.walkExpression(param.expression); | | 221 | + } | | 222 | + | 175 | 223 | return dep; | 176 | 224 | } | 177 | 225 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/ImportParserPlugin.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -248,7 +248,8 @@ class ImportParserPlugin { | 248 | 248 | namespaceObject: parser.state.module.buildMeta.strictHarmonyModule | 249 | 249 | ? "strict" | 250 | 250 | : true | 251 | | - } | | 251 | + }, | | 252 | + parser | 252 | 253 | ); | 253 | 254 | if (!dep) return; | 254 | 255 | dep.loc = expr.loc; | |
Collapse file Copy file name to clipboardExpand all lines: lib/dependencies/RequireResolveDependencyParserPlugin.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -61,7 +61,8 @@ class RequireResolveDependencyParserPlugin { | 61 | 61 | options, | 62 | 62 | { | 63 | 63 | mode: weak ? "weak" : "sync" | 64 | | - } | | 64 | + }, | | 65 | + parser | 65 | 66 | ); | 66 | 67 | if (!dep) return; | 67 | 68 | dep.loc = expr.loc; | |
Collapse file Copy file name to clipboardExpand all lines: test/cases/parsing/issue-7778/index.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,7 @@ it("should detect query strings in dynamic import as a static value 2", function | 34 | 34 | ]); | 35 | 35 | }); | 36 | 36 |
| 37 | | -it("should detect query strings in dynamic import as a static value 2", function() { | | 37 | +it("should detect query strings in dynamic import as a static value 3", function() { | 38 | 38 | var testFileName = "a"; | 39 | 39 |
| 40 | 40 | return Promise.all([ | |
|