Skip to content

Commit 33c7c8f

Browse files
committedOct 21, 2022
docs: demo for using LiquidJS with webpack
1 parent 32f613f commit 33c7c8f

File tree

10 files changed

+90
-2
lines changed

10 files changed

+90
-2
lines changed
 

‎demo/esm/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LiquidJS for Node.js
1+
# Using LiquidJS in ESM
22

33
## Get Started
44

‎demo/express/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LiquidJS for Express.js
1+
# Using LiquidJS with Express.js
22

33
## Get Started
44

‎demo/webpack/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Using LiquidJS with Webpack
2+
3+
## Get Started
4+
5+
```bash
6+
cd demo/nodejs
7+
npm install
8+
npm run build
9+
npm start
10+
```

‎demo/webpack/layouts/html.liquid

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<head><meta name="title" content="{{title}}"></head>
3+
<body>
4+
{% block %}{% endblock %}
5+
{% render "footer.liquid" %}
6+
</body>
7+
</html>

‎demo/webpack/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "liquidjs-demo-nodejs",
3+
"description": "Node.js demo for liquidjs",
4+
"private": true,
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"start": "node dist/index.js",
8+
"build": "webpack"
9+
},
10+
"dependencies": {
11+
"hello-liquid": "^1.0.0",
12+
"liquidjs": "^9.28.3"
13+
},
14+
"devDependencies": {
15+
"webpack": "^5.74.0",
16+
"webpack-cli": "^4.10.0"
17+
}
18+
}

‎demo/webpack/partials/footer.liquid

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<small>{{title}}</small>

‎demo/webpack/partials/todo.liquid

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<li>{{index}} - {{todo}}</li>

‎demo/webpack/src/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Liquid } from 'liquidjs'
2+
3+
const engine = new Liquid({
4+
extname: '.liquid',
5+
globals: { title: 'LiquidJS Demo' },
6+
// root files for `.render()` and `.parse()`
7+
root: process.cwd(),
8+
// layout files for `{% layout %}`
9+
layouts: process.cwd() + '/layouts',
10+
// partial files for `{% include %}` and `{% render %}`
11+
partials: process.cwd() + '/partials'
12+
})
13+
14+
const ctx = {
15+
todos: ['fork and clone', 'make it better', 'make a pull request']
16+
}
17+
18+
async function main () {
19+
console.log('==========renderFile===========')
20+
const html = await engine.renderFile('todolist', ctx)
21+
console.log(html)
22+
23+
console.log('===========Streamed===========')
24+
const tpls = await engine.parseFile('todolist')
25+
engine.renderToNodeStream(tpls, ctx)
26+
.on('data', data => process.stdout.write(data))
27+
.on('end', () => console.log(''))
28+
}
29+
30+
main()

‎demo/webpack/todolist.liquid

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{% layout 'html.liquid' %}
2+
<ul>
3+
{% for todo in todos %}
4+
{% render 'todo.liquid' with todo, index: forloop.index%}
5+
{% endfor %}
6+
</ul>

‎demo/webpack/webpack.config.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require('path');
2+
const { ContextReplacementPlugin } = require('webpack')
3+
4+
module.exports = {
5+
entry: './src/index.js',
6+
mode: 'development',
7+
target: 'node',
8+
plugins: [
9+
new ContextReplacementPlugin(/liquidjs/)
10+
],
11+
output: {
12+
filename: 'index.js',
13+
path: path.resolve(__dirname, 'dist'),
14+
},
15+
};

0 commit comments

Comments
 (0)
Please sign in to comment.