Nuxt/packages/webpack/src/config/base.js

331 lines
8.9 KiB
JavaScript
Raw Normal View History

2018-03-16 19:52:17 +00:00
import path from 'path'
import consola from 'consola'
2018-03-16 16:12:06 +00:00
import TimeFixPlugin from 'time-fix-plugin'
import clone from 'lodash/clone'
import cloneDeep from 'lodash/cloneDeep'
2018-03-22 09:11:16 +00:00
import VueLoader from 'vue-loader'
2018-03-23 16:28:35 +00:00
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
2018-03-24 13:02:04 +00:00
import WebpackBar from 'webpackbar'
import { isUrl, urlJoin } from '@nuxt/common'
import StyleLoader from './utils/style-loader'
2018-03-16 19:52:17 +00:00
import WarnFixPlugin from './plugins/warnfix'
import StatsPlugin from './plugins/stats'
2016-11-07 01:34:58 +00:00
2018-03-22 14:06:54 +00:00
export default class WebpackBaseConfig {
constructor(builder, options) {
this.name = options.name
this.isServer = options.isServer
this.builder = builder
this.nuxt = builder.context.nuxt
this.isStatic = builder.context.isStatic
this.options = builder.context.options
2018-03-22 14:06:54 +00:00
this.spinner = builder.spinner
this.loaders = this.options.build.loaders
2018-03-22 14:06:54 +00:00
}
get nuxtEnv() {
return {
isDev: this.options.dev,
isServer: this.isServer,
isClient: !this.isServer
}
}
2018-03-22 14:06:54 +00:00
getBabelOptions() {
const options = clone(this.options.build.babel)
2018-03-22 14:06:54 +00:00
if (typeof options.presets === 'function') {
options.presets = options.presets({ isServer: this.isServer })
}
if (!options.babelrc && !options.presets) {
options.presets = [
[
2018-08-10 13:45:58 +00:00
require.resolve('@nuxtjs/babel-preset-app'),
2018-03-22 14:06:54 +00:00
{
2018-08-10 13:45:58 +00:00
buildTarget: this.isServer ? 'server' : 'client'
2018-03-22 14:06:54 +00:00
}
]
]
}
return options
}
getFileName(key) {
let fileName = this.options.build.filenames[key]
if (typeof fileName === 'function') {
fileName = fileName(this.nuxtEnv)
}
2018-03-22 14:06:54 +00:00
if (this.options.dev) {
const hash = /\[(chunkhash|contenthash|hash)(?::(\d+))?\]/.exec(fileName)
if (hash) {
consola.warn(`Notice: Please do not use ${hash[1]} in dev mode to prevent memory leak`)
}
}
2018-03-22 14:06:54 +00:00
return fileName
}
devtool() {
return false
}
2018-03-22 14:06:54 +00:00
env() {
const env = {
'process.mode': JSON.stringify(this.options.mode),
'process.static': this.isStatic
}
Object.entries(this.options.env).forEach(([key, value]) => {
2018-03-22 14:06:54 +00:00
env['process.env.' + key] =
['boolean', 'number'].includes(typeof value)
2018-03-22 14:06:54 +00:00
? value
: JSON.stringify(value)
})
return env
}
output() {
return {
path: path.resolve(this.options.buildDir, 'dist', this.isServer ? 'server' : 'client'),
filename: this.getFileName('app'),
chunkFilename: this.getFileName('chunk'),
publicPath: isUrl(this.options.build.publicPath)
2017-06-11 14:17:36 +00:00
? this.options.build.publicPath
: urlJoin(this.options.router.base, this.options.build.publicPath)
2018-03-22 14:06:54 +00:00
}
2016-11-07 01:34:58 +00:00
}
2017-06-15 22:19:53 +00:00
optimization() {
return this.options.build.optimization
}
2018-03-22 14:06:54 +00:00
alias() {
const { srcDir, rootDir, dir: { assets: assetsDir, static: staticDir } } = this.options
2018-03-22 14:06:54 +00:00
return {
'~': path.join(srcDir),
'~~': path.join(rootDir),
'@': path.join(srcDir),
'@@': path.join(rootDir),
[assetsDir]: path.join(srcDir, assetsDir),
[staticDir]: path.join(srcDir, staticDir)
2018-03-22 14:06:54 +00:00
}
}
2018-03-22 14:06:54 +00:00
rules() {
const styleLoader = new StyleLoader(
this.options,
this.nuxt,
{ isServer: this.isServer }
)
const perfLoader = this.builder.perfLoader
2018-03-22 14:06:54 +00:00
return [
{
test: /\.vue$/,
loader: 'vue-loader',
options: this.loaders.vue
2018-03-22 14:06:54 +00:00
},
2018-04-21 07:27:48 +00:00
{
test: /\.pug$/,
oneOf: [
{
resourceQuery: /^\?vue/,
use: [{
loader: 'pug-plain-loader',
options: this.loaders.pugPlain
}]
2018-04-21 07:27:48 +00:00
},
{
use: [
'raw-loader',
{
loader: 'pug-plain-loader',
options: this.loaders.pugPlain
}
]
2018-04-21 07:27:48 +00:00
}
]
},
2018-03-22 14:06:54 +00:00
{
test: /\.jsx?$/,
exclude: (file) => {
// not exclude files outside node_modules
if (!/node_modules/.test(file)) {
return false
}
// item in transpile can be string or regex object
const modulesToTranspile = [/\.vue\.js/].concat(this.options.build.transpile)
return !modulesToTranspile.some(module => module.test(file))
},
use: perfLoader.pool('js', {
loader: require.resolve('babel-loader'),
options: this.getBabelOptions()
})
},
{
test: /\.css$/,
2018-03-29 19:26:42 +00:00
oneOf: perfLoader.poolOneOf('css', styleLoader.apply('css'))
},
{
test: /\.less$/,
oneOf: perfLoader.poolOneOf('css', styleLoader.apply('less', {
loader: 'less-loader',
options: this.loaders.less
}))
2018-03-22 14:06:54 +00:00
},
{
test: /\.sass$/,
2018-03-29 19:26:42 +00:00
oneOf: perfLoader.poolOneOf('css', styleLoader.apply('sass', {
2018-03-22 14:06:54 +00:00
loader: 'sass-loader',
options: this.loaders.sass
}))
2018-03-22 14:06:54 +00:00
},
{
test: /\.scss$/,
oneOf: perfLoader.poolOneOf('css', styleLoader.apply('scss', {
loader: 'sass-loader',
options: this.loaders.scss
}))
},
2018-03-22 14:06:54 +00:00
{
test: /\.styl(us)?$/,
oneOf: perfLoader.poolOneOf('css', styleLoader.apply('stylus', {
loader: 'stylus-loader',
options: this.loaders.stylus
}))
2018-03-22 14:06:54 +00:00
},
{
test: /\.(png|jpe?g|gif|svg|webp)$/,
use: perfLoader.pool('assets', {
loader: 'url-loader',
options: Object.assign(
this.loaders.imgUrl,
{ name: this.getFileName('img') }
)
})
2018-03-22 14:06:54 +00:00
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
use: perfLoader.pool('assets', {
loader: 'url-loader',
options: Object.assign(
this.loaders.fontUrl,
{ name: this.getFileName('font') }
)
})
2018-03-22 14:06:54 +00:00
},
{
test: /\.(webm|mp4|ogv)$/,
use: perfLoader.pool('assets', {
loader: 'file-loader',
options: Object.assign(
this.loaders.file,
{ name: this.getFileName('video') }
)
})
}
2018-03-22 14:06:54 +00:00
]
}
plugins() {
const plugins = [new VueLoader.VueLoaderPlugin()]
2018-03-22 14:06:54 +00:00
Array.prototype.push.apply(plugins, this.options.build.plugins || [])
// Add timefix-plugin before others plugins
if (this.options.dev) {
plugins.unshift(new TimeFixPlugin())
2018-03-18 08:51:56 +00:00
}
2018-03-11 21:28:56 +00:00
2018-03-22 14:06:54 +00:00
// Hide warnings about plugins without a default export (#1179)
plugins.push(new WarnFixPlugin())
2018-03-22 16:22:41 +00:00
// Build progress indicator
2018-03-31 16:22:14 +00:00
plugins.push(new WebpackBar({
profile: this.options.build.profile,
name: this.isServer ? 'server' : 'client',
color: this.isServer ? 'orange' : 'green',
compiledIn: false,
done: (states) => {
2018-03-31 16:22:14 +00:00
if (this.options.dev) {
const hasErrors = Object.values(states).some(state => state.stats.hasErrors())
if (!hasErrors) {
2018-08-15 11:48:34 +00:00
this.nuxt.showReady(false)
}
}
2018-03-31 16:22:14 +00:00
}
}))
2018-03-22 14:06:54 +00:00
2018-03-22 16:22:41 +00:00
// Add stats plugin
if (!this.options.dev && this.options.build.stats) {
2018-03-22 14:06:54 +00:00
plugins.push(new StatsPlugin(this.options.build.stats))
2018-03-22 16:22:41 +00:00
}
2017-06-15 22:19:53 +00:00
2018-03-23 16:28:35 +00:00
// CSS extraction
// MiniCssExtractPlugin does not currently supports SSR
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/48
// So we use css-loader/locals as a fallback (utils/style-loader)
if (this.options.build.extractCSS && !this.isServer) {
2018-03-23 16:28:35 +00:00
plugins.push(new MiniCssExtractPlugin(Object.assign({
filename: this.getFileName('css'),
chunkFilename: this.getFileName('css')
2018-03-23 16:28:35 +00:00
}, this.options.build.extractCSS)))
}
2018-03-22 14:06:54 +00:00
return plugins
}
extendConfig(config) {
if (typeof this.options.build.extend === 'function') {
const extendedConfig = this.options.build.extend.call(
this.builder, config, { loaders: this.loaders, ...this.nuxtEnv }
)
// Only overwrite config when something is returned for backwards compatibility
if (extendedConfig !== undefined) {
return extendedConfig
}
}
return config
}
2018-03-22 14:06:54 +00:00
config() {
// Prioritize nested node_modules in webpack search path (#2558)
const webpackModulesDir = ['node_modules'].concat(this.options.modulesDir)
const config = {
name: this.name,
mode: this.options.dev ? 'development' : 'production',
devtool: this.devtool(),
optimization: this.optimization(),
2018-03-22 14:06:54 +00:00
output: this.output(),
performance: {
maxEntrypointSize: 1000 * 1024,
hints: this.options.dev ? false : 'warning'
},
resolve: {
extensions: ['.wasm', '.mjs', '.js', '.json', '.vue', '.jsx'],
2018-03-22 14:06:54 +00:00
alias: this.alias(),
modules: webpackModulesDir
},
resolveLoader: {
modules: webpackModulesDir
},
module: {
rules: this.rules()
},
plugins: this.plugins()
}
const extendedConfig = this.extendConfig(config)
2018-03-22 14:06:54 +00:00
// Clone deep avoid leaking config between Client and Server
return cloneDeep(extendedConfig)
2018-03-22 14:06:54 +00:00
}
2016-11-07 01:34:58 +00:00
}