Skip to content

Commit

Permalink
Only include dev-server parts if for dev server (#2644)
Browse files Browse the repository at this point in the history
* Only include dev-server parts if for dev server

Without this change, webpack still includes things like window
in the build even if not using the webpack-dev-server.

When that happens, the bundle cannot be used for server-side rendering.

* Linting

* Add jest test
  • Loading branch information
justin808 authored and gauravtiwari committed Aug 16, 2020
1 parent 7799adb commit e84e34f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 34 deletions.
15 changes: 14 additions & 1 deletion package/__tests__/development.js
Expand Up @@ -11,9 +11,10 @@ describe('Development environment', () => {
describe('toWebpackConfig', () => {
beforeEach(() => jest.resetModules())

test('should use development config and environment', () => {
test('should use development config and environment including devServer if WEBPACK_DEV_SERVER', () => {
process.env.RAILS_ENV = 'development'
process.env.NODE_ENV = 'development'
process.env.WEBPACK_DEV_SERVER = 'YES'
const { environment } = require('../index')

const config = environment.toWebpackConfig()
Expand All @@ -26,5 +27,17 @@ describe('Development environment', () => {
}
})
})

test('should use development config and environment if WEBPACK_DEV_SERVER', () => {
process.env.RAILS_ENV = 'development'
process.env.NODE_ENV = 'development'
process.env.WEBPACK_DEV_SERVER = undefined
const { environment } = require('../index')

const config = environment.toWebpackConfig()
expect(config.output.path).toEqual(resolve('public', 'packs'))
expect(config.output.publicPath).toEqual('/packs/')
expect(config.devServer).toEqual(undefined)
})
})
})
72 changes: 39 additions & 33 deletions package/environments/development.js
Expand Up @@ -7,41 +7,47 @@ module.exports = class extends Base {
constructor() {
super()

if (devServer.hmr) {
this.plugins.append('HotModuleReplacement', new webpack.HotModuleReplacementPlugin())
this.config.output.filename = '[name]-[hash].js'
}

this.config.merge({
mode: 'development',
devtool: 'cheap-module-source-map',
devServer: {
clientLogLevel: 'none',
compress: devServer.compress,
quiet: devServer.quiet,
disableHostCheck: devServer.disable_host_check,
host: devServer.host,
port: devServer.port,
https: devServer.https,
hot: devServer.hmr,
contentBase,
inline: devServer.inline,
useLocalIp: devServer.use_local_ip,
public: devServer.public,
publicPath,
historyApiFallback: {
disableDotRule: true
},
headers: devServer.headers,
overlay: devServer.overlay,
stats: {
entrypoints: false,
errorDetails: true,
modules: false,
moduleTrace: false
},
watchOptions: devServer.watch_options
}
devtool: 'cheap-module-source-map'
})

if (process.env.WEBPACK_DEV_SERVER
&& process.env.WEBPACK_DEV_SERVER !== 'undefined') {
if (devServer.hmr) {
this.plugins.append('HotModuleReplacement', new webpack.HotModuleReplacementPlugin())
this.config.output.filename = '[name]-[hash].js'
}

this.config.merge({
devServer: {
clientLogLevel: 'none',
compress: devServer.compress,
quiet: devServer.quiet,
disableHostCheck: devServer.disable_host_check,
host: devServer.host,
port: devServer.port,
https: devServer.https,
hot: devServer.hmr,
contentBase,
inline: devServer.inline,
useLocalIp: devServer.use_local_ip,
public: devServer.public,
publicPath,
historyApiFallback: {
disableDotRule: true
},
headers: devServer.headers,
overlay: devServer.overlay,
stats: {
entrypoints: false,
errorDetails: true,
modules: false,
moduleTrace: false
},
watchOptions: devServer.watch_options
}
})
}
}
}

0 comments on commit e84e34f

Please sign in to comment.