* Creates an import phase resolver. * @param {boolean=} enableDeferPhase enable defer phase detection * @param {boolean=} enableSourcePhase enable source phase detection * @returns {GetImportPhase} evaluates the import phase for ast node
(enableDeferPhase, enableSourcePhase)
| 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 |
no test coverage detected