Skip to content

Commit

Permalink
Merge branch 'canary' into fix/escape-flight
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff authored May 17, 2022
2 parents 4b770cd + fe3d6b7 commit ae272c8
Show file tree
Hide file tree
Showing 21 changed files with 590 additions and 83 deletions.
4 changes: 2 additions & 2 deletions packages/next-swc/Cargo.lock

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

21 changes: 15 additions & 6 deletions packages/next-swc/crates/core/src/next_dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl Fold for NextDynamicPatcher {
})))];

let mut has_ssr_false = false;
let mut has_suspense = false;

if expr.args.len() == 2 {
if let Expr::Object(ObjectLit {
Expand Down Expand Up @@ -250,21 +251,29 @@ impl Fold for NextDynamicPatcher {
if let Some(Lit::Bool(Bool {
value: false,
span: _,
})) = match &**value {
Expr::Lit(lit) => Some(lit),
_ => None,
} {
})) = value.as_lit()
{
has_ssr_false = true
}
}
if sym == "suspense" {
if let Some(Lit::Bool(Bool {
value: true,
span: _,
})) = value.as_lit()
{
has_suspense = true
}
}
}
}
}
props.extend(options_props.iter().cloned());
}
}

if has_ssr_false && self.is_server {
// Don't need to strip the `loader` argument if suspense is true
// See https://github.com/vercel/next.js/issues/36636 for background
if has_ssr_false && !has_suspense && self.is_server {
expr.args[0] = Lit::Null(Null { span: DUMMY_SP }).as_arg();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ const DynamicClientOnlyComponent = dynamic(
() => import('../components/hello'),
{ ssr: false }
)

const DynamicClientOnlyComponentWithSuspense = dynamic(
() => import('../components/hello'),
{ ssr: false, suspense: true }
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ const DynamicClientOnlyComponent = dynamic(()=>import('../components/hello')
},
ssr: false
});
const DynamicClientOnlyComponentWithSuspense = dynamic(()=>import('../components/hello')
, {
loadableGenerated: {
modules: [
"some-file.js -> " + "../components/hello"
]
},
ssr: false,
suspense: true
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ const DynamicClientOnlyComponent = dynamic(()=>import('../components/hello')
},
ssr: false
});
const DynamicClientOnlyComponentWithSuspense = dynamic(()=>import('../components/hello')
, {
loadableGenerated: {
webpack: ()=>[
require.resolveWeak("../components/hello")
]
},
ssr: false,
suspense: true
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ const DynamicClientOnlyComponent = dynamic(null, {
},
ssr: false
});
const DynamicClientOnlyComponentWithSuspense = dynamic(()=>import('../components/hello')
, {
loadableGenerated: {
modules: [
"some-file.js -> " + "../components/hello"
]
},
ssr: false,
suspense: true
});
14 changes: 13 additions & 1 deletion packages/next/build/swc/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ function getBaseSWCOptions({
}
: null,
removeConsole: nextConfig?.compiler?.removeConsole,
reactRemoveProperties: nextConfig?.compiler?.reactRemoveProperties,
// disable "reactRemoveProperties" when "jest" is true
// otherwise the setting from next.config.js will be used
reactRemoveProperties: jest
? false
: nextConfig?.compiler?.reactRemoveProperties,
modularizeImports: nextConfig?.experimental?.modularizeImports,
relay: nextConfig?.compiler?.relay,
emotion: getEmotionOptions(nextConfig, development),
Expand Down Expand Up @@ -188,6 +192,7 @@ export function getLoaderSWCOptions({
hasReactRefresh,
nextConfig,
jsConfig,
supportedBrowsers,
// This is not passed yet as "paths" resolving is handled by webpack currently.
// resolvedBaseUrl,
}) {
Expand Down Expand Up @@ -238,6 +243,13 @@ export function getLoaderSWCOptions({
isServer,
pagesDir,
isPageFile,
...(supportedBrowsers && supportedBrowsers.length > 0
? {
env: {
targets: supportedBrowsers,
},
}
: {}),
}
}
}
29 changes: 25 additions & 4 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
REACT_LOADABLE_MANIFEST,
SERVERLESS_DIRECTORY,
SERVER_DIRECTORY,
MODERN_BROWSERSLIST_TARGET,
} from '../shared/lib/constants'
import { execOnce } from '../shared/lib/utils'
import { NextConfigComplete } from '../server/config-shared'
Expand Down Expand Up @@ -63,16 +64,31 @@ const watchOptions = Object.freeze({

function getSupportedBrowsers(
dir: string,
isDevelopment: boolean
isDevelopment: boolean,
config: NextConfigComplete
): string[] | undefined {
let browsers: any
try {
browsers = browserslist.loadConfig({
const browsersListConfig = browserslist.loadConfig({
path: dir,
env: isDevelopment ? 'development' : 'production',
})
// Running `browserslist` resolves `extends` and other config features into a list of browsers
if (browsersListConfig && browsersListConfig.length > 0) {
browsers = browserslist(browsersListConfig)
}
} catch {}
return browsers

// When user has browserslist use that target
if (browsers && browsers.length > 0) {
return browsers
}

// When user does not have browserslist use the default target
// When `experimental.legacyBrowsers: false` the modern default is used
return config.experimental.legacyBrowsers
? undefined
: MODERN_BROWSERSLIST_TARGET
}

type ExcludesFalse = <T>(x: T | false) => x is T
Expand Down Expand Up @@ -339,7 +355,8 @@ export default async function getBaseWebpackConfig(
dir,
config
)
const supportedBrowsers = await getSupportedBrowsers(dir, dev)
const supportedBrowsers = await getSupportedBrowsers(dir, dev, config)

const hasRewrites =
rewrites.beforeFiles.length > 0 ||
rewrites.afterFiles.length > 0 ||
Expand Down Expand Up @@ -466,6 +483,9 @@ export default async function getBaseWebpackConfig(
fileReading: config.experimental.swcFileReading,
nextConfig: config,
jsConfig,
supportedBrowsers: config.experimental.browsersListForSwc
? supportedBrowsers
: undefined,
},
}
: {
Expand Down Expand Up @@ -1783,6 +1803,7 @@ export default async function getBaseWebpackConfig(
relay: config.compiler?.relay,
emotion: config.experimental?.emotion,
modularizeImports: config.experimental?.modularizeImports,
legacyBrowsers: config.experimental?.legacyBrowsers,
})

const cache: any = {
Expand Down
11 changes: 9 additions & 2 deletions packages/next/build/webpack/loaders/next-swc-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {

let loaderOptions = this.getOptions() || {}

const { isServer, pagesDir, hasReactRefresh, nextConfig, jsConfig } =
loaderOptions
const {
isServer,
pagesDir,
hasReactRefresh,
nextConfig,
jsConfig,
supportedBrowsers,
} = loaderOptions
const isPageFile = filename.startsWith(pagesDir)

const swcOptions = getLoaderSWCOptions({
Expand All @@ -49,6 +55,7 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
hasReactRefresh,
nextConfig,
jsConfig,
supportedBrowsers,
})

const programmaticOptions = {
Expand Down
Loading

0 comments on commit ae272c8

Please sign in to comment.