1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/packages/next/build/babel/preset.ts
Tim Neutkens a9cf735f50
Convert babel plugins to typescript (#5789)
Slowly moving files over 💯
2018-12-02 18:30:00 +01:00

85 lines
2.6 KiB
TypeScript

import {PluginItem} from '@babel/core'
const env = process.env.NODE_ENV
const isProduction = env === 'production'
const isDevelopment = env === 'development'
const isTest = env === 'test'
type StyledJsxPlugin = [string, any] | string
type StyledJsxBabelOptions = {
plugins?: StyledJsxPlugin[]
} | undefined
// Resolve styled-jsx plugins
function styledJsxOptions (options: StyledJsxBabelOptions) {
if (!options) {
return {}
}
if (!Array.isArray(options.plugins)) {
return options
}
options.plugins = options.plugins.map((plugin: StyledJsxPlugin): StyledJsxPlugin => {
if (Array.isArray(plugin)) {
const [name, options] = plugin
return [
require.resolve(name),
options
]
}
return require.resolve(plugin)
})
return options
}
type NextBabelPresetOptions = {
'preset-env'?: any,
'preset-react'?: any,
'class-properties'?: any,
'transform-runtime'?: any,
'styled-jsx'?: StyledJsxBabelOptions
}
type BabelPreset = {
presets?: PluginItem[] | null,
plugins?: PluginItem[] | null
}
module.exports = (context: any, options: NextBabelPresetOptions = {}): BabelPreset => {
return {
presets: [
[require('@babel/preset-env').default, {
// In the test environment `modules` is often needed to be set to true, babel figures that out by itself using the `'auto'` option
// In production/development this option is set to `false` so that webpack can handle import/export with tree-shaking
modules: isDevelopment || isProduction ? false : 'auto',
...options['preset-env']
}],
[require('@babel/preset-react'), {
// This adds @babel/plugin-transform-react-jsx-source and
// @babel/plugin-transform-react-jsx-self automatically in development
development: isDevelopment || isTest,
...options['preset-react']
}]
],
plugins: [
require('babel-plugin-react-require'),
require('@babel/plugin-syntax-dynamic-import'),
require('./plugins/react-loadable-plugin'),
require('./plugins/next-to-next-server'),
[require('@babel/plugin-proposal-class-properties'), options['class-properties'] || {}],
require('@babel/plugin-proposal-object-rest-spread'),
[require('@babel/plugin-transform-runtime'), {
corejs: 2,
helpers: true,
regenerator: true,
useESModules: !isTest,
...options['transform-runtime']
}],
[require('styled-jsx/babel'), styledJsxOptions(options['styled-jsx'])],
process.env.NODE_ENV === 'production' && require('babel-plugin-transform-react-remove-prop-types')
].filter(Boolean)
}
}