2021-01-01 23:23:42 +01:00
|
|
|
import { join } from 'path'
|
2020-05-29 17:08:28 +02:00
|
|
|
import * as Log from '../../output/log'
|
2021-01-01 23:23:42 +01:00
|
|
|
import babelLoader from './babel-loader/src/index'
|
2019-05-07 14:24:47 +02:00
|
|
|
|
2021-04-22 10:26:07 +02:00
|
|
|
// increment 'p' to invalidate cache
|
2019-11-11 04:24:53 +01:00
|
|
|
// eslint-disable-next-line no-useless-concat
|
2021-04-22 10:26:07 +02:00
|
|
|
const cacheKey = 'babel-cache-' + 'p' + '-'
|
2019-07-25 04:16:32 +02:00
|
|
|
const nextBabelPreset = require('../../babel/preset')
|
|
|
|
|
2020-12-29 22:21:35 +01:00
|
|
|
const customBabelLoader = babelLoader((babel) => {
|
2019-07-25 04:16:32 +02:00
|
|
|
const presetItem = babel.createConfigItem(nextBabelPreset, {
|
2019-11-11 04:24:53 +01:00
|
|
|
type: 'preset',
|
2019-05-29 13:57:26 +02:00
|
|
|
})
|
|
|
|
const applyCommonJs = babel.createConfigItem(
|
|
|
|
require('../../babel/plugins/commonjs'),
|
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
|
|
|
const commonJsItem = babel.createConfigItem(
|
2020-11-06 03:33:14 +01:00
|
|
|
require('next/dist/compiled/babel/plugin-transform-modules-commonjs'),
|
2019-05-29 13:57:26 +02:00
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
2019-05-07 14:24:47 +02:00
|
|
|
|
|
|
|
const configs = new Set()
|
|
|
|
|
|
|
|
return {
|
2019-11-11 04:24:53 +01:00
|
|
|
customOptions(opts) {
|
2019-05-07 14:24:47 +02:00
|
|
|
const custom = {
|
|
|
|
isServer: opts.isServer,
|
2019-09-24 17:15:14 +02:00
|
|
|
pagesDir: opts.pagesDir,
|
2019-11-11 04:24:53 +01:00
|
|
|
babelPresetPlugins: opts.babelPresetPlugins,
|
2019-12-03 22:08:49 +01:00
|
|
|
development: opts.development,
|
2020-04-19 19:58:31 +02:00
|
|
|
hasReactRefresh: opts.hasReactRefresh,
|
2020-09-01 17:29:25 +02:00
|
|
|
hasJsxRuntime: opts.hasJsxRuntime,
|
2019-05-07 14:24:47 +02:00
|
|
|
}
|
|
|
|
const filename = join(opts.cwd, 'noop.js')
|
2019-05-29 13:57:26 +02:00
|
|
|
const loader = Object.assign(
|
|
|
|
opts.cache
|
|
|
|
? {
|
2019-11-11 04:24:53 +01:00
|
|
|
cacheDirectory: join(opts.distDir, 'cache', 'next-babel-loader'),
|
|
|
|
cacheIdentifier:
|
2019-05-29 13:57:26 +02:00
|
|
|
cacheKey +
|
2019-07-22 20:36:55 +02:00
|
|
|
(opts.isServer ? '-server' : '') +
|
2020-02-18 19:26:55 +01:00
|
|
|
'-new-polyfills' +
|
2019-12-03 22:08:49 +01:00
|
|
|
(opts.development ? '-development' : '-production') +
|
2020-04-19 19:58:31 +02:00
|
|
|
(opts.hasReactRefresh ? '-react-refresh' : '') +
|
2020-09-01 17:29:25 +02:00
|
|
|
(opts.hasJsxRuntime ? '-jsx-runtime' : '') +
|
2019-05-29 13:57:26 +02:00
|
|
|
JSON.stringify(
|
|
|
|
babel.loadPartialConfig({
|
|
|
|
filename,
|
|
|
|
cwd: opts.cwd,
|
2019-11-11 04:24:53 +01:00
|
|
|
sourceFileName: filename,
|
2019-05-29 13:57:26 +02:00
|
|
|
}).options
|
2019-11-11 04:24:53 +01:00
|
|
|
),
|
|
|
|
}
|
2019-05-29 13:57:26 +02:00
|
|
|
: {
|
2019-11-11 04:24:53 +01:00
|
|
|
cacheDirectory: false,
|
|
|
|
},
|
2019-05-29 13:57:26 +02:00
|
|
|
opts
|
|
|
|
)
|
2019-05-07 14:24:47 +02:00
|
|
|
|
|
|
|
delete loader.isServer
|
2019-05-17 21:39:18 +02:00
|
|
|
delete loader.cache
|
2019-05-07 14:24:47 +02:00
|
|
|
delete loader.distDir
|
2019-09-24 17:15:14 +02:00
|
|
|
delete loader.pagesDir
|
2019-11-01 20:13:13 +01:00
|
|
|
delete loader.babelPresetPlugins
|
2019-12-03 22:08:49 +01:00
|
|
|
delete loader.development
|
2020-04-19 19:58:31 +02:00
|
|
|
delete loader.hasReactRefresh
|
2020-09-01 17:29:25 +02:00
|
|
|
delete loader.hasJsxRuntime
|
2019-05-07 14:24:47 +02:00
|
|
|
return { loader, custom }
|
|
|
|
},
|
2019-11-11 04:24:53 +01:00
|
|
|
config(
|
2019-05-29 13:57:26 +02:00
|
|
|
cfg,
|
|
|
|
{
|
|
|
|
source,
|
2019-11-01 20:13:13 +01:00
|
|
|
customOptions: {
|
|
|
|
isServer,
|
|
|
|
pagesDir,
|
2019-11-11 04:24:53 +01:00
|
|
|
babelPresetPlugins,
|
2019-12-03 22:08:49 +01:00
|
|
|
development,
|
2020-04-19 19:58:31 +02:00
|
|
|
hasReactRefresh,
|
2020-09-01 17:29:25 +02:00
|
|
|
hasJsxRuntime,
|
2019-11-11 04:24:53 +01:00
|
|
|
},
|
2019-05-29 13:57:26 +02:00
|
|
|
}
|
|
|
|
) {
|
2019-05-07 14:24:47 +02:00
|
|
|
const filename = this.resourcePath
|
|
|
|
const options = Object.assign({}, cfg.options)
|
2019-09-24 17:15:14 +02:00
|
|
|
const isPageFile = filename.startsWith(pagesDir)
|
2019-07-10 21:27:11 +02:00
|
|
|
|
2019-05-07 14:24:47 +02:00
|
|
|
if (cfg.hasFilesystemConfig()) {
|
|
|
|
for (const file of [cfg.babelrc, cfg.config]) {
|
|
|
|
// We only log for client compilation otherwise there will be double output
|
|
|
|
if (file && !isServer && !configs.has(file)) {
|
|
|
|
configs.add(file)
|
2020-05-29 17:08:28 +02:00
|
|
|
Log.info(`Using external babel configuration from ${file}`)
|
2019-05-07 14:24:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Add our default preset if the no "babelrc" found.
|
|
|
|
options.presets = [...options.presets, presetItem]
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:36:55 +02:00
|
|
|
options.caller.isServer = isServer
|
2019-12-16 18:45:48 +01:00
|
|
|
options.caller.isDev = development
|
2020-09-01 17:29:25 +02:00
|
|
|
options.caller.hasJsxRuntime = hasJsxRuntime
|
2021-04-21 13:18:05 +02:00
|
|
|
options.caller.pagesDir = pagesDir
|
2019-07-22 20:36:55 +02:00
|
|
|
|
2020-06-30 15:05:29 +02:00
|
|
|
const emitWarning = this.emitWarning.bind(this)
|
|
|
|
Object.defineProperty(options.caller, 'onWarning', {
|
|
|
|
enumerable: false,
|
|
|
|
writable: false,
|
|
|
|
value: (options.caller.onWarning = function (reason) {
|
|
|
|
if (!(reason instanceof Error)) {
|
|
|
|
reason = new Error(reason)
|
|
|
|
}
|
|
|
|
emitWarning(reason)
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
2019-08-14 04:02:53 +02:00
|
|
|
options.plugins = options.plugins || []
|
|
|
|
|
2020-04-19 19:58:31 +02:00
|
|
|
if (hasReactRefresh) {
|
|
|
|
const reactRefreshPlugin = babel.createConfigItem(
|
2020-04-30 18:58:22 +02:00
|
|
|
[require('react-refresh/babel'), { skipEnvCheck: true }],
|
2020-04-19 19:58:31 +02:00
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
|
|
|
options.plugins.unshift(reactRefreshPlugin)
|
2020-06-30 15:05:29 +02:00
|
|
|
if (!isServer) {
|
|
|
|
const noAnonymousDefaultExportPlugin = babel.createConfigItem(
|
|
|
|
[require('../../babel/plugins/no-anonymous-default-export'), {}],
|
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
|
|
|
options.plugins.unshift(noAnonymousDefaultExportPlugin)
|
|
|
|
}
|
2020-04-19 19:58:31 +02:00
|
|
|
}
|
|
|
|
|
2019-07-10 21:27:11 +02:00
|
|
|
if (!isServer && isPageFile) {
|
2019-06-10 02:16:14 +02:00
|
|
|
const pageConfigPlugin = babel.createConfigItem(
|
|
|
|
[require('../../babel/plugins/next-page-config')],
|
2019-05-29 13:57:26 +02:00
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
2019-06-10 02:16:14 +02:00
|
|
|
options.plugins.push(pageConfigPlugin)
|
2020-08-05 22:21:40 +02:00
|
|
|
|
|
|
|
const diallowExportAll = babel.createConfigItem(
|
|
|
|
[
|
|
|
|
require('../../babel/plugins/next-page-disallow-re-export-all-exports'),
|
|
|
|
],
|
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
|
|
|
options.plugins.push(diallowExportAll)
|
2019-05-07 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the file has `module.exports` we have to transpile commonjs because Babel adds `import` statements
|
|
|
|
// That break webpack, since webpack doesn't support combining commonjs and esmodules
|
2020-11-18 19:30:00 +01:00
|
|
|
if (source.indexOf('module.exports') !== -1) {
|
2019-05-07 14:24:47 +02:00
|
|
|
options.plugins.push(applyCommonJs)
|
|
|
|
}
|
|
|
|
|
2019-08-14 04:02:53 +02:00
|
|
|
options.plugins.push([
|
2021-01-15 02:51:45 +01:00
|
|
|
require.resolve('next/dist/compiled/babel/plugin-transform-define'),
|
2019-12-03 22:08:49 +01:00
|
|
|
{
|
|
|
|
'process.env.NODE_ENV': development ? 'development' : 'production',
|
|
|
|
'typeof window': isServer ? 'undefined' : 'object',
|
|
|
|
'process.browser': isServer ? false : true,
|
|
|
|
},
|
2019-11-11 04:24:53 +01:00
|
|
|
'next-js-transform-define-instance',
|
2019-08-14 04:02:53 +02:00
|
|
|
])
|
|
|
|
|
2019-12-05 18:04:00 +01:00
|
|
|
if (isPageFile) {
|
2019-12-05 22:43:11 +01:00
|
|
|
if (!isServer) {
|
|
|
|
options.plugins.push([
|
|
|
|
require.resolve('../../babel/plugins/next-ssg-transform'),
|
|
|
|
{},
|
|
|
|
])
|
|
|
|
}
|
2019-12-05 18:04:00 +01:00
|
|
|
}
|
|
|
|
|
2019-05-07 14:24:47 +02:00
|
|
|
// As next-server/lib has stateful modules we have to transpile commonjs
|
|
|
|
options.overrides = [
|
|
|
|
...(options.overrides || []),
|
|
|
|
{
|
|
|
|
test: [
|
2019-09-04 16:00:54 +02:00
|
|
|
/next[\\/]dist[\\/]next-server[\\/]lib/,
|
2019-05-07 14:24:47 +02:00
|
|
|
/next[\\/]dist[\\/]client/,
|
2019-11-11 04:24:53 +01:00
|
|
|
/next[\\/]dist[\\/]pages/,
|
2019-05-07 14:24:47 +02:00
|
|
|
],
|
2019-11-11 04:24:53 +01:00
|
|
|
plugins: [commonJsItem],
|
|
|
|
},
|
2019-05-07 14:24:47 +02:00
|
|
|
]
|
|
|
|
|
2019-11-01 20:13:13 +01:00
|
|
|
for (const plugin of babelPresetPlugins) {
|
|
|
|
require(join(plugin.dir, 'src', 'babel-preset-build.js'))(
|
|
|
|
options,
|
|
|
|
plugin.config || {}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:24:47 +02:00
|
|
|
return options
|
2019-11-11 04:24:53 +01:00
|
|
|
},
|
2019-05-07 14:24:47 +02:00
|
|
|
}
|
|
|
|
})
|
2020-12-29 22:21:35 +01:00
|
|
|
|
|
|
|
export default customBabelLoader
|