1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/transpile.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-10-06 11:05:52 +00:00
import { dirname } from 'path'
import fs from 'mz/fs'
import mkdirp from 'mkdirp-then';
import { transformFile } from 'babel-core'
import preset2015 from 'babel-preset-es2015'
import presetReact from 'babel-preset-react'
import transformAsyncToGenerator from 'babel-plugin-transform-async-to-generator'
import transformClassProperties from 'babel-plugin-transform-class-properties'
import transformObjectRestSpread from 'babel-plugin-transform-object-rest-spread'
import transformRuntime from 'babel-plugin-transform-runtime'
import moduleAlias from 'babel-plugin-module-alias'
const babelRuntimePath = require.resolve('babel-runtime/package')
.replace(/[\\\/]package\.json$/, '');
const babelOptions = {
presets: [preset2015, presetReact],
plugins: [
transformAsyncToGenerator,
transformClassProperties,
transformObjectRestSpread,
transformRuntime,
[
moduleAlias,
[
{ src: `npm:${babelRuntimePath}`, expose: 'babel-runtime' },
{ src: `npm:${require.resolve('react')}`, expose: 'react' },
{ src: `npm:${require.resolve('../../lib/link')}`, expose: 'next/link' },
2016-10-07 01:57:31 +00:00
{ src: `npm:${require.resolve('../../lib/css')}`, expose: 'next/css' },
{ src: `npm:${require.resolve('../../lib/head')}`, expose: 'next/head' }
2016-10-06 11:05:52 +00:00
]
]
],
ast: false
}
export default async function transpile (src, dst) {
const code = await new Promise((resolve, reject) => {
transformFile(src, babelOptions, (err, result) => {
if (err) return reject(err)
resolve(result.code)
})
})
await writeFile(dst, code)
}
async function writeFile (path, data) {
await mkdirp(dirname(path))
await fs.writeFile(path, data, { encoding: 'utf8', flag: 'w+' })
}