Skip to content

Commit e84e34f

Browse files
justin808gauravtiwari
authored andcommitted
Only include dev-server parts if for dev server (#2644)
* 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
1 parent 7799adb commit e84e34f

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

package/__tests__/development.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ describe('Development environment', () => {
1111
describe('toWebpackConfig', () => {
1212
beforeEach(() => jest.resetModules())
1313

14-
test('should use development config and environment', () => {
14+
test('should use development config and environment including devServer if WEBPACK_DEV_SERVER', () => {
1515
process.env.RAILS_ENV = 'development'
1616
process.env.NODE_ENV = 'development'
17+
process.env.WEBPACK_DEV_SERVER = 'YES'
1718
const { environment } = require('../index')
1819

1920
const config = environment.toWebpackConfig()
@@ -26,5 +27,17 @@ describe('Development environment', () => {
2627
}
2728
})
2829
})
30+
31+
test('should use development config and environment if WEBPACK_DEV_SERVER', () => {
32+
process.env.RAILS_ENV = 'development'
33+
process.env.NODE_ENV = 'development'
34+
process.env.WEBPACK_DEV_SERVER = undefined
35+
const { environment } = require('../index')
36+
37+
const config = environment.toWebpackConfig()
38+
expect(config.output.path).toEqual(resolve('public', 'packs'))
39+
expect(config.output.publicPath).toEqual('/packs/')
40+
expect(config.devServer).toEqual(undefined)
41+
})
2942
})
3043
})

package/environments/development.js

+39-33
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,47 @@ module.exports = class extends Base {
77
constructor() {
88
super()
99

10-
if (devServer.hmr) {
11-
this.plugins.append('HotModuleReplacement', new webpack.HotModuleReplacementPlugin())
12-
this.config.output.filename = '[name]-[hash].js'
13-
}
14-
1510
this.config.merge({
1611
mode: 'development',
17-
devtool: 'cheap-module-source-map',
18-
devServer: {
19-
clientLogLevel: 'none',
20-
compress: devServer.compress,
21-
quiet: devServer.quiet,
22-
disableHostCheck: devServer.disable_host_check,
23-
host: devServer.host,
24-
port: devServer.port,
25-
https: devServer.https,
26-
hot: devServer.hmr,
27-
contentBase,
28-
inline: devServer.inline,
29-
useLocalIp: devServer.use_local_ip,
30-
public: devServer.public,
31-
publicPath,
32-
historyApiFallback: {
33-
disableDotRule: true
34-
},
35-
headers: devServer.headers,
36-
overlay: devServer.overlay,
37-
stats: {
38-
entrypoints: false,
39-
errorDetails: true,
40-
modules: false,
41-
moduleTrace: false
42-
},
43-
watchOptions: devServer.watch_options
44-
}
12+
devtool: 'cheap-module-source-map'
4513
})
14+
15+
if (process.env.WEBPACK_DEV_SERVER
16+
&& process.env.WEBPACK_DEV_SERVER !== 'undefined') {
17+
if (devServer.hmr) {
18+
this.plugins.append('HotModuleReplacement', new webpack.HotModuleReplacementPlugin())
19+
this.config.output.filename = '[name]-[hash].js'
20+
}
21+
22+
this.config.merge({
23+
devServer: {
24+
clientLogLevel: 'none',
25+
compress: devServer.compress,
26+
quiet: devServer.quiet,
27+
disableHostCheck: devServer.disable_host_check,
28+
host: devServer.host,
29+
port: devServer.port,
30+
https: devServer.https,
31+
hot: devServer.hmr,
32+
contentBase,
33+
inline: devServer.inline,
34+
useLocalIp: devServer.use_local_ip,
35+
public: devServer.public,
36+
publicPath,
37+
historyApiFallback: {
38+
disableDotRule: true
39+
},
40+
headers: devServer.headers,
41+
overlay: devServer.overlay,
42+
stats: {
43+
entrypoints: false,
44+
errorDetails: true,
45+
modules: false,
46+
moduleTrace: false
47+
},
48+
watchOptions: devServer.watch_options
49+
}
50+
})
51+
}
4652
}
4753
}

0 commit comments

Comments
 (0)