Skip to content

Commit

Permalink
feat(vite-scanner): load script content from html (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Nov 15, 2023
1 parent 68b326c commit 3bcaabc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ ariadne = "0.3.0"
async-scoped = { version = "0.7.1" }
regex = "1.10.2"
once_cell = "1.18.0"
dashmap = "5.5.3"
1 change: 1 addition & 0 deletions crates/rolldown_plugin_vite_scanner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ async-trait = { workspace = true }
regex = { workspace = true }
once_cell = { workspace = true }
rustc-hash = { workspace = true }
dashmap = { workspace = true }
34 changes: 24 additions & 10 deletions crates/rolldown_plugin_vite_scanner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use dashmap::DashMap;
use once_cell::sync::Lazy;
use regex::Regex;
use rolldown::{
Expand All @@ -6,7 +7,7 @@ use rolldown::{
};
use rolldown_fs::FileSystem;
use std::{borrow::Cow, fmt::Debug, path::PathBuf};
use util::extract_html_module_scripts;
use util::{extract_html_module_scripts, VIRTUAL_MODULE_PREFIX};
mod util;
static HTTP_URL_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(https?:)?\/\/").expect("Init HTTP_URL_REGEX failed"));
Expand Down Expand Up @@ -34,11 +35,18 @@ static HTML_TYPE_REGEX: Lazy<Regex> = Lazy::new(|| {
pub struct ViteScannerPlugin<T: FileSystem + Default> {
pub entries: Vec<String>,
pub fs: T,
// Store the scripts extracted from HTML-like files
// Using `DashMap` because resolve_id is called in parallel
pub scripts: DashMap<String, String>,
}

impl<T: FileSystem + 'static + Default> Debug for ViteScannerPlugin<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ViteScannerPlugin").field("entries", &self.entries).field("fs", &"").finish()
f.debug_struct("ViteScannerPlugin")
.field("entries", &self.entries)
.field("fs", &"")
.field("scripts", &self.scripts)
.finish()
}
}

Expand All @@ -60,13 +68,9 @@ impl<T: FileSystem + 'static + Default> Plugin for ViteScannerPlugin<T> {
return Ok(Some(HookResolveIdOutput { id: (*source).to_string(), external: Some(true) }));
}

// local scripts (`<script>` in Svelte and `<script setup>` in Vue)
// resolve local scripts (`<script>` in Svelte and `<script setup>` in Vue)
if VIRTUAL_MODULE_REGEX.is_match(source) {
return Ok(Some(HookResolveIdOutput {
// strip prefix to get valid filesystem path so bundler can resolve imports in the file
id: source.replace("virtual-module:", ""),
external: None,
}));
return Ok(Some(HookResolveIdOutput { id: (*source).to_string(), external: None }));
}

// TODO bare imports: record and externalize
Expand Down Expand Up @@ -103,11 +107,21 @@ impl<T: FileSystem + 'static + Default> Plugin for ViteScannerPlugin<T> {
if HTML_TYPE_REGEX.is_match(id) {
let path = PathBuf::from(id);
let content = self.fs.read_to_string(&path)?;
// TODO store scripts
let (content, _) = extract_html_module_scripts(&content, &path);
let (content, scripts) = extract_html_module_scripts(&content, &path);
scripts.into_iter().for_each(|(key, value)| {
self.scripts.insert(key, value);
});
return Ok(Some(HookLoadOutput { code: content }));
}

// load local scripts (`<script>` in Svelte and `<script setup>` in Vue)
if VIRTUAL_MODULE_REGEX.is_match(id) {
let key = id.replace(VIRTUAL_MODULE_PREFIX, "");
if let Some(content) = self.scripts.get(&key) {
return Ok(Some(HookLoadOutput { code: content.to_string() }));
}
}

Ok(None)
}
}
2 changes: 1 addition & 1 deletion crates/rolldown_plugin_vite_scanner/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static IMPORTS_FROM_BLOCK_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"import([\w*{}\n\r\t, ]+from)?\s*([\w\d"'\.\/]*)"#)
.expect("Init IMPORTS_FROM_BLOCK_REGEX failed")
});
static VIRTUAL_MODULE_PREFIX: &str = "virtual-module:";
pub static VIRTUAL_MODULE_PREFIX: &str = "virtual-module:";

pub fn extract_html_module_scripts(
content: &str,
Expand Down

0 comments on commit 3bcaabc

Please sign in to comment.