diff --git a/src/syntax.ts b/src/syntax.ts index 05d12f5..7a5855e 100644 --- a/src/syntax.ts +++ b/src/syntax.ts @@ -7,16 +7,19 @@ import { isNodeBuiltin, getProtocol } from "./utils"; const ESM_RE = /([\s;]|^)(import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m; +const CJS_RE = + /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; + +const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g; + const BUILTIN_EXTENSIONS = new Set([".mjs", ".cjs", ".node", ".wasm"]); export function hasESMSyntax(code: string): boolean { - return ESM_RE.test(code); + return ESM_RE.test(code.replace(COMMENT_RE, "")); } -const CJS_RE = - /([\s;]|^)(module.exports\b|exports\.\w|require\s*\(|global\.\w)/m; export function hasCJSSyntax(code: string): boolean { - return CJS_RE.test(code); + return CJS_RE.test(code.replace(COMMENT_RE, "")); } export function detectSyntax(code: string) { diff --git a/test/syntax.test.ts b/test/syntax.test.ts index c679880..4e8c286 100644 --- a/test/syntax.test.ts +++ b/test/syntax.test.ts @@ -82,6 +82,8 @@ const staticTests = { isMixed: false, }, "const a={};": { hasESM: false, hasCJS: false, isMixed: false }, + '// They\'re exposed using "export import" so that types are passed along as expected\nmodule.exports={};': + { hasESM: false, hasCJS: true, isMixed: false }, }; describe("detectSyntax", () => {