Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Jan 18, 2017
1 parent 61d78e1 commit 9f9457a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
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
}
}
]
}
}
```
40 changes: 40 additions & 0 deletions index.js
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 }
`
}
30 changes: 30 additions & 0 deletions package.json
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"
}
}

0 comments on commit 9f9457a

Please sign in to comment.