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

Commit 8206bf3

Browse files
silbinarywolfchrisbreiding
authored andcommitted
fix: make babel dependencies optional (fixes #38)
* fix default config to only require babel-loader and @babel/preset-env if there is no override * remove references to jsx * move babel deps to optional dependencies * fix formatting / add comments about optional babel deps * lazy-load default options so that babel deps don’t need to be installed
1 parent 9a5fca4 commit 8206bf3

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Object of webpack options. Just `require` in the options from your `webpack.conf
7171
module: {
7272
rules: [
7373
{
74-
test: /\.jsx?$/,
74+
test: /\.js?$/,
7575
exclude: [/node_modules/],
7676
use: [{
7777
loader: 'babel-loader',

index.js

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,31 @@ const createDeferred = require('./deferred')
77

88
const bundles = {}
99

10-
// by default, we transform JavaScript (up to anything at stage-4) and JSX
11-
const defaultOptions = {
12-
webpackOptions: {
13-
module: {
14-
rules: [
10+
// by default, we transform JavaScript supported by @babel/preset-env
11+
const defaultBabelLoaderRules = () => {
12+
return [
13+
{
14+
test: /\.js?$/,
15+
exclude: [/node_modules/],
16+
use: [
1517
{
16-
test: /\.jsx?$/,
17-
exclude: [/node_modules/],
18-
use: [
19-
{
20-
loader: require.resolve('babel-loader'),
21-
options: {
22-
presets: [require.resolve('@babel/preset-env')],
23-
},
24-
},
25-
],
18+
loader: require.resolve('babel-loader'),
19+
options: {
20+
presets: [require.resolve('@babel/preset-env')],
21+
},
2622
},
2723
],
2824
},
25+
]
26+
}
27+
28+
// we don't automatically load the rules, so that the babel dependencies are
29+
// not required if a user passes in their own configuration
30+
const defaultOptions = {
31+
webpackOptions: {
32+
module: {
33+
rules: [],
34+
},
2935
},
3036
watchOptions: {},
3137
}
@@ -63,6 +69,11 @@ const preprocessor = (options = {}) => {
6369

6470
// user can override the default options
6571
let webpackOptions = Object.assign({}, defaultOptions.webpackOptions, options.webpackOptions)
72+
// here is where we load the default rules if the user has not passed
73+
// in their own configuration
74+
if (webpackOptions.module.rules === defaultOptions.webpackOptions) {
75+
webpackOptions.module.rules = defaultBabelLoaderRules()
76+
}
6677
let watchOptions = Object.assign({}, defaultOptions.watchOptions, options.watchOptions)
6778

6879
// we're provided a default output path that lives alongside Cypress's
@@ -173,7 +184,15 @@ const preprocessor = (options = {}) => {
173184
}
174185
}
175186

176-
// provide a clone of the default options
177-
preprocessor.defaultOptions = cloneDeep(defaultOptions)
187+
// provide a clone of the default options, making sure to lazy-load
188+
// babel dependencies so that they aren't required unless the user
189+
// utilizes them
190+
Object.defineProperty(preprocessor, 'defaultOptions', {
191+
get () {
192+
const clonedDefaults = cloneDeep(defaultOptions)
193+
clonedDefaults.webpackOptions.module.rules = defaultBabelLoaderRules()
194+
return clonedDefaults
195+
},
196+
})
178197

179198
module.exports = preprocessor

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@
5959
"webpack": "^4.18.1"
6060
},
6161
"peerDependencies": {
62+
"webpack": "^4.18.1"
63+
},
64+
"optionalDependencies": {
6265
"@babel/core": "^7.0.1",
6366
"@babel/preset-env": "^7.0.0",
64-
"babel-loader": "^8.0.2",
65-
"webpack": "^4.18.1"
67+
"babel-loader": "^8.0.2"
6668
},
6769
"dependencies": {
6870
"bluebird": "3.5.0",

0 commit comments

Comments
 (0)