Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
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,51 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Ivan Kopeykin @vankop | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const makeSerializable = require("../util/makeSerializable"); | ||
const ContextDependency = require("./ContextDependency"); | ||
const ModuleDependencyTemplateAsRequireId = require("./ModuleDependencyTemplateAsRequireId"); | ||
|
||
class ImportMetaContextDependency extends ContextDependency { | ||
constructor(options, range) { | ||
super(options); | ||
|
||
this.range = range; | ||
} | ||
|
||
get category() { | ||
return "esm"; | ||
} | ||
|
||
get type() { | ||
return "import.meta.webpackContext"; | ||
} | ||
|
||
serialize(context) { | ||
const { write } = context; | ||
|
||
write(this.range); | ||
|
||
super.serialize(context); | ||
} | ||
|
||
deserialize(context) { | ||
const { read } = context; | ||
|
||
this.range = read(); | ||
|
||
super.deserialize(context); | ||
} | ||
} | ||
|
||
makeSerializable( | ||
ImportMetaContextDependency, | ||
"webpack/lib/dependencies/ImportMetaContextDependency" | ||
); | ||
|
||
ImportMetaContextDependency.Template = ModuleDependencyTemplateAsRequireId; | ||
|
||
module.exports = ImportMetaContextDependency; |
98 changes: 98 additions & 0 deletions
98
lib/dependencies/ImportMetaContextDependencyParserPlugin.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,98 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Ivan Kopeykin @vankop | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const { | ||
evaluateToIdentifier | ||
} = require("../javascript/JavascriptParserHelpers"); | ||
const ImportMetaContextDependency = require("./ImportMetaContextDependency"); | ||
|
||
/** @typedef {import("estree").Expression} ExpressionNode */ | ||
/** @typedef {import("estree").ObjectExpression} ObjectExpressionNode */ | ||
/** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */ | ||
|
||
/** | ||
* @param {JavascriptParser} parser parser | ||
* @param {ObjectExpressionNode} optionsNode node | ||
* @returns {{mode: string, recursive: boolean, regExp: RegExp}} options | ||
*/ | ||
function getOptions(parser, optionsNode) { | ||
let regExp = /^\.\/.*$/; | ||
let recursive = true; | ||
let mode = "sync"; | ||
if (optionsNode) { | ||
for (const prop of optionsNode.properties) { | ||
if (prop.type !== "Property" || prop.key.type !== "Identifier") return; | ||
switch (prop.key.name) { | ||
case "regExp": { | ||
const regExpExpr = parser.evaluateExpression( | ||
/** @type {ExpressionNode} */ (prop.value) | ||
); | ||
if (!regExpExpr.isRegExp()) return; | ||
regExp = regExpExpr.regExp; | ||
break; | ||
} | ||
case "mode": { | ||
const modeExpr = parser.evaluateExpression( | ||
/** @type {ExpressionNode} */ (prop.value) | ||
); | ||
if (!modeExpr.isString()) return; | ||
mode = modeExpr.string; | ||
break; | ||
} | ||
case "recursive": { | ||
const recursiveExpr = parser.evaluateExpression( | ||
/** @type {ExpressionNode} */ (prop.value) | ||
); | ||
if (!recursiveExpr.isBoolean()) return; | ||
recursive = recursiveExpr.bool; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return { recursive, regExp, mode }; | ||
} | ||
|
||
module.exports = class ImportMetaContextDependencyParserPlugin { | ||
apply(parser) { | ||
parser.hooks.evaluateIdentifier | ||
.for("import.meta.webpackContext") | ||
.tap("HotModuleReplacementPlugin", expr => { | ||
return evaluateToIdentifier( | ||
"import.meta.webpackContext", | ||
"import.meta", | ||
() => ["webpackContext"], | ||
true | ||
)(expr); | ||
}); | ||
parser.hooks.call | ||
.for("import.meta.webpackContext") | ||
.tap("ImportMetaContextDependencyParserPlugin", expr => { | ||
if (expr.arguments.length < 1 || expr.arguments.length > 2) return; | ||
const [directoryNode, optionsNode] = expr.arguments; | ||
if (optionsNode && optionsNode.type !== "ObjectExpression") return; | ||
const requestExpr = parser.evaluateExpression(directoryNode); | ||
if (!requestExpr.isString()) return; | ||
const request = requestExpr.string; | ||
const options = getOptions(parser, optionsNode); | ||
if (!options) return; | ||
|
||
const dep = new ImportMetaContextDependency( | ||
{ | ||
request, | ||
...options, | ||
category: "esm" | ||
}, | ||
expr.range | ||
); | ||
dep.loc = expr.loc; | ||
dep.optional = !!parser.scope.inTry; | ||
parser.state.current.addDependency(dep); | ||
return true; | ||
}); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
it("import.meta.webpackContext without arguments should work", function() { | ||
const contextRequire = import.meta.webpackContext("./dir"); | ||
expect(contextRequire("./four")).toBe(4); | ||
}); | ||
|
||
it("should not bundle context requires with asyncMode === 'weak'", function() { | ||
var contextRequire = require.context(".", false, /two/, "weak"); | ||
const contextRequire = import.meta.webpackContext(".", { | ||
recursive: false, | ||
regExp: /two/, | ||
mode: "weak" | ||
}); | ||
expect(function() { | ||
contextRequire("./two") | ||
}).toThrowError(/not available/); | ||
}); | ||
|
||
it("should find module with asyncMode === 'weak' when required elsewhere", function() { | ||
var contextRequire = require.context(".", false, /.+/, "weak"); | ||
const contextRequire = require.context(".", false, /.+/, "weak"); | ||
expect(contextRequire("./three")).toBe(3); | ||
require("./three"); // in a real app would be served as a separate chunk | ||
}); | ||
|
||
it("should find module with asyncMode === 'weak' when required elsewhere (recursive)", function() { | ||
var contextRequire = require.context(".", true, /.+/, "weak"); | ||
const contextRequire = require.context(".", true, /.+/, "weak"); | ||
expect(contextRequire("./dir/four")).toBe(4); | ||
require("./dir/four"); // in a real app would be served as a separate chunk | ||
}); |