GithubHelp home page GithubHelp logo

rollup-plugin-hash's Introduction

rollup-plugin-hash Travis Build Status

Rollup plugin to compose bundle output filenames with unique hashes, to facilitate long-term caching of your static resources.

It uses is simple placeholder pattern to substitue filename with bundle hash. I.e.

main.[hash].js

becomes:

main.07d2bf0d12655d9f51c0637718da4889.js

Install

via npm:

npm install --save-dev rollup-plugin-hash

via yarn:

yarn add --dev rollup-plugin-hash

Usage

import { rollup } from 'rollup';
import hash from 'rollup-plugin-hash';

rollup({
    entry: 'main.js',
    plugins: [
        hash({ 
			dest: 'main.[hash].js'	
		})
    ]
});

Meanwhile, if dest filename is written the following way, only first 4 characters of hash will be used in final filename: main.[hash:4].js. You could change this number to modify the output result.

Options

dest

Type: string Required: true

The template of your filename destination. Must include the placeholder [hash] to be replaced.

replace

Type: boolean Default: false Required: false

Whether the hashed version should replace the main output file generated by Rollup. Useful in CI environments where you don't need any non-hashed assets.

algorithm

Type: string Default: sha1 Required: true

Hashing algorithm used to generate hash. Can be one of md5, sha1, sha256, sha512

manifest

Type: string Required: false

Filename to write a manifest to. Will generate a JSON manifest mapping input filename to hashed output filename. Useful if you want to dynamically generate link to your hashed output server-side.

Example manifest:

{
	"main.js": "main.56770a64be1a1132502b276c4132a76bb94d9e1b.js"
}

manifestKey

Type: string Default: bundle.dest Required: false

The filename used as the input key in the generated manifest map. Useful in certain build setups where path.resolve is needed as bundle.dest but not in the manifest.

callback

Type: function Required: false

Callback which is called with the resulting hashed filename. This is useful if you are integrating with other build steps and want to store the filename locally to be used in subsequent configs etc.

License

MIT ©

rollup-plugin-hash's People

Contributors

laysent avatar phamann avatar rich-harris avatar theolampert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rollup-plugin-hash's Issues

Remove original files?

What do you think about adding an option like removeOriginal: true or replace: true that gets rid of the original output file?

Support sourcesmaps

First of all, thanks for this wonderful plugin. It's possible I'm doing something wrong.
In my rollup-config.js I have this:

import rollup from 'rollup'
import hash from 'rollup-plugin-hash';

export default {
  entry: './main.js',
  dest: './build.js',
  sourceMap: true,
  format: 'iife',
  plugins: [
    hash({
      algorithm: 'sha256',
      dest: './build.[hash].js',
      replace: true
    })
  ]
}

This created the build.[hash].js file and a build.js.map file, but as you can see the .map file name doesn't contain the hash and the contents of the .map file link to the build.js file instead of the build.[hash].js file.

Am I doing something wrong or is there a way to get the sourcemap to link up with the hashed file name?

Thanks.

Accept callback with hashed filename?

I have a situation where I have an array of sequential builds in a single config — one for the client, one for the service worker, one for the server — and each config depends on the output of the previous one (service worker needs the hashed bundle filename for the cache manifest, server needs both in order to rewrite templates correctly).

At the moment it's quite finicky to put all those pieces together. What do you think about accepting a callback that is called with the hashed filename, so it can be stored locally and used in subsequent configs?

Update existing manifest?

Hey — wondering if you'd be interested in a PR that changed the behaviour of options.manifest such that it updates an existing manifest rather than creating a new one each time (unless it doesn't already exist, of course)? My use case is that I have two Rollup configs and also need to hash the filename of some generated CSS — it would be cool if I could put all three in the same manifest.

Allow to work with experimental code splitting

Would be wonderful if this could work with code splitting.

Maybe something like this:

{
    input: [
      'src/page-a',
      'src/page-b',
    ],
    output: {
      dir: 'dist',
      format: 'system',
    },
    plugins: [
      hash({
        dest: '[name].[hash].js'
      }),
    ],
}

Automatic dest detection

For my use case, I call rollup from the CLI and pass the dest as an argument. From my understanding, the current best (only?) practice is to set plugin options in a rollup config file. However, I cannot set the dest as a hash plugin option in the config file because it is not known ahead of time (I use the same config for multiple files).

Would it be possible to fallback to the dest in the rollup options if the plugin dest is not provided? For example, bundle.[hash].js could be set as the rollup dest. Alternatively, default to converting bundle.js to bundle.[hash].js.

I'm happy to submit a PR. What do you think?

Unreachable code and possibly wrong order.

This function in src/index.js :
https://github.com/phamann/rollup-plugin-hash/blob/master/src/index.js#L30-L32

function generateManifest(input, output) {
	return JSON.stringify({[input]: output});
}

ends up in
node_modules/rollup-plugin-hash/dist/rollup-plugin-hash.js
node_modules/rollup-plugin-hash/dist/rollup-plugin-hash.es.js

as :

function generateManifest(input, output) {
	return JSON.stringify(( obj = {}, obj[input] = output, obj ));
	var obj;
}

The var obj statement looks like unreachable code to me, and should have come before the return statement, I think.

Updating rollup to version >= 0.48 breaks this plugin

As of version 0.48, rollup introduced "numerous changes to the options object".

An overview of the changes can be found in this gist.

rollup-plugin-hash is not compatible with this new format, and fails during our build with this error:

[!] TypeError: path must be a string or Buffer
TypeError: path must be a string or Buffer
    at Object.fs.unlinkSync (fs.js:1067:18)
    at Object.onwrite (.../node_modules/rollup-plugin-hash/dist/rollup-plugin-hash.js:87:8)
    at .../node_modules/rollup/dist/rollup.js:10609:40
    at next (.../node_modules/rollup/dist/rollup.js:148:27)
    at .../node_modules/rollup/dist/rollup.js:152:48
    at <anonymous>
Our rollup.config.js

const realpath = require('fs').realpathSync;
const commonjs = require('rollup-plugin-commonjs');
const nodeResolve = require('rollup-plugin-node-resolve');
const replace = require('rollup-plugin-replace');
const babel = require('rollup-plugin-babel');
const hash = require('rollup-plugin-hash');
const copy = require('rollup-plugin-copy');

const env = process.env.NODE_ENV || 'development';

const plugins = [
    replace({
        'process.env.NODE_ENV': JSON.stringify(env)
    }),
    nodeResolve({
        jsnext: true,
        main: true
    }),
    commonjs({
        sourceMap: false,
        namedExports: {
            [realpath('node_modules/react-dom/index.js')]: [ 'render' ],
            [realpath('node_modules/react/react.js')]: [ 'Component' ]
        }
    }),
    babel({ runtimeHelpers: true }),
    hash({
        dest: 'build/assets/bundle.[hash].js',
        manifest: 'build/assets-manifest.json',
        algorithm: 'sha256',
        manifestKey: 'js',
        replace: env === 'production'
    }),
    copy({
        [realpath('assets/favicon.ico')]: 'build/assets/favicon.ico'
    })
];

module.exports = {
    input: 'browser/exposeApp.js',
    output: {
        file: 'build/assets/unhashed-bundle.js',
        format: 'iife'
    },
    plugins
};

Ability to customise the manifest value

Feature Request

It would be good to be able to customise the manifest value too.
In my case, I only need the filename and not the full path to the file in the manifest.

Multiple files

How would one use this plugin on multiple files?

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.