Skip to content

Commit

Permalink
Disable core-js on Next.js core files as it's not transforming… (#10193)
Browse files Browse the repository at this point in the history
* Disable core-js on Next.js core files as it's not transforming anything important

* Move babel options to taskr plugin

* Disable transform-runtime for pages dir

* Disable correctly

* Disable corejs for core files

* Temporarily check if this fixes the error
  • Loading branch information
timneutkens committed Jan 22, 2020
1 parent b6edf81 commit 7b0118a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 81 deletions.
2 changes: 1 addition & 1 deletion packages/next/client/index.js
Expand Up @@ -20,7 +20,7 @@ import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
// So, we need to polyfill it.
// See: https://webpack.js.org/guides/code-splitting/#dynamic-imports
if (!window.Promise) {
window.Promise = Promise
window.Promise = require('@babel/runtime-corejs2/core-js/promise')
}

const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent)
Expand Down
81 changes: 78 additions & 3 deletions packages/next/taskfile-babel.js
Expand Up @@ -4,11 +4,84 @@
const extname = require('path').extname
const transform = require('@babel/core').transform

const babelClientOpts = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
esmodules: true,
},
loose: true,
exclude: ['transform-typeof-symbol'],
},
],
'@babel/preset-react',
],
plugins: [
// workaround for @taskr/esnext bug replacing `-import` with `-require(`
// eslint-disable-next-line no-useless-concat
'@babel/plugin-syntax-dynamic-impor' + 't',
['@babel/plugin-proposal-class-properties', { loose: true }],
],
}

const babelServerOpts = {
presets: [
'@babel/preset-typescript',
'@babel/preset-react',
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
node: '8.3',
},
loose: true,
exclude: ['transform-typeof-symbol'],
},
],
],
plugins: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
'babel-plugin-dynamic-import-node',
['@babel/plugin-proposal-class-properties', { loose: true }],
],
}

module.exports = function(task) {
// eslint-disable-next-line require-yield
task.plugin('babel', {}, function*(file, babelOpts, { stripExtension } = {}) {
task.plugin('babel', {}, function*(
file,
serverOrClient,
{ stripExtension } = {}
) {
// Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return

const babelOpts =
serverOrClient === 'client' ? babelClientOpts : babelServerOpts

const options = {
...babelOpts,
plugins: [
...babelOpts.plugins,
// pages dir doesn't need core-js
serverOrClient === 'client'
? [
'@babel/plugin-transform-runtime',
{
corejs: false,
helpers: true,
regenerator: false,
useESModules: false,
},
]
: false,
].filter(Boolean),
compact: true,
babelrc: false,
configFile: false,
Expand All @@ -17,8 +90,10 @@ module.exports = function(task) {
const output = transform(file.data, options)
const ext = extname(file.base)

// Include declaration files as they are
if (file.base.endsWith('.d.ts')) return
output.code = output.code.replace(
/@babel\/runtime\//g,
'@babel/runtime-corejs2/'
)

// Replace `.ts|.tsx` with `.js` in files with an extension
if (ext) {
Expand Down
88 changes: 11 additions & 77 deletions packages/next/taskfile.js
@@ -1,62 +1,6 @@
const notifier = require('node-notifier')
const relative = require('path').relative

const babelClientOpts = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
esmodules: true,
},
loose: true,
exclude: ['transform-typeof-symbol'],
},
],
'@babel/preset-react',
],
plugins: [
// workaround for @taskr/esnext bug replacing `-import` with `-require(`
// eslint-disable-next-line no-useless-concat
'@babel/plugin-syntax-dynamic-impor' + 't',
['@babel/plugin-proposal-class-properties', { loose: true }],
[
'@babel/plugin-transform-runtime',
{
corejs: 2,
helpers: true,
regenerator: false,
useESModules: false,
},
],
],
}

const babelServerOpts = {
presets: [
'@babel/preset-typescript',
[
'@babel/preset-env',
{
modules: 'commonjs',
targets: {
node: '8.3',
},
loose: true,
exclude: ['transform-typeof-symbol'],
},
],
],
plugins: [
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-proposal-nullish-coalescing-operator',
'babel-plugin-dynamic-import-node',
['@babel/plugin-proposal-class-properties', { loose: true }],
],
}

// eslint-disable-next-line camelcase
export async function ncc_arg(task, opts) {
await task
Expand Down Expand Up @@ -126,52 +70,47 @@ export async function compile(task) {
export async function bin(task, opts) {
await task
.source(opts.src || 'bin/*')
.babel(babelServerOpts, { stripExtension: true })
.babel('server', { stripExtension: true })
.target('dist/bin', { mode: '0755' })
notify('Compiled binaries')
}

export async function cli(task, opts) {
await task
.source(opts.src || 'cli/**/*.+(js|ts|tsx)')
.babel(babelServerOpts)
.babel('server')
.target('dist/cli')
notify('Compiled cli files')
}

export async function lib(task, opts) {
await task
.source(opts.src || 'lib/**/*.+(js|ts|tsx)')
.babel(babelServerOpts)
.babel('server')
.target('dist/lib')
notify('Compiled lib files')
}

export async function server(task, opts) {
const babelOpts = {
...babelServerOpts,
// the /server files may use React
presets: [...babelServerOpts.presets, '@babel/preset-react'],
}
await task
.source(opts.src || 'server/**/*.+(js|ts|tsx)')
.babel(babelOpts)
.babel('server')
.target('dist/server')
notify('Compiled server files')
}

export async function nextbuild(task, opts) {
await task
.source(opts.src || 'build/**/*.+(js|ts|tsx)')
.babel(babelServerOpts)
.babel('server')
.target('dist/build')
notify('Compiled build files')
}

export async function client(task, opts) {
await task
.source(opts.src || 'client/**/*.+(js|ts|tsx)')
.babel(babelClientOpts)
.babel('client')
.target('dist/client')
notify('Compiled client files')
}
Expand All @@ -180,34 +119,29 @@ export async function client(task, opts) {
export async function nextbuildstatic(task, opts) {
await task
.source(opts.src || 'export/**/*.+(js|ts|tsx)')
.babel(babelServerOpts)
.babel('server')
.target('dist/export')
notify('Compiled export files')
}

export async function pages_app(task) {
await task
.source('pages/_app.tsx')
.babel(babelClientOpts)
.babel('client')
.target('dist/pages')
}

export async function pages_error(task) {
await task
.source('pages/_error.tsx')
.babel(babelClientOpts)
.babel('client')
.target('dist/pages')
}

export async function pages_document(task) {
const babelOpts = {
...babelServerOpts,
presets: [...babelServerOpts.presets, '@babel/preset-react'],
}

await task
.source('pages/_document.tsx')
.babel(babelOpts)
.babel('server')
.target('dist/pages')
}

Expand All @@ -218,7 +152,7 @@ export async function pages(task, opts) {
export async function telemetry(task, opts) {
await task
.source(opts.src || 'telemetry/**/*.+(js|ts|tsx)')
.babel(babelServerOpts)
.babel('server')
.target('dist/telemetry')
notify('Compiled telemetry files')
}
Expand Down

0 comments on commit 7b0118a

Please sign in to comment.