Skip to content
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

next/dynamic: Accept pagesDir #29055

Merged
merged 7 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/build/swc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/next/build/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ swc_ecma_preset_env = "0.42"
fxhash = "0.2.1"
retain_mut = "0.1.3"
log = "0.4.14"
pathdiff = "0.2.0"


[build-dependencies]
Expand Down
425 changes: 236 additions & 189 deletions packages/next/build/swc/src/next_dynamic.rs

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions packages/next/build/swc/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
use anyhow::{Context as _, Error};
use napi::{CallContext, Env, JsBoolean, JsObject, JsString, Task};
use serde::Deserialize;
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};
use swc::{try_with_handler, Compiler, TransformOutput};
use swc_common::{chain, pass::Optional, FileName, SourceFile};
use swc_ecmascript::ast::Program;
Expand All @@ -51,13 +51,16 @@ pub enum Input {
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
#[serde(rename_all = "camelCase")]
pub struct TransformOptions {
#[serde(flatten)]
swc: swc::config::Options,
pub swc: swc::config::Options,

#[serde(default)]
pub disable_next_ssg: bool,

#[serde(default)]
pub pages_dir: Option<PathBuf>,
}

pub struct TransformTask {
Expand All @@ -78,7 +81,7 @@ impl Task for TransformTask {
hook_optimizer(),
Optional::new(next_ssg(), !self.options.disable_next_ssg),
amp_attributes(),
next_dynamic(s.name.clone()),
next_dynamic(s.name.clone(), self.options.pages_dir.clone()),
);
self.c.process_js_with_custom_pass(
s.clone(),
Expand Down Expand Up @@ -175,3 +178,12 @@ pub fn transform_sync(cx: CallContext) -> napi::Result<JsObject> {
))
})
}

#[test]
fn test_deser() {
const JSON_STR: &str = r#"{"jsc":{"parser":{"syntax":"ecmascript","dynamicImport":true,"jsx":true},"transform":{"react":{"runtime":"automatic","pragma":"React.createElement","pragmaFrag":"React.Fragment","throwIfNamespace":true,"development":false,"useBuiltins":true}},"target":"es5"},"filename":"/Users/timneutkens/projects/next.js/packages/next/dist/client/next.js","sourceMaps":false,"sourceFileName":"/Users/timneutkens/projects/next.js/packages/next/dist/client/next.js"}"#;

let tr: TransformOptions = serde_json::from_str(&JSON_STR).unwrap();

println!("{:#?}", tr);
}
89 changes: 45 additions & 44 deletions packages/next/build/swc/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::path::PathBuf;
use swc_common::{chain, comments::SingleThreadedComments, FileName};
use swc_ecma_transforms_testing::{test, test_fixture};
use swc_ecmascript::{
parser::{EsConfig, Syntax},
transforms::react::jsx,
parser::{EsConfig, Syntax},
transforms::react::jsx,
};
use testing::fixture;

Expand All @@ -18,59 +18,60 @@ mod next_dynamic;
mod next_ssg;

fn syntax() -> Syntax {
Syntax::Es(EsConfig {
jsx: true,
dynamic_import: true,
..Default::default()
})
Syntax::Es(EsConfig {
jsx: true,
dynamic_import: true,
..Default::default()
})
}

#[fixture("tests/fixture/amp/**/input.js")]
fn amp_attributes_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(syntax(), &|_tr| amp_attributes(), &input, &output);
let output = input.parent().unwrap().join("output.js");
test_fixture(syntax(), &|_tr| amp_attributes(), &input, &output);
}

#[fixture("tests/fixture/next-dynamic/**/input.js")]
fn next_dynamic_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|_tr| {
next_dynamic(FileName::Real(PathBuf::from(
"/some-project/src/some-file.js",
)))
},
&input,
&output,
);
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|_tr| {
next_dynamic(
FileName::Real(PathBuf::from("/some-project/src/some-file.js")),
Some("/some-project/src".into()),
)
},
&input,
&output,
);
}

#[fixture("tests/fixture/ssg/**/input.js")]
fn next_ssg_fixture(input: PathBuf) {
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|tr| {
let jsx = jsx::<SingleThreadedComments>(
tr.cm.clone(),
None,
swc_ecmascript::transforms::react::Options {
next: false,
runtime: None,
import_source: "".into(),
pragma: "__jsx".into(),
pragma_frag: "__jsxFrag".into(),
throw_if_namespace: false,
development: false,
use_builtins: true,
use_spread: true,
refresh: Default::default(),
let output = input.parent().unwrap().join("output.js");
test_fixture(
syntax(),
&|tr| {
let jsx = jsx::<SingleThreadedComments>(
tr.cm.clone(),
None,
swc_ecmascript::transforms::react::Options {
next: false,
runtime: None,
import_source: "".into(),
pragma: "__jsx".into(),
pragma_frag: "__jsxFrag".into(),
throw_if_namespace: false,
development: false,
use_builtins: true,
use_spread: true,
refresh: Default::default(),
},
);
chain!(next_ssg(), jsx)
},
);
chain!(next_ssg(), jsx)
},
&input,
&output,
);
&input,
&output,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import dynamic2 from 'next/dynamic'
const DynamicComponent1 = dynamic1(() => import('../components/hello1'), {
loadableGenerated: {
webpack: () => [require.resolveWeak('/some-project/src/some-file.js')],
modules: ['/some-project/src/some-file.js -> ' + '../components/hello1'],
modules: ['some-file.js -> ' + '../components/hello1'],
},
})
const DynamicComponent2 = dynamic2(() => import('../components/hello2'), {
loadableGenerated: {
webpack: () => [require.resolveWeak('/some-project/src/some-file.js')],
modules: ['/some-project/src/some-file.js -> ' + '../components/hello2'],
modules: ['some-file.js -> ' + '../components/hello2'],
},
})
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import dynamic from 'next/dynamic'
import somethingElse from 'something-else'

const DynamicComponent = dynamic(() => import('../components/hello'), {
loadableGenerated: {
webpack: () => [require.resolveWeak('/some-project/src/some-file.js')],
modules: ['/some-project/src/some-file.js -> ' + '../components/hello'],
modules: ['some-file.js -> ' + '../components/hello'],
},
})
somethingElse.dynamic('should not be transformed')
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import dynamic from 'next/dynamic'

const DynamicComponent = dynamic(() => import('../components/hello'), {
loadableGenerated: {
webpack: () => [require.resolveWeak('/some-project/src/some-file.js')],
modules: ['/some-project/src/some-file.js -> ' + '../components/hello'],
modules: ['some-file.js -> ' + '../components/hello'],
},
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import dynamic from 'next/dynamic'

const DynamicComponentWithCustomLoading = dynamic(
() => import('../components/hello'),
{ loading: () => <p>...</p> },
{
loading: () => <p>...</p>,
},
{
loadableGenerated: {
webpack: () => [require.resolveWeak('/some-project/src/some-file.js')],
modules: ['/some-project/src/some-file.js -> ' + '../components/hello'],
modules: ['some-file.js -> ' + '../components/hello'],
},
loading: () => <p>...</p>,
}
Expand Down