forked from withastro/astro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supports hoisted scripts in the static build (withastro#2414)
* Supports hoisted scripts in the static build * Adds a changeset * Update packages/astro/src/core/build/internal.ts Co-authored-by: Evan Boehs <[email protected]> * Update based on feedback * Fix lint * Fix getting hoist script for Windows * Try with the pre compiler * use compiler 0.8.2 * update compiler version * update yarn.lock Co-authored-by: Evan Boehs <[email protected]>
- Loading branch information
Showing
18 changed files
with
227 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Adds support for hoisted scripts to the static build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<div id="external-hoist"></div> | ||
<script type="module" hoist src="/src/scripts/external-hoist"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<script type="module" hoist> | ||
import { h, render } from 'preact'; | ||
|
||
|
||
const mount = document.querySelector('#inline-hoist'); | ||
|
||
function App() { | ||
return h('strong', null, 'Hello again'); | ||
} | ||
|
||
render(h(App), mount); | ||
</script> | ||
<div id="inline-hoist"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const el = document.querySelector('#external-hoist'); | ||
el.textContent = `This was loaded externally`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/astro/src/core/build/vite-plugin-hoisted-scripts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import type { Plugin as VitePlugin } from '../vite'; | ||
import type { BuildInternals } from '../../core/build/internal.js'; | ||
|
||
function virtualHoistedEntry(id: string) { | ||
return id.endsWith('.astro/hoisted.js') || id.endsWith('.md/hoisted.js'); | ||
} | ||
|
||
export function vitePluginHoistedScripts(internals: BuildInternals): VitePlugin { | ||
return { | ||
name: '@astro/rollup-plugin-astro-hoisted-scripts', | ||
|
||
resolveId(id) { | ||
if(virtualHoistedEntry(id)) { | ||
return id; | ||
} | ||
}, | ||
|
||
load(id) { | ||
if(virtualHoistedEntry(id)) { | ||
let code = ''; | ||
for(let path of internals.hoistedScriptIdToHoistedMap.get(id)!) { | ||
code += `import "${path}";` | ||
} | ||
return { | ||
code | ||
}; | ||
} | ||
return void 0; | ||
}, | ||
|
||
async generateBundle(_options, bundle) { | ||
// Find all page entry points and create a map of the entry point to the hashed hoisted script. | ||
// This is used when we render so that we can add the script to the head. | ||
for(const [id, output] of Object.entries(bundle)) { | ||
if(output.type === 'chunk' && output.facadeModuleId && virtualHoistedEntry(output.facadeModuleId)) { | ||
const facadeId = output.facadeModuleId!; | ||
const filename = facadeId.slice(0, facadeId.length - "/hoisted.js".length); | ||
internals.facadeIdToHoistedEntryMap.set(filename, id); | ||
} | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.