spack: modules (#848)

spack:
 - Better es6 module detection
 - Recursive merging
 - Support for common js modules
 - Support for node js core modules
 - Inject helpers

swc_ecma_transforms:
 - Fix dce
This commit is contained in:
강동윤 2020-06-17 23:48:34 +09:00 committed by GitHub
parent 31020e46d8
commit 66d42adf7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 875 additions and 146 deletions

View File

@ -48,7 +48,7 @@ jobs:
- name: Install node dependencies
run: |
npm config set prefix ~/npm
npm i browserslist regenerator-runtime sourcemap-validator
npm i browserslist regenerator-runtime sourcemap-validator progress
npm i -g jest
# Ensure that all components all compilable.

View File

@ -56,6 +56,7 @@ impl Fold<VarDecl> for Dce<'_> {
Some(VarDeclarator {
span: decl.span.apply_mark(self.config.used_mark),
init: self.fold_in_marking_phase(decl.init),
name: self.fold_in_marking_phase(decl.name),
..decl
})
});

View File

@ -168,6 +168,11 @@ where
idx += 1;
item
});
if !self.changed {
items = items.move_map(|item| item.fold_with(self));
}
if !self.changed {
break;
}

View File

@ -420,3 +420,11 @@ function foo() {}
console.log(foo(), a.a(), a.foo());"
);
noop!(
spack_issue_007,
"
var load = function(){}
var { progress } = load();
console.log(progress);"
);

View File

