* @param {URL} url * @param {{parentURL: string; source?: Buffer}} context * @param {boolean} ignoreErrors * @returns {string}
(url, context = { __proto__: null }, ignoreErrors)
| 162 | * @returns {string} |
| 163 | */ |
| 164 | function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreErrors) { |
| 165 | const { source } = context; |
| 166 | const ext = extname(url); |
| 167 | |
| 168 | if (ext === '.js') { |
| 169 | const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url); |
| 170 | if (packageType !== 'none') { |
| 171 | return packageType; |
| 172 | } |
| 173 | |
| 174 | // The controlling `package.json` file has no `type` field. |
| 175 | // `source` is undefined when this is called from `defaultResolve`; |
| 176 | // but this gets called again from `defaultLoad`/`defaultLoadSync`. |
| 177 | // For ambiguous files (.js, no type field) we return undefined from `resolve` and re-run the check in `load`. |
| 178 | const format = detectModuleFormat(source, url); |
| 179 | if (format === 'module' && foundPackageJson) { |
| 180 | // This module has a .js extension, a package.json with no `type` field, and ESM syntax. |
| 181 | // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. |
| 182 | warnTypelessPackageJsonFile(pjsonPath, url); |
| 183 | } |
| 184 | return format; |
| 185 | } |
| 186 | if (ext === '.ts' && getOptionValue('--strip-types')) { |
| 187 | const { type: packageType, pjsonPath, exists: foundPackageJson } = getPackageScopeConfig(url); |
| 188 | if (packageType !== 'none') { |
| 189 | return `${packageType}-typescript`; |
| 190 | } |
| 191 | // The controlling `package.json` file has no `type` field. |
| 192 | // `source` is undefined when this is called from `defaultResolve`; |
| 193 | // but this gets called again from `defaultLoad`/`defaultLoadSync`. |
| 194 | // Since strip-types depends on detect-module, we always return null if source is undefined. |
| 195 | if (!source) { return null; } |
| 196 | const { stringify } = require('internal/modules/helpers'); |
| 197 | const { stripTypeScriptModuleTypes } = require('internal/modules/typescript'); |
| 198 | const stringifiedSource = stringify(source); |
| 199 | const parsedSource = stripTypeScriptModuleTypes(stringifiedSource, fileURLToPath(url)); |
| 200 | const detectedFormat = detectModuleFormat(parsedSource, url); |
| 201 | const format = `${detectedFormat}-typescript`; |
| 202 | if (format === 'module-typescript' && foundPackageJson) { |
| 203 | // This module has a .js extension, a package.json with no `type` field, and ESM syntax. |
| 204 | // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. |
| 205 | warnTypelessPackageJsonFile(pjsonPath, url); |
| 206 | } |
| 207 | return format; |
| 208 | } |
| 209 | |
| 210 | if (ext === '') { |
| 211 | const packageType = getPackageType(url); |
| 212 | if (packageType === 'module') { |
| 213 | return getFormatOfExtensionlessFile(url); |
| 214 | } |
| 215 | if (packageType !== 'none') { |
| 216 | return packageType; // 'commonjs' or future package types |
| 217 | } |
| 218 | |
| 219 | // The controlling `package.json` file has no `type` field. |
| 220 | if (!source) { |
| 221 | return null; |
nothing calls this directly
no test coverage detected
searching dependent graphs…