Skip to content

Commit

Permalink
fix: remove addDependency and only require RefreshModuleRuntime o… (#22)
Browse files Browse the repository at this point in the history
fix: remove addDependency and only require RefreshModuleRuntime once
  • Loading branch information
pmmmwh committed Dec 17, 2019
2 parents f35c731 + a4805a9 commit c00d407
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/loader.js
@@ -1,6 +1,10 @@
const path = require('path');
const { Template } = require('webpack');
const { refreshUtils } = require('./runtime/globals');
const RefreshModuleRuntime = require('./runtime/RefreshModuleRuntime');
const RefreshModuleRuntimeString = Template.getFunctionContent(RefreshModuleRuntime)
.trim()
.replace(/^ {2}/gm, '')
.replace(/\$RefreshUtils\$/g, refreshUtils);

/** A token to match code statements similar to a React import. */
const reactModule = /['"]react['"]/;
Expand All @@ -16,21 +20,11 @@ const reactModule = /['"]react['"]/;
* @returns {string} The injected module source code.
*/
function RefreshHotLoader(source, inputSourceMap) {
// Add dependency to allow caching and invalidations
this.addDependency(path.resolve('./runtime/RefreshModuleRuntime'));

// Use callback to allow source maps to pass through
this.callback(
null,
// Only apply transform if the source code contains a React import
reactModule.test(source)
? source +
'\n\n' +
Template.getFunctionContent(require('./runtime/RefreshModuleRuntime'))
.trim()
.replace(/^ {2}/gm, '')
.replace(/\$RefreshUtils\$/g, refreshUtils)
: source,
reactModule.test(source) ? source + '\n\n' + RefreshModuleRuntimeString : source,
inputSourceMap
);
}
Expand Down

0 comments on commit c00d407

Please sign in to comment.