Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/core/src/core-server/build-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
Expand Down
30 changes: 30 additions & 0 deletions code/core/src/core-server/utils/strip-comments-and-strings.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
9 changes: 9 additions & 0 deletions code/core/src/core-server/utils/strip-comments-and-strings.ts
Original file line number Diff line number Diff line change
@@ -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;
}