Skip to content

Commit 7f9a388

Browse files
committed
simplify
1 parent 5e12ea1 commit 7f9a388

File tree

2 files changed

+48
-107
lines changed

2 files changed

+48
-107
lines changed

demo-script-tag-escape/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"version": "1.0.0",
44
"type": "module",
55
"scripts": {
6+
"build": "npm run build:rolldown && npm run build:tsdown",
67
"build:rolldown": "rolldown --config rolldown.config.js",
78
"build:tsdown": "tsdown",
8-
"test": "node test.js"
9+
"test": "npm run build && node test.js"
910
},
1011
"devDependencies": {
11-
"rolldown": "1.0.0-beta.45",
12+
"rolldown": "1.0.0-beta.1",
1213
"tsdown": "^0.15.2"
1314
}
1415
}

demo-script-tag-escape/test.js

Lines changed: 45 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -23,117 +23,57 @@ if (!fs.existsSync(tsdownPath)) {
2323
const rolldownCode = fs.readFileSync(rolldownPath, 'utf-8');
2424
const tsdownCode = fs.readFileSync(tsdownPath, 'utf-8');
2525

26-
console.log('📦 ROLLDOWN BUNDLED CODE (excerpt):');
26+
console.log('📊 SUMMARY: ESCAPING DETECTION IN BUNDLED CODE');
2727
console.log('─'.repeat(80));
28-
// Find script tags in the rolldown output
29-
const rolldownScriptMatches = [
30-
...rolldownCode.matchAll(/<script[^>]*>[\s\S]*?<\/script>/g),
31-
...rolldownCode.matchAll(/<\\\/script>/g)
32-
];
33-
if (rolldownScriptMatches.length > 0) {
34-
const match = rolldownScriptMatches[0][0];
35-
console.log(match.substring(0, 150) + (match.length > 150 ? '...' : ''));
36-
} else {
37-
// Show a snippet around dedent or script tag
38-
const scriptIndex = rolldownCode.indexOf('</script>');
39-
const escapedIndex = rolldownCode.indexOf('<\\/script>');
40-
const searchIndex = scriptIndex !== -1 ? scriptIndex : escapedIndex !== -1 ? escapedIndex : -1;
41-
if (searchIndex !== -1) {
42-
const snippet = rolldownCode.substring(Math.max(0, searchIndex - 50), searchIndex + 20);
43-
console.log(snippet);
28+
console.log('');
29+
30+
// More precise check: look for escaped script tags in specific functions
31+
const checkEscaping = (code, isLiteral) => {
32+
if (isLiteral) {
33+
// Find testTaggedTemplateLiteral function - look for dedent` followed by escaped script tag
34+
const funcStart = code.indexOf('function testTaggedTemplateLiteral()');
35+
if (funcStart === -1) return '✅ No escaping detected';
36+
const funcEnd = code.indexOf('function testFunctionCall()', funcStart);
37+
const funcCode =
38+
funcEnd !== -1 ? code.substring(funcStart, funcEnd) : code.substring(funcStart);
39+
40+
// Check if it uses dedent` (tagged template) and has escaped script tag
41+
const usesTagged = funcCode.includes('dedent`');
42+
const hasEscaped = funcCode.includes('<\\/script>');
43+
return usesTagged && hasEscaped ? '❌ found <\\/script>' : '✅ No escaping detected';
4444
} else {
45-
console.log('No script tag found');
45+
// Find testFunctionCall function - look for dedent( followed by script tag
46+
const funcStart = code.indexOf('function testFunctionCall()');
47+
if (funcStart === -1) return '✅ No escaping detected';
48+
const funcEnd = code.indexOf('const taggedResult', funcStart);
49+
const funcCode =
50+
funcEnd !== -1
51+
? code.substring(funcStart, funcEnd)
52+
: code.substring(funcStart, funcStart + 200);
53+
54+
// Check if it uses dedent( (function call) and has escaped script tag
55+
// Function call syntax should NOT have escaping since it's a regular string argument
56+
const usesFunction = funcCode.includes('dedent(');
57+
const hasEscaped = funcCode.includes('<\\/script>');
58+
59+
// Note: Some bundlers may still escape even in function calls, but the issue
60+
// is specifically with tagged template literals
61+
if (!usesFunction) return '✅ No escaping detected';
62+
return hasEscaped ? '❌ found <\\/script>' : '✅ No escaping detected';
4663
}
47-
}
48-
49-
console.log('\n');
50-
51-
console.log('📦 TSDOWN BUNDLED CODE (excerpt):');
52-
console.log('─'.repeat(80));
53-
// Find script tags in the tsdown output
54-
const tsdownScriptMatches = [
55-
...tsdownCode.matchAll(/<script[^>]*>[\s\S]*?<\/script>/g),
56-
...tsdownCode.matchAll(/<\\\/script>/g)
57-
];
58-
if (tsdownScriptMatches.length > 0) {
59-
const match = tsdownScriptMatches[0][0];
60-
console.log(match.substring(0, 150) + (match.length > 150 ? '...' : ''));
61-
} else {
62-
// Show a snippet around script tag
63-
const scriptIndex = tsdownCode.indexOf('</script>');
64-
const escapedIndex = tsdownCode.indexOf('<\\/script>');
65-
const searchIndex = scriptIndex !== -1 ? scriptIndex : escapedIndex !== -1 ? escapedIndex : -1;
66-
if (searchIndex !== -1) {
67-
const snippet = tsdownCode.substring(Math.max(0, searchIndex - 50), searchIndex + 20);
68-
console.log(snippet);
69-
} else {
70-
console.log('No script tag found');
71-
}
72-
}
73-
74-
console.log('\n');
75-
76-
// Check for escaped script tags
77-
console.log('🔍 CHECKING FOR ESCAPED SCRIPT TAGS IN BUNDLED CODE:');
78-
console.log('─'.repeat(80));
64+
};
7965

80-
const rolldownHasEscaped =
81-
rolldownCode.includes('<\\/script>') || rolldownCode.includes('</script>'.replace('/', '\\/'));
82-
const tsdownHasEscaped =
83-
tsdownCode.includes('<\\/script>') || tsdownCode.includes('</script>'.replace('/', '\\/'));
66+
console.log(' │ Literal (dedent`...`) │ Function (dedent(...))');
67+
console.log('────────────┼───────────────────────┼────────────────────────');
68+
const rolldownLiteral = checkEscaping(rolldownCode, true);
69+
const rolldownFunction = checkEscaping(rolldownCode, false);
70+
const tsdownLiteral = checkEscaping(tsdownCode, true);
71+
const tsdownFunction = checkEscaping(tsdownCode, false);
8472

85-
console.log(
86-
`Rolldown: ${rolldownHasEscaped ? '❌ HAS ESCAPED </script> (found <\\/script>)' : '✅ No escaping detected'}`
87-
);
88-
console.log(
89-
`Tsdown: ${tsdownHasEscaped ? '❌ HAS ESCAPED </script> (found <\\/script>)' : '✅ No escaping detected'}`
90-
);
91-
92-
console.log('\n');
93-
94-
// Try to run the actual code
95-
console.log('🚀 RUNTIME BEHAVIOR:');
96-
console.log('─'.repeat(80));
97-
98-
try {
99-
const rolldownModule = await import(path.join(__dirname, 'dist-rolldown', 'index.js'));
100-
const tsdownModule = await import(path.join(__dirname, 'dist-tsdown', 'index.js'));
101-
102-
console.log(
103-
'\nRolldown taggedResult:',
104-
rolldownModule.taggedResult.includes('<\\/script>') ? '❌ ESCAPED' : '✅ Not escaped'
105-
);
106-
console.log(
107-
'Rolldown functionResult:',
108-
rolldownModule.functionResult.includes('<\\/script>') ? '❌ ESCAPED' : '✅ Not escaped'
109-
);
110-
console.log(
111-
'\nTsdown taggedResult:',
112-
tsdownModule.taggedResult.includes('<\\/script>') ? '❌ ESCAPED' : '✅ Not escaped'
113-
);
114-
console.log(
115-
'Tsdown functionResult:',
116-
tsdownModule.functionResult.includes('<\\/script>') ? '❌ ESCAPED' : '✅ Not escaped'
117-
);
118-
119-
if (
120-
rolldownModule.taggedResult.includes('<\\/script>') ||
121-
tsdownModule.taggedResult.includes('<\\/script>')
122-
) {
123-
console.log('\n💡 DEMONSTRATION: Tagged template literal syntax causes escaping!');
124-
}
125-
if (
126-
!rolldownModule.functionResult.includes('<\\/script>') &&
127-
!tsdownModule.functionResult.includes('<\\/script>')
128-
) {
129-
console.log('✅ Function call syntax prevents escaping!');
130-
}
131-
} catch (e) {
132-
console.log('Could not execute built code:', e.message);
133-
console.log('(This is okay - we can still see the difference in the bundled code above)');
134-
}
73+
console.log(`Rolldown │ ${rolldownLiteral.padEnd(23)}${rolldownFunction}`);
74+
console.log(`Tsdown │ ${tsdownLiteral.padEnd(23)}${tsdownFunction}`);
75+
console.log('');
13576

136-
console.log('\n');
13777
console.log('💡 CONCLUSION:');
13878
console.log('─'.repeat(80));
13979
console.log(

0 commit comments

Comments
 (0)