@@ -12,7 +12,7 @@ const shouldIgnoreFile = file =>
1212 . match ( / n o d e _ m o d u l e s \/ ( r e a c t | r e a c t - h o t - l o a d e r ) ( [ \/ ] | $ ) / )
1313/* eslint-enable */
1414
15- module . exports = function plugin ( args ) {
15+ module . exports = function plugin ( args , options = { } ) {
1616 // This is a Babel plugin, but the user put it in the Webpack config.
1717 if ( this && this . callback ) {
1818 throw new Error (
@@ -27,34 +27,42 @@ module.exports = function plugin(args) {
2727 }
2828 const { types : t , template } = args
2929
30+ const { safetyNet = true } = options
31+
3032 const buildRegistration = template (
3133 'reactHotLoader.register(ID, NAME, FILENAME);' ,
3234 templateOptions ,
3335 )
3436 const headerTemplate = template (
3537 `(function () {
36- var enterModule = require('react-hot-loader').enterModule;
38+ var enterModule = (typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal : require('react-hot-loader') ).enterModule;
3739 enterModule && enterModule(module);
3840 }())` ,
3941 templateOptions ,
4042 )
43+ const footerTemplate = template (
44+ `(function () {
45+ var leaveModule = (typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal : require('react-hot-loader')).leaveModule;
46+ leaveModule(module);
47+ }())` ,
48+ templateOptions ,
49+ )
4150 const evalTemplate = template ( 'this[key]=eval(code);' , templateOptions )
4251
4352 // We're making the IIFE we insert at the end of the file an unused variable
4453 // because it otherwise breaks the output of the babel-node REPL (#359).
45- const buildTagger = template (
46- `
47- (function () {
48- var reactHotLoader = require('react-hot-loader').default;
49- var leaveModule = require('react-hot-loader').leaveModule;
5054
55+ const buildTagger = template (
56+ `
57+ (function () {
58+
59+ var reactHotLoader = (typeof reactHotLoaderGlobal !== 'undefined' ?reactHotLoaderGlobal : require('react-hot-loader')).default;
60+
5161 if (!reactHotLoader) {
5262 return;
5363 }
5464
55- REGISTRATIONS
56-
57- leaveModule(module);
65+ REGISTRATIONS
5866}());
5967 ` ,
6068 templateOptions ,
@@ -151,12 +159,18 @@ module.exports = function plugin(args) {
151159 registrations . length &&
152160 ! shouldIgnoreFile ( file . opts . filename )
153161 ) {
154- node . body . unshift ( headerTemplate ( ) )
162+ if ( safetyNet ) {
163+ node . body . unshift ( headerTemplate ( ) )
164+ }
155165 // Inject the generated tagging code at the very end
156166 // so that it is as minimally intrusive as possible.
157167 node . body . push ( t . emptyStatement ( ) )
158168 node . body . push ( buildTagger ( { REGISTRATIONS : registrations } ) )
159169 node . body . push ( t . emptyStatement ( ) )
170+
171+ if ( safetyNet ) {
172+ node . body . push ( footerTemplate ( ) )
173+ }
160174 }
161175 } ,
162176 } ,
0 commit comments