Unverified Commit 494e37c0 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add experimental.swcFileReading flag to disable file reading in swc (#31995)

automatically disable swc file reading when wasm builds are used and for virtual or yarn cache paths



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
Co-authored-by: 's avatarMaia Teegarden <2865858+padmaia@users.noreply.github.com>
parent 5747435d
...@@ -19,6 +19,7 @@ async function loadWasm() { ...@@ -19,6 +19,7 @@ async function loadWasm() {
bindings = await bindings.default() bindings = await bindings.default()
} }
return { return {
isWasm: true,
transform(src, options) { transform(src, options) {
return Promise.resolve( return Promise.resolve(
bindings.transformSync(src.toString(), options) bindings.transformSync(src.toString(), options)
...@@ -63,6 +64,7 @@ function loadNative() { ...@@ -63,6 +64,7 @@ function loadNative() {
if (bindings) { if (bindings) {
return { return {
isWasm: false,
transform(src, options) { transform(src, options) {
const isModule = const isModule =
typeof src !== undefined && typeof src !== undefined &&
...@@ -133,6 +135,11 @@ function toBuffer(t) { ...@@ -133,6 +135,11 @@ function toBuffer(t) {
return Buffer.from(JSON.stringify(t)) return Buffer.from(JSON.stringify(t))
} }
export async function isWasm() {
let bindings = await loadBindings()
return bindings.isWasm
}
export async function transform(src, options) { export async function transform(src, options) {
let bindings = await loadBindings() let bindings = await loadBindings()
return bindings.transform(src, options) return bindings.transform(src, options)
......
...@@ -445,6 +445,7 @@ export default async function getBaseWebpackConfig( ...@@ -445,6 +445,7 @@ export default async function getBaseWebpackConfig(
isServer: isMiddleware || isServer, isServer: isMiddleware || isServer,
pagesDir, pagesDir,
hasReactRefresh: !isMiddleware && hasReactRefresh, hasReactRefresh: !isMiddleware && hasReactRefresh,
fileReading: config.experimental.swcFileReading,
nextConfig: config, nextConfig: config,
jsConfig, jsConfig,
}, },
......
...@@ -26,7 +26,7 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ...@@ -26,7 +26,7 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. DEALINGS IN THE SOFTWARE.
*/ */
import { transform } from '../../swc' import { isWasm, transform } from '../../swc'
import { getLoaderSWCOptions } from '../../swc/options' import { getLoaderSWCOptions } from '../../swc/options'
import { isAbsolute } from 'path' import { isAbsolute } from 'path'
...@@ -93,26 +93,30 @@ async function loaderTransform(parentTrace, source, inputSourceMap) { ...@@ -93,26 +93,30 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
) )
} }
const EXCLUDED_PATHS =
/[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual__)[\\/]/g
export function pitch() { export function pitch() {
if ( const callback = this.async()
this.loaders.length - 1 === this.loaderIndex && ;(async () => {
isAbsolute(this.resourcePath) let loaderOptions = this.getOptions() || {}
) { if (
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader') loaderOptions.fileReading &&
const callback = this.async() !EXCLUDED_PATHS.test(this.resourcePath) &&
loaderSpan this.loaders.length - 1 === this.loaderIndex &&
.traceAsyncFn(() => loaderTransform.call(this, loaderSpan)) isAbsolute(this.resourcePath) &&
.then( !(await isWasm())
([transformedSource, outputSourceMap]) => { ) {
this.addDependency(this.resourcePath) const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
callback(null, transformedSource, outputSourceMap) this.addDependency(this.resourcePath)
}, return loaderSpan.traceAsyncFn(() =>
(err) => { loaderTransform.call(this, loaderSpan)
this.addDependency(this.resourcePath)
callback(err)
}
) )
} }
})().then((r) => {
if (r) return callback(null, ...r)
callback()
}, callback)
} }
export default function swcLoader(inputSource, inputSourceMap) { export default function swcLoader(inputSource, inputSourceMap) {
......
...@@ -139,6 +139,7 @@ export type NextConfig = { [key: string]: any } & { ...@@ -139,6 +139,7 @@ export type NextConfig = { [key: string]: any } & {
} }
styledComponents?: boolean styledComponents?: boolean
swcMinify?: boolean swcMinify?: boolean
swcFileReading?: boolean
cpus?: number cpus?: number
sharedPool?: boolean sharedPool?: boolean
plugins?: boolean plugins?: boolean
...@@ -243,6 +244,7 @@ export const defaultConfig: NextConfig = { ...@@ -243,6 +244,7 @@ export const defaultConfig: NextConfig = {
reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0, reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0,
disableOptimizedLoading: false, disableOptimizedLoading: false,
gzipSize: true, gzipSize: true,
swcFileReading: true,
craCompat: false, craCompat: false,
esmExternals: true, esmExternals: true,
// default to 50MB limit // default to 50MB limit
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment