Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit ce3bbc9

Browse files
committed
update to latest api
1 parent af0dded commit ce3bbc9

File tree

3 files changed

+135
-145
lines changed

3 files changed

+135
-145
lines changed

README.md

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ In your project's [plugins file](https://on.cypress.io/guides/plugins):
1717
```javascript
1818
const browserify = require('@cypress/browserify-preprocessor')
1919

20-
module.exports = (on, config) => {
21-
on('file:preprocessor', browserify(config))
20+
module.exports = (on) => {
21+
on('file:preprocessor', browserify())
2222
}
2323
```
2424

@@ -27,26 +27,50 @@ module.exports = (on, config) => {
2727
Pass in options as the second argument to `browserify`:
2828

2929
```javascript
30-
module.exports = (on, config) => {
30+
module.exports = (on) => {
3131
const options = {
3232
// options here
3333
}
3434

35-
on('file:preprocessor', browserify(config, options))
35+
on('file:preprocessor', browserify(options))
3636
}
3737
```
3838

39-
### extensions
39+
### browserifyOptions
4040

41-
Array of file extensions to supported.
41+
Object of options passed to [browserify](https://github.com/browserify/browserify#browserifyfiles--opts).
42+
43+
If you pass one of these top-level options in, it will override the default. So if pass `extensions: ['.cljs']`, the default extensions (`js, jsx, coffee, cjsx`) will no longer be supported. If you wish to add to the supported extensions, read up on [modifying the default options](#modifying-default-options).
44+
45+
As long as the config passed from Cypress indicates that the plugin should watch files, [watchify](https://github.com/browserify/watchify) is automatically configured as a plugin, so there's no need to manually specify it.
4246

4347
**Default**:
4448

4549
```javascript
46-
['.js', '.jsx', '.coffee', '.cjsx']
50+
{
51+
extensions: ['.js', '.jsx', '.coffee', '.cjsx'],
52+
transform: [
53+
[
54+
'cjsxify',
55+
{},
56+
],
57+
[
58+
'babelify',
59+
{
60+
ast: false,
61+
babelrc: false,
62+
plugins: ['babel-plugin-add-module-exports'],
63+
presets: ['babel-preset-env', 'babel-preset-react'],
64+
},
65+
],
66+
],
67+
plugin: [],
68+
cache: {},
69+
packageCache: {},
70+
}
4771
```
4872

49-
### watchOptions
73+
### watchifyOptions
5074

5175
Object of options passed to [watchify](https://github.com/browserify/watchify#options)
5276

@@ -65,30 +89,6 @@ Object of options passed to [watchify](https://github.com/browserify/watchify#op
6589
}
6690
```
6791

68-
### transforms
69-
70-
Array of transforms. Each item is an object with a `transform` set to the path to a transform package or the required module itself and `options` set to the options for that transform.
71-
72-
**Default**:
73-
74-
```javascript
75-
[
76-
{
77-
transform: 'cjsxify',
78-
options: {},
79-
},
80-
{
81-
transform: 'babelify',
82-
options: {
83-
ast: false,
84-
babelrc: false,
85-
plugins: ['babel-plugin-add-module-exports'],
86-
presets: ['babel-preset-env', 'babel-preset-react'],
87-
},
88-
},
89-
]
90-
```
91-
9292
### onBundle
9393

9494
A function that is called with the [browserify bundle](https://github.com/browserify/browserify#browserifyfiles--opts). This allows you to specify external files and plugins. See the [browserify docs](https://github.com/browserify/browserify#baddfile-opts) for methods available.
@@ -108,19 +108,33 @@ browserify({
108108

109109
The default options are provided as `browserify.defaultOptions` so they can be more easily modified.
110110

111-
If, for example, you want to update the options for the `babelify` transform to turn on `babelrc`, you could do the following:
111+
If, for example, you want to update the options for the `babelify` transform to turn on `babelrc` loading, you could do the following:
112112

113113
```javascript
114114
const browserify = require('@cypress/browserify-preprocessor')
115115

116-
module.exports = (on, config) => {
116+
module.exports = (on) => {
117117
const options = browserify.defaultOptions
118118
options.transforms[1].options.babelrc = true
119119

120-
on('file:preprocessor', browserify(config, options))
120+
on('file:preprocessor', browserify(options))
121121
}
122122
```
123123

124+
## Contributing
125+
126+
Run all tests once:
127+
128+
```shell
129+
npm test
130+
```
131+
132+
Run tests in watch mode:
133+
134+
```shell
135+
npm run test-watch
136+
```
137+
124138
## License
125139

126140
This project is licensed under the terms of the [MIT license](/LICENSE.md).

index.js

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,31 @@ const bundles = {}
1414
// by default, we transform JavaScript (up to anything at stage-4), JSX,
1515
// CoffeeScript, and CJSX (CoffeeScript + JSX)
1616
const defaultOptions = {
17-
extensions: ['.js', '.jsx', '.coffee', '.cjsx'],
18-
watchOptions: {
17+
browserifyOptions: {
18+
extensions: ['.js', '.jsx', '.coffee', '.cjsx'],
19+
transform: [
20+
[
21+
require.resolve('./cjsxify'),
22+
{},
23+
],
24+
[
25+
require.resolve('babelify'),
26+
{
27+
ast: false,
28+
babelrc: false,
29+
// irons out differents between ES6 modules and node exports
30+
plugins: ['babel-plugin-add-module-exports'].map(require.resolve),
31+
// babel-preset-env supports any JS that's stage-4, meaning it's
32+
// completely finalized in the ECMA standard
33+
presets: ['babel-preset-env', 'babel-preset-react'].map(require.resolve),
34+
},
35+
],
36+
],
37+
plugin: [],
38+
cache: {},
39+
packageCache: {},
40+
},
41+
watchifyOptions: {
1942
// ignore watching the following or the user's system can get bogged down
2043
// by watchers
2144
ignoreWatch: [
@@ -27,40 +50,19 @@ const defaultOptions = {
2750
'**/node_modules/**',
2851
],
2952
},
30-
transforms: [
31-
{
32-
transform: require.resolve('./cjsxify'),
33-
options: {},
34-
},
35-
{
36-
transform: require.resolve('babelify'),
37-
options: {
38-
ast: false,
39-
babelrc: false,
40-
// irons out differents between ES6 modules and node exports
41-
plugins: ['babel-plugin-add-module-exports'].map(require.resolve),
42-
// babel-preset-env supports any JS that's stage-4, meaning it's
43-
// completely finalized in the ECMA standard
44-
presets: ['babel-preset-env', 'babel-preset-react'].map(require.resolve),
45-
},
46-
},
47-
],
4853
}
4954

5055
// export a function that returns another function, making it easy for users
5156
// to configure like so:
5257
//
53-
// on('file:preprocessor', browserify(config, userOptions))
58+
// on('file:preprocessor', browserify(options))
5459
//
55-
const preprocessor = (config, userOptions = {}) => {
56-
log('received user options', userOptions)
57-
58-
if (!config || typeof config.isTextTerminal !== 'boolean') {
59-
throw new Error(`Cypress Browserify Preprocessor must be called with the Cypress config as its first argument. You passed: ${JSON.stringify(config, null, 2)}`)
60-
}
60+
const preprocessor = (options = {}) => {
61+
log('received user options', options)
6162

6263
// allow user to override default options
63-
const options = Object.assign({}, defaultOptions, userOptions)
64+
const browserifyOptions = Object.assign({}, defaultOptions.browserifyOptions, options.browserifyOptions)
65+
const watchifyOptions = Object.assign({}, defaultOptions.watchifyOptions, options.watchifyOptions)
6466

6567
// we return function that accepts the arguments provided by
6668
// the event 'file:preprocessor'
@@ -73,7 +75,8 @@ const preprocessor = (config, userOptions = {}) => {
7375
// when running in the GUI, it will likely get called multiple times
7476
// with the same filePath, as the user could re-run the tests, causing
7577
// the supported file and spec file to be requested again
76-
return (filePath, util) => {
78+
return (config) => {
79+
const filePath = config.filePath
7780
log('get', filePath)
7881

7982
// since this function can get called multiple times with the same
@@ -84,27 +87,25 @@ const preprocessor = (config, userOptions = {}) => {
8487
return bundles[filePath]
8588
}
8689

87-
// if we're in a text terminal, this is a one-time run, probably in CI
88-
// so we don't need to watch
89-
const shouldWatch = !config.isTextTerminal
9090
// util.getOutputPath returns a path alongside Cypress's other app data
9191
// files so we don't have to worry about where to put the bundled
9292
// file on disk
93-
const outputPath = util.getOutputPath(filePath)
93+
const outputPath = config.outputPath
9494

9595
log(`input: ${filePath}`)
9696
log(`output: ${outputPath}`)
9797

98-
const bundler = browserify({
98+
// we need to override and control entries
99+
Object.assign(browserifyOptions, {
99100
entries: [filePath],
100-
extensions: options.extensions,
101-
cache: {},
102-
packageCache: {},
103101
})
104102

105-
if (shouldWatch) {
103+
log('browserifyOptions:', browserifyOptions)
104+
const bundler = browserify(browserifyOptions)
105+
106+
if (config.shouldWatch) {
106107
log('watching')
107-
bundler.plugin(watchify, options.watchOptions || {})
108+
bundler.plugin(watchify, watchifyOptions)
108109
}
109110

110111
// yield the bundle if onBundle is specified so the user can modify it
@@ -114,15 +115,6 @@ const preprocessor = (config, userOptions = {}) => {
114115
onBundle(bundler)
115116
}
116117

117-
// transforms are part of the options so that users can easily override
118-
// the options of the default cjsxify and babelify tranforms
119-
const transforms = options.transforms
120-
if (Object.prototype.toString.call(transforms) === '[object Array]') {
121-
transforms.forEach((transform) => {
122-
bundler.transform(transform.transform, transform.options)
123-
})
124-
}
125-
126118
// this kicks off the bundling and wraps it up in a promise. the promise
127119
// is what is ultimately returned from this function
128120
// it resolves with the outputPath so Cypress knows where to serve
@@ -148,21 +140,21 @@ const preprocessor = (config, userOptions = {}) => {
148140
ws.on('error', onError)
149141

150142
bundler
151-
.bundle()
152-
.on('error', onError)
153-
.pipe(ws)
143+
.bundle()
144+
.on('error', onError)
145+
.pipe(ws)
154146
})
155147
}
156148

157-
// when we're notified of an update via watchify, we call `util.fileUpdated`
158-
// to let Cypress know to re-run the spec
149+
// when we're notified of an update via watchify, signal for Cypres to
150+
// rerun the spec
159151
bundler.on('update', () => {
160152
log(`update ${filePath}`)
161153
// we overwrite the cached bundle promise, so on subsequent invocations
162154
// it gets the latest bundle
163155
bundles[filePath] = bundle().tap(() => {
164156
log(`- update finished for ${filePath}`)
165-
util.fileUpdated(filePath)
157+
config.emit('rerun')
166158
})
167159
})
168160

@@ -174,10 +166,10 @@ const preprocessor = (config, userOptions = {}) => {
174166

175167
// when the spec or project is closed, we need to clean up the cached
176168
// bundle promise and stop the watcher via `bundler.close()`
177-
util.onClose(() => {
169+
config.on('close', () => {
178170
log(`close ${filePath}`)
179171
delete bundles[filePath]
180-
if (shouldWatch) {
172+
if (config.shouldWatch) {
181173
bundler.close()
182174
}
183175
})

0 commit comments

Comments
 (0)