@@ -12,7 +12,7 @@ const shouldIgnoreFile = file =>
12
12
. 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 ) ( [ \/ ] | $ ) / )
13
13
/* eslint-enable */
14
14
15
- module . exports = function plugin ( args ) {
15
+ module . exports = function plugin ( args , options = { } ) {
16
16
// This is a Babel plugin, but the user put it in the Webpack config.
17
17
if ( this && this . callback ) {
18
18
throw new Error (
@@ -27,34 +27,42 @@ module.exports = function plugin(args) {
27
27
}
28
28
const { types : t , template } = args
29
29
30
+ const { safetyNet = true } = options
31
+
30
32
const buildRegistration = template (
31
33
'reactHotLoader.register(ID, NAME, FILENAME);' ,
32
34
templateOptions ,
33
35
)
34
36
const headerTemplate = template (
35
37
`(function () {
36
- var enterModule = require('react-hot-loader').enterModule;
38
+ var enterModule = (typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal : require('react-hot-loader') ).enterModule;
37
39
enterModule && enterModule(module);
38
40
}())` ,
39
41
templateOptions ,
40
42
)
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
+ )
41
50
const evalTemplate = template ( 'this[key]=eval(code);' , templateOptions )
42
51
43
52
// We're making the IIFE we insert at the end of the file an unused variable
44
53
// 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;
50
54
55
+ const buildTagger = template (
56
+ `
57
+ (function () {
58
+
59
+ var reactHotLoader = (typeof reactHotLoaderGlobal !== 'undefined' ?reactHotLoaderGlobal : require('react-hot-loader')).default;
60
+
51
61
if (!reactHotLoader) {
52
62
return;
53
63
}
54
64
55
- REGISTRATIONS
56
-
57
- leaveModule(module);
65
+ REGISTRATIONS
58
66
}());
59
67
` ,
60
68
templateOptions ,
@@ -151,12 +159,18 @@ module.exports = function plugin(args) {
151
159
registrations . length &&
152
160
! shouldIgnoreFile ( file . opts . filename )
153
161
) {
154
- node . body . unshift ( headerTemplate ( ) )
162
+ if ( safetyNet ) {
163
+ node . body . unshift ( headerTemplate ( ) )
164
+ }
155
165
// Inject the generated tagging code at the very end
156
166
// so that it is as minimally intrusive as possible.
157
167
node . body . push ( t . emptyStatement ( ) )
158
168
node . body . push ( buildTagger ( { REGISTRATIONS : registrations } ) )
159
169
node . body . push ( t . emptyStatement ( ) )
170
+
171
+ if ( safetyNet ) {
172
+ node . body . push ( footerTemplate ( ) )
173
+ }
160
174
}
161
175
} ,
162
176
} ,
0 commit comments