Skip to content

Commit

Permalink
Migrate index to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed May 26, 2024
1 parent fedf88f commit bd7d3ab
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 28 deletions.
28 changes: 19 additions & 9 deletions docs/static/js/update-index.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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");
});

Expand Down
20 changes: 10 additions & 10 deletions docs/templates/update.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ <h1>Notice</h1>
e.data.hasOwnProperty("frameHeight") && (document.getElementById("footer").style.height = `${e.data.frameHeight}px`)
}
</script>
<script src="/crates/index.js"></script>
<script src="/index/commands.js"></script>
<script src="/index/books.js"></script>
<script src="/index/labels.js"></script>
<script src="/index/rfcs.js"></script>
<script src="/index/lints.js"></script>
<script src="/index/caniuse.js"></script>
<script src="/index/rustc.js"></script>
<script src="/index/targets.js"></script>
<script src="/js/update-index.js"></script>
<script src="/crates/index.js" type="module"></script>
<script src="/index/commands.js" type="module"></script>
<script src="/index/books.js" type="module"></script>
<script src="/index/labels.js" type="module"></script>
<script src="/index/rfcs.js" type="module"></script>
<script src="/index/lints.js" type="module"></script>
<script src="/index/caniuse.js" type="module"></script>
<script src="/index/rustc.js" type="module"></script>
<script src="/index/targets.js" type="module"></script>
<script src="/js/update-index.js" type="module"></script>
{% endblock footer %}
2 changes: 1 addition & 1 deletion rust/src/tasks/books.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/caniuse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/tasks/crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/labels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
),
)?;
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/rfcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tasks/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down

0 comments on commit bd7d3ab

Please sign in to comment.