-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61d78e1
commit 9f9457a
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# tag-loader | ||
|
||
Riot official webpack loader | ||
|
||
## Usage | ||
|
||
Add the riot-tag-loader in your `webpack.config.js` file | ||
```js | ||
module.exports = { | ||
module: { | ||
loaders: [ | ||
{ | ||
test: /\.tag$/, | ||
exclude: /node_modules/, | ||
loader: 'riot-tag-loader', | ||
query: { | ||
hot: false, // set it to true if you are using hmr | ||
debug: false // set it to true to enable sourcemaps debugging | ||
// add here all the other riot-compiler options riotjs.com/guide/compiler/ | ||
// template: 'pug' for example | ||
} | ||
} | ||
] | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const riot = require('riot') | ||
const loaderUtils = require('loader-utils') | ||
|
||
const TAGS_NAMES_REGEX = /riot.tag2\(['|"](.+?)['|"],/g | ||
|
||
/** | ||
* Generate the hmr code depending on the tags generated by the compiler | ||
* @param { Array } tags - array of strings | ||
* @returns { String } the code needed to handle the riot hot reload | ||
*/ | ||
function hotReload(tags) { | ||
return ` | ||
if (module.hot) { | ||
module.hot.accept() | ||
if (module.hot.data) { | ||
${ tags.map(tag => `riot.reload(${ tag })`).join('\n') } | ||
} | ||
}` | ||
} | ||
|
||
|
||
module.exports = function(source) { | ||
const query = loaderUtils.parseQuery(this.query) | ||
const code = riot.compile(source, query) | ||
const tags = [] | ||
const hotReloadCode = '' | ||
|
||
code.replace(TAGS_NAMES_REGEX, function(_, match) { | ||
tags.push(match) | ||
}) | ||
|
||
if (this.cacheable) this.cacheable() | ||
if (query.hot) hotReloadCode += hotReload(tags) | ||
|
||
return ` | ||
var riot = require('riot') | ||
${ code } | ||
${ hotReloadCode } | ||
` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "riot-tag-loader", | ||
"version": "0.0.0", | ||
"description": "Riot official webpack loader", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/riot/tag-loader.git" | ||
}, | ||
"keywords": [ | ||
"webpack", | ||
"riot", | ||
"loader" | ||
], | ||
"peerDependencies": { | ||
"riot": "^3.0.7" | ||
}, | ||
"author": "Gianluca Guarini <gianluca.guarini@gmail.com> (http://gianlucaguarini.com)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/riot/tag-loader/issues" | ||
}, | ||
"homepage": "https://github.com/riot/tag-loader#readme", | ||
"dependencies": { | ||
"loader-utils": "^0.2.16" | ||
} | ||
} |