Update swc (#33063)

This commit is contained in:
Donny/강동윤 2022-01-10 19:37:32 +09:00 committed by GitHub
parent 320986a2b8
commit 87dbd03eb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 386 additions and 576 deletions

File diff suppressed because it is too large Load diff

View file

@ -14,21 +14,21 @@ fxhash = "0.2.1"
pathdiff = "0.2.0" pathdiff = "0.2.0"
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"
styled_components = "0.6.0" styled_components = "0.9.0"
swc = "0.98.0" swc = "0.110.0"
swc_atoms = "0.2.7" swc_atoms = "0.2.7"
swc_common = { version = "0.15.0", features = ["concurrent", "sourcemap"] } swc_common = { version = "0.16.0", features = ["concurrent", "sourcemap"] }
swc_css = "0.44.0" swc_css = "0.45.0"
swc_ecma_loader = { version = "0.25.0", features = ["node", "lru"] } swc_ecma_loader = { version = "0.26.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.98.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] } swc_ecmascript = { version = "0.105.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_node_base = "0.5.1" swc_node_base = "0.5.1"
swc_stylis = "0.41.1" swc_stylis = "0.42.0"
tracing = {version = "0.1.28", features = ["release_max_level_off"]} tracing = {version = "0.1.28", features = ["release_max_level_off"]}
regex = "1.5" regex = "1.5"
[dev-dependencies] [dev-dependencies]
swc_ecma_transforms_testing = "0.51.0" swc_ecma_transforms_testing = "0.56.0"
testing = "0.16.0" testing = "0.17.0"
walkdir = "2.3.2" walkdir = "2.3.2"

View file

@ -11,7 +11,7 @@ use swc_ecmascript::{
visit::{noop_fold_type, Fold}, visit::{noop_fold_type, Fold},
}; };
/// Note: This paths requires runnning `resolver` **before** running this. /// Note: This paths requires running `resolver` **before** running this.
pub fn next_ssg() -> impl Fold { pub fn next_ssg() -> impl Fold {
Repeat::new(NextSsg { Repeat::new(NextSsg {
state: Default::default(), state: Default::default(),
@ -19,7 +19,7 @@ pub fn next_ssg() -> impl Fold {
}) })
} }
/// State of the transforms. Shared by the anayzer and the tranform. /// State of the transforms. Shared by the analyzer and the transform.
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct State { struct State {
/// Identifiers referenced by non-data function codes. /// Identifiers referenced by non-data function codes.
@ -45,11 +45,7 @@ struct State {
impl State { impl State {
fn is_data_identifier(&mut self, i: &Ident) -> Result<bool, Error> { fn is_data_identifier(&mut self, i: &Ident) -> Result<bool, Error> {
let ssg_exports = &[ let ssg_exports = &["getStaticProps", "getStaticPaths", "getServerSideProps"];
"getStaticProps",
"getStaticPaths",
"getServerSideProps",
];
if ssg_exports.contains(&&*i.sym) { if ssg_exports.contains(&&*i.sym) {
if &*i.sym == "getServerSideProps" { if &*i.sym == "getServerSideProps" {
@ -125,7 +121,9 @@ impl Fold for Analyzer<'_> {
} }
fn fold_export_named_specifier(&mut self, s: ExportNamedSpecifier) -> ExportNamedSpecifier { fn fold_export_named_specifier(&mut self, s: ExportNamedSpecifier) -> ExportNamedSpecifier {
self.add_ref(s.orig.to_id()); if let ModuleExportName::Ident(id) = &s.orig {
self.add_ref(id.to_id());
}
s s
} }
@ -189,7 +187,7 @@ impl Fold for Analyzer<'_> {
e e
} }
/// Drops [ExportDecl] if all speicifers are removed. /// Drops [ExportDecl] if all specifiers are removed.
fn fold_module_item(&mut self, s: ModuleItem) -> ModuleItem { fn fold_module_item(&mut self, s: ModuleItem) -> ModuleItem {
match s { match s {
ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(e)) if !e.specifiers.is_empty() => { ModuleItem::ModuleDecl(ModuleDecl::ExportNamed(e)) if !e.specifiers.is_empty() => {
@ -211,8 +209,7 @@ impl Fold for Analyzer<'_> {
ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(e)) => match &e.decl { ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(e)) => match &e.decl {
Decl::Fn(f) => { Decl::Fn(f) => {
// Drop getStaticProps. // Drop getStaticProps.
if let Ok(is_data_identifier) = self.state.is_data_identifier(&f.ident) if let Ok(is_data_identifier) = self.state.is_data_identifier(&f.ident) {
{
if is_data_identifier { if is_data_identifier {
return ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP })); return ModuleItem::Stmt(Stmt::Empty(EmptyStmt { span: DUMMY_SP }));
} }
@ -493,29 +490,38 @@ impl Fold for NextSsg {
n.specifiers.retain(|s| { n.specifiers.retain(|s| {
let preserve = match s { let preserve = match s {
ExportSpecifier::Namespace(ExportNamespaceSpecifier { name: exported, .. }) ExportSpecifier::Namespace(ExportNamespaceSpecifier {
name: ModuleExportName::Ident(exported),
..
})
| ExportSpecifier::Default(ExportDefaultSpecifier { exported, .. }) | ExportSpecifier::Default(ExportDefaultSpecifier { exported, .. })
| ExportSpecifier::Named(ExportNamedSpecifier { | ExportSpecifier::Named(ExportNamedSpecifier {
exported: Some(exported), exported: Some(ModuleExportName::Ident(exported)),
.. ..
}) => self }) => self
.state .state
.is_data_identifier(&exported) .is_data_identifier(&exported)
.map(|is_data_identifier| !is_data_identifier), .map(|is_data_identifier| !is_data_identifier),
ExportSpecifier::Named(s) => self ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(orig),
..
}) => self
.state .state
.is_data_identifier(&s.orig) .is_data_identifier(&orig)
.map(|is_data_identifier| !is_data_identifier), .map(|is_data_identifier| !is_data_identifier),
_ => Ok(true),
}; };
match preserve { match preserve {
Ok(false) => { Ok(false) => {
tracing::trace!( tracing::trace!("Dropping a export specifier because it's a data identifier");
"Dropping a export specifier because it's a data identifier"
);
match s { match s {
ExportSpecifier::Named(ExportNamedSpecifier { orig, .. }) => { ExportSpecifier::Named(ExportNamedSpecifier {
orig: ModuleExportName::Ident(orig),
..
}) => {
self.state.should_run_again = true; self.state.should_run_again = true;
self.state.refs_from_data_fn.insert(orig.to_id()); self.state.refs_from_data_fn.insert(orig.to_id());
} }

View file

@ -90,26 +90,45 @@ impl Fold for PageConfig {
match &kv.key { match &kv.key {
PropName::Ident(ident) => { PropName::Ident(ident) => {
if &ident.sym == "amp" { if &ident.sym == "amp" {
if let Expr::Lit(Lit::Bool(Bool { value, .. })) = &*kv.value { if let Expr::Lit(Lit::Bool(Bool {
value,
..
})) = &*kv.value
{
if *value && self.is_page_file { if *value && self.is_page_file {
self.drop_bundle = true; self.drop_bundle = true;
} }
} else if let Expr::Lit(Lit::Str(_)) = &*kv.value { } else if let Expr::Lit(Lit::Str(_)) =
// Do not replace bundle &*kv.value
{
// Do not replace
// bundle
} else { } else {
self.handle_error("Invalid value found.", export.span); self.handle_error(
"Invalid value found.",
export.span,
);
} }
} }
} }
_ => { _ => {
self.handle_error("Invalid property found.", export.span); self.handle_error(
"Invalid property found.",
export.span,
);
} }
} }
} else { } else {
self.handle_error("Invalid property or value.", export.span); self.handle_error(
"Invalid property or value.",
export.span,
);
} }
} else { } else {
self.handle_error("Property spread is not allowed.", export.span); self.handle_error(
"Property spread is not allowed.",
export.span,
);
} }
} }
} else { } else {
@ -132,16 +151,20 @@ impl Fold for PageConfig {
) -> ExportNamedSpecifier { ) -> ExportNamedSpecifier {
match &specifier.exported { match &specifier.exported {
Some(ident) => { Some(ident) => {
if let ModuleExportName::Ident(ident) = ident {
if &ident.sym == CONFIG_KEY { if &ident.sym == CONFIG_KEY {
self.handle_error("Config cannot be re-exported.", specifier.span) self.handle_error("Config cannot be re-exported.", specifier.span)
} }
} }
}
None => { None => {
if &specifier.orig.sym == CONFIG_KEY { if let ModuleExportName::Ident(ident) = &specifier.orig {
if &ident.sym == CONFIG_KEY {
self.handle_error("Config cannot be re-exported.", specifier.span) self.handle_error("Config cannot be re-exported.", specifier.span)
} }
} }
} }
}
specifier specifier
} }
} }

View file

@ -84,13 +84,17 @@ impl Fold for ExportShaker {
.filter_map(|spec| { .filter_map(|spec| {
if let ExportSpecifier::Named(named_spec) = spec { if let ExportSpecifier::Named(named_spec) = spec {
if let Some(ident) = &named_spec.exported { if let Some(ident) = &named_spec.exported {
if let ModuleExportName::Ident(ident) = ident {
if self.ignore.contains(&ident.sym) { if self.ignore.contains(&ident.sym) {
return Some(ExportSpecifier::Named(named_spec)); return Some(ExportSpecifier::Named(named_spec));
} }
} else if self.ignore.contains(&named_spec.orig.sym) { }
} else if let ModuleExportName::Ident(ident) = &named_spec.orig {
if self.ignore.contains(&ident.sym) {
return Some(ExportSpecifier::Named(named_spec)); return Some(ExportSpecifier::Named(named_spec));
} }
} }
}
None None
}) })
.collect(); .collect();

View file

@ -1,8 +1,7 @@
"use strict"; "use strict";
var _esm = _interopRequireDefault(require("esm")); var a = function(a) {
function _interopRequireDefault(a) {
return a && a.__esModule ? a : { return a && a.__esModule ? a : {
default: a default: a
}; };
} }(require("esm"));
console.log(_esm.default.foo), module.exports = _esm.default; console.log(a.default.foo), module.exports = a.default;

View file

@ -1,52 +1,47 @@
import a from "other"; import a from "other";
function _arrayLikeToArray(a, b) { function b(a, b) {
(null == b || b > a.length) && (b = a.length); (null == b || b > a.length) && (b = a.length);
for(var b = 0, d = new Array(b); b < b; b++)d[b] = a[b]; for(var c = 0, d = new Array(b); c < b; c++)d[c] = a[c];
return d; return d;
} }
function _arrayWithHoles(a) { (function(a, c) {
return (function(a) {
if (Array.isArray(a)) return a; if (Array.isArray(a)) return a;
} })(a) || (function(a, c) {
function _classCallCheck(a, b) { var d, e, f = null == a ? null : "undefined" != typeof Symbol && a[Symbol.iterator] || a["@@iterator"];
if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function"); if (null != f) {
} var g = [], h = !0, i = !1;
function _iterableToArrayLimit(a, b) {
var c, d, e = null == a ? null : "undefined" != typeof Symbol && a[Symbol.iterator] || a["@@iterator"];
if (null != e) {
var f = [], g = !0, h = !1;
try { try {
for(e = e.call(a); !(g = (c = e.next()).done) && (f.push(c.value), !b || f.length !== b); g = !0); for(f = f.call(a); !(h = (d = f.next()).done) && (g.push(d.value), !c || g.length !== c); h = !0);
} catch (i) { } catch (j) {
h = !0, d = i; i = !0, e = j;
} finally{ } finally{
try { try {
g || null == e.return || e.return(); h || null == f.return || f.return();
} finally{ } finally{
if (h) throw d; if (i) throw e;
} }
} }
return f; return g;
} }
} })(a, c) || (function(a, c) {
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _slicedToArray(a, b) {
return _arrayWithHoles(a) || _iterableToArrayLimit(a, b) || _unsupportedIterableToArray(a, b) || _nonIterableRest();
}
function _unsupportedIterableToArray(a, b) {
if (a) { if (a) {
if ("string" == typeof a) return _arrayLikeToArray(a, b); if ("string" == typeof a) return b(a, c);
var c = Object.prototype.toString.call(a).slice(8, -1); var d = Object.prototype.toString.call(a).slice(8, -1);
if ("Object" === c && a.constructor && (c = a.constructor.name), "Map" === c || "Set" === c) return Array.from(c); if ("Object" === d && a.constructor && (d = a.constructor.name), "Map" === d || "Set" === d) return Array.from(d);
if ("Arguments" === c || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)) return _arrayLikeToArray(a, b); if ("Arguments" === d || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(d)) return b(a, c);
} }
} })(a, c) || (function() {
var _other = _slicedToArray(a, 1), foo = _other[0], Foo = function() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
})();
})(a, 1)[0];
var c = function() {
"use strict"; "use strict";
_classCallCheck(this, Foo); !function(a, b) {
if (!(a instanceof b)) throw new TypeError("Cannot call a class as a function");
}(this, c);
}; };
export var __N_SSG = !0; export var __N_SSG = !0;
export default function a() { export default function d() {
return React.createElement("div", null); return React.createElement("div", null);
}; };

View file

@ -16,12 +16,12 @@ once_cell = "1.8.0"
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"
next-swc = { version = "0.0.0", path = "../core" } next-swc = { version = "0.0.0", path = "../core" }
swc = "0.98.0" swc = "0.110.0"
swc_atoms = "0.2.7" swc_atoms = "0.2.7"
swc_bundler = { version = "0.91.0", features = ["concurrent"] } swc_bundler = { version = "0.98.0", features = ["concurrent"] }
swc_common = { version = "0.15.0", features = ["concurrent", "sourcemap"] } swc_common = { version = "0.16.0", features = ["concurrent", "sourcemap"] }
swc_ecma_loader = { version = "0.25.0", features = ["node", "lru"] } swc_ecma_loader = { version = "0.26.0", features = ["node", "lru"] }
swc_ecmascript = { version = "0.98.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] } swc_ecmascript = { version = "0.105.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
swc_node_base = "0.5.1" swc_node_base = "0.5.1"
[build-dependencies] [build-dependencies]

View file

@ -76,6 +76,7 @@ impl Task for BundleTask {
disable_inliner: false, disable_inliner: false,
external_modules: builtins, external_modules: builtins,
module: swc_bundler::ModuleType::Es, module: swc_bundler::ModuleType::Es,
..Default::default()
}, },
Box::new(CustomHook), Box::new(CustomHook),
); );

View file

@ -16,9 +16,9 @@ path-clean = "0.1"
serde = {version = "1", features = ["derive"]} serde = {version = "1", features = ["derive"]}
serde_json = "1" serde_json = "1"
next-swc = { version = "0.0.0", path = "../core" } next-swc = { version = "0.0.0", path = "../core" }
swc = "0.98.0" swc = "0.110.0"
swc_common = { version = "0.15.0", features = ["concurrent", "sourcemap"] } swc_common = { version = "0.16.0", features = ["concurrent", "sourcemap"] }
swc_ecmascript = { version = "0.98.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] } swc_ecmascript = { version = "0.105.0", features = ["codegen", "minifier", "optimization", "parser", "react", "transforms", "typescript", "utils", "visit"] }
tracing = {version = "0.1.28", features = ["release_max_level_off"]} tracing = {version = "0.1.28", features = ["release_max_level_off"]}
wasm-bindgen = {version = "0.2", features = ["serde-serialize"]} wasm-bindgen = {version = "0.2", features = ["serde-serialize"]}
wasm-bindgen-futures = "0.4.8" wasm-bindgen-futures = "0.4.8"