Giter Site home page Giter Site logo

gulp-insert's Introduction

gulp-insert

String manipulation library for gulp

Usage

npm install gulp-insert
var insert = require('gulp-insert');

Append

Appends a string onto the contents.

.pipe(insert.append('world')); // Appends 'world' to the contents of every file

Prepend

Prepends a string onto the contents.

.pipe(insert.prepend('Hello')); // Prepends 'Hello' to the contents of every file

Wrap

Wraps the contents with two strings.

.pipe(insert.wrap('Hello', 'World')); // prepends 'hello' and appends 'world' to the contents

Transform

Calls a function with the contents of the file.

.pipe(insert.transform(function(contents, file) {
  return contents.toUpperCase();
}));

Transform has access to the underlying vinyl file. The following code adds a '//' comment with the full file name before the actual content.

.pipe(insert.transform(function(contents, file) {

	var comment = '// local file: ' + file.path + '\n';
	return comment + contents;
}));

See https://github.com/wearefractal/vinyl for docmentation on the 'file' parameter.

gulp-insert's People

Contributors

rschmukler avatar nfroidure avatar bschuedzig avatar pdehaan avatar yuri-karadzhov avatar fridays avatar

Stargazers

Roman avatar Jacky Wong avatar Chen Gen avatar  avatar jump-and-jump avatar Franky Pixels avatar Hejx avatar anruo avatar Marcel avatar  avatar Philipp Simon Schmidt avatar Vladimir Ivanin avatar Sergey M. avatar hamlet avatar Or avatar Ha Link avatar Andrew Faulkner avatar Dominique Müller avatar N37R09U3 avatar  avatar Jonny Scholes avatar pragmatic_jiang avatar Vinicius Dacal avatar M.Sakamaki avatar Kirill Groshkov avatar 笑笑十年 avatar tsuyoshi wada avatar Dmitry Maganov avatar elodie avatar Toomas Tahves avatar Yurii Sorokin avatar t32k avatar berstend̡̲̫̹̠̖͚͓̔̄̓̐̄͛̀͘ avatar Özgür Adem IŞIKLI avatar Rachelle Gray avatar Ruslan avatar Grigorii Chudnov avatar Nicolas Gryman avatar Tom M avatar  avatar scott avatar  avatar Joe avatar Matthew avatar LEI avatar Boris Serdiuk avatar Oleg Solomko avatar Gio d'Amelio avatar BJR Matos avatar Yuichi Murata avatar Taro Hanamura avatar Denis Luchkin-Zhou avatar Gil Barbara avatar Mykyta Shulipa avatar Theodoor van Donge avatar Colin Mutter avatar Makeev Vitaliy avatar Willy Tseng avatar Greg Franko avatar Tom Byrer avatar Takenori Nakagawa avatar Adam Lynch avatar

Watchers

 avatar  avatar James Cloos avatar Tom Byrer avatar  avatar

gulp-insert's Issues

Filename and path

Could you, please, give an example for use filename and path inside the insert function?
I saw commit b2888b8, but can't understand how it work...

Sourcemap support?

Using gulp-insert with https://github.com/floridoo/gulp-sourcemaps shifts the lines:

gulp-task:

gulp.task('app', function() {
  return gulp.src('js/*.js')
    .pipe(sourcemaps.init())
    .pipe(insert.wrap('!function(){ "use strict"; var app; ' + "\n", "\n}();")
    .pipe(sourcemaps.write('.', {includeContent: true, sourceRoot: '/'})
    .pipe(gulp.dest('dist'));
})

a JS file

debugger;
var nextline;

Breaks on var nextline caused of the insert-wrap line

Async support for insert.transform

This module is handy but it's limiting than you can't do anything asynchronous in the callback to insert.transform. A cb parameter would be nice which we could call with the new contents or support for promises. I.e. if the response of the callback I pass to transform is a Promise, then use the value it resolves as the new contents.

Side note: it looks like what I need to do can be achieved using vinyl-transform and map-stream as well. See https://www.npmjs.com/package/vinyl-transform#createstreamtransformfn

License

This project does not have a license file. Can one be added?

gulp-insert and watchify (browserify)

Hey,

I've got a little issue when using gulp-insert along with browsersync (and browserify/watchify).
It seems that, when using your plugin, everything works correctly, but it won't reload.

Here is the code I'm using:

bundler
    .bundle()
    .on('error', logBundlerError)
    .pipe(exorcist('dist/app.js.map'))
    .pipe(source('app.js'))
    .pipe(insert.prepend('var environment = ' + JSON.stringify({
      baseUrl: process.env.GO1V1_BASEURL || '/'
    }) + ';'))
    .pipe(gulp.dest('dist'))
    .pipe(sync.stream({ once: true }))

If I remove insert, scripts reload fine.
Do you have an idea on this?

Thanks!

how do I insert text to the end of the stream?

I don't want to insert text into every file. I only want to add it to the end of the whole stream, so if I have many files in my gulp.src() it would be added to the end. is that possible?

Source map support

If I'm correct your plugin does not support source maps at the moment. It would be pretty cool if it did. Any plans on adding this in the future?

Very useful plugin by the way.

Add access to file name

Sometime it is important to have an access to file name to append/prepend this data to file e.g.

// name/of/the/file.js
... file content ...

Proposed syntax:

insert.append(function(path) {
  return '//' + path.baseName;
})

Filename

It would be helpful the filename was actually part of the parameters passed to the function.

In my case, i use it as the ID that will be appended to the file.

Thanks

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.