2016-10-15 16:17:27 +00:00
|
|
|
import { resolve, join } from 'path'
|
2016-12-02 01:43:38 +00:00
|
|
|
import { createHash } from 'crypto'
|
2016-12-26 18:13:45 +00:00
|
|
|
import { existsSync } from 'fs'
|
2016-10-14 15:05:08 +00:00
|
|
|
import webpack from 'webpack'
|
|
|
|
import glob from 'glob-promise'
|
2016-10-15 19:49:42 +00:00
|
|
|
import WriteFilePlugin from 'write-file-webpack-plugin'
|
2016-12-03 22:01:15 +00:00
|
|
|
import FriendlyErrorsWebpackPlugin from 'friendly-errors-webpack-plugin'
|
2017-01-09 02:12:29 +00:00
|
|
|
import CaseSensitivePathPlugin from 'case-sensitive-paths-webpack-plugin'
|
2016-10-23 16:42:13 +00:00
|
|
|
import UnlinkFilePlugin from './plugins/unlink-file-plugin'
|
|
|
|
import WatchPagesPlugin from './plugins/watch-pages-plugin'
|
2016-12-31 12:46:23 +00:00
|
|
|
import JsonPagesPlugin from './plugins/json-pages-plugin'
|
2016-12-17 18:38:11 +00:00
|
|
|
import getConfig from '../config'
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2016-12-27 23:28:19 +00:00
|
|
|
const documentPage = join('pages', '_document.js')
|
|
|
|
const defaultPages = [
|
|
|
|
'_error.js',
|
|
|
|
'_document.js'
|
|
|
|
]
|
2017-01-12 15:39:04 +00:00
|
|
|
const nextPagesDir = join(__dirname, '..', '..', 'pages')
|
|
|
|
const nextNodeModulesDir = join(__dirname, '..', '..', '..', 'node_modules')
|
|
|
|
const interpolateNames = new Map(defaultPages.map((p) => {
|
|
|
|
return [join(nextPagesDir, p), `dist/pages/${p}`]
|
|
|
|
}))
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2016-12-21 14:39:08 +00:00
|
|
|
export default async function createCompiler (dir, { dev = false, quiet = false } = {}) {
|
2016-10-15 16:17:27 +00:00
|
|
|
dir = resolve(dir)
|
2016-12-22 01:36:00 +00:00
|
|
|
const config = getConfig(dir)
|
2016-12-16 20:33:08 +00:00
|
|
|
const defaultEntries = dev
|
|
|
|
? [join(__dirname, '..', '..', 'client/webpack-hot-middleware-client')] : []
|
2017-01-12 15:39:04 +00:00
|
|
|
const mainJS = dev
|
|
|
|
? require.resolve('../../client/next-dev') : require.resolve('../../client/next')
|
|
|
|
|
|
|
|
let minChunks
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2017-01-12 15:39:04 +00:00
|
|
|
const entry = async () => {
|
|
|
|
const entries = { 'main.js': mainJS }
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2017-01-12 15:39:04 +00:00
|
|
|
const pages = await glob('pages/**/*.js', { cwd: dir })
|
|
|
|
for (const p of pages) {
|
|
|
|
entries[join('bundles', p)] = [...defaultEntries, `./${p}?entry`]
|
2016-12-27 23:28:19 +00:00
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2017-01-12 15:39:04 +00:00
|
|
|
for (const p of defaultPages) {
|
|
|
|
const entryName = join('bundles', 'pages', p)
|
|
|
|
if (!entries[entryName]) {
|
|
|
|
entries[entryName] = [...defaultEntries, join(nextPagesDir, p) + '?entry']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate minChunks of CommonsChunkPlugin for later use
|
|
|
|
minChunks = Math.max(2, pages.filter((p) => p !== documentPage).length)
|
|
|
|
|
|
|
|
return entries
|
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2016-10-16 02:49:09 +00:00
|
|
|
const plugins = [
|
2016-12-28 18:16:52 +00:00
|
|
|
new webpack.LoaderOptionsPlugin({
|
|
|
|
options: {
|
|
|
|
context: dir,
|
|
|
|
customInterpolateName (url, name, opts) {
|
|
|
|
return interpolateNames.get(this.resourcePath) || url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2016-10-18 14:14:00 +00:00
|
|
|
new WriteFilePlugin({
|
|
|
|
exitOnErrors: false,
|
2016-10-23 16:42:13 +00:00
|
|
|
log: false,
|
|
|
|
// required not to cache removed files
|
|
|
|
useHashIndex: false
|
2016-11-28 00:15:56 +00:00
|
|
|
}),
|
|
|
|
new webpack.optimize.CommonsChunkPlugin({
|
|
|
|
name: 'commons',
|
2016-12-06 09:25:01 +00:00
|
|
|
filename: 'commons.js',
|
2017-01-12 15:39:04 +00:00
|
|
|
minChunks (module, count) {
|
|
|
|
// NOTE: it depends on the fact that the entry funtion is always called
|
|
|
|
// before applying CommonsChunkPlugin
|
|
|
|
return count >= minChunks
|
|
|
|
}
|
2016-12-31 12:46:23 +00:00
|
|
|
}),
|
2017-01-09 02:12:29 +00:00
|
|
|
new JsonPagesPlugin(),
|
|
|
|
new CaseSensitivePathPlugin()
|
2016-11-18 07:26:17 +00:00
|
|
|
]
|
|
|
|
|
2016-12-16 20:33:08 +00:00
|
|
|
if (dev) {
|
2016-11-18 07:26:17 +00:00
|
|
|
plugins.push(
|
|
|
|
new webpack.HotModuleReplacementPlugin(),
|
2017-01-12 15:39:04 +00:00
|
|
|
new webpack.NoEmitOnErrorsPlugin(),
|
2016-11-18 07:26:17 +00:00
|
|
|
new UnlinkFilePlugin(),
|
2016-12-21 14:39:08 +00:00
|
|
|
new WatchPagesPlugin(dir)
|
2016-11-18 07:26:17 +00:00
|
|
|
)
|
2016-12-21 14:39:08 +00:00
|
|
|
if (!quiet) {
|
|
|
|
plugins.push(new FriendlyErrorsWebpackPlugin())
|
|
|
|
}
|
2016-12-16 20:33:08 +00:00
|
|
|
} else {
|
|
|
|
plugins.push(
|
|
|
|
new webpack.DefinePlugin({
|
|
|
|
'process.env.NODE_ENV': JSON.stringify('production')
|
|
|
|
}),
|
|
|
|
new webpack.optimize.UglifyJsPlugin({
|
|
|
|
compress: { warnings: false },
|
|
|
|
sourceMap: false
|
|
|
|
})
|
|
|
|
)
|
2016-11-18 07:26:17 +00:00
|
|
|
}
|
2016-10-15 19:49:42 +00:00
|
|
|
|
2017-01-16 05:40:14 +00:00
|
|
|
const nodePathList = (process.env.NODE_PATH || '')
|
|
|
|
.split(process.platform === 'win32' ? ';' : ':')
|
|
|
|
.filter((p) => !!p)
|
|
|
|
|
2016-12-26 18:13:45 +00:00
|
|
|
const mainBabelOptions = {
|
|
|
|
babelrc: true,
|
2016-12-23 00:49:29 +00:00
|
|
|
cacheDirectory: true,
|
2016-12-22 01:36:00 +00:00
|
|
|
sourceMaps: dev ? 'both' : false,
|
2016-12-26 18:13:45 +00:00
|
|
|
presets: []
|
2016-12-22 01:36:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-26 18:13:45 +00:00
|
|
|
const hasBabelRc = existsSync(join(dir, '.babelrc'))
|
|
|
|
if (hasBabelRc) {
|
|
|
|
console.log('> Using .babelrc defined in your app root')
|
|
|
|
} else {
|
|
|
|
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
|
2016-12-22 01:36:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 18:16:52 +00:00
|
|
|
const rules = (dev ? [{
|
2016-12-06 09:24:42 +00:00
|
|
|
test: /\.js(\?[^?]*)?$/,
|
2016-12-01 22:46:49 +00:00
|
|
|
loader: 'hot-self-accept-loader',
|
|
|
|
include: [
|
|
|
|
join(dir, 'pages'),
|
|
|
|
nextPagesDir
|
|
|
|
]
|
2016-12-05 17:09:38 +00:00
|
|
|
}, {
|
2016-12-06 09:24:42 +00:00
|
|
|
test: /\.js(\?[^?]*)?$/,
|
2016-12-05 17:09:38 +00:00
|
|
|
loader: 'react-hot-loader/webpack',
|
|
|
|
exclude: /node_modules/
|
2016-12-01 22:46:49 +00:00
|
|
|
}] : [])
|
|
|
|
.concat([{
|
|
|
|
test: /\.json$/,
|
|
|
|
loader: 'json-loader'
|
|
|
|
}, {
|
2016-12-06 09:24:42 +00:00
|
|
|
test: /\.(js|json)(\?[^?]*)?$/,
|
2016-10-15 16:17:27 +00:00
|
|
|
loader: 'emit-file-loader',
|
2016-10-19 12:41:45 +00:00
|
|
|
include: [dir, nextPagesDir],
|
2016-10-25 09:11:39 +00:00
|
|
|
exclude (str) {
|
|
|
|
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
|
|
|
|
},
|
2016-12-28 18:16:52 +00:00
|
|
|
options: {
|
2016-10-17 03:55:09 +00:00
|
|
|
name: 'dist/[path][name].[ext]'
|
2016-10-15 16:17:27 +00:00
|
|
|
}
|
2016-12-01 22:46:49 +00:00
|
|
|
}, {
|
2016-12-28 18:16:52 +00:00
|
|
|
loader: 'babel-loader',
|
2016-11-05 16:12:21 +00:00
|
|
|
include: nextPagesDir,
|
2017-01-12 01:57:33 +00:00
|
|
|
exclude (str) {
|
|
|
|
return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
|
|
|
|
},
|
2016-12-28 18:16:52 +00:00
|
|
|
options: {
|
2016-12-11 09:09:54 +00:00
|
|
|
babelrc: false,
|
2016-12-23 00:49:29 +00:00
|
|
|
cacheDirectory: true,
|
2016-12-02 01:43:38 +00:00
|
|
|
sourceMaps: dev ? 'both' : false,
|
2017-01-12 01:57:33 +00:00
|
|
|
presets: [require.resolve('./babel/preset')]
|
2016-11-05 16:12:21 +00:00
|
|
|
}
|
|
|
|
}, {
|
2016-12-06 09:24:42 +00:00
|
|
|
test: /\.js(\?[^?]*)?$/,
|
2016-12-28 18:16:52 +00:00
|
|
|
loader: 'babel-loader',
|
2017-01-12 01:57:33 +00:00
|
|
|
include: [dir],
|
2016-10-25 09:11:39 +00:00
|
|
|
exclude (str) {
|
2017-01-12 01:57:33 +00:00
|
|
|
return /node_modules/.test(str)
|
2016-10-25 09:11:39 +00:00
|
|
|
},
|
2017-01-12 01:57:33 +00:00
|
|
|
options: mainBabelOptions
|
2016-10-19 12:41:45 +00:00
|
|
|
}])
|
|
|
|
|
2016-12-17 18:38:11 +00:00
|
|
|
let webpackConfig = {
|
2016-10-14 15:05:08 +00:00
|
|
|
context: dir,
|
|
|
|
entry,
|
|
|
|
output: {
|
2016-10-19 12:58:08 +00:00
|
|
|
path: join(dir, '.next'),
|
2016-10-14 15:05:08 +00:00
|
|
|
filename: '[name]',
|
|
|
|
libraryTarget: 'commonjs2',
|
2016-12-28 18:16:52 +00:00
|
|
|
publicPath: '/_webpack/',
|
2017-01-05 17:28:54 +00:00
|
|
|
strictModuleExceptionHandling: true,
|
2016-12-02 01:43:38 +00:00
|
|
|
devtoolModuleFilenameTemplate ({ resourcePath }) {
|
|
|
|
const hash = createHash('sha1')
|
|
|
|
hash.update(Date.now() + '')
|
|
|
|
const id = hash.digest('hex').slice(0, 7)
|
|
|
|
|
|
|
|
// append hash id for cache busting
|
|
|
|
return `webpack:///${resourcePath}?${id}`
|
|
|
|
}
|
2016-10-14 15:05:08 +00:00
|
|
|
},
|
|
|
|
resolve: {
|
2016-12-28 18:16:52 +00:00
|
|
|
modules: [
|
2016-12-31 13:17:52 +00:00
|
|
|
nextNodeModulesDir,
|
2017-01-16 05:40:14 +00:00
|
|
|
'node_modules',
|
|
|
|
...nodePathList
|
|
|
|
]
|
2016-10-14 15:05:08 +00:00
|
|
|
},
|
|
|
|
resolveLoader: {
|
2016-12-28 18:16:52 +00:00
|
|
|
modules: [
|
2016-12-31 13:17:52 +00:00
|
|
|
nextNodeModulesDir,
|
2017-01-01 05:57:13 +00:00
|
|
|
'node_modules',
|
2017-01-16 05:40:14 +00:00
|
|
|
join(__dirname, 'loaders'),
|
|
|
|
...nodePathList
|
2016-10-14 15:05:08 +00:00
|
|
|
]
|
|
|
|
},
|
2016-10-15 19:49:42 +00:00
|
|
|
plugins,
|
2016-10-14 15:05:08 +00:00
|
|
|
module: {
|
2016-12-28 18:16:52 +00:00
|
|
|
rules
|
2016-10-16 04:01:17 +00:00
|
|
|
},
|
2016-12-02 01:43:38 +00:00
|
|
|
devtool: dev ? 'inline-source-map' : false,
|
2016-12-28 18:16:52 +00:00
|
|
|
performance: { hints: false }
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2016-12-22 01:36:00 +00:00
|
|
|
|
2016-12-17 18:38:11 +00:00
|
|
|
if (config.webpack) {
|
2016-12-22 01:36:00 +00:00
|
|
|
console.log('> Using "webpack" config function defined in next.config.js.')
|
2016-12-17 18:38:11 +00:00
|
|
|
webpackConfig = await config.webpack(webpackConfig, { dev })
|
|
|
|
}
|
|
|
|
return webpack(webpackConfig)
|
2016-10-14 15:05:08 +00:00
|
|
|
}
|