2021-07-27 17:07:28 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2017 The swc Project Developers
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any
|
|
|
|
person obtaining a copy of this software and associated
|
|
|
|
documentation files (the "Software"), to deal in the
|
|
|
|
Software without restriction, including without
|
|
|
|
limitation the rights to use, copy, modify, merge,
|
|
|
|
publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software
|
|
|
|
is furnished to do so, subject to the following
|
|
|
|
conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice
|
|
|
|
shall be included in all copies or substantial portions
|
|
|
|
of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
|
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
|
|
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
|
|
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
|
|
|
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
|
|
|
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-12-02 05:16:15 +01:00
|
|
|
import { isWasm, transform } from '../../swc'
|
2021-11-08 17:35:04 +01:00
|
|
|
import { getLoaderSWCOptions } from '../../swc/options'
|
2021-11-25 15:00:14 +01:00
|
|
|
import { isAbsolute } from 'path'
|
2021-07-27 17:07:28 +02:00
|
|
|
|
2021-08-06 16:07:36 +02:00
|
|
|
async function loaderTransform(parentTrace, source, inputSourceMap) {
|
|
|
|
// Make the loader async
|
|
|
|
const filename = this.resourcePath
|
2021-07-27 17:07:28 +02:00
|
|
|
|
2021-11-02 16:13:15 +01:00
|
|
|
let loaderOptions = this.getOptions() || {}
|
2021-07-27 17:07:28 +02:00
|
|
|
|
2022-05-17 17:09:34 +02:00
|
|
|
const {
|
|
|
|
isServer,
|
|
|
|
pagesDir,
|
|
|
|
hasReactRefresh,
|
|
|
|
nextConfig,
|
|
|
|
jsConfig,
|
|
|
|
supportedBrowsers,
|
2022-06-29 23:59:52 +02:00
|
|
|
swcCacheDir,
|
2022-05-17 17:09:34 +02:00
|
|
|
} = loaderOptions
|
2021-09-13 12:20:38 +02:00
|
|
|
const isPageFile = filename.startsWith(pagesDir)
|
|
|
|
|
2021-11-08 17:35:04 +01:00
|
|
|
const swcOptions = getLoaderSWCOptions({
|
2021-09-14 18:13:11 +02:00
|
|
|
pagesDir,
|
2021-10-30 14:51:42 +02:00
|
|
|
filename,
|
2021-09-13 12:20:38 +02:00
|
|
|
isServer: isServer,
|
|
|
|
isPageFile,
|
2021-08-06 16:07:36 +02:00
|
|
|
development: this.mode === 'development',
|
2021-10-24 15:25:02 +02:00
|
|
|
hasReactRefresh,
|
2021-11-13 18:26:13 +01:00
|
|
|
nextConfig,
|
|
|
|
jsConfig,
|
2022-05-17 17:09:34 +02:00
|
|
|
supportedBrowsers,
|
2022-06-29 23:59:52 +02:00
|
|
|
swcCacheDir,
|
2021-08-06 16:07:36 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
const programmaticOptions = {
|
|
|
|
...swcOptions,
|
|
|
|
filename,
|
|
|
|
inputSourceMap: inputSourceMap ? JSON.stringify(inputSourceMap) : undefined,
|
|
|
|
|
|
|
|
// Set the default sourcemap behavior based on Webpack's mapping flag,
|
|
|
|
sourceMaps: this.sourceMap,
|
2021-10-09 16:39:53 +02:00
|
|
|
inlineSourcesContent: this.sourceMap,
|
2021-08-06 16:07:36 +02:00
|
|
|
|
|
|
|
// Ensure that Webpack will get a full absolute path in the sourcemap
|
|
|
|
// so that it can properly map the module back to its internal cached
|
|
|
|
// modules.
|
|
|
|
sourceFileName: filename,
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!programmaticOptions.inputSourceMap) {
|
|
|
|
delete programmaticOptions.inputSourceMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// auto detect development mode
|
|
|
|
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'
|
2021-07-27 17:07:28 +02:00
|
|
|
}
|
2021-08-06 16:07:36 +02:00
|
|
|
|
|
|
|
const swcSpan = parentTrace.traceChild('next-swc-transform')
|
|
|
|
return swcSpan.traceAsyncFn(() =>
|
|
|
|
transform(source, programmaticOptions).then((output) => {
|
2022-04-01 17:08:44 +02:00
|
|
|
if (output.eliminatedPackages && this.eliminatedPackages) {
|
|
|
|
for (const pkg of JSON.parse(output.eliminatedPackages)) {
|
|
|
|
this.eliminatedPackages.add(pkg)
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 16:07:36 +02:00
|
|
|
return [output.code, output.map ? JSON.parse(output.map) : undefined]
|
|
|
|
})
|
|
|
|
)
|
2021-07-27 17:07:28 +02:00
|
|
|
}
|
|
|
|
|
2021-12-02 05:16:15 +01:00
|
|
|
const EXCLUDED_PATHS =
|
|
|
|
/[\\/](cache[\\/][^\\/]+\.zip[\\/]node_modules|__virtual__)[\\/]/g
|
|
|
|
|
2021-11-25 15:00:14 +01:00
|
|
|
export function pitch() {
|
2021-12-02 05:16:15 +01:00
|
|
|
const callback = this.async()
|
|
|
|
;(async () => {
|
|
|
|
let loaderOptions = this.getOptions() || {}
|
|
|
|
if (
|
2022-01-14 16:43:30 +01:00
|
|
|
// TODO: investigate swc file reading in PnP mode?
|
|
|
|
!process.versions.pnp &&
|
2021-12-02 05:16:15 +01:00
|
|
|
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)
|
2021-11-25 15:00:14 +01:00
|
|
|
)
|
2021-12-02 05:16:15 +01:00
|
|
|
}
|
|
|
|
})().then((r) => {
|
|
|
|
if (r) return callback(null, ...r)
|
|
|
|
callback()
|
|
|
|
}, callback)
|
2021-11-25 15:00:14 +01:00
|
|
|
}
|
|
|
|
|
2021-08-06 16:07:36 +02:00
|
|
|
export default function swcLoader(inputSource, inputSourceMap) {
|
2021-08-25 10:47:16 +02:00
|
|
|
const loaderSpan = this.currentTraceSpan.traceChild('next-swc-loader')
|
2021-08-06 16:07:36 +02:00
|
|
|
const callback = this.async()
|
|
|
|
loaderSpan
|
|
|
|
.traceAsyncFn(() =>
|
|
|
|
loaderTransform.call(this, loaderSpan, inputSource, inputSourceMap)
|
|
|
|
)
|
|
|
|
.then(
|
2021-09-16 16:11:30 +02:00
|
|
|
([transformedSource, outputSourceMap]) => {
|
|
|
|
callback(null, transformedSource, outputSourceMap || inputSourceMap)
|
|
|
|
},
|
2021-08-06 16:07:36 +02:00
|
|
|
(err) => {
|
2021-09-16 16:11:30 +02:00
|
|
|
callback(err)
|
2021-08-06 16:07:36 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2021-11-05 15:12:06 +01:00
|
|
|
|
|
|
|
// accept Buffers instead of strings
|
|
|
|
export const raw = true
|