123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
- const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
- const HarmonyCompatibilityDependency = require("./HarmonyCompatibilityDependency");
- const HarmonyEvaluatedImportSpecifierDependency = require("./HarmonyEvaluatedImportSpecifierDependency");
- const HarmonyExportExpressionDependency = require("./HarmonyExportExpressionDependency");
- const HarmonyExportHeaderDependency = require("./HarmonyExportHeaderDependency");
- const HarmonyExportImportedSpecifierDependency = require("./HarmonyExportImportedSpecifierDependency");
- const HarmonyExportSpecifierDependency = require("./HarmonyExportSpecifierDependency");
- const HarmonyImportSideEffectDependency = require("./HarmonyImportSideEffectDependency");
- const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
- const HarmonyDetectionParserPlugin = require("./HarmonyDetectionParserPlugin");
- const HarmonyExportDependencyParserPlugin = require("./HarmonyExportDependencyParserPlugin");
- const HarmonyImportDependencyParserPlugin = require("./HarmonyImportDependencyParserPlugin");
- const HarmonyTopLevelThisParserPlugin = require("./HarmonyTopLevelThisParserPlugin");
- /** @typedef {import("../Compiler")} Compiler */
- class HarmonyModulesPlugin {
- constructor(options) {
- this.options = options;
- }
- /**
- * Apply the plugin
- * @param {Compiler} compiler the compiler instance
- * @returns {void}
- */
- apply(compiler) {
- compiler.hooks.compilation.tap(
- "HarmonyModulesPlugin",
- (compilation, { normalModuleFactory }) => {
- compilation.dependencyTemplates.set(
- HarmonyCompatibilityDependency,
- new HarmonyCompatibilityDependency.Template()
- );
- compilation.dependencyFactories.set(
- HarmonyImportSideEffectDependency,
- normalModuleFactory
- );
- compilation.dependencyTemplates.set(
- HarmonyImportSideEffectDependency,
- new HarmonyImportSideEffectDependency.Template()
- );
- compilation.dependencyFactories.set(
- HarmonyImportSpecifierDependency,
- normalModuleFactory
- );
- compilation.dependencyTemplates.set(
- HarmonyImportSpecifierDependency,
- new HarmonyImportSpecifierDependency.Template()
- );
- compilation.dependencyFactories.set(
- HarmonyEvaluatedImportSpecifierDependency,
- normalModuleFactory
- );
- compilation.dependencyTemplates.set(
- HarmonyEvaluatedImportSpecifierDependency,
- new HarmonyEvaluatedImportSpecifierDependency.Template()
- );
- compilation.dependencyTemplates.set(
- HarmonyExportHeaderDependency,
- new HarmonyExportHeaderDependency.Template()
- );
- compilation.dependencyTemplates.set(
- HarmonyExportExpressionDependency,
- new HarmonyExportExpressionDependency.Template()
- );
- compilation.dependencyTemplates.set(
- HarmonyExportSpecifierDependency,
- new HarmonyExportSpecifierDependency.Template()
- );
- compilation.dependencyFactories.set(
- HarmonyExportImportedSpecifierDependency,
- normalModuleFactory
- );
- compilation.dependencyTemplates.set(
- HarmonyExportImportedSpecifierDependency,
- new HarmonyExportImportedSpecifierDependency.Template()
- );
- compilation.dependencyTemplates.set(
- HarmonyAcceptDependency,
- new HarmonyAcceptDependency.Template()
- );
- compilation.dependencyFactories.set(
- HarmonyAcceptImportDependency,
- normalModuleFactory
- );
- compilation.dependencyTemplates.set(
- HarmonyAcceptImportDependency,
- new HarmonyAcceptImportDependency.Template()
- );
- const handler = (parser, parserOptions) => {
- // TODO webpack 6: rename harmony to esm or module
- if (parserOptions.harmony !== undefined && !parserOptions.harmony)
- return;
- new HarmonyDetectionParserPlugin(this.options).apply(parser);
- new HarmonyImportDependencyParserPlugin(parserOptions).apply(parser);
- new HarmonyExportDependencyParserPlugin(parserOptions).apply(parser);
- new HarmonyTopLevelThisParserPlugin().apply(parser);
- };
- normalModuleFactory.hooks.parser
- .for("javascript/auto")
- .tap("HarmonyModulesPlugin", handler);
- normalModuleFactory.hooks.parser
- .for("javascript/esm")
- .tap("HarmonyModulesPlugin", handler);
- }
- );
- }
- }
- module.exports = HarmonyModulesPlugin;
|