@ -1,6 +1,6 @@
{
"name": "@swc/core",
"version": "1.2.3",
"version": "1.2.4",
"description": "Super-fast alternative for babel",
"main": "./index.js",
"author": "강동윤 <kdy1997.dev@gmail.com>",

View File

@ -21,6 +21,7 @@ swc_ecma_utils = { path = "../ecmascript/utils" }
swc = { path = "../" }
string_enum = { version = "0.3", path ="../macros/string_enum" }
regex = "1"
once_cell = "1"
serde = { version = "1", features = ["derive"] }
anyhow = "1"
crc = "1.8"

View File

@ -3,10 +3,11 @@ use crate::{
bundler::{export::Exports, load_transformed::Specifier},
Id, ModuleId,
};
use anyhow::Error;
use anyhow::{Context, Error};
use std::{
mem::take,
ops::{Deref, DerefMut},
sync::atomic::Ordering,
};
use swc_atoms::{js_word, JsWord};
use swc_common::{
@ -14,21 +15,27 @@ use swc_common::{
DUMMY_SP,
};
use swc_ecma_ast::*;
use swc_ecma_transforms::{hygiene, noop_fold_type};
use swc_ecma_utils::{find_ids, DestructuringFinder, StmtLike};
use swc_ecma_transforms::noop_fold_type;
use swc_ecma_utils::{
find_ids, prepend, private_ident, undefined, DestructuringFinder, ExprFactory, StmtLike,
};
impl Bundler<'_> {
/// Merge `targets` into `entry`.
pub(super) fn merge_modules(
&self,
entry: ModuleId,
targets: Vec<ModuleId>,
targets: &mut Vec<ModuleId>,
) -> Result<Module, Error> {
self.swc.run(|| {
let info = self.scope.get_module(entry).unwrap();
log::info!("Merge: {} => {:?}", info.fm.name, targets);
let mut entry: Module = (*info.module).clone();
if targets.is_empty() {
return Ok((*info.module).clone());
}
log::info!("Merge: {} <= {:?}", info.fm.name, targets);
// {
// let code = self
@ -47,6 +54,13 @@ impl Bundler<'_> {
for (src, specifiers) in &info.imports.specifiers {
if !targets.contains(&src.module_id) {
log::debug!(
"Not merging: not in target: ({}):{} <= ({}):{}",
info.id,
info.fm.name,
src.module_id,
src.src.value,
);
continue;
}
log::debug!("Merging: {} <= {}", info.fm.name, src.src.value);
@ -58,139 +72,292 @@ impl Bundler<'_> {
src.module_id
)
}
if src.is_unconditional {
if let Some(imported) = self.scope.get_module(src.module_id) {
let mut dep: Module = (*imported.module).clone();
info.helpers.extend(&imported.helpers);
//{
// let code = self
// .swc
// .print(
// &dep.clone().fold_with(&mut HygieneVisualizer),
// info.fm.clone(),
// false,
// false,
// )
// .unwrap()
// .code;
// In the case of
//
// println!("Dep before drop_unused:\n{}\n\n\n", code);
//}
// Tree-shaking
dep = self.drop_unused(imported.fm.clone(), dep, Some(&specifiers));
//{
// let code = self
// .swc
// .print(
// &dep.clone().fold_with(&mut HygieneVisualizer),
// info.fm.clone(),
// false,
// false,
// )
// .unwrap()
// .code;
// a <- b
// b <- c
//
// println!("Dep after drop_unused:\n{}\n\n\n", code);
//}
// we change it to
//
// a <- b + chunk(c)
//
let mut dep =
self.merge_modules(src.module_id, targets)
.with_context(|| {
format!(
"failed to merge: ({}):{} <= ({}):{}",
info.id, info.fm.name, src.module_id, src.src.value
)
})?;
targets.remove_item(&info.id);
if imported.is_es6 {
//{
// let code = self
// .swc
// .print(
// &dep.clone().fold_with(&mut HygieneVisualizer),
// info.fm.clone(),
// false,
// false,
// )
// .unwrap()
// .code;
//
// println!("Dep before drop_unused:\n{}\n\n\n", code);
//}
// Tree-shaking
dep = self.drop_unused(imported.fm.clone(), dep, Some(&specifiers));
//{
// let code = self
// .swc
// .print(
// &dep.clone().fold_with(&mut HygieneVisualizer),
// info.fm.clone(),
// false,
// false,
// )
// .unwrap()
// .code;
//
// println!("Dep after drop_unused:\n{}\n\n\n", code);
//}
if let Some(imports) = info
.imports
.specifiers
.iter()
.find(|(s, _)| s.module_id == imported.id)
.map(|v| &v.1)
{
dep = dep.fold_with(&mut ExportRenamer {
mark: imported.mark(),
_exports: &imported.exports,
imports: &imports,
extras: vec![],
});
}
dep = dep.fold_with(&mut Unexporter);
if !specifiers.is_empty() {
entry = entry.fold_with(&mut LocalMarker {
mark: imported.mark(),
specifiers: &specifiers,
excluded: vec![],
});
// // Note: this does not handle `export default
// foo`
// dep = dep.fold_with(&mut LocalMarker {
// mark: imported.mark(),
// specifiers: &imported.exports.items,
// });
}
dep = dep.fold_with(&mut GlobalMarker {
used_mark: self.used_mark,
module_mark: imported.mark(),
});
// {
// let code = self
// .swc
// .print(
// &dep.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("Dep:\n{}\n\n\n", code);
// }
// {
// let code = self
// .swc
// .print(
// &entry.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("@: Before merging:\n{}\n\n\n", code);
// }
// Replace import statement / require with module body
let mut injector = Es6ModuleInjector {
imported: dep.body.clone(),
src: src.src.clone(),
};
entry.body.visit_mut_with(&mut injector);
// {
// let code = self
// .swc
// .print(
// &entry.clone().fold_with(&mut
// HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("Merged:\n{}\n\n\n", code);
// }
if injector.imported.is_empty() {
continue;
}
}
if let Some(imports) = info
.imports
.specifiers
.iter()
.find(|(s, _)| s.module_id == imported.id)
.map(|v| &v.1)
{
dep = dep.fold_with(&mut ExportRenamer {
mark: imported.mark(),
_exports: &imported.exports,
imports: &imports,
extras: vec![],
});
}
// common js module is transpiled as
//
// Src:
// const foo = require('foo');
//
// Output:
//
// const load = __spack__require.bind(void 0, function(module,
// exports){
// // ... body of foo
// }); const foo = load();
//
// As usual, this behavior depends on hygiene.
dep = dep.fold_with(&mut Unexporter);
let load_var = private_ident!("load");
if !specifiers.is_empty() {
entry = entry.fold_with(&mut LocalMarker {
mark: imported.mark(),
specifiers: &specifiers,
excluded: vec![],
{
// ... body of foo
let module_fn = Expr::Fn(FnExpr {
ident: None,
function: Function {
params: vec![
// module
Param {
span: DUMMY_SP.apply_mark(self.top_level_mark),
decorators: Default::default(),
pat: Pat::Ident(Ident::new(
"module".into(),
DUMMY_SP,
)),
},
// exports
Param {
span: DUMMY_SP.apply_mark(self.top_level_mark),
decorators: Default::default(),
pat: Pat::Ident(Ident::new(
"exports".into(),
DUMMY_SP,
)),
},
],
decorators: vec![],
span: DUMMY_SP,
body: Some(BlockStmt {
span: dep.span,
stmts: dep
.body
.into_iter()
.map(|v| match v {
ModuleItem::ModuleDecl(_) => unreachable!(
"module item found but is_es6 is false"
),
ModuleItem::Stmt(s) => s,
})
.collect(),
}),
is_generator: false,
is_async: false,
type_params: None,
return_type: None,
},
});
// var load = __spack_require__.bind(void 0, moduleDecl)
let load_var = Stmt::Decl(Decl::Var(VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
declare: false,
decls: vec![VarDeclarator {
span: DUMMY_SP,
name: Pat::Ident(load_var.clone()),
init: Some(box Expr::Call(CallExpr {
span: DUMMY_SP,
callee: {
info.helpers.require.store(true, Ordering::SeqCst);
Ident::new(
"__spack_require__".into(),
DUMMY_SP.apply_mark(self.top_level_mark),
)
.member(Ident::new("bind".into(), DUMMY_SP))
.as_callee()
},
args: vec![
undefined(DUMMY_SP).as_arg(),
module_fn.as_arg(),
],
type_args: None,
})),
definite: false,
}],
}));
prepend(&mut entry.body, ModuleItem::Stmt(load_var));
log::warn!("Injecting load");
}
let load = CallExpr {
span: DUMMY_SP,
callee: load_var.as_callee(),
args: vec![],
type_args: None,
};
entry.body.visit_mut_with(&mut RequireReplacer {
src: src.src.value.clone(),
load,
});
// // Note: this does not handle `export default
// foo`
// dep = dep.fold_with(&mut LocalMarker {
// mark: imported.mark(),
// specifiers: &imported.exports.items,
// });
// {
// let code = self
// .swc
// .print(
// &entry.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("@: After replace-require:\n{}\n\n\n", code);
// }
log::info!("Replaced requires with load");
}
dep = dep.fold_with(&mut GlobalMarker {
used_mark: self.used_mark,
module_mark: imported.mark(),
});
// {
// let code = self
// .swc
// .print(
// &dep.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("Dep:\n{}\n\n\n", code);
// }
// {
// let code = self
// .swc
// .print(
// &entry.clone().fold_with(&mut HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("@: Before merging:\n{}\n\n\n", code);
// }
// Replace import statement / require with module body
entry.body.visit_mut_with(&mut Injector {
imported: dep.body,
src: src.src.clone(),
});
// {
// let code = self
// .swc
// .print(
// &entry.clone().fold_with(&mut
// HygieneVisualizer),
// SourceMapsConfig::Bool(false),
// None,
// false,
// )
// .unwrap()
// .code;
//
// println!("Merged:\n{}\n\n\n", code);
// }
}
} else {
unimplemented!("conditional dependency: {} -> {}", info.id, src.module_id)
}
}
Ok(entry.fold_with(&mut hygiene()))
Ok(entry)
})
}
}
@ -603,12 +770,12 @@ impl Fold<Ident> for LocalMarker<'_> {
}
}
struct Injector {
struct Es6ModuleInjector {
imported: Vec<ModuleItem>,
src: Str,
}
impl VisitMut<Vec<ModuleItem>> for Injector {
impl VisitMut<Vec<ModuleItem>> for Es6ModuleInjector {
fn visit_mut(&mut self, orig: &mut Vec<ModuleItem>) {
let items = take(orig);
let mut buf = Vec::with_capacity(self.imported.len() + items.len());
@ -661,3 +828,127 @@ impl Fold<Span> for GlobalMarker {
span
}
}
struct RequireReplacer {
src: JsWord,
load: CallExpr,
}
impl VisitMut<ModuleItem> for RequireReplacer {
fn visit_mut(&mut self, node: &mut ModuleItem) {
node.visit_mut_children(self);
match node {
ModuleItem::ModuleDecl(ModuleDecl::Import(i)) => {
// Replace import progress from 'progress';
if i.src.value == self.src {
// Side effech import
if i.specifiers.is_empty() {
*node = ModuleItem::Stmt(
CallExpr {
span: DUMMY_SP,
callee: self.load.clone().as_callee(),
args: vec![],
type_args: None,
}
.into_stmt(),
);
return;
}
let mut props = vec![];
for spec in i.specifiers.clone() {
match spec {
ImportSpecifier::Named(s) => {
if let Some(imported) = s.imported {
props.push(ObjectPatProp::KeyValue(KeyValuePatProp {
key: imported.into(),
value: box s.local.into(),
}));
} else {
props.push(ObjectPatProp::Assign(AssignPatProp {
span: s.span,
key: s.local,
value: None,
}));
}
}
ImportSpecifier::Default(s) => {
props.push(ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(Ident::new("default".into(), DUMMY_SP)),
value: box s.local.into(),
}));
}
ImportSpecifier::Namespace(ns) => {
*node = ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
span: i.span,
kind: VarDeclKind::Var,
declare: false,
decls: vec![VarDeclarator {
span: ns.span,
name: ns.local.into(),
init: Some(
box CallExpr {
span: DUMMY_SP,
callee: self.load.clone().as_callee(),
args: vec![],
type_args: None,
}
.into(),
),
definite: false,
}],
})));
return;
}
}
}
*node = ModuleItem::Stmt(Stmt::Decl(Decl::Var(VarDecl {
span: i.span,
kind: VarDeclKind::Var,
declare: false,
decls: vec![VarDeclarator {
span: i.span,
name: Pat::Object(ObjectPat {
span: DUMMY_SP,
props,
optional: false,
type_ann: None,
}),
init: Some(box self.load.clone().into()),
definite: false,
}],
})));
return;
}
}
_ => {}
}
}
}
impl VisitMut<CallExpr> for RequireReplacer {
fn visit_mut(&mut self, node: &mut CallExpr) {
node.visit_mut_children(self);
match &node.callee {
ExprOrSuper::Expr(box Expr::Ident(i)) => {
// TODO: Check for global mark
if i.sym == *"require" && node.args.len() == 1 {
match &*node.args[0].expr {
Expr::Lit(Lit::Str(s)) => {
if self.src == s.value {
*node = self.load.clone();
log::debug!("Found, and replacing require");
}
}
_ => {}
}
}
}
_ => {}
}
}
}

