This repository has been archived by the owner on Apr 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8032909
Showing
14 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* text=auto | ||
*.js eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ] } | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
}; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |