2020-03-30 03:25:10 +02:00
|
|
|
import babelLoader from 'next/dist/compiled/babel-loader'
|
2020-03-29 05:05:26 +02:00
|
|
|
import hash from 'next/dist/compiled/string-hash'
|
2020-04-19 19:58:31 +02:00
|
|
|
import { basename, join } from 'path'
|
2019-05-07 14:24:47 +02:00
|
|
|
|
2020-04-19 19:58:31 +02:00
|
|
|
// increment 'k' to invalidate cache
|
2019-11-11 04:24:53 +01:00
|
|
|
// eslint-disable-next-line no-useless-concat
|
2020-04-19 19:58:31 +02:00
|
|
|
const cacheKey = 'babel-cache-' + 'k' + '-'
|
2019-07-25 04:16:32 +02:00
|
|
|
const nextBabelPreset = require('../../babel/preset')
|
|
|
|
|
|
|
|
const getModernOptions = (babelOptions = {}) => {
|
|
|
|
const presetEnvOptions = Object.assign({}, babelOptions['preset-env'])
|
|
|
|
const transformRuntimeOptions = Object.assign(
|
|
|
|
{},
|
|
|
|
babelOptions['transform-runtime'],
|
|
|
|
{ regenerator: false }
|
|
|
|
)
|
|
|
|
|
|
|
|
presetEnvOptions.targets = {
|
2019-11-11 04:24:53 +01:00
|
|
|
esmodules: true,
|
2019-07-25 04:16:32 +02:00
|
|
|
}
|
|
|
|
presetEnvOptions.exclude = [
|
|
|
|
...(presetEnvOptions.exclude || []),
|
|
|
|
// Blacklist accidental inclusions
|
|
|
|
'transform-regenerator',
|
2019-11-11 04:24:53 +01:00
|
|
|
'transform-async-to-generator',
|
2019-07-25 04:16:32 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
return {
|
|
|
|
...babelOptions,
|
|
|
|
'preset-env': presetEnvOptions,
|
2019-11-11 04:24:53 +01:00
|
|
|
'transform-runtime': transformRuntimeOptions,
|
2019-07-25 04:16:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextBabelPresetModern = presetOptions => context =>
|
|
|
|
nextBabelPreset(context, getModernOptions(presetOptions))
|
2019-05-07 14:24:47 +02:00
|
|
|
|
|
|
|
module.exports = babelLoader.custom(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-03-29 21:07:55 +02:00
|
|
|
require('@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-08-12 06:10:12 +02:00
|
|
|
isModern: opts.isModern,
|
2019-09-24 17:15:14 +02:00
|
|
|
pagesDir: opts.pagesDir,
|
2019-11-01 20:13:13 +01:00
|
|
|
hasModern: opts.hasModern,
|
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,
|
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
|
|
|
cacheCompression: false,
|
|
|
|
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' : '') +
|
2019-07-25 04:16:32 +02:00
|
|
|
(opts.isModern ? '-modern' : '') +
|
2019-08-12 06:10:12 +02:00
|
|
|
(opts.hasModern ? '-has-modern' : '') +
|
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' : '') +
|
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-07-25 04:16:32 +02:00
|
|
|
delete loader.isModern
|
2019-08-12 06:10:12 +02:00
|
|
|
delete loader.hasModern
|
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
|
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,
|
|
|
|
isModern,
|
|
|
|
hasModern,
|
|
|
|
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,
|
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)
|
|
|
|
console.log(`> Using external babel configuration`)
|
|
|
|
console.log(`> Location: "${file}"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} 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-08-27 23:20:07 +02:00
|
|
|
options.caller.isModern = isModern
|
2019-12-16 18:45:48 +01:00
|
|
|
options.caller.isDev = development
|
2019-07-22 20:36:55 +02:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
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)
|
2019-05-07 14:24:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isServer && source.indexOf('next/data') !== -1) {
|
2019-05-29 13:57:26 +02:00
|
|
|
const nextDataPlugin = babel.createConfigItem(
|
|
|
|
[
|
|
|
|
require('../../babel/plugins/next-data'),
|
2019-11-11 04:24:53 +01:00
|
|
|
{ key: basename(filename) + '-' + hash(filename) },
|
2019-05-29 13:57:26 +02:00
|
|
|
],
|
|
|
|
{ type: 'plugin' }
|
|
|
|
)
|
2019-05-07 14:24:47 +02:00
|
|
|
options.plugins.push(nextDataPlugin)
|
|
|
|
}
|
|
|
|
|
2019-07-25 04:16:32 +02:00
|
|
|
if (isModern) {
|
|
|
|
const nextPreset = options.presets.find(
|
|
|
|
preset => preset && preset.value === nextBabelPreset
|
|
|
|
) || { options: {} }
|
|
|
|
|
|
|
|
const additionalPresets = options.presets.filter(
|
|
|
|
preset => preset !== nextPreset
|
|
|
|
)
|
|
|
|
|
|
|
|
const presetItemModern = babel.createConfigItem(
|
|
|
|
nextBabelPresetModern(nextPreset.options),
|
|
|
|
{
|
2019-11-11 04:24:53 +01:00
|
|
|
type: 'preset',
|
2019-07-25 04:16:32 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
options.presets = [...additionalPresets, presetItemModern]
|
|
|
|
}
|
|
|
|
|
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
|
2019-08-12 06:10:12 +02:00
|
|
|
if (!hasModern && 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([
|
2020-03-29 21:07:55 +02:00
|
|
|
require.resolve('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
|
|
|
}
|
|
|
|
})
|