( output: Sourcemap, body: Node, resolveImport: (specifier: string) => string = String, resolveFile: (specifier: string) => string = String )
| 152 | } |
| 153 | |
| 154 | function rewriteImportExpressions( |
| 155 | output: Sourcemap, |
| 156 | body: Node, |
| 157 | resolveImport: (specifier: string) => string = String, |
| 158 | resolveFile: (specifier: string) => string = String |
| 159 | ): void { |
| 160 | function rewriteImportSource(source: StringLiteral) { |
| 161 | output.replaceLeft(source.start, source.end, JSON.stringify(resolveImport(getStringLiteralValue(source)))); |
| 162 | } |
| 163 | simple(body, { |
| 164 | ImportExpression(node) { |
| 165 | const source = node.source; |
| 166 | if (isStringLiteral(source)) { |
| 167 | rewriteImportSource(source); |
| 168 | } |
| 169 | }, |
| 170 | CallExpression(node) { |
| 171 | const source = node.arguments[0]; |
| 172 | if (isImportMetaResolve(node) && isStringLiteral(source)) { |
| 173 | const value = getStringLiteralValue(source); |
| 174 | const resolution = isPathImport(value) && !isJavaScript(value) ? resolveFile(value) : resolveImport(value); |
| 175 | output.replaceLeft( |
| 176 | node.start, |
| 177 | node.end, |
| 178 | isPathImport(resolution) |
| 179 | ? `new URL(${JSON.stringify(resolution)}, location).href` |
| 180 | : JSON.stringify(resolution) |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | function rewriteImportDeclarations( |
| 188 | output: Sourcemap, |
no outgoing calls
no test coverage detected