Skip to content

Commit 4e241d9

Browse files
committed
Use build script
1 parent f3b9a4a commit 4e241d9

File tree

7 files changed

+46
-46
lines changed

7 files changed

+46
-46
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ resolver = "2"
138138
wasmer-wasix = { version = "0.600.0", default-features = false }
139139
wide = "0.7.32"
140140
# precomputed-map = "0.1"
141-
precomputed-map = { path = "../../precomputed-map" }
141+
precomputed-map = { git = "https://github.com/quininer/precomputed-map" }
142142
foldhash = "0.1"
143143

144144
[profile.release]

crates/swc_ecma_preset_env/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ swc_ecma_transforms = { version = "23.0.0", path = "../swc_ecma_transforms", fea
4343
swc_ecma_utils = { version = "18.0.2", path = "../swc_ecma_utils" }
4444
swc_ecma_visit = { version = "13.0.0", path = "../swc_ecma_visit" }
4545

46+
[build-dependencies]
47+
anyhow = { workspace = true }
48+
serde_json = { workspace = true }
49+
precomputed-map = { workspace = true, features = ["builder"] }
50+
foldhash = { workspace = true }
51+
4652
[dev-dependencies]
4753
codspeed-criterion-compat = { workspace = true }
4854
criterion = { workspace = true }

xtask/src/es/codegen.rs renamed to crates/swc_ecma_preset_env/build.rs

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
use std::fs;
2-
use std::io::Write;
2+
use std::io::{ self, Write };
33
use std::path::Path;
4-
use std::collections::HashMap;
5-
use clap::Args;
4+
use std::collections::{ HashMap, BTreeMap };
65
use anyhow::Context;
76

8-
#[derive(Debug, Args)]
9-
pub(super) struct CodegenCmd {}
10-
11-
impl CodegenCmd {
12-
pub fn run(self) -> anyhow::Result<()> {
13-
let dir = Path::new(env!("CARGO_MANIFEST_DIR"));
14-
let dir = dir.parent().unwrap();
15-
16-
es_preset_env_corejs3_entry(dir)
17-
}
7+
fn main() -> anyhow::Result<()> {
8+
es_preset_env_corejs3_entry()
189
}
1910

20-
fn es_preset_env_corejs3_entry(dir: &Path) -> anyhow::Result<()> {
21-
use std::collections::BTreeMap;
11+
fn es_preset_env_corejs3_entry() -> anyhow::Result<()> {
12+
const SEED: u64 = 16416001479773392852;
2213

23-
let crate_dir = dir.join("crates/swc_ecma_preset_env/");
14+
let crate_dir = std::env::var("CARGO_MANIFEST_DIR")?;
15+
let crate_dir = Path::new(&crate_dir);
16+
17+
let out_dir = std::env::var("OUT_DIR").unwrap();
18+
let out_dir = Path::new(&out_dir);
19+
let out_dir = out_dir.join("corejs3_entries");
20+
21+
22+
let entry_path = crate_dir.join("data/core-js-compat/entries.json");
23+
println!("cargo::rerun-if-changed={}", entry_path.display());
2424

25-
let entry_data = fs::read_to_string(crate_dir.join("data/core-js-compat/entries.json"))?;
25+
let entry_data = fs::read_to_string(entry_path)?;
2626
let entry_data: BTreeMap<&str, Vec<&str>> = serde_json::from_str(&entry_data)
2727
.context("failed to parse entries.json from core js 3")?;
2828
let (keys, values): (Vec<_>, Vec<_>) = entry_data.into_iter().unzip();
@@ -32,6 +32,7 @@ fn es_preset_env_corejs3_entry(dir: &Path) -> anyhow::Result<()> {
3232
let mut values_index = Vec::new();
3333
for list in values {
3434
let start: u32 = values_strid.len().try_into().unwrap();
35+
3536
for s in list {
3637
values_strid.push(strpool.insert(s));
3738
}
@@ -40,7 +41,7 @@ fn es_preset_env_corejs3_entry(dir: &Path) -> anyhow::Result<()> {
4041
}
4142

4243
let mapout = precomputed_map::builder::MapBuilder::<&str>::new()
43-
.set_seed(16416001479773392852)
44+
.set_seed(SEED)
4445
.set_hash(&|seed, &v| {
4546
use std::hash::{ Hash, Hasher };
4647

@@ -52,32 +53,30 @@ fn es_preset_env_corejs3_entry(dir: &Path) -> anyhow::Result<()> {
5253
seed + c
5354
})
5455
.build(&keys)?;
55-
println!("seed: {:?}", mapout.seed());
56+
57+
if let Some(seed) = mapout.seed() {
58+
if seed != SEED {
59+
println!("cargo::warning=The seed has changed, please update the seed to {} for faster builds", seed);
60+
}
61+
}
5662

5763
// clean file
5864
{
59-
for entry in fs::read_dir(crate_dir.join("src/generated")).ok().into_iter().flatten() {
60-
let entry = entry?;
61-
let path = entry.path();
62-
63-
if entry.file_type()?.is_file()
64-
&& path.file_name()
65-
.and_then(|name| name.to_str())
66-
.filter(|name| !name.starts_with('.'))
67-
.is_some()
68-
{
69-
fs::remove_file(entry.path())?;
70-
}
71-
}
65+
fs::remove_dir_all(&out_dir)
66+
.or_else(|err| match err.kind() {
67+
io::ErrorKind::NotFound => Ok(()),
68+
_ => Err(err)
69+
})?;
70+
fs::create_dir(&out_dir)?;
7271
}
7372

7473
let mut u8seq = precomputed_map::builder::U8SeqWriter::new(
7574
"PrecomputedU8Seq".into(),
76-
crate_dir.join("src/generated/corejs3_entries.u8")
75+
out_dir.join("u8.bin")
7776
);
7877
let mut u32seq = precomputed_map::builder::U32SeqWriter::new(
7978
"PrecomputedU32Seq".into(),
80-
crate_dir.join("src/generated/corejs3_entries.u32")
79+
out_dir.join("u32.bin")
8180
);
8281

8382
let mut builder = precomputed_map::builder::CodeBuilder::new(
@@ -91,15 +90,15 @@ fn es_preset_env_corejs3_entry(dir: &Path) -> anyhow::Result<()> {
9190
builder.create_u32_seq("EntryValuesStringId".into(), values_strid.iter().copied())?;
9291
mapout.create_map("ENTRY_INDEX".into(), k, &mut builder)?;
9392

94-
let mut codeout = fs::File::create(crate_dir.join("src/generated/corejs3_entries.rs"))?;
93+
let mut codeout = fs::File::create(out_dir.join("lib.rs"))?;
9594
builder.write_to(&mut codeout)?;
9695
u8seq.write_to(&mut codeout)?;
9796
u32seq.write_to(&mut codeout)?;
9897

99-
fs::write(crate_dir.join("src/generated/corejs3_entries.strpool"), strpool.pool.as_bytes())?;
98+
fs::write(out_dir.join("str.bin"), strpool.pool.as_bytes())?;
10099

101100
writeln!(codeout,
102-
"static ENTRY_VALUES_STRING_STORE: &str = include_str!(\"corejs3_entries.strpool\");
101+
"static ENTRY_VALUES_STRING_STORE: &str = include_str!(\"str.bin\");
103102
static ENTRY_VALUES_LIST: &[Range<u32>] = &["
104103
)?;
105104
for range in mapout.reorder(&values_index) {

crates/swc_ecma_preset_env/src/corejs3/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::util::SwcFold;
1515

1616
use super::{compat::DATA as CORE_JS_COMPAT_DATA, data::MODULES_BY_VERSION};
1717

18-
include!("../generated/corejs3_entries.rs");
18+
include!(concat!(env!("OUT_DIR"), "/corejs3_entries/lib.rs"));
1919

2020
pub struct FeatureSet(Range<u32>);
2121

crates/swc_ecma_preset_env/src/generated/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

xtask/src/es/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ use anyhow::Result;
22
use clap::{Args, Subcommand};
33

44
use self::minifier::MinifierCmd;
5-
use self::codegen::CodegenCmd;
65

76
mod minifier;
8-
mod codegen;
97

108
/// Commands for ECMAScript crates.
119
#[derive(Debug, Args)]
@@ -18,13 +16,11 @@ impl EsCmd {
1816
pub fn run(self) -> Result<()> {
1917
match self.cmd {
2018
Cmd::Minifier(cmd) => cmd.run(),
21-
Cmd::Codegen(cmd) => cmd.run(),
2219
}
2320
}
2421
}
2522

2623
#[derive(Debug, Subcommand)]
2724
enum Cmd {
2825
Minifier(MinifierCmd),
29-
Codegen(CodegenCmd),
3026
}

0 commit comments

Comments
 (0)