mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
use webpack for transpiling too
This commit is contained in:
parent
82f23e2aca
commit
577e2c27a7
|
@ -1,53 +0,0 @@
|
|||
import { resolve, dirname, basename } from 'path'
|
||||
import webpack from 'webpack'
|
||||
|
||||
export default function bundle (src, dst) {
|
||||
const compiler = webpack({
|
||||
entry: src,
|
||||
output: {
|
||||
path: dirname(dst),
|
||||
filename: basename(dst),
|
||||
libraryTarget: 'commonjs2'
|
||||
},
|
||||
externals: [
|
||||
'react',
|
||||
'react-dom',
|
||||
{
|
||||
[require.resolve('react')]: 'react',
|
||||
[require.resolve('../../lib/link')]: 'next/link',
|
||||
[require.resolve('../../lib/css')]: 'next/css',
|
||||
[require.resolve('../../lib/head')]: 'next/head'
|
||||
}
|
||||
],
|
||||
resolveLoader: {
|
||||
root: resolve(__dirname, '..', '..', 'node_modules')
|
||||
},
|
||||
plugins: [
|
||||
new webpack.optimize.UglifyJsPlugin({
|
||||
compress: { warnings: false },
|
||||
sourceMap: false
|
||||
})
|
||||
],
|
||||
module: {
|
||||
preLoaders: [
|
||||
{ test: /\.json$/, loader: 'json-loader' }
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
compiler.run((err, stats) => {
|
||||
if (err) return reject(err)
|
||||
|
||||
const jsonStats = stats.toJson()
|
||||
if (jsonStats.errors.length > 0) {
|
||||
const error = new Error(jsonStats.errors[0])
|
||||
error.errors = jsonStats.errors
|
||||
error.warnings = jsonStats.warnings
|
||||
return reject(error)
|
||||
}
|
||||
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
|
@ -1,26 +1,21 @@
|
|||
import { resolve } from 'path'
|
||||
import glob from 'glob-promise'
|
||||
import transpile from './transpile'
|
||||
import bundle from './bundle'
|
||||
import webpack from './webpack'
|
||||
|
||||
export default async function build (dir) {
|
||||
const dstDir = resolve(dir, '.next')
|
||||
const templateDir = resolve(__dirname, '..', '..', 'pages')
|
||||
const compiler = await webpack(dir)
|
||||
|
||||
// create `.next/pages/_error.js`
|
||||
// which may be overwriten by the user sciprt, `pages/_error.js`
|
||||
const templatPaths = await glob('**/*.js', { cwd: templateDir })
|
||||
await Promise.all(templatPaths.map(async (p) => {
|
||||
await transpile(resolve(templateDir, p), resolve(dstDir, 'pages', p))
|
||||
}))
|
||||
return new Promise((resolve, reject) => {
|
||||
compiler.run((err, stats) => {
|
||||
if (err) return reject(err)
|
||||
|
||||
const paths = await glob('**/*.js', { cwd: dir, ignore: 'node_modules/**' })
|
||||
await Promise.all(paths.map(async (p) => {
|
||||
await transpile(resolve(dir, p), resolve(dstDir, p))
|
||||
}))
|
||||
const jsonStats = stats.toJson()
|
||||
if (jsonStats.errors.length > 0) {
|
||||
const error = new Error(jsonStats.errors[0])
|
||||
error.errors = jsonStats.errors
|
||||
error.warnings = jsonStats.warnings
|
||||
return reject(error)
|
||||
}
|
||||
|
||||
const pagePaths = await glob('pages/**/*.js', { cwd: dstDir })
|
||||
await Promise.all(pagePaths.map(async (p) => {
|
||||
await bundle(resolve(dstDir, p), resolve(dstDir, '_bundles', p))
|
||||
}))
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
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' },
|
||||
{ src: `npm:${require.resolve('../../lib/css')}`, expose: 'next/css' },
|
||||
{ src: `npm:${require.resolve('../../lib/head')}`, expose: 'next/head' }
|
||||
]
|
||||
]
|
||||
],
|
||||
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+' })
|
||||
}
|
Loading…
Reference in a new issue