| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Haijie Xie @hai-x
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const memoize = require("../util/memoize");
|
|---|
| 9 |
|
|---|
| 10 | const getCommentCompilationWarning = memoize(() =>
|
|---|
| 11 | require("../errors/CommentCompilationWarning")
|
|---|
| 12 | );
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("../javascript/JavascriptParser")} JavascriptParser */
|
|---|
| 15 | /** @typedef {import("../javascript/JavascriptParser").ExportAllDeclaration} ExportAllDeclaration */
|
|---|
| 16 | /** @typedef {import("../javascript/JavascriptParser").ExportNamedDeclaration} ExportNamedDeclaration */
|
|---|
| 17 | /** @typedef {import("../javascript/JavascriptParser").ImportDeclaration} ImportDeclaration */
|
|---|
| 18 | /** @typedef {import("../javascript/JavascriptParser").ImportExpression} ImportExpression */
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {typeof ImportPhase.Evaluation | typeof ImportPhase.Defer | typeof ImportPhase.Source} ImportPhaseType */
|
|---|
| 21 |
|
|---|
| 22 | const ImportPhase = Object.freeze({
|
|---|
| 23 | Evaluation: 0b00,
|
|---|
| 24 | Defer: 0b01,
|
|---|
| 25 | Source: 0b10
|
|---|
| 26 | });
|
|---|
| 27 |
|
|---|
| 28 | /** @typedef {"defer" | "source" | "evaluation"} ImportPhaseName */
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Defines the import phase utils type used by this module.
|
|---|
| 32 | * @typedef {object} ImportPhaseUtils
|
|---|
| 33 | * @property {(phase: ImportPhaseType | undefined) => boolean} isEvaluation true if phase is evaluation
|
|---|
| 34 | * @property {(phase: ImportPhaseType | undefined) => boolean} isDefer true if phase is defer
|
|---|
| 35 | * @property {(phase: ImportPhaseType | undefined) => boolean} isSource true if phase is source
|
|---|
| 36 | * @property {(phase: ImportPhaseType) => ImportPhaseName} stringify return stringified name of phase
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | /** @type {ImportPhaseUtils} */
|
|---|
| 40 | const ImportPhaseUtils = {
|
|---|
| 41 | isEvaluation(phase) {
|
|---|
| 42 | return phase === ImportPhase.Evaluation;
|
|---|
| 43 | },
|
|---|
| 44 | isDefer(phase) {
|
|---|
| 45 | return phase === ImportPhase.Defer;
|
|---|
| 46 | },
|
|---|
| 47 | isSource(phase) {
|
|---|
| 48 | return phase === ImportPhase.Source;
|
|---|
| 49 | },
|
|---|
| 50 | stringify(phase) {
|
|---|
| 51 | switch (phase) {
|
|---|
| 52 | case ImportPhase.Defer:
|
|---|
| 53 | return "defer";
|
|---|
| 54 | case ImportPhase.Source:
|
|---|
| 55 | return "source";
|
|---|
| 56 | default:
|
|---|
| 57 | return "evaluation";
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * Defines the get comment options type used by this module.
|
|---|
| 64 | * @typedef {() => Record<string, EXPECTED_ANY> | null} GetCommentOptions
|
|---|
| 65 | */
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Defines the get import phase callback.
|
|---|
| 69 | * @callback GetImportPhase
|
|---|
| 70 | * @param {JavascriptParser} parser parser
|
|---|
| 71 | * @param {ExportNamedDeclaration | ExportAllDeclaration | ImportDeclaration | ImportExpression} node node
|
|---|
| 72 | * @param {GetCommentOptions=} getCommentOptions optional function that returns the comment options object.
|
|---|
| 73 | * @returns {ImportPhaseType} import phase
|
|---|
| 74 | */
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Creates an import phase resolver.
|
|---|
| 78 | * @param {boolean=} enableDeferPhase enable defer phase detection
|
|---|
| 79 | * @param {boolean=} enableSourcePhase enable source phase detection
|
|---|
| 80 | * @returns {GetImportPhase} evaluates the import phase for ast node
|
|---|
| 81 | */
|
|---|
| 82 | function createGetImportPhase(enableDeferPhase, enableSourcePhase) {
|
|---|
| 83 | return (parser, node, getCommentOptions) => {
|
|---|
| 84 | if (!enableDeferPhase && !enableSourcePhase) return ImportPhase.Evaluation;
|
|---|
| 85 |
|
|---|
| 86 | // We now only support `defer import` and `source import` syntax
|
|---|
| 87 | const phaseBySyntax =
|
|---|
| 88 | "phase" in node
|
|---|
| 89 | ? node.phase === "defer" && enableDeferPhase
|
|---|
| 90 | ? ImportPhase.Defer
|
|---|
| 91 | : node.phase === "source" && enableSourcePhase
|
|---|
| 92 | ? ImportPhase.Source
|
|---|
| 93 | : ImportPhase.Evaluation
|
|---|
| 94 | : ImportPhase.Evaluation;
|
|---|
| 95 |
|
|---|
| 96 | if (!node.range) {
|
|---|
| 97 | return phaseBySyntax;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | getCommentOptions =
|
|---|
| 101 | getCommentOptions ||
|
|---|
| 102 | (() => {
|
|---|
| 103 | if (!node.range) return null;
|
|---|
| 104 | const { options, errors } = parser.parseCommentOptions(node.range);
|
|---|
| 105 | if (errors) {
|
|---|
| 106 | for (const e of errors) {
|
|---|
| 107 | const { comment } = e;
|
|---|
| 108 | if (!comment.loc) continue;
|
|---|
| 109 |
|
|---|
| 110 | const CommentCompilationWarning = getCommentCompilationWarning();
|
|---|
| 111 | parser.state.module.addWarning(
|
|---|
| 112 | new CommentCompilationWarning(
|
|---|
| 113 | `Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
|
|---|
| 114 | comment.loc
|
|---|
| 115 | )
|
|---|
| 116 | );
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 | return options;
|
|---|
| 120 | });
|
|---|
| 121 |
|
|---|
| 122 | const options = getCommentOptions();
|
|---|
| 123 |
|
|---|
| 124 | if (!options) {
|
|---|
| 125 | return phaseBySyntax;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | if (!options.webpackDefer && !options.webpackSource) {
|
|---|
| 129 | return phaseBySyntax;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | const { webpackDefer, webpackSource } = options;
|
|---|
| 133 |
|
|---|
| 134 | if (enableDeferPhase && typeof options.webpackDefer !== "undefined") {
|
|---|
| 135 | if (typeof webpackDefer === "boolean") {
|
|---|
| 136 | return webpackDefer ? ImportPhase.Defer : phaseBySyntax;
|
|---|
| 137 | } else if (node.loc) {
|
|---|
| 138 | const CommentCompilationWarning = getCommentCompilationWarning();
|
|---|
| 139 |
|
|---|
| 140 | parser.state.module.addWarning(
|
|---|
| 141 | new CommentCompilationWarning(
|
|---|
| 142 | "webpackDefer magic comment expected a boolean value.",
|
|---|
| 143 | node.loc
|
|---|
| 144 | )
|
|---|
| 145 | );
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | if (enableSourcePhase && typeof options.webpackSource !== "undefined") {
|
|---|
| 150 | if (typeof webpackSource === "boolean") {
|
|---|
| 151 | return webpackSource ? ImportPhase.Source : phaseBySyntax;
|
|---|
| 152 | } else if (node.loc) {
|
|---|
| 153 | const CommentCompilationWarning = getCommentCompilationWarning();
|
|---|
| 154 |
|
|---|
| 155 | parser.state.module.addWarning(
|
|---|
| 156 | new CommentCompilationWarning(
|
|---|
| 157 | "webpackSource magic comment expected a boolean value.",
|
|---|
| 158 | node.loc
|
|---|
| 159 | )
|
|---|
| 160 | );
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | return phaseBySyntax;
|
|---|
| 165 | };
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | module.exports = {
|
|---|
| 169 | ImportPhase,
|
|---|
| 170 | ImportPhaseUtils,
|
|---|
| 171 | createGetImportPhase
|
|---|
| 172 | };
|
|---|