Commit 08a60666 authored by Luis Fernando Alvarez D's avatar Luis Fernando Alvarez D Committed by Tim Neutkens

Replace the typescript compiler with @babel/preset-typescript (#7016)

* Added babel typescript for the core files

* Add __NEXT_VERSION

* Added noop.js to the babel taskfile

* Only include dynamic-import-node for the bin task

* Added babel-plugin-dynamic-import-node

* Use loose option to prevent Object.defineProperty

* Enable loose in preset and enable compact mode

* Disable compact and loose to compare

* Re-enable compact and loose
parent db5b3dc8
......@@ -34,7 +34,6 @@
},
"taskr": {
"requires": [
"./taskfile-typescript.js",
"./taskfile-ncc.js",
"./taskfile-babel.js"
]
......@@ -92,6 +91,7 @@
"devDependencies": {
"@types/styled-jsx": "2.2.8",
"@babel/parser": "7.2.0",
"@babel/preset-typescript": "7.3.3",
"@taskr/clear": "1.1.0",
"@taskr/esnext": "1.1.0",
"@taskr/watch": "1.1.0",
......@@ -115,6 +115,7 @@
"@types/webpack-sources": "0.1.5",
"@zeit/ncc": "0.15.2",
"arg": "4.1.0",
"babel-plugin-dynamic-import-node": "2.2.0",
"nanoid": "2.0.1",
"resolve": "1.5.0",
"taskr": "1.1.0",
......
......@@ -2,22 +2,35 @@
// https://github.com/lukeed/taskr/pull/305
'use strict'
const extname = require('path').extname
const transform = require('@babel/core').transform
module.exports = function (task) {
task.plugin('babel', {}, function * (file, opts) {
task.plugin('babel', {}, function * (file, babelOpts, { stripExtension } = {}) {
const options = {
...opts
...babelOpts,
compact: true,
babelrc: false,
configFile: false,
filename: file.base
}
options.filename = file.base
options.plugins = [
require('@babel/plugin-syntax-dynamic-import'),
...(options.plugins || [])
]
options.babelrc = false
options.configFile = false
options.babelrcRoots = false
const output = transform(file.data, options)
file.data = Buffer.from(output.code)
const ext = extname(file.base)
// Replace `.ts|.tsx` with `.js` in files with an extension
if (ext) {
const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i')
// Remove the extension if stripExtension is enabled or replace it with `.js`
file.base = file.base.replace(extRegex, stripExtension ? '' : '.js')
}
// Workaround for noop.js loading
if (file.base === 'next-dev.js') output.code = output.code.replace('// REPLACE_NOOP_IMPORT', `import('./noop');`)
file.data = Buffer.from(setNextVersion(output.code))
})
}
function setNextVersion (code) {
return code.replace(/process\.env\.__NEXT_VERSION/, `"${require('./package.json').version}"`)
}
'use strict'
try {
const ts = require('typescript')
const extname = require('path').extname
const config = require('./tsconfig.json')
module.exports = function (task) {
task.plugin('typescript', { every: true }, function * (file, options) {
const { stripExtension } = options
const opts = {
fileName: file.base,
compilerOptions: {
...config.compilerOptions,
...options,
stripExtension: undefined // since it's an option of the typescript taskr plugin
}
}
const ext = extname(file.base)
// For example files without an extension don't have to be rewritten
if (ext) {
// Replace `.ts` with `.js`
const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i')
file.base = file.base.replace(extRegex, stripExtension ? '' : '.js')
}
// compile output
const result = ts.transpileModule(file.data.toString(), opts)
if (opts.compilerOptions.sourceMap && result.sourceMapText) {
// add sourcemap to `files` array
this._.files.push({
dir: file.dir,
base: `${file.base}.map`,
data: Buffer.from(JSON.stringify(result.sourceMapText), 'utf8')
})
}
// Workaround for noop.js loading
if (file.base === 'next-dev.js') result.outputText = result.outputText.replace('// REPLACE_NOOP_IMPORT', `import('./noop');`)
// update file's data
file.data = Buffer.from(result.outputText.replace(/process\.env\.__NEXT_VERSION/, `"${require('./package.json').version}"`), 'utf8')
})
}
} catch (err) {
console.error(err)
}
const notifier = require('node-notifier')
const relative = require('path').relative
const babelOpts = {
const babelClientOpts = {
presets: [
'@babel/preset-typescript',
['@babel/preset-env', {
modules: 'commonjs',
'targets': {
'browsers': ['IE 11']
targets: {
browsers: ['IE 11']
},
loose: true,
exclude: ['transform-typeof-symbol']
}]
}],
'@babel/preset-react'
],
plugins: [
['@babel/plugin-proposal-class-properties', { loose: true }],
['@babel/plugin-transform-runtime', {
corejs: 2,
helpers: true,
......@@ -24,6 +28,23 @@ const babelOpts = {
]
}
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-class-properties', { loose: true }]
]
}
// eslint-disable-next-line camelcase
export async function ncc_arg (task, opts) {
await task
......@@ -73,43 +94,52 @@ export async function compile (task) {
}
export async function bin (task, opts) {
await task.source(opts.src || 'bin/*').typescript({ module: 'commonjs', stripExtension: true }).target('dist/bin', { mode: '0755' })
const babelOpts = {
...babelServerOpts,
plugins: [ ...babelServerOpts.plugins, 'babel-plugin-dynamic-import-node' ]
}
await task.source(opts.src || 'bin/*').babel(babelOpts, { 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)').typescript({ module: 'commonjs' }).target('dist/cli')
await task.source(opts.src || 'cli/**/*.+(js|ts|tsx)').babel(babelServerOpts).target('dist/cli')
notify('Compiled cli files')
}
export async function lib (task, opts) {
await task.source(opts.src || 'lib/**/*.+(js|ts|tsx)').typescript({ module: 'commonjs' }).target('dist/lib')
await task.source(opts.src || 'lib/**/*.+(js|ts|tsx)').babel(babelServerOpts).target('dist/lib')
notify('Compiled lib files')
}
export async function server (task, opts) {
await task.source(opts.src || 'server/**/*.+(js|ts|tsx)').typescript({ module: 'commonjs' }).target('dist/server')
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).target('dist/server')
notify('Compiled server files')
}
export async function nextbuild (task, opts) {
await task.source(opts.src || 'build/**/*.+(js|ts|tsx)').typescript({ module: 'commonjs' }).target('dist/build')
await task.source(opts.src || 'build/**/*.+(js|ts|tsx)').babel(babelServerOpts).target('dist/build')
notify('Compiled build files')
}
export async function client (task, opts) {
await task.source(opts.src || 'client/**/*.+(js|ts|tsx)').typescript({ module: 'commonjs' }).babel(babelOpts).target('dist/client')
await task.source(opts.src || 'client/**/*.+(js|ts|tsx)').babel(babelClientOpts).target('dist/client')
notify('Compiled client files')
}
// export is a reserved keyword for functions
export async function nextbuildstatic (task, opts) {
await task.source(opts.src || 'export/**/*.+(js|ts|tsx)').typescript({ module: 'commonjs' }).target('dist/export')
await task.source(opts.src || 'export/**/*.+(js|ts|tsx)').babel(babelServerOpts).target('dist/export')
notify('Compiled export files')
}
export async function pages (task, opts) {
await task.source(opts.src || 'pages/**/*.+(js|ts|tsx)').typescript({ module: 'commonjs' }).babel(babelOpts).target('dist/pages')
await task.source(opts.src || 'pages/**/*.+(js|ts|tsx)').babel(babelClientOpts).target('dist/pages')
}
export async function build (task) {
......
......@@ -712,7 +712,7 @@
"@babel/plugin-transform-react-jsx-self" "^7.0.0"
"@babel/plugin-transform-react-jsx-source" "^7.0.0"
"@babel/preset-typescript@^7.0.0":
"@babel/preset-typescript@7.3.3", "@babel/preset-typescript@^7.0.0":
version "7.3.3"
resolved "https://registry.yarnpkg.com/@babel/preset-typescript/-/preset-typescript-7.3.3.tgz#88669911053fa16b2b276ea2ede2ca603b3f307a"
integrity sha512-mzMVuIP4lqtn4du2ynEfdO0+RYcslwrZiJHXu4MGaC1ctJiW2fyaeDrtjJGs7R/KebZ1sgowcIoWf4uRpEfKEg==
......@@ -2583,6 +2583,13 @@ babel-messages@^6.23.0:
dependencies:
babel-runtime "^6.22.0"
babel-plugin-dynamic-import-node@2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.2.0.tgz#c0adfb07d95f4a4495e9aaac6ec386c4d7c2524e"
integrity sha512-fP899ELUnTaBcIzmrW7nniyqqdYWrWuJUyPWHxFa/c7r7hS6KC8FscNfLlBNIoPSc55kYMGEEKjPjJGCLbE1qA==
dependencies:
object.assign "^4.1.0"
babel-plugin-istanbul@^4.1.6:
version "4.1.6"
resolved "https://registry.yarnpkg.com/babel-plugin-istanbul/-/babel-plugin-istanbul-4.1.6.tgz#36c59b2192efce81c5b378321b74175add1c9a45"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment