Skip to content

Commit fa0f593

Browse files
authored
Fix script inline with directRenderScript (#10686)
1 parent 903ed31 commit fa0f593

File tree

7 files changed

+43
-3
lines changed

7 files changed

+43
-3
lines changed

.changeset/sour-chairs-compare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": patch
3+
---
4+
5+
Prevents inlining scripts if used by other chunks when using the `experimental.directRenderScript` option

packages/astro/src/core/build/plugins/plugin-scripts.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,31 @@ export function vitePluginScripts(internals: BuildInternals): VitePlugin {
1717
},
1818

1919
async generateBundle(_options, bundle) {
20-
for (const [id, output] of Object.entries(bundle)) {
20+
const outputs = Object.values(bundle);
21+
22+
// Track ids that are imported by chunks so we don't inline scripts that are imported
23+
const importedIds = new Set<string>();
24+
for (const output of outputs) {
25+
if (output.type === 'chunk') {
26+
for (const id of output.imports) {
27+
importedIds.add(id);
28+
}
29+
}
30+
}
31+
32+
for (const output of outputs) {
2133
// Try to inline scripts that don't import anything as is within the inline limit
2234
if (
2335
output.type === 'chunk' &&
2436
output.facadeModuleId &&
2537
internals.discoveredScripts.has(output.facadeModuleId) &&
38+
!importedIds.has(output.fileName) &&
2639
output.imports.length === 0 &&
2740
output.dynamicImports.length === 0 &&
2841
shouldInlineAsset(output.code, output.fileName, assetInlineLimit)
2942
) {
3043
internals.inlinedScripts.set(output.facadeModuleId, output.code.trim());
31-
delete bundle[id];
44+
delete bundle[output.fileName];
3245
}
3346
}
3447
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="./script.ts"></script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
import './script.ts';
3+
console.log('shared-script B');
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('shared-scripts');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
import A from '../components/shared-scripts/A.astro'
3+
import B from '../components/shared-scripts/B.astro'
4+
---
5+
6+
<A />
7+
<B />

packages/astro/test/hoisted-imports.test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,21 @@ describe('Hoisted Imports', () => {
7171
assert.equal($('script').length, 1);
7272
});
7373

74-
it('inlines if script is larger than vite.assetInlineLimit: 100', async () => {
74+
it('does not inline if script is larger than vite.assetInlineLimit: 100', async () => {
7575
const html = await fixture.readFile('/no-inline/index.html');
7676
const $ = cheerio.load(html);
7777
const scripts = $('script');
7878
assert.equal(scripts.length, 1);
7979
assert.ok(scripts[0].attribs.src);
8080
});
81+
82+
it('does not inline if script it has shared chunks', async () => {
83+
const html = await fixture.readFile('/no-inline-if-shared/index.html');
84+
const $ = cheerio.load(html);
85+
const scripts = $('script');
86+
assert.equal(scripts.length, 2);
87+
assert.ok(scripts[0].attribs.src);
88+
assert.ok(scripts[1].attribs.src);
89+
});
8190
});
8291
});

0 commit comments

Comments
 (0)