npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-pimp

v0.1.1

Published

Pimp your imports!

Downloads

10

Readme

gulp-pimp npm travis codecov

Pimp your imports!

pimp discovers your modules/components/whatever and automatically imports them in the file you want. You can then process it with the tool of your choice. It supports commonjs, sass, less, css by default. You can customize everything, of course.

That's not all, pimp is also a reducer, so you can use it for other purposes than simply importing stuff. As it's compatible with all plugins consuming gulp-data, you can use it with template engines for example.

Install

npm install --save-dev gulp-pimp

Usage

Scripts

gulp.src('app/components/*.js', { read: false })
  .pipe(pimp('app.js'))
  .pipe(browserify())
  .pipe(gulp.dest('dist/'));

app.js will contain:

var a = require('app/components/a.js');
var b = require('app/components/b.js');

CSS/SASS/LESS

return gulp.src('app/components/_*.scss', { read: false })
  .pipe(pimp('app.scss'))
  .pipe(sass())
  .pipe(gulp.dest('dist/'));

app.scss will contain:

@import 'app/components/_a.scss';
@import 'app/components/_b.scss';

Custom

return gulp.src('app/components/*.html', { read: false })
  .pipe(pimp('index.html', (output, file) => output + `{% include '${path}' %}` ))
  .pipe(template())
  .pipe(gulp.dest('dist/'));

index.html will contain:

{% include 'app/components/a.html' %}
{% include 'app/components/b.html' %}

API

pimp(manifest, [options])

manifest {string}

Name of the manifest file. If it already exists, imports are appended to the end of the file. If not the file is created.

options {object|function}

Set of options. If a function is passed, this is equivalent to the reducer option.

rules {object}

A set of rules to apply to input files. A rule is a key-value pair. The key is a glob to match, and the value is a template string that will be used to output the import statement.

The template string accepts several variables that will vary for each file being processed:

  • ${name}: name of the file, without the extension
  • ${path}: absolute path of the file

Example:

pimp('app.js', {
  rules: {
    'components/*/*.js': "import ${name} from '${path}'"
  }
});
reducer {function}

The reducer gives you full control over statement substitution. It is called for each file. The function has 2 arguments:

  • output: which is the final output of all statements, or the data attribute if data is specified
  • file
    • name: name of the file, without the extension
    • ext: extension of the file
    • basename: name of the file, with the extension
    • path: absolute path of the file
    • contents: contents of the file as a string

As it's a reducer, make sure to always return output.

Example:

pimp('index.html', (output, file) => output + `<script src="https://wootcdn.com/${path}"></script>`);
intro {function}

Executed just before the reducer, it gives you the ability to setup and prepend something to output.

outro {function}

Executed just after the reducer, it gives you the ability to cleanup and append something to output.

data {boolean|string}

Alters pimp behavior by adding a data attribute to the manifest file, without modifying its content. This is mainly designed to be used with other plugins that consume gulp-data. If data is a string, collected files will be set in the given namespace (i.e. data.components).

data makes pimp switch of reducer. If you specify a custom reducer, it will take an object as first argument, instead of a string. intro and outro will have no effect.

Note that you should remove { read: false } when using this as you might actually need contents of your collected files.

Rules

But default, pimp comes with default rules:

{
  '*.{scss,less,css}': "@import '${path}';",
  '*.{js,jsx}': "var ${name} = require('${path}');"
}

If you want to modify those default, you can alter the pimp.RULES object.

Moar examples

ES6

pimp('app.js', {
  rules: {
    '*.js': 'import ${name} from ${path};'
  }
})

AMD

const components = []

pimp('app.js', {
  intro: () => 'define([',
  reducer: (output, file) => { components.push(file.name); return output + file.name },
  outro: () => `], function(${components.join(', '}) {\n});\n`
})

Templates

This is an idea of what can be done using data.

Here is an index.html which will be processed by gulp-template:

<body>
  <%= components.header %>
  <main>
    <%= components.content %>
    <%= components.aside %>
  </main>
  <%= components.footer %>
</body>

pimp collects components and passes then to gulp-template:

gulp.src('app/components/*/*.html')
  .pipe(pimp('index.html', { data: 'components' }))
  .pipe(template(pkg))
  .pipe(gulp.dest('dist/'))

License

MIT © Nicolas Gryman