Giter VIP home page Giter VIP logo

gulp-html-to-js's Introduction

Overview

A gulp plugin that converts arbitrary text files into JavaScript modules. Not limited to HTML.

Installation

npm i -E gulp-html-to-js
# or
yarn add -E gulp-html-to-js

Usage

In your gulpfile.js:

const htmlToJs = require('gulp-html-to-js')

// Without concatenation
gulp.task('html:compile', () => (
  gulp.src('src/html/**/*')
    .pipe(htmlToJs())
    .pipe(gulp.dest('dist'))
))

// With concatenation
gulp.task('html:compile', () => (
  gulp.src('src/html/**/*')
    .pipe(htmlToJs({concat: 'html.js'}))
    .pipe(gulp.dest('dist'))
))

Without the concat option, each module exports a single string:

<p>Hello world!</p>

Becomes:

module.exports = '<p>Hello world!</p>'

With concat, files are grouped into one module, where strings are keyed by file paths:

module.exports = Object.create(null)
module.exports['index.html'] = '<p>Hello world!</p>'

In your app, import the result like so (directory nesting depends on your build configuration):

import html from './html.js'
// or
const html = require('./html.js')

Options

See the concat option above. You can also modify it with:

  • prefix: Prepends a path prefix to all keys of the resulting module object.

For {prefix: 'templates'} the resulting file from the above example is:

module.exports = Object.create(null)
module.exports['templates/index.html'] = '<p>Hello world!</p>'
  • global: Requires concat. Assigns the resulting object to some global identifier other than module.exports (default).

For {global: 'window.templates', concat: 'templates.js'} the example above would produce this:

window.templates = Object.create(null)
window.templates['index.html'] = '<p>Hello world!</p>'

gulp-html-to-js's People

Contributors

kant2002 avatar mitranim avatar vkareh avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gulp-html-to-js's Issues

Error: Received a non-Vinyl object in `dest()`

Tried to execute a task from the readme but getting this error:

Error: Received a non-Vinyl object in dest()

Task:

gulp.task('views:compile', function() {
return gulp.src('chrome/html/**/*') <- this is my htmls path
.pipe(htmlToJs())
.pipe(gulp.dest('dist'));
});

package.json:

"devDependencies": {
"gulp": "^4.0.0",
"gulp-html-to-js": "0.0.5",
},

Any ideas on how to overcome that are much appreciated

Thanks!

Support for CRLF

Hi, would be great to have full support for CRLF and CR line endings.

htmlToJs({global: 'window.templates'} ) doesn't work for me.

Hello!
I'm running gulp task such as:
var outputdir = "./Debug"; var gulp = require('gulp'); var htmlToJs = require('gulp-html-to-js'); gulp.task('inject', function() { return gulp.src('./FieldValueEditor.html') .pipe(htmlToJs({global: 'window.templates'} )) .pipe(gulp.dest(outputdir)); });

Unfortunately the resulting file still contains:
'use strict';module.exports = '<div>Hello Wordl!</div>\n<div></div>';

Breaks with latest versions of node.js

This is a wonderful plug-in and worked for me in the previous versions of node.js But now with the latest versions of node.js breaks the pipe with an error on path:

Path must be a string

Have you tried the plug-in with versions from 8.12.0 onwards?

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.