From bd7d3abe027e320be1e4cd61abe192264c841913 Mon Sep 17 00:00:00 2001 From: Folyd Date: Sun, 26 May 2024 15:51:11 -0700 Subject: [PATCH] Migrate index to ESM --- docs/static/js/update-index.js | 28 +++++++++++++++++++--------- docs/templates/update.html | 20 ++++++++++---------- rust/src/tasks/books.rs | 2 +- rust/src/tasks/caniuse.rs | 2 +- rust/src/tasks/crates.rs | 4 ++-- rust/src/tasks/labels.rs | 2 +- rust/src/tasks/lints.rs | 2 +- rust/src/tasks/rfcs.rs | 2 +- rust/src/tasks/rustc.rs | 2 +- rust/src/tasks/targets.rs | 2 +- 10 files changed, 38 insertions(+), 28 deletions(-) diff --git a/docs/static/js/update-index.js b/docs/static/js/update-index.js index 6f382892..3e295196 100644 --- a/docs/static/js/update-index.js +++ b/docs/static/js/update-index.js @@ -1,3 +1,13 @@ +import caniuseIndex from "./index/caniuse.js"; +import booksIndex from "./index/books.js"; +import commandsIndex from "./index/commands.js"; +import labelsIndex from "./index/labels.js"; +import lintsIndex from "./index/lints.js"; +import rfcsIndex from "./index/rfcs.js"; +import rustcIndex from "./index/rustc.js"; +import targetsIndex from "./index/targets.js"; +import { mapping, crateIndex } from "./crates/index.js"; + const indexList = document.querySelector(".index-list"); function updateIndex(target, index, extra) { @@ -22,31 +32,31 @@ function renderSuccessMessage(message) { } document.addEventListener("DOMContentLoaded", async () => { - updateIndex("book", window.booksIndex).then(() => { + updateIndex("book", booksIndex).then(() => { renderSuccessMessage("Book index"); }); - updateIndex("lint", window.lintsIndex).then(() => { + updateIndex("lint", lintsIndex).then(() => { renderSuccessMessage("Clippy lint index"); }); - updateIndex("caniuse", window.caniuseIndex).then(() => { + updateIndex("caniuse", caniuseIndex).then(() => { renderSuccessMessage("Caniuse index"); }); - updateIndex("label", window.labelsIndex).then(() => { + updateIndex("label", labelsIndex).then(() => { renderSuccessMessage("Github rust-lang/rust repository label index"); }); - updateIndex("rfc", window.rfcsIndex).then(() => { + updateIndex("rfc", rfcsIndex).then(() => { renderSuccessMessage("Rust RFC index"); }); - updateIndex("rustc", window.rustcIndex).then(() => { + updateIndex("rustc", rustcIndex).then(() => { renderSuccessMessage("`:rustc` command index"); }); - updateIndex("target", window.targetsIndex).then(() => { + updateIndex("target", targetsIndex).then(() => { renderSuccessMessage("`:target` command index"); }); - updateIndex("crate", window.crateIndex, { mapping: window.mapping }).then(() => { + updateIndex("crate", crateIndex, { mapping: mapping }).then(() => { renderSuccessMessage("Top 20K crate index"); }); - updateIndex("command", window.commandsIndex).then(() => { + updateIndex("command", commandsIndex).then(() => { renderSuccessMessage("Command index"); }); diff --git a/docs/templates/update.html b/docs/templates/update.html index 0117254f..29f82474 100644 --- a/docs/templates/update.html +++ b/docs/templates/update.html @@ -37,14 +37,14 @@

Notice

e.data.hasOwnProperty("frameHeight") && (document.getElementById("footer").style.height = `${e.data.frameHeight}px`) } - - - - - - - - - - + + + + + + + + + + {% endblock footer %} \ No newline at end of file diff --git a/rust/src/tasks/books.rs b/rust/src/tasks/books.rs index e2756747..bfe02681 100644 --- a/rust/src/tasks/books.rs +++ b/rust/src/tasks/books.rs @@ -155,7 +155,7 @@ impl BooksTask { Ok(result) => { let books: Vec<_> = result.into_iter().filter(|book| !book.is_empty()).collect(); let contents = format!( - "var N=null;var booksIndex={};", + "let N=null;const booksIndex={};export default booksIndex;", serde_json::to_string(&books)? ); let path = Path::new(&self.dest_path); diff --git a/rust/src/tasks/caniuse.rs b/rust/src/tasks/caniuse.rs index fd59fde2..d09a7b86 100644 --- a/rust/src/tasks/caniuse.rs +++ b/rust/src/tasks/caniuse.rs @@ -106,7 +106,7 @@ impl Task for CaniuseTask { let path = Path::new(&self.dest_path); fs::write( path, - format!("var caniuseIndex={};", serde_json::to_string(&feats)?), + format!("const caniuseIndex={};export default caniuseIndex;", serde_json::to_string(&feats)?), )?; Ok(()) } diff --git a/rust/src/tasks/crates.rs b/rust/src/tasks/crates.rs index b66b73b8..0a11a622 100644 --- a/rust/src/tasks/crates.rs +++ b/rust/src/tasks/crates.rs @@ -110,7 +110,7 @@ fn generate_javascript_crates_index(crates: &[Crate], minifier: &Minifier) -> St }) .collect(); let crate_index = format!( - "var crateIndex={};", + "const crateIndex={};export default crateIndex;", serde_json::to_string(&crates_map).unwrap() ); contents.push_str(&Minifier::minify_js(&crate_index)); @@ -175,7 +175,7 @@ impl Task for CratesTask { let minifier = Minifier::new(&frequency_words); let mapping = minifier.get_key_to_word_mapping(); let mut contents = format!( - "var mapping=JSON.parse('{}');", + "let mapping=JSON.parse('{}');", serde_json::to_string(&mapping)? ); contents.push_str(&generate_javascript_crates_index(&crates, &minifier)); diff --git a/rust/src/tasks/labels.rs b/rust/src/tasks/labels.rs index fc7bfb80..e3328f5a 100644 --- a/rust/src/tasks/labels.rs +++ b/rust/src/tasks/labels.rs @@ -69,7 +69,7 @@ impl LabelsTask { fs::write( Path::new(&self.dest_path), format!( - "var labelsIndex={};", + "var labelsIndex={};export default labelsIndex;", serde_json::to_string(&labels).unwrap() ), )?; diff --git a/rust/src/tasks/lints.rs b/rust/src/tasks/lints.rs index c5b2c7ff..8f0e110a 100644 --- a/rust/src/tasks/lints.rs +++ b/rust/src/tasks/lints.rs @@ -90,7 +90,7 @@ impl LintsTask { }) .collect(); - let contents = format!("var lintsIndex={};", serde_json::to_string(&lints)?); + let contents = format!("const lintsIndex={};export default lintsIndex;", serde_json::to_string(&lints)?); let path = Path::new(&self.dest_path); fs::write(path, Minifier::minify_js(&contents))?; println!("\nGenerate javascript lints index successful!"); diff --git a/rust/src/tasks/rfcs.rs b/rust/src/tasks/rfcs.rs index 77a18422..fec296bb 100644 --- a/rust/src/tasks/rfcs.rs +++ b/rust/src/tasks/rfcs.rs @@ -63,7 +63,7 @@ impl Task for RfcsTask { let path = Path::new(&self.dest_path); fs::write( path, - format!("var rfcsIndex={};", serde_json::to_string(&metadatas)?), + format!("const rfcsIndex={};export default rfcsIndex;", serde_json::to_string(&metadatas)?), )?; Ok(()) diff --git a/rust/src/tasks/rustc.rs b/rust/src/tasks/rustc.rs index 5b4744fc..4e6412ad 100644 --- a/rust/src/tasks/rustc.rs +++ b/rust/src/tasks/rustc.rs @@ -67,7 +67,7 @@ impl RustcTask { fs::write( Path::new(&self.dest_path), - format!("var rustcIndex={};", serde_json::to_string(&map).unwrap()), + format!("const rustcIndex={};export default rustcIndex;", serde_json::to_string(&map).unwrap()), )?; println!("\nGenerate rustc index successful!"); Ok(()) diff --git a/rust/src/tasks/targets.rs b/rust/src/tasks/targets.rs index 1f3f778d..d45fdada 100644 --- a/rust/src/tasks/targets.rs +++ b/rust/src/tasks/targets.rs @@ -118,7 +118,7 @@ impl TargetsTask { fs::write( Path::new(&self.dest_path), - format!("var targetsIndex={};", serde_json::to_string(&map).unwrap()), + format!("const targetsIndex={};export default targetsIndex;", serde_json::to_string(&map).unwrap()), )?; println!("\nGenerate targets index successful!"); Ok(())