chore(es/minifier): Improve scripts (#4339)

This commit is contained in:
Donny/강동윤 2022-04-17 19:53:47 +09:00 committed by GitHub
parent 5f34b0bd92
commit 2563c7f8ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 124 additions and 3 deletions

View File

@ -0,0 +1,85 @@
//! This example applies only AST compressor.
//!
//! This is used along with creduce
#![deny(warnings)]
extern crate swc_node_base;
use std::{env::args, fs, path::Path};
use swc_common::{sync::Lrc, Mark, SourceMap};
use swc_ecma_codegen::text_writer::JsWriter;
use swc_ecma_minifier::{
optimize,
option::{ExtraOptions, MinifyOptions},
};
use swc_ecma_parser::parse_file_as_module;
use swc_ecma_transforms_base::{fixer::fixer, resolver::resolver_with_mark};
use swc_ecma_visit::FoldWith;
fn main() {
let file = args().nth(1).expect("should provide a path to file");
eprintln!("File: {}", file);
testing::run_test2(false, |cm, handler| {
let fm = cm.load_file(Path::new(&file)).expect("failed to load file");
let top_level_mark = Mark::fresh(Mark::root());
let program = parse_file_as_module(
&fm,
Default::default(),
Default::default(),
None,
&mut vec![],
)
.map_err(|err| {
err.into_diagnostic(&handler).emit();
})
.map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark)))
.unwrap();
let output = optimize(
program,
cm.clone(),
None,
None,
&MinifyOptions {
compress: Some(Default::default()),
mangle: None,
..Default::default()
},
&ExtraOptions { top_level_mark },
);
let output = output.fold_with(&mut fixer(None));
let code = print(cm, &[output], true);
fs::write("output.js", code.as_bytes()).expect("failed to write output");
Ok(())
})
.unwrap();
}
fn print<N: swc_ecma_codegen::Node>(cm: Lrc<SourceMap>, nodes: &[N], minify: bool) -> String {
let mut buf = vec![];
{
let mut emitter = swc_ecma_codegen::Emitter {
cfg: swc_ecma_codegen::Config { minify },
cm: cm.clone(),
comments: None,
wr: Box::new(JsWriter::new(cm, "\n", &mut buf, None)),
};
for n in nodes {
n.emit_with(&mut emitter).unwrap();
}
}
String::from_utf8(buf).unwrap()
}

View File

@ -0,0 +1,35 @@
#!/usr/bin/env bash
#
# Usage:
#
# From your clone of next.js, you can invoke this script like
#
# /absolute/path/to/this/script.sh examples/foo
#
# This script will
#
# - build native binary (`next-swc`)
# - install dependnecies using `yarn``
# - remove some dependencies (`next`, `react`, `react-dom`)
# - yarn next build examples/foo
# - yarn next start examples/foo
set -eu
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
dir="$1"
# Ensure that next-swc is up to date
(cd ./packages/next-swc && yarn build-native)
# Install dependencies
(cd $dir && yarn)
# Remove some packages
(cd $dir && rm -rf node_modules/react)
(cd $dir && rm -rf node_modules/next)
(cd $dir && rm -rf node_modules/react-dom)
# Build and start
yarn next build $dir
yarn next start $dir

View File

@ -14,7 +14,7 @@ echo "Reducing $1"
ls -al $1
# Build swc minifier
export MINIFY=$(cargo profile bin-path --release --example minifier)
export MINIFY=$(cargo profile bin-path --release --example compress)
wd="$(mktemp -d)"
echo "Using $MINIFY; dir = $wd"
@ -23,9 +23,9 @@ cp $1 "$wd/input.js"
dir="$(dirname $1)"
# Verify that we can run `creduce`
(cd $wd && $SCRIPT_DIR/_/reduce/compare.sh)
(cd $wd && "$SCRIPT_DIR/../_/reduce/compare.sh")
(cd $wd && creduce "$SCRIPT_DIR/_/reduce/compare.sh" "$wd/input.js")
(cd $wd && creduce "$SCRIPT_DIR/../_/reduce/compare.sh" "$wd/input.js")
REDUCED_SIZE=$(wc -c <"$wd/input.js")
hash=$(sha1sum < "$wd/input.js")

View File

@ -35,6 +35,7 @@
"constness",
"consts",
"corejs",
"creduce",
"ctxt",
"ctxts",
"declarators",