2018-01-30 15:40:52 +00:00
|
|
|
import path, {sep} from 'path'
|
2016-10-14 15:05:08 +00:00
|
|
|
import webpack from 'webpack'
|
2018-01-30 15:40:52 +00:00
|
|
|
import resolve from 'resolve'
|
|
|
|
import UglifyJSPlugin from 'uglifyjs-webpack-plugin'
|
|
|
|
import CaseSensitivePathPlugin from 'case-sensitive-paths-webpack-plugin'
|
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'
|
2018-01-30 15:40:52 +00:00
|
|
|
import {getPages} from './webpack/utils'
|
2017-04-03 18:10:24 +00:00
|
|
|
import PagesPlugin from './plugins/pages-plugin'
|
2018-01-30 15:40:52 +00:00
|
|
|
import NextJsSsrImportPlugin from './plugins/nextjs-ssr-import'
|
2017-04-17 20:15:50 +00:00
|
|
|
import DynamicChunksPlugin from './plugins/dynamic-chunks-plugin'
|
2018-01-30 15:40:52 +00:00
|
|
|
import UnlinkFilePlugin from './plugins/unlink-file-plugin'
|
2017-02-12 21:18:22 +00:00
|
|
|
import findBabelConfig from './babel/find-config'
|
2016-10-14 15:05:08 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
const nextDir = path.join(__dirname, '..', '..', '..')
|
|
|
|
const nextNodeModulesDir = path.join(nextDir, 'node_modules')
|
|
|
|
const nextPagesDir = path.join(nextDir, 'pages')
|
2016-12-27 23:28:19 +00:00
|
|
|
const defaultPages = [
|
|
|
|
'_error.js',
|
|
|
|
'_document.js'
|
|
|
|
]
|
2017-01-12 15:39:04 +00:00
|
|
|
const interpolateNames = new Map(defaultPages.map((p) => {
|
2018-01-30 15:40:52 +00:00
|
|
|
return [path.join(nextPagesDir, p), `dist/bundles/pages/${p}`]
|
2017-01-12 15:39:04 +00:00
|
|
|
}))
|
2016-12-27 23:28:19 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
function babelConfig (dir, {isServer, dev}) {
|
2016-12-26 18:13:45 +00:00
|
|
|
const mainBabelOptions = {
|
2016-12-23 00:49:29 +00:00
|
|
|
cacheDirectory: true,
|
2018-01-30 15:40:52 +00:00
|
|
|
presets: [],
|
|
|
|
plugins: [
|
|
|
|
dev && !isServer && require.resolve('react-hot-loader/babel')
|
|
|
|
].filter(Boolean)
|
2016-12-22 01:36:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 21:18:22 +00:00
|
|
|
const externalBabelConfig = findBabelConfig(dir)
|
|
|
|
if (externalBabelConfig) {
|
2018-01-30 15:40:52 +00:00
|
|
|
// Log it out once
|
|
|
|
if (!isServer) {
|
|
|
|
console.log(`> Using external babel configuration`)
|
|
|
|
console.log(`> Location: "${externalBabelConfig.loc}"`)
|
|
|
|
}
|
2017-02-12 21:18:22 +00:00
|
|
|
// It's possible to turn off babelrc support via babelrc itself.
|
|
|
|
// In that case, we should add our default preset.
|
|
|
|
// That's why we need to do this.
|
|
|
|
const { options } = externalBabelConfig
|
|
|
|
mainBabelOptions.babelrc = options.babelrc !== false
|
2016-12-26 18:13:45 +00:00
|
|
|
} else {
|
2017-02-03 20:18:44 +00:00
|
|
|
mainBabelOptions.babelrc = false
|
2016-12-22 01:36:00 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 21:18:22 +00:00
|
|
|
// Add our default preset if the no "babelrc" found.
|
|
|
|
if (!mainBabelOptions.babelrc) {
|
|
|
|
mainBabelOptions.presets.push(require.resolve('./babel/preset'))
|
|
|
|
}
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
return mainBabelOptions
|
|
|
|
}
|
2018-01-03 12:43:48 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
function externalsConfig (dir, isServer) {
|
|
|
|
const externals = []
|
2017-12-05 23:46:06 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
if (!isServer) {
|
|
|
|
return externals
|
|
|
|
}
|
2017-12-05 23:46:06 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
externals.push((context, request, callback) => {
|
|
|
|
resolve(request, { basedir: dir, preserveSymlinks: true }, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
return callback()
|
|
|
|
}
|
2017-02-16 02:07:29 +00:00
|
|
|
|
2018-02-01 09:30:52 +00:00
|
|
|
// Webpack itself has to be compiled because it doesn't always use module relative paths
|
2018-02-06 12:09:41 +00:00
|
|
|
if (res.match(/node_modules[/\\]next[/\\]dist[/\\]pages/)) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.match(/node_modules[/\\]webpack/)) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.match(/node_modules[/\\].*\.js/)) {
|
2018-01-30 15:40:52 +00:00
|
|
|
return callback(null, `commonjs ${request}`)
|
|
|
|
}
|
2017-01-31 06:31:27 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
2017-08-30 10:49:40 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
return externals
|
|
|
|
}
|
2017-08-30 10:49:40 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
export default async function getBaseWebpackConfig (dir, {dev = false, isServer = false, buildId, config}) {
|
|
|
|
const babelLoaderOptions = babelConfig(dir, {dev, isServer})
|
2017-08-30 10:49:40 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
const defaultLoaders = {
|
|
|
|
babel: {
|
|
|
|
loader: 'babel-loader',
|
|
|
|
options: babelLoaderOptions
|
2016-10-15 16:17:27 +00:00
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-01 15:21:18 +00:00
|
|
|
// Support for NODE_PATH
|
|
|
|
const nodePathList = (process.env.NODE_PATH || '')
|
|
|
|
.split(process.platform === 'win32' ? ';' : ':')
|
|
|
|
.filter((p) => !!p)
|
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
let totalPages
|
2016-10-19 12:41:45 +00:00
|
|
|
|
2016-12-17 18:38:11 +00:00
|
|
|
let webpackConfig = {
|
2018-01-30 15:40:52 +00:00
|
|
|
devtool: dev ? 'source-map' : false,
|
|
|
|
name: isServer ? 'server' : 'client',
|
|
|
|
cache: true,
|
|
|
|
target: isServer ? 'node' : 'web',
|
|
|
|
externals: externalsConfig(dir, isServer),
|
2016-10-14 15:05:08 +00:00
|
|
|
context: dir,
|
2018-01-30 15:40:52 +00:00
|
|
|
entry: async () => {
|
2018-02-14 15:20:41 +00:00
|
|
|
const pages = await getPages(dir, {dev, isServer, pageExtensions: config.pageExtensions.join('|')})
|
2018-01-30 15:40:52 +00:00
|
|
|
totalPages = Object.keys(pages).length
|
|
|
|
const mainJS = require.resolve(`../../client/next${dev ? '-dev' : ''}`)
|
|
|
|
const clientConfig = !isServer ? {
|
|
|
|
'main.js': [
|
|
|
|
dev && !isServer && path.join(__dirname, '..', '..', 'client', 'webpack-hot-middleware-client'),
|
|
|
|
dev && !isServer && path.join(__dirname, '..', '..', 'client', 'on-demand-entries-client'),
|
|
|
|
mainJS
|
|
|
|
].filter(Boolean)
|
|
|
|
} : {}
|
|
|
|
return {
|
|
|
|
...clientConfig,
|
|
|
|
...pages
|
|
|
|
}
|
|
|
|
},
|
2016-10-14 15:05:08 +00:00
|
|
|
output: {
|
2018-01-30 15:40:52 +00:00
|
|
|
path: path.join(dir, config.distDir, isServer ? 'dist' : ''), // server compilation goes to `.next/dist`
|
2016-10-14 15:05:08 +00:00
|
|
|
filename: '[name]',
|
|
|
|
libraryTarget: 'commonjs2',
|
2017-04-17 15:33:40 +00:00
|
|
|
// This saves chunks with the name given via require.ensure()
|
2018-01-30 15:40:52 +00:00
|
|
|
chunkFilename: '[name]-[chunkhash].js',
|
|
|
|
strictModuleExceptionHandling: true,
|
|
|
|
devtoolModuleFilenameTemplate: '[absolute-resource-path]'
|
2016-10-14 15:05:08 +00:00
|
|
|
},
|
2018-01-30 15:40:52 +00:00
|
|
|
performance: { hints: false },
|
2016-10-14 15:05:08 +00:00
|
|
|
resolve: {
|
2018-01-30 15:40:52 +00:00
|
|
|
extensions: ['.js', '.jsx', '.json'],
|
|
|
|
modules: [
|
|
|
|
nextNodeModulesDir,
|
2018-02-01 15:21:18 +00:00
|
|
|
'node_modules',
|
|
|
|
...nodePathList // Support for NODE_PATH environment variable
|
2018-01-30 15:40:52 +00:00
|
|
|
],
|
2018-01-03 12:43:48 +00:00
|
|
|
alias: {
|
2018-01-30 15:40:52 +00:00
|
|
|
next: nextDir,
|
2018-02-08 08:51:01 +00:00
|
|
|
// React already does something similar to this.
|
|
|
|
// But if the user has react-devtools, it'll throw an error showing that
|
|
|
|
// we haven't done dead code elimination (via uglifyjs).
|
|
|
|
// We purposly do not uglify React code to save the build time.
|
|
|
|
// (But it didn't increase the overall build size)
|
|
|
|
// Here we are doing an exact match with '$'
|
|
|
|
// So, you can still require nested modules like `react-dom/server`
|
|
|
|
react$: dev ? 'react/cjs/react.development.js' : 'react/cjs/react.production.min.js',
|
|
|
|
'react-dom$': dev ? 'react-dom/cjs/react-dom.development.js' : 'react-dom/cjs/react-dom.production.min.js'
|
2018-01-30 15:40:52 +00:00
|
|
|
}
|
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',
|
2018-02-01 15:21:18 +00:00
|
|
|
path.join(__dirname, 'loaders'),
|
|
|
|
...nodePathList // Support for NODE_PATH environment variable
|
2016-10-14 15:05:08 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
module: {
|
2018-01-03 12:43:48 +00:00
|
|
|
rules: [
|
2018-01-30 15:40:52 +00:00
|
|
|
dev && !isServer && {
|
|
|
|
test: /\.(js|jsx)(\?[^?]*)?$/,
|
|
|
|
loader: 'hot-self-accept-loader',
|
|
|
|
include: [
|
|
|
|
path.join(dir, 'pages'),
|
|
|
|
nextPagesDir
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: /\.+(js|jsx)$/,
|
|
|
|
include: [dir],
|
|
|
|
exclude: /node_modules/,
|
|
|
|
use: defaultLoaders.babel
|
|
|
|
}
|
|
|
|
].filter(Boolean)
|
2016-10-16 04:01:17 +00:00
|
|
|
},
|
2018-01-30 15:40:52 +00:00
|
|
|
plugins: [
|
|
|
|
new webpack.IgnorePlugin(/(precomputed)/, /node_modules.+(elliptic)/),
|
|
|
|
dev && new webpack.NoEmitOnErrorsPlugin(),
|
|
|
|
dev && !isServer && new FriendlyErrorsWebpackPlugin(),
|
|
|
|
dev && new webpack.NamedModulesPlugin(),
|
|
|
|
dev && !isServer && new webpack.HotModuleReplacementPlugin(), // Hot module replacement
|
|
|
|
dev && new UnlinkFilePlugin(),
|
|
|
|
dev && new CaseSensitivePathPlugin(), // Since on macOS the filesystem is case-insensitive this will make sure your path are case-sensitive
|
|
|
|
dev && new webpack.LoaderOptionsPlugin({
|
|
|
|
options: {
|
|
|
|
context: dir,
|
|
|
|
customInterpolateName (url, name, opts) {
|
|
|
|
return interpolateNames.get(this.resourcePath) || url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
dev && new WriteFilePlugin({
|
|
|
|
exitOnErrors: false,
|
|
|
|
log: false,
|
|
|
|
// required not to cache removed files
|
|
|
|
useHashIndex: false
|
|
|
|
}),
|
|
|
|
!dev && new webpack.IgnorePlugin(/react-hot-loader/),
|
|
|
|
!isServer && !dev && new UglifyJSPlugin({
|
|
|
|
exclude: /react\.js/,
|
|
|
|
parallel: true,
|
|
|
|
sourceMap: false,
|
|
|
|
uglifyOptions: {
|
|
|
|
compress: {
|
|
|
|
arrows: false,
|
|
|
|
booleans: false,
|
|
|
|
collapse_vars: false,
|
|
|
|
comparisons: false,
|
|
|
|
computed_props: false,
|
|
|
|
hoist_funs: false,
|
|
|
|
hoist_props: false,
|
|
|
|
hoist_vars: false,
|
|
|
|
if_return: false,
|
|
|
|
inline: false,
|
|
|
|
join_vars: false,
|
|
|
|
keep_infinity: true,
|
|
|
|
loops: false,
|
|
|
|
negate_iife: false,
|
|
|
|
properties: false,
|
|
|
|
reduce_funcs: false,
|
|
|
|
reduce_vars: false,
|
|
|
|
sequences: false,
|
|
|
|
side_effects: false,
|
|
|
|
switches: false,
|
|
|
|
top_retain: false,
|
|
|
|
toplevel: false,
|
|
|
|
typeofs: false,
|
|
|
|
unused: false,
|
2018-03-08 08:51:40 +00:00
|
|
|
conditionals: true,
|
2018-01-30 15:40:52 +00:00
|
|
|
dead_code: true,
|
2018-03-08 08:51:40 +00:00
|
|
|
evaluate: true
|
2018-01-30 15:40:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
new webpack.DefinePlugin({
|
|
|
|
'process.env.NODE_ENV': JSON.stringify(dev ? 'development' : 'production')
|
|
|
|
}),
|
|
|
|
!dev && new webpack.optimize.ModuleConcatenationPlugin(),
|
|
|
|
!isServer && new PagesPlugin(),
|
|
|
|
!isServer && new DynamicChunksPlugin(),
|
2018-02-19 10:49:41 +00:00
|
|
|
isServer && new NextJsSsrImportPlugin(),
|
2018-03-06 09:45:29 +00:00
|
|
|
// In dev mode, we don't move anything to the commons bundle.
|
|
|
|
// In production we move common modules into the existing main.js bundle
|
2018-03-21 11:16:44 +00:00
|
|
|
!isServer && new webpack.optimize.CommonsChunkPlugin({
|
2018-03-06 09:45:29 +00:00
|
|
|
name: 'main.js',
|
|
|
|
filename: 'main.js',
|
2018-01-30 15:40:52 +00:00
|
|
|
minChunks (module, count) {
|
2018-03-21 11:16:44 +00:00
|
|
|
// React and React DOM are used everywhere in Next.js. So they should always be common. Even in development mode, to speed up compilation.
|
2018-03-06 09:45:29 +00:00
|
|
|
if (module.resource && module.resource.includes(`${sep}react-dom${sep}`) && count >= 0) {
|
2018-01-30 15:40:52 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-03-06 09:45:29 +00:00
|
|
|
if (module.resource && module.resource.includes(`${sep}react${sep}`) && count >= 0) {
|
2018-01-30 15:40:52 +00:00
|
|
|
return true
|
|
|
|
}
|
2018-03-21 11:16:44 +00:00
|
|
|
|
|
|
|
// In the dev we use on-demand-entries.
|
|
|
|
// So, it makes no sense to use commonChunks based on the minChunks count.
|
|
|
|
// Instead, we move all the code in node_modules into each of the pages.
|
|
|
|
if (dev) {
|
|
|
|
return false
|
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
2018-03-06 09:45:29 +00:00
|
|
|
// commons
|
2018-01-30 15:40:52 +00:00
|
|
|
// If there are one or two pages, only move modules to common if they are
|
|
|
|
// used in all of the pages. Otherwise, move modules used in at-least
|
|
|
|
// 1/2 of the total pages into commons.
|
|
|
|
if (totalPages <= 2) {
|
|
|
|
return count >= totalPages
|
|
|
|
}
|
|
|
|
return count >= totalPages * 0.5
|
2018-03-06 09:45:29 +00:00
|
|
|
// commons end
|
2018-01-30 15:40:52 +00:00
|
|
|
}
|
|
|
|
}),
|
2018-03-06 09:45:29 +00:00
|
|
|
// We use a manifest file in development to speed up HMR
|
|
|
|
dev && !isServer && new webpack.optimize.CommonsChunkPlugin({
|
2018-01-30 15:40:52 +00:00
|
|
|
name: 'manifest',
|
|
|
|
filename: 'manifest.js'
|
|
|
|
})
|
|
|
|
].filter(Boolean)
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2016-12-22 01:36:00 +00:00
|
|
|
|
2018-01-30 15:40:52 +00:00
|
|
|
if (typeof config.webpack === 'function') {
|
2018-02-17 11:37:33 +00:00
|
|
|
webpackConfig = config.webpack(webpackConfig, {dir, dev, isServer, buildId, config, defaultLoaders, totalPages})
|
2016-12-17 18:38:11 +00:00
|
|
|
}
|
2018-01-30 15:40:52 +00:00
|
|
|
|
|
|
|
return webpackConfig
|
2016-10-14 15:05:08 +00:00
|
|
|
}
|