Skip to content

Commit

Permalink
Move babel options to taskr plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jan 21, 2020
1 parent 4722270 commit bc4193d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 81 deletions.
72 changes: 68 additions & 4 deletions packages/next/taskfile-babel.js
Expand Up @@ -4,9 +4,76 @@
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 }],
[
'@babel/plugin-transform-runtime',
{
corejs: 2,
helpers: true,
regenerator: false,
useESModules: false,
},
],
],
}

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,
compact: true,
Expand All @@ -17,9 +84,6 @@ 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

// Replace `.ts|.tsx` with `.js` in files with an extension
if (ext) {
const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i')
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: false,
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 bc4193d

Please sign in to comment.