Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix script inline with directRenderScript #10686

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use better checks
  • Loading branch information
bluwy committed Apr 5, 2024
commit 38e96535cfb26477faea2c9fe664b49a4562f273
18 changes: 15 additions & 3 deletions packages/astro/src/core/build/plugins/plugin-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@ export function vitePluginScripts(internals: BuildInternals): VitePlugin {
},

async generateBundle(_options, bundle) {
for (const [id, output] of Object.entries(bundle)) {
const outputs = Object.values(bundle);

// Track ids that are imported by chunks so we don't inline scripts that are imported
const importedIds = new Set<string>();
for (const output of outputs) {
if (output.type === 'chunk') {
for (const id of output.imports) {
importedIds.add(id);
}
}
}

for (const output of outputs) {
// Try to inline scripts that don't import anything as is within the inline limit
if (
output.type === 'chunk' &&
output.facadeModuleId &&
internals.discoveredScripts.has(output.facadeModuleId) &&
!importedIds.has(output.fileName) &&
output.imports.length === 0 &&
output.exports.length === 0 &&
output.dynamicImports.length === 0 &&
shouldInlineAsset(output.code, output.fileName, assetInlineLimit)
) {
internals.inlinedScripts.set(output.facadeModuleId, output.code.trim());
delete bundle[id];
delete bundle[output.fileName];
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="./script.ts"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<script>
import './script.ts';
console.log('shared-script B');
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('shared-scripts');

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
import A from '../components/shared-scripts/A.astro'
import B from '../components/shared-scripts/B.astro'
---

<A />
<B />
7 changes: 4 additions & 3 deletions packages/astro/test/hoisted-imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ describe('Hoisted Imports', () => {
assert.ok(scripts[0].attribs.src);
});

it('does not inline if script ihas exports', async () => {
const html = await fixture.readFile('/no-inline-if-exports/index.html');
it('does not inline if script it has shared chunks', async () => {
const html = await fixture.readFile('/no-inline-if-shared/index.html');
const $ = cheerio.load(html);
const scripts = $('script');
assert.equal(scripts.length, 1);
assert.equal(scripts.length, 2);
assert.ok(scripts[0].attribs.src);
assert.ok(scripts[1].attribs.src);
});
});
});
Loading