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

Commit 6715584

Browse files
committed
include default options
1 parent 93a413a commit 6715584

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

cjsxify.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// wrapper for cjsxify to prevent coffee script from rewriting Error.prepareStackTrace
2+
3+
// hold onto the original
4+
const prepareStackTrace = Error.prepareStackTrace
5+
6+
const cjsxify = require('cjsxify')
7+
8+
// restore
9+
Error.prepareStackTrace = prepareStackTrace
10+
11+
module.exports = cjsxify

index.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,41 @@ const log = require('debug')('cypress:browserify')
1111

1212
const bundles = {}
1313

14+
const defaults = {
15+
extensions: ['.js', '.jsx', '.coffee', '.cjsx'],
16+
watchOptions: {
17+
ignoreWatch: [
18+
'**/.git/**',
19+
'**/.nyc_output/**',
20+
'**/.sass-cache/**',
21+
'**/bower_components/**',
22+
'**/coverage/**',
23+
'**/node_modules/**',
24+
],
25+
},
26+
transforms: [
27+
{
28+
transform: require.resolve('./cjsxify'),
29+
options: {},
30+
},
31+
{
32+
transform: require.resolve('babelify'),
33+
options: {
34+
ast: false,
35+
babelrc: false,
36+
plugins: ['babel-plugin-add-module-exports'].map(require.resolve),
37+
presets: ['babel-preset-env', 'babel-preset-react'].map(require.resolve),
38+
},
39+
},
40+
],
41+
}
42+
1443
module.exports = (config, userOptions = {}) => {
1544
log('received user options', userOptions)
1645

1746
return (filePath, util) => {
47+
log('get', filePath)
48+
1849
if (bundles[filePath]) {
1950
log(`already have bundle for ${filePath}`)
2051
return bundles[filePath]
@@ -28,22 +59,22 @@ module.exports = (config, userOptions = {}) => {
2859

2960
const bundler = browserify({
3061
entries: [filePath],
31-
extensions: userOptions.extensions || ['.js'],
62+
extensions: options.extensions,
3263
cache: {},
3364
packageCache: {},
3465
})
3566

3667
if (shouldWatch) {
3768
log('watching')
38-
bundler.plugin(watchify, userOptions.watchifyOptions || {})
69+
bundler.plugin(watchify, options.watchOptions || {})
3970
}
4071

41-
const onBundle = userOptions.onBundle
72+
const onBundle = options.onBundle
4273
if (typeof onBundle === 'function') {
4374
onBundle(bundler)
4475
}
4576

46-
const transforms = userOptions.transforms
77+
const transforms = options.transforms
4778
if (Object.prototype.toString.call(transforms) === '[object Array]') {
4879
transforms.forEach((transform) => {
4980
bundler.transform(transform.transform, transform.options)

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@
6969
"sinon-chai": "2.13.0"
7070
},
7171
"dependencies": {
72+
"babel-plugin-add-module-exports": "^0.2.1",
73+
"babel-preset-env": "^1.6.0",
74+
"babel-preset-react": "^6.24.1",
75+
"babelify": "^7.3.0",
7276
"bluebird": "3.5.0",
7377
"browserify": "14.4.0",
78+
"cjsxify": "^0.3.0",
7479
"debug": "3.0.1",
7580
"fs-extra": "4.0.1",
7681
"watchify": "3.9.0"

test/index_spec.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ describe('browserify preprocessor', function () {
4545

4646
sandbox.stub(fs, 'ensureDirAsync').resolves()
4747

48-
this.config = {}
48+
this.config = {
49+
isTextTerminal: true,
50+
}
4951
this.userOptions = {}
5052
this.filePath = 'path/to/file.js'
5153
this.outputPath = 'output/output.js'
@@ -62,7 +64,7 @@ describe('browserify preprocessor', function () {
6264

6365
describe('exported function', function () {
6466
it('receives user options and returns a preprocessor function', function () {
65-
expect(preprocessor(this.userOptions)).to.be.a('function')
67+
expect(preprocessor(this.config, this.userOptions)).to.be.a('function')
6668
})
6769
})
6870

@@ -86,7 +88,7 @@ describe('browserify preprocessor', function () {
8688
browserify.reset()
8789
browserify.returns(this.bundlerApi)
8890

89-
const run = preprocessor(this.userOptions)
91+
const run = preprocessor(this.config, this.userOptions)
9092
return run(this.filePath, this.util)
9193
.then(() => {
9294
return run(this.filePath, this.util)
@@ -104,7 +106,7 @@ describe('browserify preprocessor', function () {
104106

105107
it('specifies default extensions if none provided', function () {
106108
return this.run().then(() => {
107-
expect(browserify.lastCall.args[0].extensions).to.eql(['.js'])
109+
expect(browserify.lastCall.args[0].extensions).to.eql(['.js', '.jsx', '.coffee', '.cjsx'])
108110
})
109111
})
110112

@@ -122,9 +124,25 @@ describe('browserify preprocessor', function () {
122124
})
123125
})
124126

125-
it('includes watchifyOptions if provided', function () {
127+
it('use default watchOptions if not provided', function () {
128+
this.config.isTextTerminal = false
129+
return this.run().then(() => {
130+
expect(this.bundlerApi.plugin).to.be.calledWith(watchify, {
131+
ignoreWatch: [
132+
'**/.git/**',
133+
'**/.nyc_output/**',
134+
'**/.sass-cache/**',
135+
'**/bower_components/**',
136+
'**/coverage/**',
137+
'**/node_modules/**',
138+
],
139+
})
140+
})
141+
})
142+
143+
it('includes watchOptions if provided', function () {
126144
this.config.isTextTerminal = false
127-
this.userOptions.watchifyOptions = { ignoreWatch: ['node_modules'] }
145+
this.userOptions.watchOptions = { ignoreWatch: ['node_modules'] }
128146
return this.run().then(() => {
129147
expect(this.bundlerApi.plugin).to.be.calledWith(watchify, {
130148
ignoreWatch: ['node_modules'],
@@ -133,7 +151,6 @@ describe('browserify preprocessor', function () {
133151
})
134152

135153
it('does not watch when isTextTerminal is true', function () {
136-
this.config.isTextTerminal = true
137154
return this.run().then(() => {
138155
expect(this.bundlerApi.plugin).not.to.be.called
139156
})
@@ -210,7 +227,6 @@ describe('browserify preprocessor', function () {
210227
})
211228

212229
it('does not close bundler when isTextTerminal is true and onClose callback is called', function () {
213-
this.config.isTextTerminal = true
214230
return this.run().then(() => {
215231
this.util.onClose.lastCall.args[0]()
216232
expect(this.bundlerApi.close).not.to.be.called

0 commit comments

Comments
 (0)