fix: fix NODE_ENV and webpack config (#818)

This commit is contained in:
Nolan Lawson 2018-12-15 19:21:20 -08:00 committed by GitHub
parent f2f5508144
commit 3ae532aee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 17 deletions

View File

@ -7,7 +7,7 @@
"lint-fix": "standard --fix && standard --fix --plugin html 'src/routes/**/*.html'", "lint-fix": "standard --fix && standard --fix --plugin html 'src/routes/**/*.html'",
"dev": "run-s build-svg build-inline-script serve-dev", "dev": "run-s build-svg build-inline-script serve-dev",
"serve-dev": "run-p --race build-sass-watch sapper-dev", "serve-dev": "run-p --race build-sass-watch sapper-dev",
"sapper-dev": "cross-env PORT=4002 sapper dev", "sapper-dev": "cross-env NODE_ENV=development PORT=4002 sapper dev",
"sapper-prod": "cross-env PORT=4002 node __sapper__/build", "sapper-prod": "cross-env PORT=4002 node __sapper__/build",
"build": "cross-env NODE_ENV=production npm run build-steps", "build": "cross-env NODE_ENV=production npm run build-steps",
"build-steps": "run-s build-sass build-svg build-inline-script sapper-build", "build-steps": "run-s build-sass build-svg build-inline-script sapper-build",

View File

@ -9,6 +9,8 @@ loadPolyfills().then(() => {
sapper.start({ target: document.querySelector('#sapper') }) sapper.start({ target: document.querySelector('#sapper') })
}) })
console.log('process.env.NODE_ENV', process.env.NODE_ENV)
if (module.hot) { if (module.hot) {
module.hot.accept() module.hot.accept()
} }

View File

@ -3,7 +3,7 @@ const config = require('sapper/config/webpack.js')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin') const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const terser = require('./terser.config') const terser = require('./terser.config')
const isDev = process.env.NODE_ENV === 'development' const { mode, dev } = require('./shared.config')
module.exports = { module.exports = {
entry: config.client.entry(), entry: config.client.entry(),
@ -12,7 +12,7 @@ module.exports = {
extensions: ['.js', '.json', '.html'], extensions: ['.js', '.json', '.html'],
mainFields: ['svelte', 'module', 'browser', 'main'] mainFields: ['svelte', 'module', 'browser', 'main']
}, },
mode: isDev ? 'development' : 'production', mode,
module: { module: {
rules: [ rules: [
{ {
@ -20,10 +20,10 @@ module.exports = {
use: { use: {
loader: 'svelte-loader', loader: 'svelte-loader',
options: { options: {
dev: isDev, dev,
hydratable: true, hydratable: true,
store: true, store: true,
hotReload: isDev hotReload: dev
} }
} }
} }
@ -32,7 +32,7 @@ module.exports = {
node: { node: {
setImmediate: false setImmediate: false
}, },
optimization: isDev ? {} : { optimization: dev ? {} : {
minimizer: [ minimizer: [
terser() terser()
], ],
@ -51,14 +51,14 @@ module.exports = {
new LodashModuleReplacementPlugin({ new LodashModuleReplacementPlugin({
paths: true paths: true
}) })
].concat(isDev ? [ ].concat(dev ? [
new webpack.HotModuleReplacementPlugin({ new webpack.HotModuleReplacementPlugin({
requestTimeout: 120000 requestTimeout: 120000
}) })
] : [ ] : [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.browser': true, 'process.browser': true,
'process.env.NODE_ENV': '"production"' 'process.env.NODE_ENV': JSON.stringify(mode)
}), }),
new BundleAnalyzerPlugin({ // generates report.html and stats.json new BundleAnalyzerPlugin({ // generates report.html and stats.json
analyzerMode: 'static', analyzerMode: 'static',
@ -71,5 +71,5 @@ module.exports = {
logLevel: 'silent' // do not bother Webpacker, who runs with --json and parses stdout logLevel: 'silent' // do not bother Webpacker, who runs with --json and parses stdout
}) })
]), ]),
devtool: isDev ? 'inline-source-map' : 'source-map' devtool: dev ? 'inline-source-map' : 'source-map'
} }

View File

@ -1,7 +1,6 @@
const config = require('sapper/config/webpack.js') const config = require('sapper/config/webpack.js')
const pkg = require('../package.json') const pkg = require('../package.json')
const { mode, dev } = require('./shared.config')
const dev = process.env.NODE_ENV === 'development'
module.exports = { module.exports = {
entry: config.server.entry(), entry: config.server.entry(),
@ -29,7 +28,7 @@ module.exports = {
} }
] ]
}, },
mode: process.env.NODE_ENV, mode,
performance: { performance: {
hints: false // it doesn't matter if server.js is large hints: false // it doesn't matter if server.js is large
} }

View File

@ -1,18 +1,17 @@
const config = require('sapper/config/webpack.js') const config = require('sapper/config/webpack.js')
const terser = require('./terser.config') const terser = require('./terser.config')
const webpack = require('webpack') const webpack = require('webpack')
const { mode, dev } = require('./shared.config')
const isDev = config.dev
module.exports = { module.exports = {
entry: config.serviceworker.entry(), entry: config.serviceworker.entry(),
output: config.serviceworker.output(), output: config.serviceworker.output(),
mode: process.env.NODE_ENV, mode,
devtool: isDev ? 'inline-source-map' : 'source-map', devtool: dev ? 'inline-source-map' : 'source-map',
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.browser': true, 'process.browser': true,
'process.env.NODE_ENV': '"production"', 'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.SAPPER_TIMESTAMP': process.env.SAPPER_TIMESTAMP || Date.now() 'process.env.SAPPER_TIMESTAMP': process.env.SAPPER_TIMESTAMP || Date.now()
}), }),
terser() terser()

7
webpack/shared.config.js Normal file
View File

@ -0,0 +1,7 @@
const mode = process.env.NODE_ENV
const dev = mode === 'development'
module.exports = {
mode,
dev
}