1
0
Fork 0
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:
nkzawa 2016-10-17 10:54:36 +09:00
parent 82f23e2aca
commit 577e2c27a7
3 changed files with 15 additions and 124 deletions

View file

@ -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()
})
})
}

View file

@ -1,26 +1,21 @@
import { resolve } from 'path' import webpack from './webpack'
import glob from 'glob-promise'
import transpile from './transpile'
import bundle from './bundle'
export default async function build (dir) { export default async function build (dir) {
const dstDir = resolve(dir, '.next') const compiler = await webpack(dir)
const templateDir = resolve(__dirname, '..', '..', 'pages')
// create `.next/pages/_error.js` return new Promise((resolve, reject) => {
// which may be overwriten by the user sciprt, `pages/_error.js` compiler.run((err, stats) => {
const templatPaths = await glob('**/*.js', { cwd: templateDir }) if (err) return reject(err)
await Promise.all(templatPaths.map(async (p) => {
await transpile(resolve(templateDir, p), resolve(dstDir, 'pages', p))
}))
const paths = await glob('**/*.js', { cwd: dir, ignore: 'node_modules/**' }) const jsonStats = stats.toJson()
await Promise.all(paths.map(async (p) => { if (jsonStats.errors.length > 0) {
await transpile(resolve(dir, p), resolve(dstDir, p)) 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 }) resolve()
await Promise.all(pagePaths.map(async (p) => { })
await bundle(resolve(dstDir, p), resolve(dstDir, '_bundles', p)) })
}))
} }

View file

@ -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+' })
}