Giter VIP home page Giter VIP logo

html-includes's Introduction

HTML Includes

Commandline HTML compilation with partial includes. Useful for super quick templating, or perhaps bundling things into an iframe.

"Aren't there a million of these?"

Yes. However, they all miss at least one of the following requirements:

  • No webpack necessary
  • Glob folder support
  • Watch
  • Relative paths
  • Nested includes
  • Filetypes other than .html, e.g. .js
  • Minification
  • Saving compiled files with ignoreable filenames
  • Passing parameters to includes, like props to a component

Install

npm i --save-dev html-includes

Add the script into your package.json along the lines of:

"scripts": {
  "compile": "html-includes --src src --dest dist",
  "compile:min": "html-includes --src src --dest dist --minify"
},

See options below for more options.

Run

npm run compile

Use

There is an example project here with more features but put simply, here is how you'd use the script parameters above:

src/index.html

<html>
    <head>
        ${require('./_meta.html')}
        <script>
            ${require('./_script.js')}
        </script>
    </head>
    <body>
        <main>
            ${require('./_main.html') foo=`and you can also pass props`}
        </main>
    </body>
</html>

src/_meta.html

<meta meta="meta" />

src/_script.js

console.log("Hello World!");

src/_main.html

<p>Main content ${props.foo}</p>

Result

In /dist you'd have simply index.html, containing:

<html>
    <head>
        <meta meta="meta" />
        <script>
            console.log("Hello World!");
        </script>
    </head>
    <body>
        <main>
            <p>Main content and you can also pass props</p>
        </main>
    </body>
</html>

Or with the --minify flag, you'd get:

<html>
    <head>
        <meta meta="meta" />
    </head>
    <body>
        <main><p>Main content</p></main>
    </body>
</html>

Options

Flag Description Default
--watch Watch for file changes false
--src Source dir "src"
--dest Compiled output dir "dist"
--minify Enable Minification of HTML false
--minify option=[boolean] Set any of the boolean options in https://github.com/kangax/html-minifier#options-quick-reference - e.g --minify conservativeCollapse=true Various typical values
--quiet Silence successful save logs false

Notes

  • Filenames starting with an _ underscore will not be saved into destination (in the style of partial files in Sass).

html-includes's People

Contributors

amodelbello avatar chopfitzroy avatar dependabot-preview[bot] avatar entozoon avatar mack-ludwin avatar nickolasjadams avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

html-includes's Issues

Specify require paths relative to src directory

With the 4.0.0 update, we can have files in subdirectories of src that are rendered in equivalent subdirectories of dist. That's fantastic, but one downside is that now the location of included files will vary according to the relative location of the source file.

Currently, as far as I can tell, all file paths in require statements are concatenated to the location of the current file. Absolute paths, therefore, break. It'd be great if absolute paths were taken as relative to the src folder (or maybe the project root). For example, if my project directory is ~/Code/myproject and the file being rendered is in src/red/blue.html:

Specified path Resolved path OK? Desired path resolution
_header.html ~/Code/myproject/src/red/_header.html βœ” ~/Code/myproject/src/red/_header.html
./_header.html ~/Code/myproject/src/red/./_header.html βœ” ~/Code/myproject/src/red/./_header.html
/_header.html ~/Code/myproject/src/red//_header.html ❌ ~/Code/myproject/src/_header.html

How does that sound?

I always get: No such file or directory

Hi,

No matter what I try I'm always getting the error: No such file or directory.

My node version is v8.4.0
My npm version is 5.3.0

Any idea how to overcome this?

Thanks

--watch not working

When I try running
npm run compile --watch
the --watch flag seems to make no difference, it just runs once like without it...
is there any special setting necessary?
Thanks

index.html in rootdir not created

Whilst I do understand the idea of dist/-folders I prefer my --dist to be .
Files are created as wanted, only index.html is said to be written but is not created.
Sadly my coding knowledge is not good enough to look over, please see this only as a hint, not a request for fixing ;)

Can we remove the clear screen effect?

Thanks for this cool and useful tool!

Just a small request:
Would it be possible to remove the clear screen command from line 54 of compile.js?
It is a bit confusing because it hides the other log statements that came before (now I must scroll instead of seeing everything at once). Also, it messes up logs in my CI.

Or maybe the clear screen could be optional, for people who want to have it.

I appreciate your work - thank you!

Recreate src directory structure in dist

Hi there! I'm really enjoying this module, but I'm having a problem with 3.1.0 where all rendered files end up in the root of the dist directory regardless of how far down inside src they are. This causes files to overwrite each other. For example, in package.json:

"build": html-includes --src src --dest dist

And when I npm run build:

Reading: src/_footer.html
Reading: src/_head_imports.html
Reading: src/_navheader.html
Reading: src/index.html
Saving: src/index.html-> dist/index.html
Reading: src/learn/index.html
Saving: src/learn/index.html-> dist/index.html

Is it possible for html-imports to create the required subdirectories in dist as required (or even to just get the files to the right subdirectory if those subdirectories exist)? Thanks!

Cannot read property 'content' of undefined

Whenever I try to include a file in a parent dir I get a error message saying

/Users/diederik/sites/loc.motion-delta.nl/node_modules/html-includes/bin/compile.js:111
let filesContent = files.filter(f => f.id == filesId)[0].content;

TypeError: Cannot read property 'content' of undefined at file.content.file.content.replace.require (/Users/diederik/sites/loc.motion-delta.nl/node_modules/html-includes/bin/compile.js:111:69)

