Skip to content

Commit 206c0db

Browse files
authoredJan 3, 2023
1 parent 1602f66 commit 206c0db

File tree

16 files changed

+194
-27
lines changed

16 files changed

+194
-27
lines changed
 

‎Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": false
6+
},
7+
"target": "es2015",
8+
"loose": false,
9+
"minify": {
10+
"compress": false,
11+
"mangle": {
12+
"toplevel": false,
13+
"keep_classnames": false,
14+
"keep_fnames": false,
15+
"keep_private_props": false,
16+
"ie8": false,
17+
"safari10": false
18+
}
19+
}
20+
},
21+
"module": {
22+
"type": "es6"
23+
},
24+
"minify": false,
25+
"isModule": true
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Plugin, PluginBuild } from 'esbuild';
2+
3+
export const styleLoader = (): Plugin => {
4+
return {
5+
setup(build: PluginBuild) {
6+
build.onLoad(async (args) => {
7+
8+
});
9+
},
10+
};
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import r from "@swc/helpers/src/_async_to_generator.mjs";
2+
export const styleLoader = ()=>{
3+
return {
4+
setup (t) {
5+
t.onLoad(function() {
6+
var t = r(function*(r) {});
7+
return function(r) {
8+
return t.apply(this, arguments);
9+
};
10+
}());
11+
}
12+
};
13+
};

‎crates/swc/tests/projects.rs

+4
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ fn should_visit() {
812812

813813
#[testing::fixture("tests/fixture/**/input/")]
814814
#[testing::fixture("tests/vercel/**/input/")]
815+
fn fixture(input_dir: PathBuf) {
816+
tests(input_dir)
817+
}
818+
815819
fn tests(input_dir: PathBuf) {
816820
let output = input_dir.parent().unwrap().join("output");
817821

‎crates/swc/tests/vercel/full/next-31419/1/output/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export var listOfUser = function(t) {
6464
2
6565
];
6666
});
67-
}), function(r, e) {
67+
}), function(r, n) {
6868
return e.apply(this, arguments);
6969
}));
7070
};

‎crates/swc/tests/vercel/full/next-31419/2/output/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const listOfUser = function(n) {
1919
postgreSQL.query(t, null, function(n, t) {
2020
n ? e(n) : r(t.rows);
2121
});
22-
}), function(r, e) {
22+
}), function(r, n) {
2323
return e.apply(this, arguments);
2424
}));
2525
};

‎crates/swc_ecma_minifier/src/compress/optimize/iife.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use swc_atoms::js_word;
55
use swc_common::{pass::Either, util::take::Take, Mark, Spanned, SyntaxContext, DUMMY_SP};
66
use swc_ecma_ast::*;
77
use swc_ecma_utils::{
8-
contains_arguments, contains_this_expr, find_pat_ids, undefined, ExprFactory,
8+
contains_arguments, contains_this_expr, find_pat_ids, undefined, ExprFactory, Remapper,
99
};
1010
use swc_ecma_visit::VisitMutWith;
1111

1212
use super::{util::NormalMultiReplacer, Optimizer};
1313
#[cfg(feature = "debug")]
1414
use crate::debug::dump;
1515
use crate::{
16-
compress::optimize::{util::Remapper, Ctx},
16+
compress::optimize::Ctx,
1717
mode::Mode,
1818
util::{idents_captured_by, idents_used_by, make_number},
1919
};
@@ -578,7 +578,7 @@ where
578578
if self.vars.inline_with_multi_replacer(body) {
579579
self.changed = true;
580580
}
581-
body.visit_mut_with(&mut Remapper { vars: remap });
581+
body.visit_mut_with(&mut Remapper::new(&remap));
582582
exprs.push(body.take());
583583

584584
report_change!("inline: Inlining a call to an arrow function");
@@ -889,7 +889,7 @@ where
889889
if self.vars.inline_with_multi_replacer(body) {
890890
self.changed = true;
891891
}
892-
body.visit_mut_with(&mut Remapper { vars: remap });
892+
body.visit_mut_with(&mut Remapper::new(&remap));
893893

