| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const Module = require("../Module");
|
|---|
| 9 | const { JAVASCRIPT_TYPE } = require("../ModuleSourceTypeConstants");
|
|---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 11 | const HarmonyImportDependency = require("./HarmonyImportDependency");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
|---|
| 14 | /** @typedef {import("../Dependency")} Dependency */
|
|---|
| 15 | /** @typedef {import("../Dependency").GetConditionFn} GetConditionFn */
|
|---|
| 16 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 17 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 18 | /** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
|---|
| 19 | /** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
|---|
| 20 | /** @typedef {import("./ImportPhase").ImportPhaseType} ImportPhaseType */
|
|---|
| 21 |
|
|---|
| 22 | class HarmonyImportSideEffectDependency extends HarmonyImportDependency {
|
|---|
| 23 | /**
|
|---|
| 24 | * Creates an instance of HarmonyImportSideEffectDependency.
|
|---|
| 25 | * @param {string} request the request string
|
|---|
| 26 | * @param {number} sourceOrder source order
|
|---|
| 27 | * @param {ImportPhaseType} phase import phase
|
|---|
| 28 | * @param {ImportAttributes=} attributes import attributes
|
|---|
| 29 | */
|
|---|
| 30 | constructor(request, sourceOrder, phase, attributes) {
|
|---|
| 31 | super(request, sourceOrder, phase, attributes);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | get type() {
|
|---|
| 35 | return "harmony side effect evaluation";
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Returns function to determine if the connection is active.
|
|---|
| 40 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 41 | * @returns {null | false | GetConditionFn} function to determine if the connection is active
|
|---|
| 42 | */
|
|---|
| 43 | getCondition(moduleGraph) {
|
|---|
| 44 | return (connection) => {
|
|---|
| 45 | const refModule = connection.resolvedModule;
|
|---|
| 46 | if (!refModule) return true;
|
|---|
| 47 | return refModule.getSideEffectsConnectionState(moduleGraph);
|
|---|
| 48 | };
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Gets module evaluation side effects state.
|
|---|
| 53 | * @param {ModuleGraph} moduleGraph the module graph
|
|---|
| 54 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
|---|
| 55 | */
|
|---|
| 56 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
|---|
| 57 | const refModule = moduleGraph.getModule(this);
|
|---|
| 58 | if (!refModule) return true;
|
|---|
| 59 | return refModule.getSideEffectsConnectionState(moduleGraph);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | makeSerializable(
|
|---|
| 64 | HarmonyImportSideEffectDependency,
|
|---|
| 65 | "webpack/lib/dependencies/HarmonyImportSideEffectDependency"
|
|---|
| 66 | );
|
|---|
| 67 |
|
|---|
| 68 | HarmonyImportSideEffectDependency.Template = class HarmonyImportSideEffectDependencyTemplate extends (
|
|---|
| 69 | HarmonyImportDependency.Template
|
|---|
| 70 | ) {
|
|---|
| 71 | /**
|
|---|
| 72 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 73 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 74 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 75 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 76 | * @returns {void}
|
|---|
| 77 | */
|
|---|
| 78 | apply(dependency, source, templateContext) {
|
|---|
| 79 | const { moduleGraph, concatenationScope } = templateContext;
|
|---|
| 80 |
|
|---|
| 81 | const module = /** @type {Module} */ (moduleGraph.getModule(dependency));
|
|---|
| 82 |
|
|---|
| 83 | if (module && !Module.getSourceBasicTypes(module).has(JAVASCRIPT_TYPE)) {
|
|---|
| 84 | // no need to render import
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | if (concatenationScope && concatenationScope.isModuleInScope(module)) {
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 | super.apply(dependency, source, templateContext);
|
|---|
| 92 | }
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | module.exports = HarmonyImportSideEffectDependency;
|
|---|