-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(plugin:emotion): panic when target to chrome 40 #1527
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c85e8a
refactor: 🎨 no more clone
stormslowly 74cc263
refactor: 🎨 preset env should go last
stormslowly b864cfb
refactor: 🎨 extract plugin transform js to tranform mod
stormslowly c5d09db
chore: 🔧 support stand alone
stormslowly c6c403f
chore: 🔧 update mako config to trigger emotion source map error
stormslowly d408550
fix: 🐛 folder orders
stormslowly 16fd868
refactor: 🎨 caller is responsibility to setup need scoped value
stormslowly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use swc_core::common::errors::HANDLER; | ||
use swc_core::common::GLOBALS; | ||
use swc_core::css::ast::{AtRule, AtRulePrelude, ImportHref, Rule, Str, Stylesheet, UrlValue}; | ||
use swc_core::css::compat::compiler::{self, Compiler}; | ||
use swc_core::css::{compat as swc_css_compat, prefixer, visit as swc_css_visit}; | ||
use swc_core::ecma::preset_env::{self as swc_preset_env}; | ||
use swc_core::ecma::transforms::base::feature::FeatureFlag; | ||
use swc_core::ecma::transforms::base::fixer::paren_remover; | ||
use swc_core::ecma::transforms::base::helpers::{Helpers, HELPERS}; | ||
use swc_core::ecma::transforms::base::{resolver, Assumptions}; | ||
use swc_core::ecma::transforms::compat::reserved_words; | ||
use swc_core::ecma::transforms::optimization::simplifier; | ||
use swc_core::ecma::transforms::optimization::simplify::{dce, Config as SimpilifyConfig}; | ||
use swc_core::ecma::transforms::proposal::decorators; | ||
use swc_core::ecma::visit::{Fold, VisitMut}; | ||
use swc_error_reporters::handler::try_with_handler; | ||
|
||
use crate::ast::css_ast::CssAst; | ||
use crate::ast::file::File; | ||
|
@@ -23,6 +26,7 @@ use crate::compiler::Context; | |
use crate::config::Mode; | ||
use crate::features; | ||
use crate::module::ModuleAst; | ||
use crate::plugin::PluginTransformJsParam; | ||
use crate::plugins::context_module::ContextModuleVisitor; | ||
use crate::visitors::css_assets::CSSAssets; | ||
use crate::visitors::css_flexbugs::CSSFlexbugs; | ||
|
@@ -107,7 +111,7 @@ impl Transform { | |
&& is_browser; | ||
if is_jsx { | ||
visitors.push(react( | ||
cm, | ||
cm.clone(), | ||
context.clone(), | ||
use_refresh, | ||
&top_level_mark, | ||
|
@@ -169,34 +173,6 @@ impl Transform { | |
let comments = origin_comments.get_swc_comments().clone(); | ||
let assumptions = context.assumptions_for(file); | ||
|
||
folders.push(Box::new(swc_preset_env::preset_env( | ||
unresolved_mark, | ||
Some(comments), | ||
swc_preset_env::Config { | ||
mode: Some(swc_preset_env::Mode::Entry), | ||
targets: Some(swc_preset_env_targets_from_map( | ||
context.config.targets.clone(), | ||
)), | ||
..Default::default() | ||
}, | ||
assumptions, | ||
&mut FeatureFlag::default(), | ||
))); | ||
folders.push(Box::new(reserved_words::reserved_words())); | ||
folders.push(Box::new(paren_remover(Default::default()))); | ||
// simplify, but keep top level dead code | ||
// e.g. import x from 'foo'; but x is not used | ||
// this must be kept for tree shaking to work | ||
folders.push(Box::new(simplifier( | ||
unresolved_mark, | ||
SimpilifyConfig { | ||
dce: dce::Config { | ||
top_level: false, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}, | ||
))); | ||
// NOTICE: remove optimize_package_imports temporarily | ||
// folders.push(Box::new(Optional { | ||
// enabled: should_optimize(file.path.to_str().unwrap(), context.clone()), | ||
|
@@ -206,7 +182,59 @@ impl Transform { | |
// ), | ||
// })); | ||
|
||
ast.transform(&mut visitors, &mut folders, file, true, context.clone())?; | ||
ast.transform(&mut visitors, &mut folders, false, context.clone())?; | ||
|
||
// run transform js in plugins | ||
try_with_handler(cm.clone(), Default::default(), |handler| { | ||
HELPERS.set(&Helpers::new(true), || { | ||
HANDLER.set(handler, || { | ||
// transform with plugin | ||
context.plugin_driver.transform_js( | ||
&PluginTransformJsParam { | ||
handler, | ||
path: file.path.to_str().unwrap(), | ||
top_level_mark, | ||
unresolved_mark, | ||
}, | ||
&mut ast.ast, | ||
&context, | ||
) | ||
}) | ||
}) | ||
})?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 集成插件转换 通过 |
||
|
||
// preset_env should go last | ||
let mut preset_folders: Vec<Box<dyn Fold>> = vec![ | ||
Box::new(swc_preset_env::preset_env( | ||
unresolved_mark, | ||
Some(comments), | ||
swc_preset_env::Config { | ||
mode: Some(swc_preset_env::Mode::Entry), | ||
targets: Some(swc_preset_env_targets_from_map( | ||
context.config.targets.clone(), | ||
)), | ||
..Default::default() | ||
}, | ||
assumptions, | ||
&mut FeatureFlag::default(), | ||
)), | ||
Box::new(reserved_words::reserved_words()), | ||
Box::new(paren_remover(Default::default())), | ||
// simplify, but keep top level dead code | ||
// e.g. import x from 'foo'; but x is not used | ||
// this must be kept for tree shaking to work | ||
Box::new(simplifier( | ||
unresolved_mark, | ||
SimpilifyConfig { | ||
dce: dce::Config { | ||
top_level: false, | ||
..Default::default() | ||
}, | ||
..Default::default() | ||
}, | ||
)), | ||
]; | ||
ast.transform(&mut vec![], &mut preset_folders, true, context.clone())?; | ||
|
||
Ok(()) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,7 @@ const test = async () => { | |
} | ||
}; | ||
module.exports = test; | ||
|
||
if (!module.parent) { | ||
test(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
{ | ||
"emotion": true | ||
"emotion": true, | ||
"targets": { | ||
"chrome": 40 | ||
}, | ||
"progress": false | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
优化 AST 转换逻辑
通过直接使用
ast.take()
方法创建module
,简化了控制流,减少了对 AST 的操作步骤。这种简化有助于提高代码的可读性和性能。