View File

@ -8,7 +8,7 @@ use fxhash::{FxHashMap, FxHashSet};
use petgraph::{graphmap::DiGraphMap, visit::Bfs};
use rayon::prelude::*;
use swc_common::fold::FoldWith;
use swc_ecma_transforms::{fixer, optimization::simplify::dce::dce};
use swc_ecma_transforms::{fixer, hygiene, optimization::simplify::dce::dce};
mod merge;
@ -49,15 +49,16 @@ impl Bundler<'_> {
Ok(entries
.into_par_iter()
.map(
|(kind, id, module_ids_to_merge): (BundleKind, ModuleId, _)| {
|(kind, id, mut module_ids_to_merge): (BundleKind, ModuleId, _)| {
self.swc().run(|| {
let module = self
.merge_modules(id, module_ids_to_merge)
.merge_modules(id, &mut module_ids_to_merge)
.context("failed to merge module")
.unwrap(); // TODO
let module = module
.fold_with(&mut dce(Default::default()))
.fold_with(&mut hygiene())
.fold_with(&mut fixer());
Bundle { kind, id, module }

View File

@ -0,0 +1,17 @@
function __spack_require__(mod) {
var cache;
return function () {
if (cache) {
return cache;
}
var module = {
exports: {}
};
mod(module, module.exports);
cache = module.exports;
return cache;
}
}

View File

@ -0,0 +1,75 @@
use once_cell::sync::Lazy;
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
use swc_common::{FileName, FoldWith};
use swc_ecma_ast::*;
use swc_ecma_parser::{lexer::Lexer, Parser, SourceFileInput};
use swc_ecma_utils::{
options::{CM, SESSION},
prepend_stmts, DropSpan,
};
#[derive(Debug, Default)]
pub(super) struct Helpers {
/// `__spack_require__`
pub require: AtomicBool,
}
macro_rules! define {
(
$(
$name:ident {
build: $build:ident
}
)*
) => {
$(
fn $build(to: &mut Vec<ModuleItem>) {
static STMTS: Lazy<Vec<ModuleItem>> = Lazy::new(|| {
let code = include_str!(concat!("_", stringify!($name), ".js"));
let fm =
CM.new_source_file(FileName::Custom(stringify!($name).into()), code.into());
let lexer = Lexer::new(
*SESSION,
Default::default(),
Default::default(),
SourceFileInput::from(&*fm),
None,
);
let stmts = Parser::new_from(*SESSION, lexer)
.parse_module()
.map(|script| script.body.fold_with(&mut DropSpan))
.map_err(|mut e| {
e.emit();
()
})
.unwrap();
stmts
});
to.extend((*STMTS).clone());
}
)*
};
}
define!(require {
build: build_spack_require
});
impl Helpers {
pub fn extend(&self, rhs: &Self) {
if rhs.require.load(SeqCst) {
self.require.store(true, SeqCst);
}
}
pub fn append_to(&self, to: &mut Vec<ModuleItem>) {
let mut buf = vec![];
if self.require.load(SeqCst) {
build_spack_require(&mut buf);
}
prepend_stmts(to, buf.into_iter());
}
}

View File

@ -1,6 +1,7 @@
use super::Bundler;
use anyhow::{Context, Error};
use fxhash::{FxHashMap, FxHashSet};
use node_resolve::is_core_module;
use std::{
mem::replace,
path::{Path, PathBuf},
@ -101,6 +102,9 @@ noop_fold_type!(ImportHandler<'_, '_>);
impl ImportHandler<'_, '_> {
fn mark_for(&self, src: &str) -> Option<Mark> {
if is_core_module(src) {
return None;
}
let path = self.bundler.resolve(self.path, src).ok()?;
let (_, mark) = self.bundler.scope.module_id_gen.gen(&path);
Some(mark)
@ -110,6 +114,10 @@ impl ImportHandler<'_, '_> {
impl Fold<ImportDecl> for ImportHandler<'_, '_> {
fn fold(&mut self, import: ImportDecl) -> ImportDecl {
if !self.deglob_phase {
if is_core_module(&import.src.value) {
return import;
}
self.info.imports.push(import.clone());
return import;
}
@ -337,10 +345,10 @@ impl Fold<Expr> for ImportHandler<'_, '_> {
type_only: false,
};
// if self.top_level {
// self.info.imports.push(decl);
// return *undefined(span);
// }
if self.top_level {
self.info.imports.push(decl);
return Expr::Call(e);
}
self.info.lazy_imports.push(decl);
return Expr::Call(e);
@ -395,6 +403,9 @@ impl Fold<VarDeclarator> for ImportHandler<'_, '_> {
} => s.clone(),
_ => return node,
};
if is_core_module(&src.value) {
return node;
}
let ids: Vec<Ident> = find_ids(&node.name);

View File

@ -2,6 +2,7 @@ use super::Bundler;
use crate::{
bundler::{
export::{Exports, RawExports},
helpers::Helpers,
import::RawImports,
},
debug::assert_clean,
@ -15,8 +16,10 @@ use std::{
sync::Arc,
};
use swc_atoms::js_word;
use swc_common::{fold::FoldWith, FileName, Mark, SourceFile};
use swc_ecma_ast::{ImportDecl, ImportSpecifier, Module, Program, Str};
use swc_common::{fold::FoldWith, FileName, Mark, SourceFile, Visit, VisitWith};
use swc_ecma_ast::{
Expr, ExprOrSuper, ImportDecl, ImportSpecifier, MemberExpr, Module, ModuleDecl, Program, Str,
};
use swc_ecma_transforms::resolver::resolver_with_mark;
#[cfg(test)]
@ -31,6 +34,13 @@ pub(super) struct TransformedModule {
pub imports: Arc<Imports>,
pub exports: Arc<Exports>,
/// If false, the module will be wrapped with helper function just like
/// wwbpack.
pub is_es6: bool,
/// Used helpers
pub helpers: Arc<Helpers>,
mark: Mark,
}
@ -234,8 +244,18 @@ impl Bundler<'_> {
let imports = imports?;
let exports = exports?;
let module = module?;
let module = self.drop_unused(fm.clone(), module, None);
let mut module = module?;
let is_es6 = {
let mut v = Es6ModuleDetector {
forced_es6: false,
found_other: false,
};
module.visit_with(&mut v);
v.forced_es6 || !v.found_other
};
if is_es6 {
module = self.drop_unused(fm.clone(), module, None);
}
let module = Arc::new(module);
@ -245,6 +265,8 @@ impl Bundler<'_> {
module,
imports: Arc::new(imports),
exports: Arc::new(exports),
is_es6,
helpers: Default::default(),
mark,
})
})
@ -373,3 +395,56 @@ impl Bundler<'_> {
})
}
}
struct Es6ModuleDetector {
/// If import statement or export is detected, it's an es6 module regardless
/// of other codes.
forced_es6: bool,
/// True if other module system is detected.
found_other: bool,
}
impl Visit<MemberExpr> for Es6ModuleDetector {
fn visit(&mut self, e: &MemberExpr) {
e.obj.visit_with(self);
if e.computed {
e.prop.visit_with(self);
}
match &e.obj {
ExprOrSuper::Expr(box Expr::Ident(i)) => {
// TODO: Check syntax context (Check if marker is the global mark)
if i.sym == *"module" {
self.found_other = true;
}
if i.sym == *"exports" {
self.found_other = true;
}
}
_ => {}
}
//
}
}
impl Visit<ModuleDecl> for Es6ModuleDetector {
fn visit(&mut self, decl: &ModuleDecl) {
match decl {
ModuleDecl::Import(_)
| ModuleDecl::ExportDecl(_)
| ModuleDecl::ExportNamed(_)
| ModuleDecl::ExportDefaultDecl(_)
| ModuleDecl::ExportDefaultExpr(_)
| ModuleDecl::ExportAll(_) => {
self.forced_es6 = true;
}
ModuleDecl::TsImportEquals(_) => {}
ModuleDecl::TsExportAssignment(_) => {}
ModuleDecl::TsNamespaceExport(_) => {}
}
}
}

View File

@ -16,6 +16,7 @@ use swc_ecma_ast::Module;
mod chunk;
mod export;
mod helpers;
mod import;
mod load_transformed;
mod rename;
@ -141,7 +142,7 @@ impl<'a> Bundler<'a> {
let bundles = self.chunk(local)?;
Ok(self.rename(bundles)?)
Ok(self.finalize(bundles)?)
}
pub fn swc(&self) -> &swc::Compiler {

View File

@ -14,12 +14,25 @@ use swc_ecma_codegen::{text_writer::WriteJs, Emitter};
use swc_ecma_transforms::noop_fold_type;
impl Bundler<'_> {
pub(super) fn rename(&self, bundles: Vec<Bundle>) -> Result<Vec<Bundle>, Error> {
pub(super) fn finalize(&self, bundles: Vec<Bundle>) -> Result<Vec<Bundle>, Error> {
let mut new = Vec::with_capacity(bundles.len());
let mut renamed = FxHashMap::default();
for bundle in bundles {
for mut bundle in bundles {
match bundle.kind {
BundleKind::Named { .. } => {
// Inject helpers
let helpers = self
.scope
.get_module(bundle.id)
.expect("module should exist at this point")
.helpers;
self.swc
.run_transform(true, || helpers.append_to(&mut bundle.module.body));
new.push(Bundle { ..bundle });
}
BundleKind::Lib { name } => {
let hash = self.calc_hash(&bundle.module)?;
let mut new_name = PathBuf::from(name);

View File

@ -2,6 +2,7 @@
#![feature(box_patterns)]
#![feature(specialization)]
#![feature(try_blocks)]
#![feature(vec_remove_item)]
#![cfg_attr(test, feature(test))]
#[cfg(test)]

View File

@ -0,0 +1 @@
import {join} from 'path';

View File

@ -0,0 +1,3 @@
import progress from 'progress';
console.log(progress);

View File

@ -0,0 +1,219 @@
function __spack_require__(mod) {
var cache;
return function() {
if (cache) {
return cache;
}
var module = {
exports: {
}
};
mod(module, module.exports);
cache = module.exports;
return cache;
};
}
var load = __spack_require__.bind(void 0, function(module1, exports) {
var load1 = __spack_require__.bind(void 0, function(module1, exports1) /*!
* node-progress
* Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Expose `ProgressBar`.
*/
{
exports = module.exports = ProgressBar;
/**
* Initialize a `ProgressBar` with the given `fmt` string and `options` or
* `total`.
*
* Options:
*
* - `curr` current completed index
* - `total` total number of ticks to complete
* - `width` the displayed width of the progress bar defaulting to total
* - `stream` the output stream defaulting to stderr
* - `head` head character defaulting to complete character
* - `complete` completion character defaulting to "="
* - `incomplete` incomplete character defaulting to "-"
* - `renderThrottle` minimum time between updates in milliseconds defaulting to 16
* - `callback` optional function to call when the progress bar completes
* - `clear` will clear the progress bar upon termination
*
* Tokens:
*
* - `:bar` the progress bar itself
* - `:current` current tick number
* - `:total` total ticks
* - `:elapsed` time elapsed in seconds
* - `:percent` completion percentage
* - `:eta` eta in seconds
* - `:rate` rate of ticks per second
*
* @param {string} fmt
* @param {object|number} options or total
* @api public
*/
function ProgressBar(fmt, options) {
this.stream = options.stream || process.stderr;
if (typeof options == 'number') {
var total = options;
options = {
};
options.total = total;
} else {
options = options || {
};
if ('string' != typeof fmt) throw new Error('format required');
if ('number' != typeof options.total) throw new Error('total required');
}
this.fmt = fmt;
this.curr = options.curr || 0;
this.total = options.total;
this.width = options.width || this.total;
this.clear = options.clear;
this.chars = {
complete: options.complete || '=',
incomplete: options.incomplete || '-',
head: options.head || (options.complete || '=')
};
this.renderThrottle = options.renderThrottle !== 0 ? options.renderThrottle || 16 : 0;
this.lastRender = -Infinity;
this.callback = options.callback || function() {
};
this.tokens = {
};
this.lastDraw = '';
}
/**
* "tick" the progress bar with optional `len` and optional `tokens`.
*
* @param {number|object} len or tokens
* @param {object} tokens
* @api public
*/
ProgressBar.prototype.tick = function(len, tokens) {
if (len !== 0) len = len || 1;
// swap tokens
if ('object' == typeof len) tokens = len, len = 1;
if (tokens) this.tokens = tokens;
// start time for eta
if (0 == this.curr) this.start = new Date;
this.curr += len;
// try to render
this.render();
// progress complete
if (this.curr >= this.total) {
this.render(undefined, true);
this.complete = true;
this.terminate();
this.callback(this);
return;
}
};
/**
* Method to render the progress bar with optional `tokens` to place in the
* progress bar's `fmt` field.
*
* @param {object} tokens
* @api public
*/
ProgressBar.prototype.render = function(tokens, force) {
force = force !== undefined ? force : false;
if (tokens) this.tokens = tokens;
if (!this.stream.isTTY) return;
var now = Date.now();
var delta = now - this.lastRender;
if (!force && delta < this.renderThrottle) {
return;
} else {
this.lastRender = now;
}
var ratio = this.curr / this.total;
ratio = Math.min(Math.max(ratio, 0), 1);
var percent = Math.floor(ratio * 100);
var incomplete, complete, completeLength;
var elapsed = new Date - this.start;
var eta = percent == 100 ? 0 : elapsed * (this.total / this.curr - 1);
var rate = this.curr / (elapsed / 1000);
/* populate the bar template with percentages and timestamps */
var str = this.fmt.replace(':current', this.curr).replace(':total', this.total).replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1)).replace(':eta', isNaN(eta) || !isFinite(eta) ? '0.0' : (eta / 1000).toFixed(1)).replace(':percent', percent.toFixed(0) + '%').replace(':rate', Math.round(rate));
/* compute the available space (non-zero) for the bar */
var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
if (availableSpace && process.platform === 'win32') {
availableSpace = availableSpace - 1;
}
var width = Math.min(this.width, availableSpace);
/* TODO: the following assumes the user has one ':bar' token */
completeLength = Math.round(width * ratio);
complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);
/* add head to the complete string */
if (completeLength > 0) complete = complete.slice(0, -1) + this.chars.head;
/* fill in the actual progress bar */
str = str.replace(':bar', complete + incomplete);
/* replace the extra tokens */
if (this.tokens) for(var key in this.tokens)str = str.replace(':' + key, this.tokens[key]);
if (this.lastDraw !== str) {
this.stream.cursorTo(0);
this.stream.write(str);
this.stream.clearLine(1);
this.lastDraw = str;
}
};
/**
* "update" the progress bar to represent an exact percentage.
* The ratio (between 0 and 1) specified will be multiplied by `total` and
* floored, representing the closest available "tick." For example, if a
* progress bar has a length of 3 and `update(0.5)` is called, the progress
* will be set to 1.
*
* A ratio of 0.5 will attempt to set the progress to halfway.
*
* @param {number} ratio The ratio (between 0 and 1 inclusive) to set the
* overall completion to.
* @api public
*/
ProgressBar.prototype.update = function(ratio, tokens) {
var goal = Math.floor(ratio * this.total);
var delta = goal - this.curr;
this.tick(delta, tokens);
};
/**
* "interrupt" the progress bar and write a message above it.
* @param {string} message The message to write.
* @api public
*/
ProgressBar.prototype.interrupt = function(message) {
// clear the current line
this.stream.clearLine();
// move the cursor to the start of the line
this.stream.cursorTo(0);
// write the message text
this.stream.write(message);
// terminate the line after writing the message
this.stream.write('\n');
// re-display the progress bar with its lastDraw
this.stream.write(this.lastDraw);
};
/**
* Terminates a progress bar.
*
* @api public
*/
ProgressBar.prototype.terminate = function() {
if (this.clear) {
if (this.stream.clearLine) {
this.stream.clearLine();
this.stream.cursorTo(0);
}
} else {
this.stream.write('\n');
}
};
});
module.exports = load1();
});
var { default: progress } = load();
console.log(progress);

View File

@ -364,20 +364,26 @@ impl Compiler {
.with_context(|| format!("failed to load config for file '{:?}'", name))
}
// TODO: Handle source map
pub fn run_transform<F, Ret>(&self, external_helpers: bool, op: F) -> Ret
where
F: FnOnce() -> Ret,
{
self.run(|| {
helpers::HELPERS.set(&Helpers::new(external_helpers), || {
util::HANDLER.set(&self.handler, || op())
})
})
}
pub fn transform(
&self,
program: Program,
external_helpers: bool,
mut pass: impl Pass,
) -> Program {
self.run(|| {
helpers::HELPERS.set(&Helpers::new(external_helpers), || {
util::HANDLER.set(&self.handler, || {
// Fold module
program.fold_with(&mut pass)
})
})
self.run_transform(external_helpers, || {
// Fold module
program.fold_with(&mut pass)
})
}