And my code would be something like this:

./src/html/contact/index.html trying to included ./src/html/includes/_html-head.html

${require('../includes/_html-head.html') pageTitle="Contact | My Project"}

Possibility of passing HTML tags in the props

Great tool!
Would it be possible to pass on simple HTML tags as properties as well? As an example, I have tried passing an anchor tag using props, but it looks like the build process gets broken as soon as it encounters symbols like = or ".

${require('./_main.html') foo="<a href='http://example.com'>Example</a>"}

Removing the Include tags

Hi Michael!
This is fantastic!! But is there any way to remove the holding include tags from the outputs?

For example;
scr = <div include="/common/_footer.html"></div>
dist = <footer> This is a footer </footer>

The current version is rendering <div > tags which aren't required. Any chance you can add the option to remove these please?

Thanks!

Feature Request: Adapt to allow HTML comment includes or attribute includes

In order to even increase helpfulness of this plugin, additionally allow html-comment includes.

Problem:
When developing e.g. with IntelliJ's live edit, you don't want another watch process.
Currently, we use JS-includes in the development environment for header, footer etc.
We want to use your solution to insert includes for production in order to not require client-side scripting.
Currently, in dev env, we have ${require('./inc/meta.html') artifacts.

Possible Solution:
Allow requiring via comments, e.g. <!--${require('./inc/meta.html')}-->: Both solutions would work like this.

Possible Solution 2:
Preferably allow requiring via attributes, e.g. <div html-include=".inc/header.html"></div>. This would allow <div w3-include-html="./inc/header.html" html-include="./inc/header.html"></div> and would work for both JS-includes (dev env) and html-includes (compilation of prod build).
If you allow to variably pick html attribute name via cmd line option, even better. E.g. having includes like <div w3-include-html="./inc/header.html"></div> and invocation html-includes --src . --dest dist --attr-includes "w3-include-html".

Thanks for your work, if I find the time I might do some hands-on myself.

Replace ',' with '/' needed to make it work

Thanks for a great useful package.

I apologize if this is user error but the only way I was able to make this work was by adding this line into
node_modules/bin/compile.js

line 98, before console.log(filePath)

filePath = filePath.replace(',','/')

Here was my scenario. I'm using the package to add common '_header.html' and '_footer.html' to a file called 'dashboard.html'

I have 2 directories:
user_interface/src/UserGuideFiles containing the 3 files listed above
user_interface/src/UserGuideDest for the output

in my package.json, in my 'scripts' object

"compile": "html-includes --src src/UserGuideFiles --dest src/UserGuideDist"

Before the fix, I get this error:

Error: ENOENT: no such file or directory, open 'c:(full path to project)\user_interface\src,UserGuideFiles_header.html'

after the fix the ',' is replaced with '/' and it works and the output is:

Reading: src/UserGuideFiles/_footer.html
Reading: src/UserGuideFiles/_header.html
Reading: src/UserGuideFiles/dashboard.html
src/UserGuideFiles/./_header.html
src/UserGuideFiles/./_footer.html
Saving: src/UserGuideFiles/dashboard.html-> src/UserGuideDist/dashboard.html

And the full file is written as expected

Thanks

Support other file extensions

It would be cool if the tool also supported other file extensions (for including svg, js, ...). Ideally, with an option that could specify the list of file extensions to support (and fallback to .html when the option is missing).

Other directories not copied into dist

My folder structure is like this

src
β”œβ”€β”€ css
β”‚Β Β  └── main.css
β”œβ”€β”€ _footer.html
β”œβ”€β”€ _footer_menu.html
β”œβ”€β”€ images
β”‚Β Β  └── logo.png
β”œβ”€β”€ index.html
β”œβ”€β”€ js
β”‚Β Β  └── main.js
β”œβ”€β”€ _menu.html
β”œβ”€β”€ _meta.html
└── _top_menu.html

In the dist folder only index.html files are copied. Other relative folders such as css, js and images are not copied.

Props not working

Hey Michael, I hope you’re doing well!

This plugin is very useful, thanks for sharing it. I'm having an issue now when trying to use the props. I've been using this plugin for a while this year. And a few weeks ago tried to use the props feature.

For me is not working. I'm using the same example that you show in the documentation, but the output is

Main content "The resulting value should be here"

without the prop value in the compiled file. What do you think is happening?

Feature Request: Option to turn off minification

The minification is doing some bad things to my code at the moment, but even if it wasn't, I might want to leave the HTML unminified anyway just for easier readability (especially if I'm using html-includes for prototyping). Might be nice to have a flag to turn it off.

Feature Request: Src file pattern

Problem:
Having a project with many non-relevant sub-folders that contain html files, running html-includes --src . --dest dist results in compiling a lot of non-relevant sub-folder files.

Possible Solution:
Allow a file pattern like ./*.* (following glob pattern).

Include-holding tag not being removed from output

Hi!

When I try to reproduce the examples given my output doesn't come out as:

<p>Hello</p>
<p>Footer stuff</p>

But instead as:

<p>Hello</p>
<div ><p>Footer stuff</p>
</div>

The input that went in was exactly as it was in your example:

<p>Hello</p>
<div include="_footer.html"></div>

I have used npm install --save-dev html-includes and npm install -g html-includes to install it and then ran html-includes --src source --dest build manually on the command line instead of using the package.json way. Could that change the result like this?

I'm using nodejs v9.2.1.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.