rsnext/server/build/webpack.js

148 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-10-15 18:17:27 +02:00
import { resolve, join } from 'path'
2016-10-14 17:05:08 +02:00
import webpack from 'webpack'
import glob from 'glob-promise'
2016-10-15 21:49:42 +02:00
import WriteFilePlugin from 'write-file-webpack-plugin'
2016-10-23 18:42:13 +02:00
import UnlinkFilePlugin from './plugins/unlink-file-plugin'
import WatchPagesPlugin from './plugins/watch-pages-plugin'
import WatchRemoveEventPlugin from './plugins/watch-remove-event-plugin'
import DynamicEntryPlugin from './plugins/dynamic-entry-plugin'
2016-10-14 17:05:08 +02:00
export default async function createCompiler (dir, { hotReload = false } = {}) {
2016-10-15 18:17:27 +02:00
dir = resolve(dir)
const pages = await glob('pages/**/*.js', { cwd: dir })
2016-10-14 17:05:08 +02:00
const entry = {}
const defaultEntries = hotReload ? ['webpack/hot/only-dev-server'] : []
for (const p of pages) {
2016-10-17 05:55:09 +02:00
entry[join('bundles', p)] = defaultEntries.concat(['./' + p])
2016-10-14 17:05:08 +02:00
}
2016-10-19 14:58:08 +02:00
const nextPagesDir = join(__dirname, '..', '..', 'pages')
2016-10-19 14:41:45 +02:00
2016-10-17 05:55:09 +02:00
const errorEntry = join('bundles', 'pages', '_error.js')
2016-10-19 14:58:08 +02:00
const defaultErrorPath = join(nextPagesDir, '_error.js')
2016-10-17 05:55:09 +02:00
if (!entry[errorEntry]) entry[errorEntry] = defaultErrorPath
2016-10-14 17:05:08 +02:00
2016-10-19 14:41:45 +02:00
const errorDebugEntry = join('bundles', 'pages', '_error-debug.js')
2016-10-19 14:58:08 +02:00
const errorDebugPath = join(nextPagesDir, '_error-debug.js')
2016-10-19 14:41:45 +02:00
entry[errorDebugEntry] = errorDebugPath
2016-10-19 14:58:08 +02:00
const nodeModulesDir = join(__dirname, '..', '..', '..', 'node_modules')
2016-10-14 17:05:08 +02:00
2016-10-16 04:49:09 +02:00
const plugins = [
2016-10-18 16:14:00 +02:00
new WriteFilePlugin({
exitOnErrors: false,
2016-10-23 18:42:13 +02:00
log: false,
// required not to cache removed files
useHashIndex: false
})
].concat(hotReload ? [
new webpack.HotModuleReplacementPlugin(),
new DynamicEntryPlugin(),
new UnlinkFilePlugin(),
new WatchRemoveEventPlugin(),
new WatchPagesPlugin(dir)
] : [
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false },
sourceMap: false
2016-10-18 16:14:00 +02:00
})
2016-10-23 18:42:13 +02:00
])
2016-10-15 21:49:42 +02:00
2016-10-14 17:05:08 +02:00
const babelRuntimePath = require.resolve('babel-runtime/package')
.replace(/[\\\/]package\.json$/, '')
2016-10-14 17:05:08 +02:00
const loaders = [{
2016-10-15 18:17:27 +02:00
test: /\.js$/,
loader: 'emit-file-loader',
2016-10-19 14:41:45 +02:00
include: [dir, nextPagesDir],
2016-10-25 11:11:39 +02:00
exclude (str) {
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
},
2016-10-15 18:17:27 +02:00
query: {
2016-10-17 05:55:09 +02:00
name: 'dist/[path][name].[ext]'
2016-10-15 18:17:27 +02:00
}
2016-10-19 14:41:45 +02:00
}]
.concat(hotReload ? [{
test: /\.js$/,
loader: 'hot-self-accept-loader',
2016-10-19 14:58:08 +02:00
include: join(dir, 'pages')
2016-10-19 14:41:45 +02:00
}] : [])
.concat([{
2016-10-14 17:05:08 +02:00
test: /\.js$/,
loader: 'babel',
2016-10-19 14:41:45 +02:00
include: [dir, nextPagesDir],
2016-10-25 11:11:39 +02:00
exclude (str) {
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
},
2016-10-14 17:05:08 +02:00
query: {
presets: ['es2015', 'react'],
plugins: [
'transform-async-to-generator',
'transform-object-rest-spread',
'transform-class-properties',
'transform-runtime',
[
'module-alias',
[
{ 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' }
]
]
]
}
2016-10-19 14:41:45 +02:00
}])
const interpolateNames = new Map([
[defaultErrorPath, 'dist/pages/_error.js'],
[errorDebugPath, 'dist/pages/_error-debug.js']
])
2016-10-14 17:05:08 +02:00
return webpack({
context: dir,
entry,
output: {
2016-10-19 14:58:08 +02:00
path: join(dir, '.next'),
2016-10-14 17:05:08 +02:00
filename: '[name]',
libraryTarget: 'commonjs2',
publicPath: hotReload ? 'http://localhost:3030/' : null
},
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'
}
],
resolve: {
root: [
nodeModulesDir,
2016-10-19 14:58:08 +02:00
join(dir, 'node_modules')
2016-10-14 17:05:08 +02:00
]
},
resolveLoader: {
root: [
nodeModulesDir,
2016-10-19 14:58:08 +02:00
join(__dirname, 'loaders')
2016-10-14 17:05:08 +02:00
]
},
2016-10-15 21:49:42 +02:00
plugins,
2016-10-14 17:05:08 +02:00
module: {
preLoaders: [
{ test: /\.json$/, loader: 'json-loader' }
],
loaders
2016-10-16 06:01:17 +02:00
},
customInterpolateName: function (url, name, opts) {
2016-10-19 14:41:45 +02:00
return interpolateNames.get(this.resourcePath) || url
2016-10-14 17:05:08 +02:00
}
})
}