Skip to content

Commit

Permalink
add experimental.swcFileReading flag to disable file reading in swc (v…
Browse files Browse the repository at this point in the history
…ercel#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: Maia Teegarden <[email protected]>
  • Loading branch information
sokra and padmaia authored Dec 2, 2021
1 parent 5747435 commit 494e37c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
7 changes: 7 additions & 0 deletions packages/next/build/swc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ async function loadWasm() {
bindings = await bindings.default()
}
return {
isWasm: true,
transform(src, options) {
return Promise.resolve(
bindings.transformSync(src.toString(), options)
Expand Down Expand Up @@ -63,6 +64,7 @@ function loadNative() {

if (bindings) {
return {
isWasm: false,
transform(src, options) {
const isModule =
typeof src !== undefined &&
Expand Down Expand Up @@ -133,6 +135,11 @@ function toBuffer(t) {
return Buffer.from(JSON.stringify(t))
}

export async function isWasm() {
let bindings = await loadBindings()
return bindings.isWasm
}

export async function transform(src, options) {
let bindings = await loadBindings()
return bindings.transform(src, options)
Expand Down
1 change: 1 addition & 0 deletions packages/next/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ export default async function getBaseWebpackConfig(
isServer: isMiddleware || isServer,
pagesDir,
hasReactRefresh: !isMiddleware && hasReactRefresh,
fileReading: config.experimental.swcFileReading,
nextConfig: config,
jsConfig,
},
Expand Down
42 changes: 23 additions & 19 deletions packages/next/build/webpack/loaders/next-swc-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

import { transform } from '../../swc'
import { isWasm, transform } from '../../swc'
import { getLoaderSWCOptions } from '../../swc/options'
import { isAbsolute } from 'path'

Expand Down Expand Up @@ -93,26 +93,30 @@ async function loaderTransform(parentTrace, source, inputSourceMap) {
)
}

const EXCLUDED_PATHS =
/[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual__)[\\/]/g

export function pitch() {
if (
this.loaders.length - 1 === this.loaderIndex &&
isAbsolute(this.resourcePath)
) {
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
const callback = this.async()
loaderSpan
.traceAsyncFn(() => loaderTransform.call(this, loaderSpan))
.then(
([transformedSource, outputSourceMap]) => {
this.addDependency(this.resourcePath)
callback(null, transformedSource, outputSourceMap)
},
(err) => {
this.addDependency(this.resourcePath)
callback(err)
}
const callback = this.async()
;(async () => {
let loaderOptions = this.getOptions() || {}
if (
loaderOptions.fileReading &&
!EXCLUDED_PATHS.test(this.resourcePath) &&
this.loaders.length - 1 === this.loaderIndex &&
isAbsolute(this.resourcePath) &&
!(await isWasm())
) {
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
this.addDependency(this.resourcePath)
return loaderSpan.traceAsyncFn(() =>
loaderTransform.call(this, loaderSpan)
)
}
}
})().then((r) => {
if (r) return callback(null, ...r)
callback()
}, callback)
}

export default function swcLoader(inputSource, inputSourceMap) {
Expand Down
2 changes: 2 additions & 0 deletions packages/next/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export type NextConfig = { [key: string]: any } & {
}
styledComponents?: boolean
swcMinify?: boolean
swcFileReading?: boolean
cpus?: number
sharedPool?: boolean
plugins?: boolean
Expand Down Expand Up @@ -243,6 +244,7 @@ export const defaultConfig: NextConfig = {
reactRoot: Number(process.env.NEXT_PRIVATE_REACT_ROOT) > 0,
disableOptimizedLoading: false,
gzipSize: true,
swcFileReading: true,
craCompat: false,
esmExternals: true,
// default to 50MB limit
Expand Down

0 comments on commit 494e37c

Please sign in to comment.