894894
let mut vars = Vec::new();
895895
let mut exprs = Vec::new();

‎crates/swc_ecma_minifier/src/compress/optimize/util.rs

+1-19
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55

66
use rustc_hash::{FxHashMap, FxHashSet};
77
use swc_atoms::js_word;
8-
use swc_common::{util::take::Take, Span, SyntaxContext, DUMMY_SP};
8+
use swc_common::{util::take::Take, Span, DUMMY_SP};
99
use swc_ecma_ast::*;
1010
use swc_ecma_transforms_base::perf::{Parallel, ParallelExt};
1111
use swc_ecma_utils::{ExprCtx, ExprExt};
@@ -185,24 +185,6 @@ pub(crate) fn is_valid_for_lhs(e: &Expr) -> bool {
185185
!matches!(e, Expr::Lit(..) | Expr::Unary(..))
186186
}
187187

188-
/// Variable remapper
189-
///
190-
/// - Used for evaluating IIFEs
191-
192-
pub(crate) struct Remapper {
193-
pub vars: FxHashMap<Id, SyntaxContext>,
194-
}
195-
196-
impl VisitMut for Remapper {
197-
noop_visit_mut_type!();
198-
199-
fn visit_mut_ident(&mut self, i: &mut Ident) {
200-
if let Some(new_ctxt) = self.vars.get(&i.to_id()).copied() {
201-
i.span.ctxt = new_ctxt;
202-
}
203-
}
204-
}
205-
206188
/// A visitor responsible for inlining special kind of variables and removing
207189
/// (some) unused variables. Due to the order of visit, the main visitor cannot
208190
/// handle all edge cases and this type is the complement for it.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"defaults": false
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
2+
try {
3+
var info = gen[key](arg);
4+
var value = info.value;
5+
} catch (error) {
6+
reject(error);
7+
return;
8+
}
9+
if (info.done) {
10+
resolve(value);
11+
} else {
12+
Promise.resolve(value).then(_next, _throw);
13+
}
14+
}
15+
function _asyncToGenerator(fn) {
16+
return function () {
17+
var self = this, args = arguments;
18+
return new Promise(function (resolve, reject) {
19+
var gen = fn.apply(self, args);
20+
function _next(value) {
21+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
22+
}
23+
function _throw(err) {
24+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
25+
}
26+
_next(undefined);
27+
});
28+
};
29+
}
30+
export const styleLoader = () => {
31+
return {
32+
name: 'style-loader',
33+
setup(build) {
34+
build.onLoad({
35+
filter: /.*/,
36+
namespace: 'less'
37+
}, function () {
38+
var _ref = _asyncToGenerator(function* (args) { });
39+
return function (args) {
40+
return _ref.apply(this, arguments);
41+
};
42+
}());
43+
}
44+
};
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"toplevel": true
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function n(n, e, t, r, o, u, i) {
2+
try {
3+
var a = n[u](i);
4+
var c = a.value;
5+
} catch (s) {
6+
t(s);
7+
return;
8+
}
9+
if (a.done) e(c);
10+
else Promise.resolve(c).then(r, o);
11+
}
12+
function e(e) {
13+
return function() {
14+
var t = this, r = arguments;
15+
return new Promise(function(o, u) {
16+
var i = e.apply(t, r);
17+
function a(e) {
18+
n(i, o, u, a, c, "next", e);
19+
}
20+
function c(e) {
21+
n(i, o, u, a, c, "throw", e);
22+
}
23+
a(void 0);
24+
});
25+
};
26+
}
27+
export const styleLoader = ()=>{
28+
return {
29+
name: 'style-loader',
30+
setup (n) {
31+
n.onLoad({
32+
filter: /.*/,
33+
namespace: 'less'
34+
}, function() {
35+
var n = e(function*(n) {});
36+
return function(e) {
37+
return n.apply(this, arguments);
38+
};
39+
}());
40+
}
41+
};
42+
};

‎crates/swc_ecma_transforms_compat/src/es2017/async_to_generator.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use swc_ecma_ast::*;
66
use swc_ecma_transforms_base::{helper, helper_expr, perf::Check};
77
use swc_ecma_transforms_macros::fast_path;
88
use swc_ecma_utils::{
9-
contains_this_expr,
9+
contains_this_expr, find_pat_ids,
1010
function::{FnEnvHoister, FnWrapperResult, FunctionWrapper},
11-
private_ident, quote_ident, ExprFactory, StmtLike,
11+
private_ident, quote_ident, ExprFactory, Remapper, StmtLike,
1212
};
1313
use swc_ecma_visit::{
1414
as_folder, noop_visit_mut_type, noop_visit_type, Fold, Visit, VisitMut, VisitMutWith, VisitWith,
@@ -393,6 +393,16 @@ impl<C: Comments> Actual<C> {
393393
/// `_asyncToGenerator(function*() {})` from `async function() {}`;
394394
#[tracing::instrument(level = "info", skip_all)]
395395
fn make_fn_ref(mut expr: FnExpr) -> Expr {
396+
{
397+
let param_ids: Vec<Id> = find_pat_ids(&expr.function.params);
398+
let mapping = param_ids
399+
.into_iter()
400+
.map(|id| (id, SyntaxContext::empty().apply_mark(Mark::new())))
401+
.collect();
402+
403+
expr.function.visit_mut_with(&mut Remapper::new(&mapping));
404+
}
405+
396406
expr.function.body.visit_mut_with(&mut AsyncFnBodyHandler {
397407
is_async_generator: expr.function.is_generator,
398408
});

‎crates/swc_ecma_utils/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ indexmap = "1.6.1"
2424
num_cpus = "1.13.1"
2525
once_cell = "1.10.0"
2626
rayon = {version = "1.5.1", optional = true}
27+
rustc-hash = "1.1.0"
2728
swc_atoms = {version = "0.4.32", path = "../swc_atoms"}
2829
swc_common = {version = "0.29.25", path = "../swc_common"}
2930
swc_ecma_ast = {version = "0.95.9", path = "../swc_ecma_ast"}

‎crates/swc_ecma_utils/src/lib.rs

+26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::{
1818
ops::Add,
1919
};
2020

21+
use rustc_hash::FxHashMap;
2122
use swc_atoms::{js_word, JsWord};
2223
use swc_common::{collections::AHashSet, Mark, Span, Spanned, SyntaxContext, DUMMY_SP};
2324
use swc_ecma_ast::*;
@@ -2825,6 +2826,31 @@ pub fn contains_top_level_await<V: VisitWith<TopLevelAwait>>(t: &V) -> bool {
28252826
finder.found
28262827
}
28272828

2829+
/// Variable remapper
2830+
///
2831+
/// This visitor modifies [SyntaxContext] while preserving the symbol of
2832+
/// [Ident]s.
2833+
2834+
pub struct Remapper<'a> {
2835+
vars: &'a FxHashMap<Id, SyntaxContext>,
2836+
}
2837+
2838+
impl<'a> Remapper<'a> {
2839+
pub fn new(vars: &'a FxHashMap<Id, SyntaxContext>) -> Self {
2840+
Self { vars }
2841+
}
2842+
}
2843+
2844+
impl VisitMut for Remapper<'_> {
2845+
noop_visit_mut_type!();
2846+
2847+
fn visit_mut_ident(&mut self, i: &mut Ident) {
2848+
if let Some(new_ctxt) = self.vars.get(&i.to_id()).copied() {
2849+
i.span.ctxt = new_ctxt;
2850+
}
2851+
}
2852+
}
2853+
28282854
#[cfg(test)]
28292855
mod test {
28302856
use swc_common::{input::StringInput, BytePos};

0 commit comments

Comments
 (0)
Please sign in to comment.