Skip to content

Commit

Permalink
Filter local import
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Apr 1, 2022
1 parent b9fbb5b commit 9b8e95a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/next-swc/crates/core/src/next_ssg.rs
Expand Up @@ -360,7 +360,11 @@ impl Fold for NextSsg {
| ImportSpecifier::Default(ImportDefaultSpecifier { local, .. })
| ImportSpecifier::Namespace(ImportStarAsSpecifier { local, .. }) => {
if self.should_remove(local.to_id()) {
if self.state.is_server_props {
if self.state.is_server_props
// filter out non-packages import
// third part packages must start with `a-z` or `@`
&& import_src.starts_with(|c: char| c.is_ascii_lowercase() || c == '@')
{
self.state
.eliminated_packages
.borrow_mut()
Expand Down
59 changes: 59 additions & 0 deletions packages/next-swc/crates/core/tests/telemetry.rs
@@ -0,0 +1,59 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;

use fxhash::FxHashSet;
use next_swc::next_ssg::next_ssg;
use once_cell::sync::Lazy;
use swc::{try_with_handler, Compiler};
use swc_common::{FileName, FilePathMapping, SourceMap};
use swc_ecmascript::transforms::pass::noop;

static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));

Arc::new(Compiler::new(cm))
});

#[test]
fn should_collect_estimated_third_part_packages() {
let eliminated_packages: Rc<RefCell<FxHashSet<String>>> = Default::default();
let fm = COMPILER.cm.new_source_file(
FileName::Real("fixture.js".into()),
r#"import http from 'http'
import { hash } from '@napi-rs/bcrypt'
import { omit } from '~/utils/omit'
import config from './data.json'
export default () => 'Hello World'
export function getServerSideProps() {
console.log(http)
console.log(config)
return { props: { digest: hash('hello') } }
}
"#
.to_owned(),
);
assert!(
try_with_handler(COMPILER.cm.clone(), Default::default(), |handler| {
COMPILER.process_js_with_custom_pass(
fm,
None,
handler,
&Default::default(),
|_, _| next_ssg(eliminated_packages.clone()),
|_, _| noop(),
)
})
.is_ok()
);
assert_eq!(
eliminated_packages
.borrow()
.iter()
.collect::<Vec<&String>>(),
vec!["@napi-rs/bcrypt", "http"]
);
}

0 comments on commit 9b8e95a

Please sign in to comment.