|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import type { NextConfig } from '../../../types' |
|
|
import { type WebpackLayerName, WEBPACK_LAYERS } from '../../../lib/constants' |
|
|
import { isWasm, transform } from '../../swc' |
|
|
import { getLoaderSWCOptions } from '../../swc/options' |
|
|
import path, { isAbsolute } from 'path' |
|
|
import { babelIncludeRegexes } from '../../webpack-config' |
|
|
import { isResourceInPackages } from '../../handle-externals' |
|
|
import type { TelemetryLoaderContext } from '../plugins/telemetry-plugin/telemetry-plugin' |
|
|
import { |
|
|
updateTelemetryLoaderCtxFromTransformOutput, |
|
|
type SwcTransformTelemetryOutput, |
|
|
} from '../plugins/telemetry-plugin/update-telemetry-loader-context-from-swc' |
|
|
import type { LoaderContext } from 'webpack' |
|
|
import { |
|
|
COMPILER_NAMES, |
|
|
type CompilerNameValues, |
|
|
} from '../../../shared/lib/constants' |
|
|
|
|
|
const maybeExclude = ( |
|
|
excludePath: string, |
|
|
transpilePackages: string[] |
|
|
): boolean => { |
|
|
if (babelIncludeRegexes.some((r) => r.test(excludePath))) { |
|
|
return false |
|
|
} |
|
|
|
|
|
const shouldBeBundled = isResourceInPackages(excludePath, transpilePackages) |
|
|
if (shouldBeBundled) return false |
|
|
|
|
|
return excludePath.includes('node_modules') |
|
|
} |
|
|
|
|
|
export interface SWCLoaderOptions { |
|
|
rootDir: string |
|
|
isServer: boolean |
|
|
compilerType: CompilerNameValues |
|
|
pagesDir?: string |
|
|
appDir?: string |
|
|
hasReactRefresh: boolean |
|
|
optimizeServerReact?: boolean |
|
|
nextConfig: NextConfig |
|
|
jsConfig: any |
|
|
supportedBrowsers: string[] | undefined |
|
|
swcCacheDir: string |
|
|
serverComponents?: boolean |
|
|
serverReferenceHashSalt: string |
|
|
bundleLayer?: WebpackLayerName |
|
|
esm?: boolean |
|
|
transpilePackages?: string[] |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const FORCE_TRANSPILE_CONDITIONS = |
|
|
/next\/font|next\/dynamic|use server|use client|use cache/ |
|
|
|
|
|
|
|
|
const FORCE_TRANSPILE_CONDITIONS_WITH_IMPORT = new RegExp( |
|
|
String.raw`(?:${FORCE_TRANSPILE_CONDITIONS.source})|import\s*\(` |
|
|
) |
|
|
|
|
|
async function loaderTransform( |
|
|
this: LoaderContext<SWCLoaderOptions> & TelemetryLoaderContext, |
|
|
source?: string, |
|
|
inputSourceMap?: any |
|
|
) { |
|
|
|
|
|
const filename = this.resourcePath |
|
|
|
|
|
|
|
|
if (filename.endsWith('.d.ts')) { |
|
|
return [source, inputSourceMap] |
|
|
} |
|
|
|
|
|
let loaderOptions: SWCLoaderOptions = this.getOptions() || {} |
|
|
const shouldMaybeExclude = maybeExclude( |
|
|
filename, |
|
|
loaderOptions.transpilePackages || [] |
|
|
) |
|
|
|
|
|
const trackDynamicImports = shouldTrackDynamicImports(loaderOptions) |
|
|
|
|
|
if (shouldMaybeExclude) { |
|
|
if (!source) { |
|
|
throw new Error(`Invariant might be excluded but missing source`) |
|
|
} |
|
|
|
|
|
const forceTranspileConditions = trackDynamicImports |
|
|
? FORCE_TRANSPILE_CONDITIONS_WITH_IMPORT |
|
|
: FORCE_TRANSPILE_CONDITIONS |
|
|
|
|
|
if (!forceTranspileConditions.test(source)) { |
|
|
return [source, inputSourceMap] |
|
|
} |
|
|
} |
|
|
|
|
|
const { |
|
|
isServer, |
|
|
rootDir, |
|
|
pagesDir, |
|
|
appDir, |
|
|
hasReactRefresh, |
|
|
nextConfig, |
|
|
jsConfig, |
|
|
supportedBrowsers, |
|
|
swcCacheDir, |
|
|
serverComponents, |
|
|
serverReferenceHashSalt, |
|
|
bundleLayer, |
|
|
esm, |
|
|
} = loaderOptions |
|
|
const isPageFile = pagesDir ? filename.startsWith(pagesDir) : false |
|
|
const relativeFilePathFromRoot = path.relative(rootDir, filename) |
|
|
|
|
|
const swcOptions = getLoaderSWCOptions({ |
|
|
pagesDir, |
|
|
appDir, |
|
|
filename, |
|
|
isServer, |
|
|
isPageFile, |
|
|
development: |
|
|
this.mode === 'development' || |
|
|
!!nextConfig.experimental?.allowDevelopmentBuild, |
|
|
isCacheComponents: nextConfig.experimental?.cacheComponents, |
|
|
hasReactRefresh, |
|
|
modularizeImports: nextConfig?.modularizeImports, |
|
|
optimizePackageImports: nextConfig?.experimental?.optimizePackageImports, |
|
|
swcPlugins: nextConfig?.experimental?.swcPlugins, |
|
|
compilerOptions: nextConfig?.compiler, |
|
|
optimizeServerReact: nextConfig?.experimental?.optimizeServerReact, |
|
|
jsConfig, |
|
|
supportedBrowsers, |
|
|
swcCacheDir, |
|
|
relativeFilePathFromRoot, |
|
|
serverComponents, |
|
|
serverReferenceHashSalt, |
|
|
bundleLayer, |
|
|
esm, |
|
|
cacheHandlers: nextConfig.experimental?.cacheHandlers, |
|
|
useCacheEnabled: nextConfig.experimental?.useCache, |
|
|
trackDynamicImports, |
|
|
}) |
|
|
|
|
|
const programmaticOptions = { |
|
|
...swcOptions, |
|
|
filename, |
|
|
inputSourceMap: inputSourceMap ? JSON.stringify(inputSourceMap) : undefined, |
|
|
|
|
|
|
|
|
sourceMaps: this.sourceMap, |
|
|
inlineSourcesContent: this.sourceMap, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sourceFileName: filename, |
|
|
} |
|
|
|
|
|
if (!programmaticOptions.inputSourceMap) { |
|
|
delete programmaticOptions.inputSourceMap |
|
|
} |
|
|
|
|
|
|
|
|
if ( |
|
|
this.mode && |
|
|
programmaticOptions.jsc && |
|
|
programmaticOptions.jsc.transform && |
|
|
programmaticOptions.jsc.transform.react && |
|
|
!Object.prototype.hasOwnProperty.call( |
|
|
programmaticOptions.jsc.transform.react, |
|
|
'development' |
|
|
) |
|
|
) { |
|
|
programmaticOptions.jsc.transform.react.development = |
|
|
this.mode === 'development' |
|
|
} |
|
|
|
|
|
return transform(source as any, programmaticOptions).then( |
|
|
( |
|
|
output: { |
|
|
code: string |
|
|
map?: string |
|
|
} & SwcTransformTelemetryOutput |
|
|
) => { |
|
|
updateTelemetryLoaderCtxFromTransformOutput(this, output) |
|
|
return [output.code, output.map ? JSON.parse(output.map) : undefined] |
|
|
} |
|
|
) |
|
|
} |
|
|
|
|
|
function shouldTrackDynamicImports(loaderOptions: SWCLoaderOptions): boolean { |
|
|
|
|
|
|
|
|
const { nextConfig, bundleLayer, compilerType } = loaderOptions |
|
|
return ( |
|
|
!!nextConfig.experimental?.cacheComponents && |
|
|
|
|
|
|
|
|
compilerType === COMPILER_NAMES.server && |
|
|
(bundleLayer === WEBPACK_LAYERS.reactServerComponents || |
|
|
bundleLayer === WEBPACK_LAYERS.serverSideRendering) |
|
|
) |
|
|
} |
|
|
|
|
|
const EXCLUDED_PATHS = |
|
|
/[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual__)[\\/]/g |
|
|
|
|
|
export function pitch(this: any) { |
|
|
const callback = this.async() |
|
|
let loaderOptions: SWCLoaderOptions = this.getOptions() || {} |
|
|
|
|
|
const shouldMaybeExclude = maybeExclude( |
|
|
this.resourcePath, |
|
|
loaderOptions.transpilePackages || [] |
|
|
) |
|
|
|
|
|
;(async () => { |
|
|
if ( |
|
|
|
|
|
!shouldMaybeExclude && |
|
|
|
|
|
!process.versions.pnp && |
|
|
!EXCLUDED_PATHS.test(this.resourcePath) && |
|
|
this.loaders.length - 1 === this.loaderIndex && |
|
|
isAbsolute(this.resourcePath) && |
|
|
!(await isWasm()) |
|
|
) { |
|
|
this.addDependency(this.resourcePath) |
|
|
return loaderTransform.call(this) |
|
|
} |
|
|
})().then((r) => { |
|
|
if (r) return callback(null, ...r) |
|
|
callback() |
|
|
}, callback) |
|
|
} |
|
|
|
|
|
export default function swcLoader( |
|
|
this: any, |
|
|
inputSource: string, |
|
|
inputSourceMap: any |
|
|
) { |
|
|
const callback = this.async() |
|
|
loaderTransform.call(this, inputSource, inputSourceMap).then( |
|
|
([transformedSource, outputSourceMap]: any) => { |
|
|
callback(null, transformedSource, outputSourceMap || inputSourceMap) |
|
|
}, |
|
|
(err: Error) => { |
|
|
callback(err) |
|
|
} |
|
|
) |
|
|
} |
|
|
|
|
|
|
|
|
export const raw = true |
|
|
|