Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/0.7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha Goldhofer committed Nov 1, 2017
2 parents 3faa25f + 8af5db8 commit 3124b5f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
42 changes: 32 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,51 @@
# mithril-render-loader

> First shot at a [webpack-Loader](https://webpack.js.org/) to render a [mithril](https://mithril.js.org/) [component](https://mithril.js.org/components.html) to html
> [webpack-Loader](https://webpack.js.org/) to render a [mithril](https://mithril.js.org/) [component](https://mithril.js.org/components.html) to html
**install** `npm install mithril-render-loader`
**install** `npm install mithril-render-loader --save-dev`

**requirements** `node v6+`


## Usage
## Options

| option | type | default | description |
| ----------------- |:-----------------:| ----------|-------------------------------------------------------------- |
| model | Mixed | {} | required data for component. Pass as vnode.attrs |
| export | Boolean | false | export using `module.exports` or return a string (html-loader)|
| cacheable | Boolean | true | deactivate cache, forcing a complete rebuild each time |
| profile | Boolean | false | log render times to console |
| escapeAttributes | Function|Boolean | false | Escape HTML-Attributes. You may pass a function(value):value |
| escapeString | Function|Boolean | false | Escape HTML-TextNodes. You may pass a function(value):value |
| strict | Boolean | false | Render the html as xml/xhtml


## Usage Example

The `index.view.js`

```js
const m = require("mithril");
const View = {
view(vnode) {
const data = vnode.attrs.model;
return m("Hello User");
}
}
```

The webpack-config might look something along theese lines:

```js
{

entry: [
"./test/app/index.view.js"
],

entry: "./test/app/index.view.js",
resolve: {
modules: [".", "node_modules"]
},

output: {
path: path.join(__dirname, "build")
},

module: {
rules: [
{
Expand All @@ -48,7 +68,7 @@ The webpack-config might look something along theese lines:
minimize: false, // deactivate minimize. It destroys valid inline css syntax
interpolate: false,
minifyCSS: false, // bugged
root: __dirname,
root: __dirname
}
},
{
Expand All @@ -69,3 +89,5 @@ The webpack-config might look something along theese lines:
}
};
```

If the _html-loader_ is omitted and mithril-render-loader should export a string, add the option `export: true`
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function logTime(message, startTime, endTime) {

function mithrilRenderLoader(view) {
// prevents some(!) messed up states - the loader is currently fast enough enough
this.cacheable(false);

const timeStart = Date.now();
// the render-mithril operation is async
Expand All @@ -22,8 +21,9 @@ function mithrilRenderLoader(view) {
profile: false, // log build times
model: null, // data passed to component
"export": false, // use module.exports or return result as string (html-loader)
cacheable: true, // deactivate cache, forcing a rebuild each time
// mithril-render-node options @see https://github.com/MithrilJS/mithril-node-render#options
escapeAttributeValue: false, // either a boolean or a function (value) => value to parse attributes
escapeAttributes: false, // either a boolean or a function (value) => value to parse attributes
escapeString: true, // A filter function for string nodes
strict: false // true for xml/xhtml
}, this.query);
Expand All @@ -33,6 +33,10 @@ function mithrilRenderLoader(view) {
o.model = {};
}

if (o.cacheable === false) {
this.cacheable(false);
}

// pass a uid to mithril component
o.model.ID = `${this.resourcePath}${Date.now()}${Math.random()}`;
o.model.COMPONENT_ID = this.resourcePath;
Expand Down Expand Up @@ -73,10 +77,10 @@ function mithrilRenderLoader(view) {

// gather renderer options
const renderOptions = { strict: o.strict };
if (o.escapeAttributeValue === false) {
if (o.escapeAttributes === false) {
renderOptions.escapeAttributeValue = (value) => value;
} else if (typeof o.escapeAttributeValue === "function") {
renderOptions.escapeAttributeValue = o.escapeAttributeValue;
} else if (typeof o.escapeAttributes === "function") {
renderOptions.escapeAttributeValue = o.escapeAttributes;
}
if (o.escapeString === false) {
renderOptions.escapeString = (value) => value;
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mithril-render-loader",
"version": "0.6.0",
"version": "0.7.0",
"description": "",
"main": "index.js",
"scripts": {
Expand All @@ -15,6 +15,9 @@
"mithril": "^1.1.5",
"mithril-node-render": "^2.2.0"
},
"publishConfig": {
"registry": "http://registry.npmjs.org"
},
"devDependencies": {
"eslint": "^3.19.0",
"extract-loader": "^1.0.1",
Expand Down

0 comments on commit 3124b5f

Please sign in to comment.