-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15455 from webpack/feature/support-in-operator
add "in" operator support
- Loading branch information
Showing
17 changed files
with
230 additions
and
14 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
lib/dependencies/HarmonyEvaluatedImportSpecifierDependency.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Ivan Kopeykin @vankop | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const makeSerializable = require("../util/makeSerializable"); | ||
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency"); | ||
|
||
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */ | ||
/** @typedef {import("../ChunkGraph")} ChunkGraph */ | ||
/** @typedef {import("../Dependency")} Dependency */ | ||
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */ | ||
|
||
/** | ||
* Dependency for static evaluating import specifier. e.g. | ||
* @example | ||
* import a from "a"; | ||
* "x" in a; | ||
* a.x !== undefined; // if x value statically analyzable | ||
*/ | ||
class HarmonyEvaluatedImportSpecifierDependency extends HarmonyImportSpecifierDependency { | ||
constructor(request, sourceOrder, ids, name, range, assertions, operator) { | ||
super(request, sourceOrder, ids, name, range, false, assertions); | ||
this.operator = operator; | ||
} | ||
|
||
get type() { | ||
return `evaluated X ${this.operator} harmony import specifier`; | ||
} | ||
|
||
serialize(context) { | ||
super.serialize(context); | ||
const { write } = context; | ||
write(this.operator); | ||
} | ||
|
||
deserialize(context) { | ||
super.deserialize(context); | ||
const { read } = context; | ||
this.operator = read(); | ||
} | ||
} | ||
|
||
makeSerializable( | ||
HarmonyEvaluatedImportSpecifierDependency, | ||
"webpack/lib/dependencies/HarmonyEvaluatedImportSpecifierDependency" | ||
); | ||
|
||
HarmonyEvaluatedImportSpecifierDependency.Template = class HarmonyEvaluatedImportSpecifierDependencyTemplate extends ( | ||
HarmonyImportSpecifierDependency.Template | ||
) { | ||
/** | ||
* @param {Dependency} dependency the dependency for which the template should be applied | ||
* @param {ReplaceSource} source the current replace source which can be modified | ||
* @param {DependencyTemplateContext} templateContext the context object | ||
* @returns {void} | ||
*/ | ||
apply(dependency, source, templateContext) { | ||
const dep = /** @type {HarmonyEvaluatedImportSpecifierDependency} */ ( | ||
dependency | ||
); | ||
const { moduleGraph, runtime } = templateContext; | ||
const connection = moduleGraph.getConnection(dep); | ||
// Skip rendering depending when dependency is conditional | ||
if (connection && !connection.isTargetActive(runtime)) return; | ||
|
||
const exportsInfo = moduleGraph.getExportsInfo(connection.module); | ||
const ids = dep.getIds(moduleGraph); | ||
const value = exportsInfo.isExportProvided(ids); | ||
|
||
if (typeof value === "boolean") { | ||
source.replace(dep.range[0], dep.range[1] - 1, `${value}`); | ||
} else { | ||
const usedName = exportsInfo.getUsedName(ids, runtime); | ||
|
||
const code = this._getCodeForIds( | ||
dep, | ||
source, | ||
templateContext, | ||
ids.slice(0, -1) | ||
); | ||
source.replace( | ||
dep.range[0], | ||
dep.range[1] - 1, | ||
`${ | ||
usedName ? JSON.stringify(usedName[usedName.length - 1]) : '""' | ||
} in ${code}` | ||
); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = HarmonyEvaluatedImportSpecifierDependency; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const aaa = 1; | ||
export const bbb = 2; | ||
export * as ddd from "./n"; | ||
export const usedA = __webpack_exports_info__.aaa.used; | ||
export const canMangleA = __webpack_exports_info__.ccc.canMangle; | ||
export const usedB = __webpack_exports_info__.bbb.used; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const ccc = 3; | ||
export const mmm = () => ({}); | ||
export const aaa = 1; | ||
export const usedA = __webpack_exports_info__.a.used; | ||
export const canMangleC = __webpack_exports_info__.c.canMangle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const exports_ = { aaa: 1, bbb: 2 }; | ||
module.exports = exports_; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./o"; |
1 change: 1 addition & 0 deletions
1
test/cases/parsing/harmony-export-import-specifier/side-effect-free/m.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "../m"; |
1 change: 1 addition & 0 deletions
1
test/cases/parsing/harmony-export-import-specifier/side-effect-free/o.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "../o"; |
3 changes: 3 additions & 0 deletions
3
test/cases/parsing/harmony-export-import-specifier/side-effect-free/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters