Skip to content

Commit

Permalink
add onlyJS
Browse files Browse the repository at this point in the history
  • Loading branch information
JaylanChen committed Jul 17, 2018
1 parent 57d14f6 commit 86e67f9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Allowed values are as follows
|**[`jsRootDir`](#)**|`{String}`||The js relative root path relative project root path, like: client/js/|
|**[`templateRootDir`](#)**|`{String}`||The template relative root path relative project root path, like: client/view/|
|**[`nameFormat`](#)**|`{Function}`||Allows format template name base on the JS file name|
|**[`jsHotAccept`](#)**|`{Boolean}`|`true`|If `true` adds 'module.hot.accept()' to the bottom of modules allows js `hot module replace`|
|**[`jsHotAccept`](#)**|`{Boolean}`|`true`|If `true` add `module.hot.accept()` to the bottom of modules allows js `hot module replace`|
|**[`onlyJS`](#)**|`{Boolean}`|`false`|If `true` just add `module.hot.accept()` to the bottom of modules allows js `hot module replace`, and will not support template reload|

## Why use `jsRootDir`, `templateRootDir`

Expand Down
24 changes: 20 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ module.exports = function (source, map) {
return source;
}

// inject `module.hot.accept()` support js hmr
let jsHotAccept = options.jsHotAccept || true;
let js_hmr = jsHotAccept ? 'module.hot.accept();' + "\n" : '';

if (jsHotAccept) {
// just inject js `module.hot.accept()` support js hmr
let onlyJS = options.onlyJS;
if (onlyJS === "undefined") {
onlyJS = false;
}
if (onlyJS) {
return '' +
source + "\n" +
"\n" +
'if (module.hot) {' +
js_hmr +
'}' + "\n";
}
}

// The js relative root path relative project root path, like: client/js/
let jsRootDir = options.jsRootDir;

Expand Down Expand Up @@ -62,10 +82,6 @@ module.exports = function (source, map) {
// The template file name relative the js, like: ../../../client/view/home/index.hbs
let relativeTemplateFileName = jsRelativeRoot + templateFileName;

// inject `module.hot.accept()` support js hmr
let jsHotAccept = options.jsHotAccept || true;
var js_hmr = jsHotAccept ? 'module.hot.accept();' + "\n" : '';

// Determines whether the template file exists, if not, will not modify.
if (!fs.existsSync(path.resolve(projectRoot, templateFileName))) {
return source;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-template-reload",
"version": "1.0.7",
"version": "1.0.8",
"description": "Tiny webpack plugin that make express template(like handlebars) support reload page when changed.",
"main": "index.js",
"scripts": {
Expand All @@ -16,7 +16,8 @@
"express",
"handlebars",
"template",
"reload"
"reload",
"module hot accept"
],
"author": "JaylanChen",
"license": "MIT",
Expand Down

0 comments on commit 86e67f9

Please sign in to comment.