Skip to content

Commit bf22ac6

Browse files
authored
fix: script minifiy match regex (#7918)
1 parent f8671d6 commit bf22ac6

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

.changeset/smart-showers-think.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/runtime': patch
3+
---
4+
5+
fix: use minify regex
6+
fix: 用最小匹配来替换

packages/runtime/plugin-runtime/src/document/cli/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export const documentPlugin = (): CliPluginFuture<AppTools<'shared'>> => ({
251251

252252
html = html.replace(
253253
new RegExp(
254-
`${DOCUMENT_SCRIPT_PLACEHOLDER_START}${DOCUMENT_SCRIPT_ATTRIBUTES_START}(.*)${DOCUMENT_SCRIPT_ATTRIBUTES_END}(.*?)${DOCUMENT_SCRIPT_PLACEHOLDER_END}`,
254+
`${DOCUMENT_SCRIPT_PLACEHOLDER_START}${DOCUMENT_SCRIPT_ATTRIBUTES_START}(.*?)${DOCUMENT_SCRIPT_ATTRIBUTES_END}(.*?)${DOCUMENT_SCRIPT_PLACEHOLDER_END}`,
255255
'g',
256256
),
257257
(_scriptStr, $1, $2) =>

tests/integration/app-document/src/sub/Document.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ export default function Document(): React.ReactElement {
5555
console.log('this is a IIFE function');
5656
}}
5757
/>
58+
<Script
59+
defer
60+
async
61+
id="script-has-id2"
62+
content={() => {
63+
console.log('this is another IIFE function');
64+
}}
65+
/>
5866
</Head>
5967
<Body {...{ dir: 'ltr' }}>
6068
<Root {...{ class: 'root' }}>

tests/integration/app-document/tests/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ describe('test dev and build', () => {
172172
htmlWithDoc.includes('console.log("this is a IIFE function")'),
173173
).toBe(true);
174174
});
175+
176+
test('should not break Script origin script properties', async () => {
177+
const htmlWithDoc = fs.readFileSync(
178+
path.join(appDir, 'dist', 'html/sub/index.html'),
179+
'utf-8',
180+
);
181+
182+
expect(
183+
htmlWithDoc.includes('<script defer="" async="" id="script-has-id2">'),
184+
).toBe(true);
185+
// IIFE should worked
186+
expect(
187+
htmlWithDoc.includes('console.log("this is another IIFE function")'),
188+
).toBe(true);
189+
});
175190
});
176191

177192
describe('test dev', () => {

0 commit comments

Comments
 (0)