Git Product home page Git Product logo

gulp-source-injector's Introduction

gulp-source-injector

Injects any source into any file.

npm JavaScript Style Guide Travis (.com) David License

Table of Contents

Description

gulp-source-injector transforms content of each source file to a string and injects each transformed string into placeholders in the target stream files.

This plugin does not do any minification to source files, so whitespaces will be preserved. It's better to use it after transformations like gulp-terser or gulp-clean-css.

Install

npm install --save-dev gulp-source-injector

Usage

Injection placeholders are comments as html syntax <!-- inject: filePath --> and css/js syntax /* inject: filePath */

By default the injected file path is relative to each target file's cwd. If the provided path starts with /, it will be considered relative to the directory of gulpfile.js

Example

Project structure

├── src
│   ├── css
│   │   └── style.css
│   ├── js
│   │   └── script.js
│   ├── template
│   │   └── head.html
│   └── index.html
└── gulpfile.js

Target file src/index.html

<html>
  <head>
    <!-- inject: /src/template/head.html -->
    <style>
      /* inject: ./css/style.css */
    </style>
    <script>
      /*inject:js/script.js*/
    </script>
  </head>
  <body>
    <h1>Lorem Ipsum</h1>
  </body>
</html>

gulpfile.js

const { task, src, dest } = require('gulp')
const inject = require('gulp-source-injector')

task('inject', () => {
  return src('src/index.html')
    .pipe(inject())
    .pipe(dest('dist'))
})

or you can

import inject from 'gulp-source-injector'

and then

dist/index.html after running gulp inject

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <meta title="test">
    <style>
      body {
        background-color: #333;
      }
      h1 {
        color: #EEE;
      }
    </style>
    <script>
      console.log('foobar')
    </script>
  </head>
  <body>
    <h1>Lorem Ipsum</h1>
  </body>
</html>

Indentation

Note that existing indentation won't be preserved.

Target file src/index.html

<html>
  <head>
    <style>
      /* inject: ./css/style.css */
    </style>
  </head>
  <body>
    <h1>Lorem Ipsum</h1>
  </body>
</html>

Source file src/css/style.css

body {
  background-color: #333;
}
h1 {
  color: #EEE;
}

dist/index.html

<html>
  <head>
    <style>
      body {
  background-color: #333;
}
h1 {
  color: #EEE;
}
    </style>
  </head>
  <body>
    <h1>Lorem Ipsum</h1>
  </body>
</html>

License

MIT

Donate

Buy Me A Coffee

gulp-source-injector's People

Contributors

dependabot[bot] avatar exuanbo avatar renovate-bot avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

cologler

gulp-source-injector's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm eslint-plugin-node Available
npm eslint-plugin-standard Unavailable

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): replace dependency eslint-plugin-node with eslint-plugin-n 14.0.0
  • fix(deps): update dependency through2 to v4.0.2
  • chore(deps): update dependency eslint to v7.32.0
  • chore(deps): update dependency @rollup/plugin-commonjs to v26
  • chore(deps): update dependency chai to v5
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency eslint-config-prettier to v9
  • chore(deps): update dependency eslint-config-standard to v17
  • chore(deps): update dependency eslint-plugin-prettier to v5
  • chore(deps): update dependency eslint-plugin-promise to v7
  • chore(deps): update dependency eslint-plugin-standard to v5
  • chore(deps): update dependency mocha to v10
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency prettier-config-standard to v7
  • chore(deps): update dependency rollup to v4
  • chore(deps): update dependency vinyl to v3
  • fix(deps): update dependency plugin-error to v2
  • 🔐 Create all rate-limited PRs at once 🔐

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • plugin-error ^1.0.1
  • through2 ^4.0.0
  • @rollup/plugin-commonjs 16.0.0
  • chai 4.2.0
  • eslint 7.18.0
  • eslint-config-prettier 6.15.0
  • eslint-config-prettier-standard 3.0.1
  • eslint-config-standard 16.0.2
  • eslint-plugin-import 2.22.1
  • eslint-plugin-node 11.1.0
  • eslint-plugin-prettier 3.3.1
  • eslint-plugin-promise 4.2.1
  • eslint-plugin-standard 4.1.0
  • mocha 8.2.1
  • prettier 2.2.1
  • prettier-config-standard 1.0.1
  • rollup 2.37.1
  • vinyl 2.2.1
travis
.travis.yml
  • node 10

  • Check this box to trigger a request for Renovate to run again on this repository

sample code won't run

I got the follow error when running your example:

[13:37:40] Starting 'inject'...
[13:37:40] The following tasks did not complete: inject
[13:37:40] Did you forget to signal async completion?
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] gulp: `gulp "inject"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] gulp script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\chuan\AppData\Roaming\npm-cache\_logs\2020-05-16T20_37_40_738Z-debug.log

Add a return statement fixed the error for me.

const injectInline = require('@exuanbo/gulp-inject-inline')

gulp.task('inject', () => {    
  return gulp.src('src/index.html') // <=== the "return" statement fixes the errpr
    .pipe(injectInline())
    .pipe(gulp.dest("dist"))
})

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

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.