Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ opt-level = 3

[profile.test.package.pretty_assertions]
opt-level = 3

[patch.crates-io]
string_cache = { git = 'https://github.com/kdy1/string-cache', branch = 'drop' }
67 changes: 67 additions & 0 deletions crates/dbg-swc/src/extra.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{path::PathBuf, sync::Arc};

use anyhow::{anyhow, Result};
use clap::{Args, Subcommand};
use swc_atoms::JsWord;
use swc_common::SourceMap;
use swc_ecma_ast::{EsVersion, Ident};
use swc_ecma_parser::{parse_file_as_program, TsConfig};
use swc_ecma_visit::{Visit, VisitWith};

#[derive(Debug, Subcommand)]
pub enum ExtraCommand {
PrintAtoms(PrintAtomsComamnd),
}

impl ExtraCommand {
pub fn run(self, cm: Arc<SourceMap>) -> Result<()> {
match self {
ExtraCommand::PrintAtoms(cmd) => cmd.run(cm),
}
}
}

#[derive(Debug, Args)]
pub struct PrintAtomsComamnd {
pub path: PathBuf,
}

impl PrintAtomsComamnd {
pub fn run(self, cm: Arc<SourceMap>) -> Result<()> {
let fm = cm.load_file(&self.path)?;

let p = parse_file_as_program(
&fm,
swc_ecma_parser::Syntax::Typescript(TsConfig {
dts: true,
..Default::default()
}),
EsVersion::latest(),
None,
&mut vec![],
)
.map_err(|err| anyhow!("failed to parse: {:?}", err))?;

let mut v = IdentCollector::default();
p.visit_with(&mut v);
v.ident.sort();
v.ident.dedup();

for i in v.ident {
println!("{}", i);
}

Ok(())
}
}

#[derive(Default)]
struct IdentCollector {
ident: Vec<JsWord>,
}

impl Visit for IdentCollector {
fn visit_ident(&mut self, ident: &Ident) {
self.ident.push(ident.sym.clone());
}
}
6 changes: 6 additions & 0 deletions crates/dbg-swc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ use tracing_subscriber::EnvFilter;

use self::{
bundle::BundleCommand,
extra::ExtraCommand,
minify::MinifyCommand,
test::TestCommand,
util::{minifier::get_esbuild_output, print_js},
};
use crate::util::minifier::{get_minified, get_terser_output};

mod bundle;
mod extra;
mod minify;
mod test;
mod util;
Expand All @@ -42,6 +44,9 @@ enum Cmd {
Minify(MinifyCommand),
#[clap(subcommand)]
Test(TestCommand),

#[clap(subcommand)]
X(ExtraCommand),
}

fn init() -> Result<()> {
Expand Down Expand Up @@ -150,6 +155,7 @@ fn main() -> Result<()> {
Cmd::Bundle(_) => todo!(),
Cmd::Minify(cmd) => cmd.run(cm),
Cmd::Test(cmd) => cmd.run(cm),
Cmd::X(cmd) => cmd.run(cm),
})
})
},
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_atoms/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{env, path::Path};

fn main() {
print!("cargo:rerun-if-changed=build.rs");
print!("cargo:rerun-if-changed=words.txt");

let strs = include_str!("words.txt")
.lines()
.map(|l| l.trim())
Expand Down
10 changes: 10 additions & 0 deletions crates/swc_atoms/scripts/add-all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash
set -eu

cargo build -p dbg-swc
BINARY="$(cargo metadata --format-version 1 | jq -r '.target_directory')/debug/dbg-swc"
echo "Binary:$BINARY"
# TypeScript libraries has lots of commonly used identifiers.
ls ../../node_modules/typescript/lib/lib.*.d.ts | xargs -L 1 -I {} $BINARY x print-atoms {} | sed -r '/^.{,3}$/d' >> words.txt || true

./scripts/sort.sh
Loading