.
This commit is contained in:
+20
@@ -0,0 +1,20 @@
|
||||
import type { NextBabelLoaderOptions, NextJsLoaderContext } from './types';
|
||||
import { type SourceMap, type BabelLoaderTransformOptions } from './util';
|
||||
/**
|
||||
* An internal (non-exported) type used by babel.
|
||||
*/
|
||||
export type ResolvedBabelConfig = {
|
||||
options: BabelLoaderTransformOptions;
|
||||
passes: BabelPluginPasses;
|
||||
externalDependencies: ReadonlyArray<string>;
|
||||
};
|
||||
export type BabelPlugin = unknown;
|
||||
export type BabelPluginPassList = ReadonlyArray<BabelPlugin>;
|
||||
export type BabelPluginPasses = ReadonlyArray<BabelPluginPassList>;
|
||||
export default function getConfig(ctx: NextJsLoaderContext, { source, target, loaderOptions, filename, inputSourceMap, }: {
|
||||
source: string;
|
||||
loaderOptions: NextBabelLoaderOptions;
|
||||
target: string;
|
||||
filename: string;
|
||||
inputSourceMap?: SourceMap | undefined;
|
||||
}): Promise<ResolvedBabelConfig | null>;
|
||||
+429
@@ -0,0 +1,429 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "default", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return getConfig;
|
||||
}
|
||||
});
|
||||
const _nodefs = require("node:fs");
|
||||
const _nodeutil = require("node:util");
|
||||
const _json5 = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/json5"));
|
||||
const _core = require("next/dist/compiled/babel/core");
|
||||
const _corelibconfig = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/core-lib-config"));
|
||||
const _util = require("./util");
|
||||
const _log = /*#__PURE__*/ _interop_require_wildcard(require("../../output/log"));
|
||||
const _swc = require("../../swc");
|
||||
const _installbindings = require("../../swc/install-bindings");
|
||||
function _interop_require_default(obj) {
|
||||
return obj && obj.__esModule ? obj : {
|
||||
default: obj
|
||||
};
|
||||
}
|
||||
function _getRequireWildcardCache(nodeInterop) {
|
||||
if (typeof WeakMap !== "function") return null;
|
||||
var cacheBabelInterop = new WeakMap();
|
||||
var cacheNodeInterop = new WeakMap();
|
||||
return (_getRequireWildcardCache = function(nodeInterop) {
|
||||
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
||||
})(nodeInterop);
|
||||
}
|
||||
function _interop_require_wildcard(obj, nodeInterop) {
|
||||
if (!nodeInterop && obj && obj.__esModule) {
|
||||
return obj;
|
||||
}
|
||||
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
||||
return {
|
||||
default: obj
|
||||
};
|
||||
}
|
||||
var cache = _getRequireWildcardCache(nodeInterop);
|
||||
if (cache && cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
var newObj = {
|
||||
__proto__: null
|
||||
};
|
||||
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
||||
for(var key in obj){
|
||||
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
||||
if (desc && (desc.get || desc.set)) {
|
||||
Object.defineProperty(newObj, key, desc);
|
||||
} else {
|
||||
newObj[key] = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
newObj.default = obj;
|
||||
if (cache) {
|
||||
cache.set(obj, newObj);
|
||||
}
|
||||
return newObj;
|
||||
}
|
||||
const nextDistPath = /(next[\\/]dist[\\/]shared[\\/]lib)|(next[\\/]dist[\\/]client)|(next[\\/]dist[\\/]pages)/;
|
||||
function shouldSkipBabel(transformMode, configFilePath, hasReactCompiler) {
|
||||
return transformMode === 'standalone' && configFilePath == null && !hasReactCompiler;
|
||||
}
|
||||
const fileExtensionRegex = /\.([a-z]+)$/;
|
||||
async function getCacheCharacteristics(loaderOptions, source, filename) {
|
||||
var _fileExtensionRegex_exec;
|
||||
let isStandalone, isServer, pagesDir;
|
||||
switch(loaderOptions.transformMode){
|
||||
case 'default':
|
||||
isStandalone = false;
|
||||
isServer = loaderOptions.isServer;
|
||||
pagesDir = loaderOptions.pagesDir;
|
||||
break;
|
||||
case 'standalone':
|
||||
isStandalone = true;
|
||||
break;
|
||||
default:
|
||||
throw Object.defineProperty(new Error(`unsupported transformMode in loader options: ${(0, _nodeutil.inspect)(loaderOptions)}`), "__NEXT_ERROR_CODE", {
|
||||
value: "E811",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
const isPageFile = pagesDir != null && filename.startsWith(pagesDir);
|
||||
const isNextDist = nextDistPath.test(filename);
|
||||
const hasModuleExports = source.indexOf('module.exports') !== -1;
|
||||
const fileExt = ((_fileExtensionRegex_exec = fileExtensionRegex.exec(filename)) == null ? void 0 : _fileExtensionRegex_exec[1]) || 'unknown';
|
||||
let { reactCompilerPlugins, reactCompilerExclude, configFile: configFilePath, transformMode } = loaderOptions;
|
||||
// Compute `hasReactCompiler` as part of the cache characteristics / key,
|
||||
// rather than inside of `getFreshConfig`:
|
||||
// - `isReactCompilerRequired` depends on the file contents
|
||||
// - `node_modules` and `reactCompilerExclude` depend on the file path, which
|
||||
// isn't part of the cache characteristics
|
||||
let hasReactCompiler = reactCompilerPlugins != null && reactCompilerPlugins.length !== 0 && !loaderOptions.isServer && !/[/\\]node_modules[/\\]/.test(filename) && // Assumption: `reactCompilerExclude` is cheap because it should only
|
||||
// operate on the file path and *not* the file contents (it's sync)
|
||||
!(reactCompilerExclude == null ? void 0 : reactCompilerExclude(filename));
|
||||
// `isReactCompilerRequired` is expensive to run (parses/visits with SWC), so
|
||||
// only run it if there's a good chance we might be able to skip calling Babel
|
||||
// entirely (speculatively call `shouldSkipBabel`).
|
||||
//
|
||||
// Otherwise, we can let react compiler handle this logic for us. It should
|
||||
// behave equivalently.
|
||||
if (hasReactCompiler && shouldSkipBabel(transformMode, configFilePath, /*hasReactCompiler*/ false)) {
|
||||
hasReactCompiler &&= await (0, _swc.isReactCompilerRequired)(filename);
|
||||
}
|
||||
return {
|
||||
isStandalone,
|
||||
isServer,
|
||||
isPageFile,
|
||||
isNextDist,
|
||||
hasModuleExports,
|
||||
hasReactCompiler,
|
||||
fileExt,
|
||||
configFilePath
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Return an array of Babel plugins, conditioned upon loader options and
|
||||
* source file characteristics.
|
||||
*/ function getPlugins(loaderOptions, cacheCharacteristics) {
|
||||
const { isServer, isPageFile, isNextDist, hasModuleExports } = cacheCharacteristics;
|
||||
const { development, hasReactRefresh } = loaderOptions;
|
||||
const applyCommonJsItem = hasModuleExports ? (0, _core.createConfigItem)(require('../plugins/commonjs'), {
|
||||
type: 'plugin'
|
||||
}) : null;
|
||||
const reactRefreshItem = hasReactRefresh ? (0, _core.createConfigItem)([
|
||||
require('next/dist/compiled/react-refresh/babel'),
|
||||
{
|
||||
skipEnvCheck: true
|
||||
}
|
||||
], {
|
||||
type: 'plugin'
|
||||
}) : null;
|
||||
const pageConfigItem = !isServer && isPageFile ? (0, _core.createConfigItem)([
|
||||
require('../plugins/next-page-config')
|
||||
], {
|
||||
type: 'plugin'
|
||||
}) : null;
|
||||
const disallowExportAllItem = !isServer && isPageFile ? (0, _core.createConfigItem)([
|
||||
require('../plugins/next-page-disallow-re-export-all-exports')
|
||||
], {
|
||||
type: 'plugin'
|
||||
}) : null;
|
||||
const transformDefineItem = (0, _core.createConfigItem)([
|
||||
require.resolve('next/dist/compiled/babel/plugin-transform-define'),
|
||||
{
|
||||
'process.env.NODE_ENV': development ? 'development' : 'production',
|
||||
'typeof window': isServer ? 'undefined' : 'object',
|
||||
'process.browser': isServer ? false : true
|
||||
},
|
||||
'next-js-transform-define-instance'
|
||||
], {
|
||||
type: 'plugin'
|
||||
});
|
||||
const nextSsgItem = !isServer && isPageFile ? (0, _core.createConfigItem)([
|
||||
require.resolve('../plugins/next-ssg-transform')
|
||||
], {
|
||||
type: 'plugin'
|
||||
}) : null;
|
||||
const commonJsItem = isNextDist ? (0, _core.createConfigItem)(require('next/dist/compiled/babel/plugin-transform-modules-commonjs'), {
|
||||
type: 'plugin'
|
||||
}) : null;
|
||||
const nextFontUnsupported = (0, _core.createConfigItem)([
|
||||
require('../plugins/next-font-unsupported')
|
||||
], {
|
||||
type: 'plugin'
|
||||
});
|
||||
return [
|
||||
reactRefreshItem,
|
||||
pageConfigItem,
|
||||
disallowExportAllItem,
|
||||
applyCommonJsItem,
|
||||
transformDefineItem,
|
||||
nextSsgItem,
|
||||
commonJsItem,
|
||||
nextFontUnsupported
|
||||
].filter(Boolean);
|
||||
}
|
||||
const isJsonFile = /\.(json|babelrc)$/;
|
||||
const isJsFile = /\.js$/;
|
||||
/**
|
||||
* While this function does block execution while reading from disk, it
|
||||
* should not introduce any issues. The function is only invoked when
|
||||
* generating a fresh config, and only a small handful of configs should
|
||||
* be generated during compilation.
|
||||
*/ function getCustomBabelConfig(configFilePath) {
|
||||
if (isJsonFile.exec(configFilePath)) {
|
||||
const babelConfigRaw = (0, _nodefs.readFileSync)(configFilePath, 'utf8');
|
||||
return _json5.default.parse(babelConfigRaw);
|
||||
} else if (isJsFile.exec(configFilePath)) {
|
||||
return require(configFilePath);
|
||||
}
|
||||
throw Object.defineProperty(new Error('The Next.js Babel loader does not support .mjs or .cjs config files.'), "__NEXT_ERROR_CODE", {
|
||||
value: "E477",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
let babelConfigWarned = false;
|
||||
/**
|
||||
* Check if custom babel configuration from user only contains options that
|
||||
* can be migrated into latest Next.js features supported by SWC.
|
||||
*
|
||||
* This raises soft warning messages only, not making any errors yet.
|
||||
*/ function checkCustomBabelConfigDeprecation(config) {
|
||||
if (!config || Object.keys(config).length === 0) {
|
||||
return;
|
||||
}
|
||||
const { plugins, presets, ...otherOptions } = config;
|
||||
if (Object.keys(otherOptions ?? {}).length > 0) {
|
||||
return;
|
||||
}
|
||||
if (babelConfigWarned) {
|
||||
return;
|
||||
}
|
||||
babelConfigWarned = true;
|
||||
const isPresetReadyToDeprecate = !presets || presets.length === 0 || presets.length === 1 && presets[0] === 'next/babel';
|
||||
const pluginReasons = [];
|
||||
const unsupportedPlugins = [];
|
||||
if (Array.isArray(plugins)) {
|
||||
for (const plugin of plugins){
|
||||
const pluginName = Array.isArray(plugin) ? plugin[0] : plugin;
|
||||
// [NOTE]: We cannot detect if the user uses babel-plugin-macro based transform plugins,
|
||||
// such as `styled-components/macro` in here.
|
||||
switch(pluginName){
|
||||
case 'styled-components':
|
||||
case 'babel-plugin-styled-components':
|
||||
pluginReasons.push(`\t- 'styled-components' can be enabled via 'compiler.styledComponents' in 'next.config.js'`);
|
||||
break;
|
||||
case '@emotion/babel-plugin':
|
||||
pluginReasons.push(`\t- '@emotion/babel-plugin' can be enabled via 'compiler.emotion' in 'next.config.js'`);
|
||||
break;
|
||||
case 'babel-plugin-relay':
|
||||
pluginReasons.push(`\t- 'babel-plugin-relay' can be enabled via 'compiler.relay' in 'next.config.js'`);
|
||||
break;
|
||||
case 'react-remove-properties':
|
||||
pluginReasons.push(`\t- 'react-remove-properties' can be enabled via 'compiler.reactRemoveProperties' in 'next.config.js'`);
|
||||
break;
|
||||
case 'transform-remove-console':
|
||||
pluginReasons.push(`\t- 'transform-remove-console' can be enabled via 'compiler.removeConsole' in 'next.config.js'`);
|
||||
break;
|
||||
default:
|
||||
unsupportedPlugins.push(pluginName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isPresetReadyToDeprecate && unsupportedPlugins.length === 0) {
|
||||
_log.warn(`It looks like there is a custom Babel configuration that can be removed${pluginReasons.length > 0 ? ':' : '.'}`);
|
||||
if (pluginReasons.length > 0) {
|
||||
_log.warn(`Next.js supports the following features natively: `);
|
||||
_log.warn(pluginReasons.join(''));
|
||||
_log.warn(`For more details configuration options, please refer https://nextjs.org/docs/architecture/nextjs-compiler#supported-features`);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Generate a new, flat Babel config, ready to be handed to Babel-traverse.
|
||||
* This config should have no unresolved overrides, presets, etc.
|
||||
*
|
||||
* The config returned by this function is cached, so the function should not
|
||||
* depend on file-specific configuration or configuration that could change
|
||||
* across invocations without a process restart.
|
||||
*/ async function getFreshConfig(ctx, cacheCharacteristics, loaderOptions, target) {
|
||||
const { transformMode } = loaderOptions;
|
||||
const { hasReactCompiler, configFilePath, fileExt } = cacheCharacteristics;
|
||||
let customConfig = configFilePath && getCustomBabelConfig(configFilePath);
|
||||
if (shouldSkipBabel(transformMode, configFilePath, hasReactCompiler)) {
|
||||
// Optimization: There's nothing useful to do, bail out and skip babel on
|
||||
// this file
|
||||
return null;
|
||||
}
|
||||
checkCustomBabelConfigDeprecation(customConfig);
|
||||
// We can assume that `reactCompilerPlugins` does not change without a process
|
||||
// restart (it's safe to cache), as it's specified in the `next.config.js`,
|
||||
// which always causes a full restart of `next dev` if changed.
|
||||
const reactCompilerPluginsIfEnabled = hasReactCompiler ? loaderOptions.reactCompilerPlugins ?? [] : [];
|
||||
let isServer, pagesDir, srcDir, development;
|
||||
if (transformMode === 'default') {
|
||||
isServer = loaderOptions.isServer;
|
||||
pagesDir = loaderOptions.pagesDir;
|
||||
srcDir = loaderOptions.srcDir;
|
||||
development = loaderOptions.development;
|
||||
}
|
||||
let options = {
|
||||
babelrc: false,
|
||||
cloneInputAst: false,
|
||||
// Use placeholder file info. `updateBabelConfigWithFileDetails` will
|
||||
// replace this after caching.
|
||||
filename: `basename.${fileExt}`,
|
||||
inputSourceMap: undefined,
|
||||
sourceFileName: `basename.${fileExt}`,
|
||||
// Set the default sourcemap behavior based on Webpack's mapping flag,
|
||||
// but allow users to override if they want.
|
||||
sourceMaps: loaderOptions.sourceMaps === undefined ? ctx.sourceMap : loaderOptions.sourceMaps
|
||||
};
|
||||
const baseCaller = {
|
||||
name: 'next-babel-turbo-loader',
|
||||
supportsStaticESM: true,
|
||||
supportsDynamicImport: true,
|
||||
// Provide plugins with insight into webpack target.
|
||||
// https://github.com/babel/babel-loader/issues/787
|
||||
target,
|
||||
// Webpack 5 supports TLA behind a flag. We enable it by default
|
||||
// for Babel, and then webpack will throw an error if the experimental
|
||||
// flag isn't enabled.
|
||||
supportsTopLevelAwait: true,
|
||||
isServer,
|
||||
srcDir,
|
||||
pagesDir,
|
||||
isDev: development,
|
||||
transformMode,
|
||||
...loaderOptions.caller
|
||||
};
|
||||
options.plugins = [
|
||||
...transformMode === 'default' ? getPlugins(loaderOptions, cacheCharacteristics) : [],
|
||||
...reactCompilerPluginsIfEnabled,
|
||||
...(customConfig == null ? void 0 : customConfig.plugins) || []
|
||||
];
|
||||
// target can be provided in babelrc
|
||||
options.target = isServer ? undefined : customConfig == null ? void 0 : customConfig.target;
|
||||
// env can be provided in babelrc
|
||||
options.env = customConfig == null ? void 0 : customConfig.env;
|
||||
options.presets = (()=>{
|
||||
// If presets is defined the user will have next/babel in their babelrc
|
||||
if (customConfig == null ? void 0 : customConfig.presets) {
|
||||
return customConfig.presets;
|
||||
}
|
||||
// If presets is not defined the user will likely have "env" in their babelrc
|
||||
if (customConfig) {
|
||||
return undefined;
|
||||
}
|
||||
// If no custom config is provided the default is to use next/babel
|
||||
return [
|
||||
'next/babel'
|
||||
];
|
||||
})();
|
||||
options.overrides = loaderOptions.overrides;
|
||||
options.caller = {
|
||||
...baseCaller,
|
||||
hasJsxRuntime: transformMode === 'default' ? loaderOptions.hasJsxRuntime : undefined
|
||||
};
|
||||
// Babel does strict checks on the config so undefined is not allowed
|
||||
if (typeof options.target === 'undefined') {
|
||||
delete options.target;
|
||||
}
|
||||
Object.defineProperty(options.caller, 'onWarning', {
|
||||
enumerable: false,
|
||||
writable: false,
|
||||
value: (reason)=>{
|
||||
if (!(reason instanceof Error)) {
|
||||
reason = Object.defineProperty(new Error(reason), "__NEXT_ERROR_CODE", {
|
||||
value: "E394",
|
||||
enumerable: false,
|
||||
configurable: true
|
||||
});
|
||||
}
|
||||
ctx.emitWarning(reason);
|
||||
}
|
||||
});
|
||||
const loadedOptions = (0, _core.loadOptions)(options);
|
||||
const config = (0, _util.consumeIterator)((0, _corelibconfig.default)(loadedOptions));
|
||||
return config;
|
||||
}
|
||||
/**
|
||||
* Each key returned here corresponds with a Babel config that can be shared.
|
||||
* The conditions of permissible sharing between files is dependent on specific
|
||||
* file attributes and Next.js compiler states: `CharacteristicsGermaneToCaching`.
|
||||
*/ function getCacheKey(cacheCharacteristics) {
|
||||
const { isStandalone, isServer, isPageFile, isNextDist, hasModuleExports, hasReactCompiler, fileExt, configFilePath } = cacheCharacteristics;
|
||||
const flags = 0 | (isStandalone ? 1 : 0) | (isServer ? 2 : 0) | (isPageFile ? 4 : 0) | (isNextDist ? 8 : 0) | (hasModuleExports ? 16 : 0) | (hasReactCompiler ? 32 : 0);
|
||||
// separate strings with null bytes, assuming null bytes are not valid in file
|
||||
// paths
|
||||
return `${configFilePath || ''}\x00${fileExt}\x00${flags}`;
|
||||
}
|
||||
const configCache = new Map();
|
||||
const configFiles = new Set();
|
||||
/**
|
||||
* Applies file-specific values to a potentially-cached configuration object.
|
||||
*/ function updateBabelConfigWithFileDetails(cachedConfig, loaderOptions, filename, inputSourceMap) {
|
||||
if (cachedConfig == null) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
...cachedConfig,
|
||||
options: {
|
||||
...cachedConfig.options,
|
||||
cwd: loaderOptions.cwd,
|
||||
root: loaderOptions.cwd,
|
||||
filename,
|
||||
inputSourceMap,
|
||||
// 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
|
||||
}
|
||||
};
|
||||
}
|
||||
async function getConfig(ctx, { source, target, loaderOptions, filename, inputSourceMap }) {
|
||||
// Install bindings early so they are definitely available to the loader.
|
||||
// When run by webpack in next this is already done with correct configuration so this is a no-op.
|
||||
// In turbopack loaders are run in a subprocess so it may or may not be done.
|
||||
await (0, _installbindings.installBindings)();
|
||||
const cacheCharacteristics = await getCacheCharacteristics(loaderOptions, source, filename);
|
||||
if (loaderOptions.configFile) {
|
||||
// Ensures webpack invalidates the cache for this loader when the config file changes
|
||||
ctx.addDependency(loaderOptions.configFile);
|
||||
}
|
||||
const cacheKey = getCacheKey(cacheCharacteristics);
|
||||
const cachedConfig = configCache.get(cacheKey);
|
||||
if (cachedConfig !== undefined) {
|
||||
return updateBabelConfigWithFileDetails(cachedConfig, loaderOptions, filename, inputSourceMap);
|
||||
}
|
||||
if (loaderOptions.configFile && !configFiles.has(loaderOptions.configFile)) {
|
||||
configFiles.add(loaderOptions.configFile);
|
||||
_log.info(`Using external babel configuration from ${loaderOptions.configFile}`);
|
||||
}
|
||||
const freshConfig = await getFreshConfig(ctx, cacheCharacteristics, loaderOptions, target);
|
||||
configCache.set(cacheKey, freshConfig);
|
||||
return updateBabelConfigWithFileDetails(freshConfig, loaderOptions, filename, inputSourceMap);
|
||||
}
|
||||
|
||||
//# sourceMappingURL=get-config.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+3
@@ -0,0 +1,3 @@
|
||||
import type { NextJsLoaderContext } from './types';
|
||||
declare function nextBabelLoaderOuter(this: NextJsLoaderContext, inputSource: string, inputSourceMap?: any): void;
|
||||
export default nextBabelLoaderOuter;
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "default", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return _default;
|
||||
}
|
||||
});
|
||||
const _transform = /*#__PURE__*/ _interop_require_default(require("./transform"));
|
||||
function _interop_require_default(obj) {
|
||||
return obj && obj.__esModule ? obj : {
|
||||
default: obj
|
||||
};
|
||||
}
|
||||
async function nextBabelLoader(ctx, parentTrace, inputSource, inputSourceMap) {
|
||||
const filename = ctx.resourcePath;
|
||||
// Ensure `.d.ts` are not processed.
|
||||
if (filename.endsWith('.d.ts')) {
|
||||
return [
|
||||
inputSource,
|
||||
inputSourceMap
|
||||
];
|
||||
}
|
||||
const target = ctx.target;
|
||||
const loaderOptions = parentTrace.traceChild('get-options')// @ts-ignore TODO: remove ignore once webpack 5 types are used
|
||||
.traceFn(()=>ctx.getOptions());
|
||||
if (loaderOptions.exclude && loaderOptions.exclude(filename)) {
|
||||
return [
|
||||
inputSource,
|
||||
inputSourceMap
|
||||
];
|
||||
}
|
||||
const loaderSpanInner = parentTrace.traceChild('next-babel-turbo-transform');
|
||||
const { code: transformedSource, map: outputSourceMap } = await loaderSpanInner.traceAsyncFn(async ()=>await (0, _transform.default)(ctx, inputSource, inputSourceMap, loaderOptions, filename, target, loaderSpanInner));
|
||||
return [
|
||||
transformedSource,
|
||||
outputSourceMap
|
||||
];
|
||||
}
|
||||
function nextBabelLoaderOuter(inputSource, // webpack's source map format is compatible with babel, but the type signature doesn't match
|
||||
inputSourceMap) {
|
||||
const callback = this.async();
|
||||
const loaderSpan = this.currentTraceSpan.traceChild('next-babel-turbo-loader');
|
||||
loaderSpan.traceAsyncFn(()=>nextBabelLoader(this, loaderSpan, inputSource, inputSourceMap)).then(([transformedSource, outputSourceMap])=>callback == null ? void 0 : callback(/* err */ null, transformedSource, outputSourceMap ?? inputSourceMap), (err)=>{
|
||||
callback == null ? void 0 : callback(err);
|
||||
});
|
||||
}
|
||||
// check this type matches `webpack.LoaderDefinitionFunction`, but be careful
|
||||
// not to publicly rely on the webpack type since the generated typescript
|
||||
// declarations will be wrong.
|
||||
const _nextBabelLoaderOuter = nextBabelLoaderOuter;
|
||||
const _default = nextBabelLoaderOuter;
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/loader/index.ts"],"sourcesContent":["import type { Span } from '../../../trace'\nimport transform from './transform'\nimport type { NextJsLoaderContext } from './types'\nimport type { SourceMap } from './util'\nimport type { webpack } from 'next/dist/compiled/webpack/webpack'\n\nasync function nextBabelLoader(\n ctx: NextJsLoaderContext,\n parentTrace: Span,\n inputSource: string,\n inputSourceMap: SourceMap | null | undefined\n): Promise<[string, SourceMap | null | undefined]> {\n const filename = ctx.resourcePath\n\n // Ensure `.d.ts` are not processed.\n if (filename.endsWith('.d.ts')) {\n return [inputSource, inputSourceMap]\n }\n\n const target = ctx.target\n const loaderOptions: any = parentTrace\n .traceChild('get-options')\n // @ts-ignore TODO: remove ignore once webpack 5 types are used\n .traceFn(() => ctx.getOptions())\n\n if (loaderOptions.exclude && loaderOptions.exclude(filename)) {\n return [inputSource, inputSourceMap]\n }\n\n const loaderSpanInner = parentTrace.traceChild('next-babel-turbo-transform')\n const { code: transformedSource, map: outputSourceMap } =\n await loaderSpanInner.traceAsyncFn(\n async () =>\n await transform(\n ctx,\n inputSource,\n inputSourceMap,\n loaderOptions,\n filename,\n target,\n loaderSpanInner\n )\n )\n\n return [transformedSource, outputSourceMap]\n}\n\nfunction nextBabelLoaderOuter(\n this: NextJsLoaderContext,\n inputSource: string,\n // webpack's source map format is compatible with babel, but the type signature doesn't match\n inputSourceMap?: any\n) {\n const callback = this.async()\n\n const loaderSpan = this.currentTraceSpan.traceChild('next-babel-turbo-loader')\n loaderSpan\n .traceAsyncFn(() =>\n nextBabelLoader(this, loaderSpan, inputSource, inputSourceMap)\n )\n .then(\n ([transformedSource, outputSourceMap]) =>\n callback?.(\n /* err */ null,\n transformedSource,\n outputSourceMap ?? inputSourceMap\n ),\n (err) => {\n callback?.(err)\n }\n )\n}\n\n// check this type matches `webpack.LoaderDefinitionFunction`, but be careful\n// not to publicly rely on the webpack type since the generated typescript\n// declarations will be wrong.\nconst _nextBabelLoaderOuter: webpack.LoaderDefinitionFunction<\n {},\n NextJsLoaderContext\n> = nextBabelLoaderOuter\n\nexport default nextBabelLoaderOuter\n"],"names":["nextBabelLoader","ctx","parentTrace","inputSource","inputSourceMap","filename","resourcePath","endsWith","target","loaderOptions","traceChild","traceFn","getOptions","exclude","loaderSpanInner","code","transformedSource","map","outputSourceMap","traceAsyncFn","transform","nextBabelLoaderOuter","callback","async","loaderSpan","currentTraceSpan","then","err","_nextBabelLoaderOuter"],"mappings":";;;;+BAiFA;;;eAAA;;;kEAhFsB;;;;;;AAKtB,eAAeA,gBACbC,GAAwB,EACxBC,WAAiB,EACjBC,WAAmB,EACnBC,cAA4C;IAE5C,MAAMC,WAAWJ,IAAIK,YAAY;IAEjC,oCAAoC;IACpC,IAAID,SAASE,QAAQ,CAAC,UAAU;QAC9B,OAAO;YAACJ;YAAaC;SAAe;IACtC;IAEA,MAAMI,SAASP,IAAIO,MAAM;IACzB,MAAMC,gBAAqBP,YACxBQ,UAAU,CAAC,cACZ,+DAA+D;KAC9DC,OAAO,CAAC,IAAMV,IAAIW,UAAU;IAE/B,IAAIH,cAAcI,OAAO,IAAIJ,cAAcI,OAAO,CAACR,WAAW;QAC5D,OAAO;YAACF;YAAaC;SAAe;IACtC;IAEA,MAAMU,kBAAkBZ,YAAYQ,UAAU,CAAC;IAC/C,MAAM,EAAEK,MAAMC,iBAAiB,EAAEC,KAAKC,eAAe,EAAE,GACrD,MAAMJ,gBAAgBK,YAAY,CAChC,UACE,MAAMC,IAAAA,kBAAS,EACbnB,KACAE,aACAC,gBACAK,eACAJ,UACAG,QACAM;IAIR,OAAO;QAACE;QAAmBE;KAAgB;AAC7C;AAEA,SAASG,qBAEPlB,WAAmB,EACnB,6FAA6F;AAC7FC,cAAoB;IAEpB,MAAMkB,WAAW,IAAI,CAACC,KAAK;IAE3B,MAAMC,aAAa,IAAI,CAACC,gBAAgB,CAACf,UAAU,CAAC;IACpDc,WACGL,YAAY,CAAC,IACZnB,gBAAgB,IAAI,EAAEwB,YAAYrB,aAAaC,iBAEhDsB,IAAI,CACH,CAAC,CAACV,mBAAmBE,gBAAgB,GACnCI,4BAAAA,SACE,OAAO,GAAG,MACVN,mBACAE,mBAAmBd,iBAEvB,CAACuB;QACCL,4BAAAA,SAAWK;IACb;AAEN;AAEA,6EAA6E;AAC7E,0EAA0E;AAC1E,8BAA8B;AAC9B,MAAMC,wBAGFP;MAEJ,WAAeA","ignoreList":[0]}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { type GeneratorResult } from 'next/dist/compiled/babel/generator';
|
||||
import type { Span } from '../../../trace';
|
||||
import type { NextJsLoaderContext } from './types';
|
||||
import type { SourceMap } from './util';
|
||||
export default function transform(ctx: NextJsLoaderContext, source: string, inputSourceMap: SourceMap | null | undefined, loaderOptions: any, filename: string, target: string, parentSpan: Span): Promise<GeneratorResult>;
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Partially adapted from @babel/core (MIT license).
|
||||
*/ "use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "default", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return transform;
|
||||
}
|
||||
});
|
||||
const _traverse = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/traverse"));
|
||||
const _generator = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/generator"));
|
||||
const _corelibnormalizefile = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/core-lib-normalize-file"));
|
||||
const _corelibnormalizeopts = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/core-lib-normalize-opts"));
|
||||
const _corelibblockhoistplugin = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/core-lib-block-hoist-plugin"));
|
||||
const _corelibpluginpass = /*#__PURE__*/ _interop_require_default(require("next/dist/compiled/babel/core-lib-plugin-pass"));
|
||||
const _getconfig = /*#__PURE__*/ _interop_require_default(require("./get-config"));
|
||||
const _util = require("./util");
|
||||
function _interop_require_default(obj) {
|
||||
return obj && obj.__esModule ? obj : {
|
||||
default: obj
|
||||
};
|
||||
}
|
||||
function getTraversalParams(file, pluginPairs) {
|
||||
const passPairs = [];
|
||||
const passes = [];
|
||||
const visitors = [];
|
||||
for (const plugin of pluginPairs.concat((0, _corelibblockhoistplugin.default)())){
|
||||
const pass = new _corelibpluginpass.default(file, plugin.key, plugin.options);
|
||||
passPairs.push([
|
||||
plugin,
|
||||
pass
|
||||
]);
|
||||
passes.push(pass);
|
||||
visitors.push(plugin.visitor);
|
||||
}
|
||||
return {
|
||||
passPairs,
|
||||
passes,
|
||||
visitors
|
||||
};
|
||||
}
|
||||
function invokePluginPre(file, passPairs) {
|
||||
for (const [{ pre }, pass] of passPairs){
|
||||
if (pre) {
|
||||
pre.call(pass, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
function invokePluginPost(file, passPairs) {
|
||||
for (const [{ post }, pass] of passPairs){
|
||||
if (post) {
|
||||
post.call(pass, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
function transformAstPass(file, pluginPairs, parentSpan) {
|
||||
const { passPairs, passes, visitors } = getTraversalParams(file, pluginPairs);
|
||||
invokePluginPre(file, passPairs);
|
||||
const visitor = _traverse.default.visitors.merge(visitors, passes, // @ts-ignore - the exported types are incorrect here
|
||||
file.opts.wrapPluginVisitorMethod);
|
||||
parentSpan.traceChild('babel-turbo-traverse').traceFn(()=>(0, _traverse.default)(file.ast, visitor, file.scope));
|
||||
invokePluginPost(file, passPairs);
|
||||
}
|
||||
function transformAst(file, babelConfig, parentSpan) {
|
||||
for (const pluginPairs of babelConfig.passes){
|
||||
transformAstPass(file, pluginPairs, parentSpan);
|
||||
}
|
||||
}
|
||||
async function transform(ctx, source, inputSourceMap, loaderOptions, filename, target, parentSpan) {
|
||||
const getConfigSpan = parentSpan.traceChild('babel-turbo-get-config');
|
||||
const babelConfig = await (0, _getconfig.default)(ctx, {
|
||||
source,
|
||||
loaderOptions,
|
||||
inputSourceMap: inputSourceMap ?? undefined,
|
||||
target,
|
||||
filename
|
||||
});
|
||||
if (!babelConfig) {
|
||||
return {
|
||||
code: source,
|
||||
map: inputSourceMap ?? null
|
||||
};
|
||||
}
|
||||
getConfigSpan.stop();
|
||||
const normalizeSpan = parentSpan.traceChild('babel-turbo-normalize-file');
|
||||
const file = (0, _util.consumeIterator)((0, _corelibnormalizefile.default)(babelConfig.passes, (0, _corelibnormalizeopts.default)(babelConfig), source));
|
||||
normalizeSpan.stop();
|
||||
const transformSpan = parentSpan.traceChild('babel-turbo-transform');
|
||||
transformAst(file, babelConfig, transformSpan);
|
||||
transformSpan.stop();
|
||||
const generateSpan = parentSpan.traceChild('babel-turbo-generate');
|
||||
const { code, map } = (0, _generator.default)(file.ast, file.opts.generatorOpts, file.code);
|
||||
generateSpan.stop();
|
||||
return {
|
||||
code,
|
||||
map
|
||||
};
|
||||
}
|
||||
|
||||
//# sourceMappingURL=transform.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+77
@@ -0,0 +1,77 @@
|
||||
import type { webpack } from 'next/dist/compiled/webpack/webpack'
|
||||
import type { JSONValue } from '../../../server/config-shared'
|
||||
import type { Span } from '../../../trace'
|
||||
|
||||
export interface NextJsLoaderContext extends webpack.LoaderContext<{}> {
|
||||
currentTraceSpan: Span
|
||||
target: string
|
||||
}
|
||||
|
||||
export interface NextBabelLoaderBaseOptions {
|
||||
cwd: string
|
||||
|
||||
/**
|
||||
* Should we read the user-provided custom babel config? Used in both `transformMode`s.
|
||||
*/
|
||||
configFile?: string
|
||||
|
||||
/**
|
||||
* Custom plugins to be added to the generated babel options.
|
||||
*/
|
||||
reactCompilerPlugins?: Array<JSONValue>
|
||||
|
||||
/**
|
||||
* Paths that the loader should not apply the react-compiler to.
|
||||
*/
|
||||
reactCompilerExclude?: (excludePath: string) => boolean
|
||||
|
||||
overrides?: any
|
||||
|
||||
/**
|
||||
* Extra fields to pass to presets/plugins via the Babel 'caller' API.
|
||||
*/
|
||||
caller?: any
|
||||
|
||||
/**
|
||||
* Advanced: Can override webpack's sourcemap behavior (see `NextJsLoaderContext["sourceMap"]`).
|
||||
*/
|
||||
sourceMaps?: boolean | 'inline' | 'both' | null | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Options to create babel loader for the default transformations.
|
||||
*
|
||||
* This is primary usecase of babel-loader configuration for running
|
||||
* all of the necessary transforms for the ecmascript instead of swc loader.
|
||||
*/
|
||||
export type NextBabelLoaderOptionDefaultPresets = NextBabelLoaderBaseOptions & {
|
||||
transformMode: 'default'
|
||||
|
||||
isServer: boolean
|
||||
distDir: string
|
||||
pagesDir: string | undefined
|
||||
srcDir: string
|
||||
development: boolean
|
||||
hasJsxRuntime: boolean
|
||||
hasReactRefresh: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Options to create babel loader for 'standalone' transformations.
|
||||
*
|
||||
* This'll create a babel loader does not enable any of the default Next.js
|
||||
* presets or plugins that perform transformations. Non-transforming syntax
|
||||
* plugins (e.g. Typescript) will still be enabled.
|
||||
*
|
||||
* This is useful when Babel is used in combination with SWC, and we expect SWC
|
||||
* to perform downleveling and Next.js-specific transforms. Standalone mode is
|
||||
* used in Turbopack and when React Compiler is used with webpack.
|
||||
*/
|
||||
export type NextBabelLoaderOptionStandalone = NextBabelLoaderBaseOptions & {
|
||||
transformMode: 'standalone'
|
||||
isServer: boolean
|
||||
}
|
||||
|
||||
export type NextBabelLoaderOptions =
|
||||
| NextBabelLoaderOptionDefaultPresets
|
||||
| NextBabelLoaderOptionStandalone
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import type { TransformOptions } from 'next/dist/compiled/babel/core';
|
||||
export declare function consumeIterator(iter: Iterator<any>): any;
|
||||
/**
|
||||
* Source map standard format as to revision 3.
|
||||
*
|
||||
* `TransformOptions` uses this type, but doesn't export it separately
|
||||
*/
|
||||
export type SourceMap = NonNullable<TransformOptions['inputSourceMap']>;
|
||||
/**
|
||||
* An extension of the normal babel configuration, with extra `babel-loader`-specific fields that transforms can read.
|
||||
*
|
||||
* See: https://github.com/babel/babel-loader/blob/main/src/injectCaller.js
|
||||
*/
|
||||
export type BabelLoaderTransformOptions = TransformOptions & {
|
||||
target?: string;
|
||||
};
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
Object.defineProperty(exports, "consumeIterator", {
|
||||
enumerable: true,
|
||||
get: function() {
|
||||
return consumeIterator;
|
||||
}
|
||||
});
|
||||
function consumeIterator(iter) {
|
||||
while(true){
|
||||
const { value, done } = iter.next();
|
||||
if (done) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=util.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"sources":["../../../../src/build/babel/loader/util.ts"],"sourcesContent":["import type { TransformOptions } from 'next/dist/compiled/babel/core'\n\nexport function consumeIterator(iter: Iterator<any>) {\n while (true) {\n const { value, done } = iter.next()\n if (done) {\n return value\n }\n }\n}\n\n/**\n * Source map standard format as to revision 3.\n *\n * `TransformOptions` uses this type, but doesn't export it separately\n */\nexport type SourceMap = NonNullable<TransformOptions['inputSourceMap']>\n\n/**\n * An extension of the normal babel configuration, with extra `babel-loader`-specific fields that transforms can read.\n *\n * See: https://github.com/babel/babel-loader/blob/main/src/injectCaller.js\n */\nexport type BabelLoaderTransformOptions = TransformOptions & {\n target?: string\n}\n"],"names":["consumeIterator","iter","value","done","next"],"mappings":";;;;+BAEgBA;;;eAAAA;;;AAAT,SAASA,gBAAgBC,IAAmB;IACjD,MAAO,KAAM;QACX,MAAM,EAAEC,KAAK,EAAEC,IAAI,EAAE,GAAGF,KAAKG,IAAI;QACjC,IAAID,MAAM;YACR,OAAOD;QACT;IACF;AACF","ignoreList":[0]}
|
||||
Reference in New Issue
Block a user