From f92963e36bb7e3792f6cd96f394a8ace7eaa4ba7 Mon Sep 17 00:00:00 2001 From: reeseo3o Date: Sun, 18 Jan 2026 01:11:58 +0900 Subject: [PATCH] fix: false-positive CJS warning when 'exports' appears in strings or comments --- code/core/src/core-server/build-dev.ts | 4 ++- .../utils/strip-comments-and-strings.test.ts | 30 +++++++++++++++++++ .../utils/strip-comments-and-strings.ts | 9 ++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 code/core/src/core-server/utils/strip-comments-and-strings.test.ts create mode 100644 code/core/src/core-server/utils/strip-comments-and-strings.ts diff --git a/code/core/src/core-server/build-dev.ts b/code/core/src/core-server/build-dev.ts index 1d605a5dc1e0..2f9f02502af1 100644 --- a/code/core/src/core-server/build-dev.ts +++ b/code/core/src/core-server/build-dev.ts @@ -31,6 +31,7 @@ import { getManagerBuilder, getPreviewBuilder } from './utils/get-builders'; import { outputStartupInformation } from './utils/output-startup-information'; import { outputStats } from './utils/output-stats'; import { getServerChannelUrl, getServerPort } from './utils/server-address'; +import { stripCommentsAndStrings } from './utils/strip-comments-and-strings'; import { updateCheck } from './utils/update-check'; import { warnOnIncompatibleAddons } from './utils/warnOnIncompatibleAddons'; import { warnWhenUsingArgTypesRegex } from './utils/warnWhenUsingArgTypesRegex'; @@ -186,7 +187,8 @@ export async function buildDevStandalone( // Regex that matches any CommonJS-specific syntax, stolen from Vite: https://github.com/vitejs/vite/blob/91a18c2f7da796ff8217417a4bf189ddda719895/packages/vite/src/node/ssr/ssrExternal.ts#L87 const CJS_CONTENT_REGEX = /\bmodule\.exports\b|\bexports[.[]|\brequire\s*\(|\bObject\.(?:defineProperty|defineProperties|assign)\s*\(\s*exports\b/; - if (CJS_CONTENT_REGEX.test(mainJsContent)) { + const strippedContent = stripCommentsAndStrings(mainJsContent); + if (CJS_CONTENT_REGEX.test(strippedContent)) { deprecate(deprecationMessage); } } diff --git a/code/core/src/core-server/utils/strip-comments-and-strings.test.ts b/code/core/src/core-server/utils/strip-comments-and-strings.test.ts new file mode 100644 index 000000000000..72b12719f374 --- /dev/null +++ b/code/core/src/core-server/utils/strip-comments-and-strings.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from 'vitest'; + +import { stripCommentsAndStrings } from './strip-comments-and-strings'; + +describe('stripCommentsAndStrings', () => { + it('should remove single-quoted strings', () => { + expect(stripCommentsAndStrings("import x from './drei-exports.ts'")).toBe('import x from ""'); + }); + + it('should remove double-quoted strings', () => { + expect(stripCommentsAndStrings('const file = "exports.ts"')).toBe('const file = ""'); + }); + + it('should remove template strings', () => { + expect(stripCommentsAndStrings('const path = `${dir}/exports.ts`')).toBe('const path = ""'); + }); + + it('should remove line comments', () => { + expect(stripCommentsAndStrings('const x = 1 // exports.foo')).toBe('const x = 1 '); + }); + + it('should remove block comments', () => { + expect(stripCommentsAndStrings('/* exports.foo = 1 */ const x = 1')).toBe(' const x = 1'); + }); + + it('should preserve code outside strings and comments', () => { + expect(stripCommentsAndStrings('module.exports = {}')).toBe('module.exports = {}'); + expect(stripCommentsAndStrings('exports.foo = bar')).toBe('exports.foo = bar'); + }); +}); diff --git a/code/core/src/core-server/utils/strip-comments-and-strings.ts b/code/core/src/core-server/utils/strip-comments-and-strings.ts new file mode 100644 index 000000000000..2447e391c83e --- /dev/null +++ b/code/core/src/core-server/utils/strip-comments-and-strings.ts @@ -0,0 +1,9 @@ +export function stripCommentsAndStrings(code: string): string { + let result = code.replace(/(['"`])(?:\\.|(?!\1)[\s\S])*?\1/g, '""'); + result = result.replace(/\/\*[\s\S]*?\*\//g, ''); + result = result + .split('\n') + .map((line) => line.split('//')[0]) + .join('\n'); + return result; +}