Skip to content
This repository has been archived by the owner on Apr 21, 2020. It is now read-only.

Commit

Permalink
Bootstrap example.
Browse files Browse the repository at this point in the history
  • Loading branch information
Comandeer committed Dec 24, 2019
0 parents commit 8032909
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = tab
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto
*.js eol=lf
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules/
*.*~
*.db
.goutputstream-*
*.bac
/.project
coverage/
.nyc_output/
/dist
tests/fixtures/cli/**/output
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016-2019 Comandeer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# HTML import example

Example for the [blog post about importing HTML in Node.js](https://blog.comandeer.pl/html-w-node.html).

## Installation

```bash
npm install
```

## Usage

For `require.js` and `require-pirate.js`:

```bash
node <file-name>
```

For `esm.mjs`:

```bash
node --experimental-modules --experimental-loader ./hooks/html-loader.mjs esm.mjs
```

All hooks are located in `hooks` directory. Sample HTML template is located in `templates` directory.

## License

See [LICENSE](./LICENSE) file for details.
5 changes: 5 additions & 0 deletions esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import template from './templates/hello.html';

console.log( template.render( {
user: 'Comandeer'
} ) );
32 changes: 32 additions & 0 deletions hooks/html-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { URL, pathToFileURL, fileURLToPath } from 'url';
import { promises as fs } from 'fs';
import hogan from 'hogan.js';

const { readFile } = fs;
const baseURL = pathToFileURL( process.cwd() ).href;

export async function resolve( path, parentModuleURL = baseURL, defaultResolve ) {
if ( !path.endsWith( '.html' ) ) {
return defaultResolve( path, parentModuleURL );
}

const url = new URL( path, parentModuleURL ).href;

return {
url,
format: 'dynamic'
};
};

export async function dynamicInstantiate( url ) {
const path = fileURLToPath( url );
const html = await readFile( path, 'utf8' );
const template = hogan.compile( html );

return {
exports: [ 'default' ],
execute( exports ) {
exports.default.set( template );
}
};
}
16 changes: 16 additions & 0 deletions hooks/pirates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { readFileSync } = require( 'fs' );
const { addHook } = require( 'pirates' );

module.exports = function() {
addHook(
( code, path ) => {
const html = readFileSync( path, 'utf8' );

return `const hogan = require( 'hogan.js' );
const template = hogan.compile( \`${ html }\` );
module.exports = template;`;
},
{ exts: [ '.html' ] }
);
};
13 changes: 13 additions & 0 deletions hooks/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { readFileSync } = require( 'fs' );

module.exports = function() {
require.extensions[ '.html' ] = ( module, path ) => {
const html = readFileSync( path, 'utf8' );
const code = `const hogan = require( 'hogan.js' );
const template = hogan.compile( \`${ html }\` );
module.exports = template;`;

module._compile( code, path );
};
};
48 changes: 48 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "html-import-example",
"version": "0.0.0",
"private": true,
"description": "Example for blog post about require extensions.",
"main": "require.js",
"scripts": {},
"repository": {
"type": "git",
"url": "git+https://github.com/Comandeer/html-import-example.git"
},
"keywords": [
"require"
],
"author": "Comandeer",
"license": "MIT",
"bugs": {
"url": "https://github.com/Comandeer/html-import-example/issues"
},
"homepage": "https://github.com/Comandeer/html-import-example#readme",
"dependencies": {
"hogan.js": "^3.0.2",
"pirates": "^4.0.1"
}
}
7 changes: 7 additions & 0 deletions require-pirates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require( './hooks/pirates.js' )();

const template = require( './templates/hello.html' );

console.log( template.render( {
user: 'Comandeer'
} ) );
7 changes: 7 additions & 0 deletions require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require( './hooks/require.js' )();

const template = require( './templates/hello.html' );

console.log( template.render( {
user: 'Comandeer'
} ) );
4 changes: 4 additions & 0 deletions templates/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<article>
<h1>Hello, {{user}}!</h1>
<p>Welcome to our site.</p>
</article>

0 comments on commit 8032909

Please sign in to comment.