fixes #8 and hot reload

This commit is contained in:
Gianluca Guarini 2019-05-14 22:14:59 +02:00
parent 602a65c82f
commit 97a1479fef
7 changed files with 11 additions and 8 deletions

View File

@ -32,7 +32,7 @@ component(App)(document.querySelector('#root'), {
If you want compile your tags using custom riot compiler options you can create a `riot.config.js` in the root folder of your project If you want compile your tags using custom riot compiler options you can create a `riot.config.js` in the root folder of your project
```js ```js
export default { module.exports = {
hot: false // set it to true if you are using hmr hot: false // set it to true if you are using hmr
// add here all the other @riotjs/compiler options riot.js.org/compiler // add here all the other @riotjs/compiler options riot.js.org/compiler
// template: 'pug' for example // template: 'pug' for example

View File

@ -1,5 +1,6 @@
const { compile } = require('@riotjs/compiler') const { compile } = require('@riotjs/compiler')
const { Asset } = require('parcel-bundler') const { Asset } = require('parcel-bundler')
const { relative, dirname } = require('path')
/** /**
* Generate the hmr code depending on the tag generated by the compiler * Generate the hmr code depending on the tag generated by the compiler
@ -12,7 +13,7 @@ function hotReload(path) {
const hotReload = require('@riotjs/hot-reload').default const hotReload = require('@riotjs/hot-reload').default
module.hot.accept() module.hot.accept()
if (module.hot.data) { if (module.hot.data) {
const component = require('${path}').default; const component = require('./${path}').default;
hotReload(component) hotReload(component)
} }
} }
@ -36,7 +37,7 @@ class RiotAsset extends Asset {
{ {
sourceMap: this.options.sourceMaps ? map : false, sourceMap: this.options.sourceMaps ? map : false,
type: 'js', type: 'js',
value: `${code}${options.hot ? hotReload(this.relativeName) : ''}` value: `${code}${options.hot ? hotReload(relative(dirname(this.name), this.relativeName)) : ''}`
} }
] ]
} }

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@riotjs/parcel-plugin-riot", "name": "@riotjs/parcel-plugin-riot",
"version": "4.0.0-alpha.1", "version": "4.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -4,7 +4,6 @@
"description": "A parcel plugin for riot.js", "description": "A parcel plugin for riot.js",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"prepare": "npm i --no-save @riotjs/compiler parcel-bundler",
"test": "npx eslint *.js && npm run test-bundle", "test": "npx eslint *.js && npm run test-bundle",
"test-bundle": "cd test && npm i" "test-bundle": "cd test && npm i"
}, },
@ -15,7 +14,7 @@
}, },
"peerDependencies": { "peerDependencies": {
"parcel-bundler": "^1.12.3", "parcel-bundler": "^1.12.3",
"@riotjs/compiler": "^4.0.0" "@riotjs/compiler": "^4.1.1"
}, },
"devDependencies": { "devDependencies": {
"eslint": "^5.16.0", "eslint": "^5.16.0",

View File

@ -1,5 +1,3 @@
import MyComponent from './my-component.riot' import MyComponent from './my-component.riot'
console.log(MyComponent)
export default MyComponent export default MyComponent

View File

@ -10,6 +10,8 @@
}, },
"devDependencies": { "devDependencies": {
"@riotjs/compiler": "^4.0.0", "@riotjs/compiler": "^4.0.0",
"@riotjs/hot-reload": "^4.0.0-rc.1",
"riot": "^4.0.0-rc.13",
"@riotjs/parcel-plugin-riot": "file:../" "@riotjs/parcel-plugin-riot": "file:../"
} }
} }

3
test/riot.config.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
hot: true
}