-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace
import(...)
with Promise.resolve(...)
- Loading branch information
Showing
8 changed files
with
168 additions
and
65 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 |
---|---|---|
|
@@ -3,5 +3,4 @@ | |
.yarn | ||
.yarnrc.yml | ||
.pnp.js | ||
tsconfig.json | ||
yarn.lock |
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,6 +1,3 @@ | ||
{ | ||
"recommendations": [ | ||
"arcanis.vscode-zipfs", | ||
"esbenp.prettier-vscode" | ||
] | ||
"recommendations": ["arcanis.vscode-zipfs", "esbenp.prettier-vscode"] | ||
} |
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,75 @@ | ||
/** | ||
* @copyright 2021-present Kriasoft (https://git.io/JtoKE) | ||
* | ||
* @typedef {import("webpack").Compiler} Compiler | ||
* @typedef {import("webpack").javascript.JavascriptParser} JavascriptParser | ||
*/ | ||
|
||
const path = require("path"); | ||
const webpack = require("webpack"); | ||
const ImportDependency = require("./ImportDependency"); | ||
|
||
/** | ||
* Excludes dynamically imported dependencies from the output bundle. | ||
*/ | ||
class IgnoreAsyncImportsPlugin { | ||
/** | ||
* Creates a new instance of the plugin. | ||
* | ||
* @param {Object} config Ignore options. | ||
*/ | ||
constructor(config = {}) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* @param {Compiler} compiler | ||
*/ | ||
apply(compiler) { | ||
this.name = this.constructor.name; | ||
const handleParser = this.handleParser.bind(this); | ||
|
||
compiler.hooks.compilation.tap( | ||
this.name, | ||
(compilation, { normalModuleFactory }) => { | ||
compilation.dependencyFactories.set( | ||
ImportDependency, | ||
normalModuleFactory | ||
); | ||
compilation.dependencyTemplates.set( | ||
ImportDependency, | ||
new ImportDependency.Template() | ||
); | ||
|
||
normalModuleFactory.hooks.parser | ||
.for("javascript/auto") | ||
.tap(this.name, handleParser); | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/dynamic") | ||
.tap(this.name, handleParser); | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/esm") | ||
.tap(this.name, handleParser); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param {JavascriptParser} parser | ||
*/ | ||
handleParser(parser, parserOptions) { | ||
if (parserOptions.import !== undefined && !parserOptions.import) { | ||
return; | ||
} | ||
|
||
// Replace import(...) calls with Promise.resolve(...) | ||
parser.hooks.importCall.tap(this.name, (expr) => { | ||
const dep = new ImportDependency(expr.source.value, expr.range); | ||
dep.loc = expr.loc; | ||
parser.state.module.addDependency(dep); | ||
return true; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = IgnoreAsyncImportsPlugin; |
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,73 @@ | ||
/* | ||
MIT License http://www.opensource.org/licenses/mit-license.php | ||
Author Florent Cailhol @ooflorent | ||
*/ | ||
|
||
"use strict"; | ||
|
||
const InitFragment = require("webpack/lib/InitFragment"); | ||
const ModuleDependency = require("webpack/lib/dependencies/ModuleDependency"); | ||
|
||
/** @typedef {import("webpack").sources.ReplaceSource} ReplaceSource */ | ||
/** @typedef {import("webpack").ChunkGraph} ChunkGraph */ | ||
/** @typedef {import("webpack").Dependency} Dependency */ | ||
/** @typedef {import("webpack").Dependency.UpdateHashContext} UpdateHashContext */ | ||
/** @typedef {import("webpack").util.Hash} Hash */ | ||
|
||
class ImportDependency extends ModuleDependency { | ||
constructor(request, range) { | ||
super(request); | ||
this.range = range; | ||
} | ||
|
||
get type() { | ||
return "async import"; | ||
} | ||
|
||
get category() { | ||
return "esm"; | ||
} | ||
|
||
/** | ||
* Update the hash | ||
* @param {Hash} hash hash to be updated | ||
* @param {UpdateHashContext} context context | ||
* @returns {void} | ||
*/ | ||
updateHash(hash, context) { | ||
hash.update(this.request); | ||
} | ||
|
||
serialize(context) { | ||
const { write } = context; | ||
write(this.request); | ||
write(this.userRequest); | ||
write(this.range); | ||
super.serialize(context); | ||
} | ||
|
||
deserialize(context) { | ||
const { read } = context; | ||
this.request = read(); | ||
this.userRequest = read(); | ||
this.range = read(); | ||
super.deserialize(context); | ||
} | ||
} | ||
|
||
class ImportDependencyTemplate extends ModuleDependency.Template { | ||
/** | ||
* @param {Dependency} dependency the dependency for which the template should be applied | ||
* @param {ReplaceSource} source the current replace source which can be modified | ||
* @returns {void} | ||
*/ | ||
apply(dependency, source) { | ||
const dep = /** @type {ImportDependency} */ (dependency); | ||
// TODO: Provide the requested chunk name | ||
source.replace(dep.range[0], dep.range[1] - 1, "Promise.resolve({})"); | ||
} | ||
} | ||
|
||
ImportDependency.Template = ImportDependencyTemplate; | ||
|
||
module.exports = ImportDependency; |
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,62 +1,9 @@ | ||
/** | ||
* @copyright 2021-present Kriasoft (https://git.io/JtoKE) | ||
* | ||
* @typedef {import("webpack").Compiler} Compiler | ||
* @typedef {import("webpack").javascript.JavascriptParser} JavascriptParser | ||
*/ | ||
|
||
const path = require("path"); | ||
const webpack = require("webpack"); | ||
const IgnoreAsyncImportsPlugin = require("./IgnoreAsyncImportsPlugin"); | ||
|
||
/** | ||
* Excludes dynamically imported dependencies from the output bundle. | ||
*/ | ||
class IgnoreAsyncImportsPlugin { | ||
/** | ||
* Creates a new instance of the plugin. | ||
* | ||
* @param {Object} config Ignore options. | ||
*/ | ||
constructor(config = {}) { | ||
this.config = config; | ||
} | ||
|
||
/** | ||
* @param {Compiler} compiler | ||
*/ | ||
apply(compiler) { | ||
this.name = this.constructor.name; | ||
const handleParser = this.handleParser.bind(this); | ||
|
||
compiler.hooks.compilation.tap( | ||
this.name, | ||
(compilation, { normalModuleFactory }) => { | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/auto") | ||
.tap(this.name, handleParser); | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/dynamic") | ||
.tap(this.name, handleParser); | ||
normalModuleFactory.hooks.parser | ||
.for("javascript/esm") | ||
.tap(this.name, handleParser); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param {JavascriptParser} parser | ||
*/ | ||
handleParser(parser, parserOptions) { | ||
if (parserOptions.import !== undefined && !parserOptions.import) { | ||
return; | ||
} | ||
|
||
// Exclude dynamically imported dependencies. | ||
parser.hooks.importCall.tap(this.name, () => { | ||
return false; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = { IgnoreAsyncImportsPlugin }; | ||
module.exports = { | ||
IgnoreAsyncImportsPlugin, | ||
}; |
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