Skip to content

Commit e90a25c

Browse files
committed
fix: RHL babel plugin will ignore react and react-hot-loader, fixes #900
1 parent 2ec2521 commit e90a25c

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/babel.dev.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const templateOptions = {
44
placeholderPattern: /^([A-Z0-9]+)([A-Z0-9_]+)$/,
55
}
66

7+
/* eslint-disable */
8+
const shouldIgnoreFile = file =>
9+
!!file
10+
.split('\\')
11+
.join('/')
12+
.match(/node_modules\/(react|react-hot-loader)([\/]|$)/)
13+
/* eslint-enable */
14+
715
module.exports = function plugin(args) {
816
// This is a Babel plugin, but the user put it in the Webpack config.
917
if (this && this.callback) {
@@ -130,12 +138,16 @@ module.exports = function plugin(args) {
130138
/* eslint-enable */
131139
},
132140

133-
exit({ node }) {
141+
exit({ node }, { file }) {
134142
const registrations = node[REGISTRATIONS]
135143
node[REGISTRATIONS] = null // eslint-disable-line no-param-reassign
136144

137145
// inject the code only if applicable
138-
if (registrations && registrations.length) {
146+
if (
147+
registrations &&
148+
registrations.length &&
149+
!shouldIgnoreFile(file.opts.filename)
150+
) {
139151
node.body.unshift(headerTemplate())
140152
// Inject the generated tagging code at the very end
141153
// so that it is as minimally intrusive as possible.
@@ -191,3 +203,5 @@ module.exports = function plugin(args) {
191203
},
192204
}
193205
}
206+
207+
module.exports.shouldIgnoreFile = shouldIgnoreFile

test/babel.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import fs from 'fs'
33
import { transformFileSync } from 'babel-core'
44
/* eslint-disable import/no-unresolved, import/extensions */
55
import { getOptions, TARGETS } from '../testConfig/babel'
6+
import plugin from '../src/babel.dev'
67
/* eslint-enable import/no-unresolved, import/extensions */
78

89
const babelPlugin = path.resolve(__dirname, '../src/babel.dev')
@@ -61,4 +62,26 @@ describe('babel', () => {
6162
})
6263
})
6364
})
65+
66+
describe('babel helpers', () => {
67+
const { shouldIgnoreFile } = plugin
68+
it('should ignore react and hot-loader', () => {
69+
expect(shouldIgnoreFile('node_modules/react')).toBe(true)
70+
expect(shouldIgnoreFile('node_modules\\react')).toBe(true)
71+
expect(shouldIgnoreFile('node_modules/react/xyz')).toBe(true)
72+
73+
expect(shouldIgnoreFile('node_modules/react-hot-loader')).toBe(true)
74+
expect(shouldIgnoreFile('node_modules/react-hot-loader/xyz')).toBe(true)
75+
})
76+
77+
it('should pass all other files', () => {
78+
expect(shouldIgnoreFile('react')).toBe(false)
79+
expect(shouldIgnoreFile('node_modules/react-select')).toBe(false)
80+
expect(shouldIgnoreFile('node_modules\\react-select')).toBe(false)
81+
expect(shouldIgnoreFile('some_modules/react/xyz')).toBe(false)
82+
83+
expect(shouldIgnoreFile('node_modules/react-cold-loader')).toBe(false)
84+
expect(shouldIgnoreFile('react-hot-loader.js')).toBe(false)
85+
})
86+
})
6487
})

0 commit comments

Comments
 (0)