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-resources-inline

v0.3.5

Published

Extracts js/css/less and other resources from html, also incline scripts

Downloads

58

Readme

#gulp-resources

Extracts js/css/less and other resources from html

Installation

npm install --save-dev gulp-resources

Usage

With this gulp plugin you can extract js/css/less resources from your html and pipe them to other plugins.


var gulp = require('gulp'),
    resources = require('gulp-resources');

gulp.task('default', function () {
    return gulp.src('./template.html')
        .pipe(resources())
        .pipe(gulp.dest('./tmp'));
});

Running this example task for such html:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

	<link href="css/style1.css" rel="stylesheet" type="text/css">
    <script src="scripts/script1.js"></script>
    <script src="scripts/script2.js"></script>
    <script>
        console.log("inline script should not be touched by gulp-resources");
    </script>
</head>
<body>
    <p>gulp-resources example</p>
</body>
</html>

will produce such folder with sources:

tmp
└─css
    └─style1.css
└─scripts
    ├─script1.js
    └─script2.js

Features and tips

gulp-resources considers every resource entry as a glob so you can do such thing in your HTML:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">

	<link href="css/**/*.css" rel="stylesheet" type="text/css">
    <script src="scripts/**/*.js"></script>
</head>
<body>
    <p>gulp-resources example</p>
</body>
</html>

You can use built-in options (see API) to filter resources but if you want to run gulp-resources once so good solution is to use the gulp-if plugin:


var gulp = require('gulp'),
    gif = require('gulp-if'),
    concat = require('gulp-concat'),
    resources = require('gulp-resources');

gulp.task('default', function () {
    return gulp.src('./template.html')
        .pipe(resources())
        .pipe(gif('**/*.js', concat('concat.js')))
        .pipe(gif('**/*.css', concat('concat.css')))
        .pipe(gulp.dest('./tmp'));
});

Mostly you also need to replace all the resources references with concatenated and/or uglified version. Here the gulp-replace can help:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    
    <!--startjs-->
    <script src="scripts/vendor/script1.js"></script>
    <script src="scripts/vendor/script2.js"></script>
    <script src="scripts/vendor/script3.js"></script>
    
    <script src="scripts/core.js"></script>
    <script src="scripts/modules/**/*.js"></script>
    <!--endjs-->
</head>
<body>
    <p>gulp-resources example</p>
</body>
</html>

var gulp = require('gulp'),
    gif = require('gulp-if'),
    concat = require('gulp-concat'),
    uglify = require('gulp-uglify'),
    replace = require('gulp-replace'),
    resources = require('gulp-resources');

gulp.task('default', function () {
    return gulp.src('./template.html')
        .pipe(resources())
        .pipe(gif('**/*.js', concat('all.js')))
        .pipe(gif('**/*.js', uglify()))
        .pipe(gif('**/*.html', replace(/<!--startjs-->[^]+<!--endjs-->/, '<script src="js/all.js"></script>')))
        .pipe(gulp.dest('./tmp'));
});

After running the task you will have such html:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    
    <script src="js/all.js"></script>
</head>
<body>
    <p>gulp-resources example</p>
</body>
</html>

It also supports resources with query parameters:


<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <link href="css/**/*.css?key=A34-12s" rel="stylesheet" type="text/css">
    <script src="scripts/vendor/script1.js?v=3.0"></script>
    <script src="scripts/modules/**/*.js?z=As42"></script>
</head>
<body>
    <p>gulp-resources example</p>
</body>
</html>

When appendQueryToPath option is true (false by default), vinyl files path property will contain query parameters, so if you need to read the file from file.path property, remove the query parameters before:

function readFileContents(file) {
    var queryIdx = file.path.indexOf('?');
    queryIdx = queryIdx < 0 ? file.path.length : queryIdx;
    return fs.readFileSync(file.path.substring(0, queryIdx)).toString('utf8');
}

API

resources(options)

Returns a stream with extracted resources.

options.cwd

Type: String or Array
Default: none

Without this value only working directory of processing HTML file is used to search resources. Specifying this property allows you to add another location/locations to search for resources files if they were not found with HTML's working directory.

options.js

Type: Boolean Default: true

Specify whether to search js files

options.css

Type: Boolean Default: true

Specify whether to search css files

options.less

Type: Boolean Default: true

Specify whether to search less files

options.favicon

Type: Boolean Default: false

Specify whether to search favicon file

options.skipNotExistingFiles

Type: Boolean Default: false

Specify whether to skip errors when resource files were not found.

options.appendQueryToPath

Type: Boolean Default: false

Append query to file.path if exists. When true, <script src="myscript.js?v3.0"></script> will produce this vinyl file:

{
    base: '/',
    cwd: '/',
    stat: Object,
    path: '/myscript.js?v3.0',
    contents: Object
}

When false, it will produce this one:

{
    base: '/',
    cwd: '/',
    stat: Object,
    path: '/myscript.js',
    contents: Object
}

License

MIT @ Eugene Gluhotorenko