Skip to content

Commit

Permalink
Merge pull request #1003 from gaearon/webpack4-babel-cycle
Browse files Browse the repository at this point in the history
fix: RHL babel plugin will ignore react and react-hot-loader, fixes #900
  • Loading branch information
theKashey committed Jun 5, 2018
2 parents c864343 + e90a25c commit 1be2966
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/babel.dev.js
Expand Up @@ -4,6 +4,14 @@ const templateOptions = {
placeholderPattern: /^([A-Z0-9]+)([A-Z0-9_]+)$/,
}

/* eslint-disable */
const shouldIgnoreFile = file =>
!!file
.split('\\')
.join('/')
.match(/node_modules\/(react|react-hot-loader)([\/]|$)/)
/* eslint-enable */

module.exports = function plugin(args) {
// This is a Babel plugin, but the user put it in the Webpack config.
if (this && this.callback) {
Expand Down Expand Up @@ -130,12 +138,16 @@ module.exports = function plugin(args) {
/* eslint-enable */
},

exit({ node }) {
exit({ node }, { file }) {
const registrations = node[REGISTRATIONS]
node[REGISTRATIONS] = null // eslint-disable-line no-param-reassign

// inject the code only if applicable
if (registrations && registrations.length) {
if (
registrations &&
registrations.length &&
!shouldIgnoreFile(file.opts.filename)
) {
node.body.unshift(headerTemplate())
// Inject the generated tagging code at the very end
// so that it is as minimally intrusive as possible.
Expand Down Expand Up @@ -191,3 +203,5 @@ module.exports = function plugin(args) {
},
}
}

module.exports.shouldIgnoreFile = shouldIgnoreFile
23 changes: 23 additions & 0 deletions test/babel.test.js
Expand Up @@ -3,6 +3,7 @@ import fs from 'fs'
import { transformFileSync } from 'babel-core'
/* eslint-disable import/no-unresolved, import/extensions */
import { getOptions, TARGETS } from '../testConfig/babel'
import plugin from '../src/babel.dev'
/* eslint-enable import/no-unresolved, import/extensions */

const babelPlugin = path.resolve(__dirname, '../src/babel.dev')
Expand Down Expand Up @@ -61,4 +62,26 @@ describe('babel', () => {
})
})
})

describe('babel helpers', () => {
const { shouldIgnoreFile } = plugin
it('should ignore react and hot-loader', () => {
expect(shouldIgnoreFile('node_modules/react')).toBe(true)
expect(shouldIgnoreFile('node_modules\\react')).toBe(true)
expect(shouldIgnoreFile('node_modules/react/xyz')).toBe(true)

expect(shouldIgnoreFile('node_modules/react-hot-loader')).toBe(true)
expect(shouldIgnoreFile('node_modules/react-hot-loader/xyz')).toBe(true)
})

it('should pass all other files', () => {
expect(shouldIgnoreFile('react')).toBe(false)
expect(shouldIgnoreFile('node_modules/react-select')).toBe(false)
expect(shouldIgnoreFile('node_modules\\react-select')).toBe(false)
expect(shouldIgnoreFile('some_modules/react/xyz')).toBe(false)

expect(shouldIgnoreFile('node_modules/react-cold-loader')).toBe(false)
expect(shouldIgnoreFile('react-hot-loader.js')).toBe(false)
})
})
})

0 comments on commit 1be2966

Please sign in to comment.