Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Commit 7a0d804

Browse files
committed
cleanup / DRY up code with lodash, update readme and add test to verify devtool is always set unless false
1 parent 752ae97 commit 7a0d804

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Object of webpack options. Just `require` in the options from your `webpack.conf
9393
}
9494
```
9595

96-
Source maps are always enabled unless explicitly disabled by specifying `devtool: false` or `mode: "production"`.
96+
Source maps are **always enabled** unless explicitly disabled by specifying `devtool: false`.
9797

9898
Webpack [mode](https://webpack.js.org/configuration/mode/) is set to `development` if not present. You can set `mode` to "development", "production" or "none".
9999

index.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as webpack from 'webpack'
21
import * as Promise from 'bluebird'
32
import * as events from 'events'
4-
3+
import * as _ from 'lodash'
4+
import * as webpack from 'webpack'
55
import { createDeferred } from './deferred'
66

77
const path = require('path')
@@ -122,40 +122,45 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F
122122
return bundles[filePath]
123123
}
124124

125-
// user can override the default options
126-
let webpackOptions: webpack.Configuration = options.webpackOptions || getDefaultWebpackOptions()
127-
const watchOptions = options.watchOptions || {}
125+
const defaultWebpackOptions = getDefaultWebpackOptions()
128126

129-
debug('webpackOptions: %o', webpackOptions)
130-
debug('watchOptions: %o', watchOptions)
131-
132-
const entry = [filePath].concat(options.additionalEntries || [])
133127
// we're provided a default output path that lives alongside Cypress's
134128
// app data files so we don't have to worry about where to put the bundled
135129
// file on disk
136130
const outputPath = path.extname(file.outputPath) === '.js'
137131
? file.outputPath
138132
: `${file.outputPath}.js`
139133

140-
// we need to set entry and output
141-
webpackOptions = Object.assign(webpackOptions, {
134+
const entry = [filePath].concat(options.additionalEntries || [])
135+
136+
const watchOptions = options.watchOptions || {}
137+
138+
// user can override the default options
139+
const webpackOptions: webpack.Configuration = _
140+
.chain(options.webpackOptions)
141+
.defaultTo(defaultWebpackOptions)
142+
.defaults({
143+
mode: defaultWebpackOptions.mode,
144+
})
145+
.assign({
146+
// we need to set entry and output
142147
entry,
143148
output: {
144149
path: path.dirname(outputPath),
145150
filename: path.basename(outputPath),
146151
},
147152
})
153+
.tap((opts) => {
154+
if (opts.devtool !== false) {
155+
debug('setting devtool to inline-source-map')
148156

149-
if (webpackOptions.devtool !== false) {
150-
debug('setting devtool to inline-source-map')
151-
webpackOptions.devtool = 'inline-source-map'
152-
}
153-
154-
if (!('mode' in webpackOptions)) {
155-
debug('setting webpack mode to development')
156-
webpackOptions.mode = 'development'
157-
}
157+
opts.devtool = 'inline-source-map'
158+
}
159+
})
160+
.value() as any
158161

162+
debug('webpackOptions: %o', webpackOptions)
163+
debug('watchOptions: %o', watchOptions)
159164
debug(`input: ${filePath}`)
160165
debug(`output: ${outputPath}`)
161166

@@ -282,8 +287,7 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F
282287
}
283288
}
284289

285-
// provide a clone of the default options, lazy-loading them
286-
// so they aren't required unless the user utilizes them
290+
// provide a clone of the default options
287291
Object.defineProperty(preprocessor, 'defaultOptions', {
288292
get () {
289293
debug('get default options')

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
},
3030
"dependencies": {
3131
"bluebird": "3.7.1",
32-
"debug": "4.1.1"
32+
"debug": "4.1.1",
33+
"lodash": "4.17.15"
3334
},
3435
"devDependencies": {
3536
"@babel/core": "^7.0.1",

test/unit/index.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ describe('webpack preprocessor', function () {
167167
})
168168
})
169169
})
170+
171+
it('always sets devtool even when mode is "production"', function () {
172+
const options = { webpackOptions: { mode: 'production' } }
173+
174+
return this.run(options).then(() => {
175+
expect(webpack).to.be.calledWithMatch({
176+
devtool: 'inline-source-map',
177+
})
170178
})
171179
})
172180
})
@@ -184,7 +192,9 @@ describe('webpack preprocessor', function () {
184192
const options = { webpackOptions: { mode: 'production' } }
185193

186194
return this.run(options).then(() => {
187-
expect(webpack.lastCall.args[0].mode).to.equal('production')
195+
expect(webpack).to.be.calledWithMatch({
196+
mode: 'production',
197+
})
188198
})
189199
})
190200
})

0 commit comments

Comments
 (0)