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

webpack-merge-and-include-globally

v2.3.4

Published

Merge multiple files (js,css..) into single file to include somewhere

Downloads

136,560

Readme

MERGE INTO SINGLE FILE PLUGIN FOR WEBPACK

Webpack plugin to merge your source files together into single file, to be included in index.html, and achieving same effect as you would by including them all separately through <script> or <link>.

Getting Started

npm install --save-dev webpack-merge-and-include-globally

Usage

Lets say you want to make libraries like jquery, moment (including 3 languages) and toastr available globally, and you're struggling to make them global with webpack or just importing them (in cases they aren't written well) because require() wraps the code into new scope and you want to execute it against a global scope, and you don't want to do this:

  <script src="/node_modules/jquery/dist/jquery.min.js"></script>
  <script src="/node_modules/moment/moment.js"></script>
  <script src="/node_modules/moment/locale/cs.js"></script>
  <script src="/node_modules/moment/locale/de.js"></script>
  <script src="/node_modules/moment/locale/nl.js"></script>
  <script src="/node_modules/toastr/build/toastr.min.js"></script>
  
  <link rel="stylesheet" href="/node_modules/toastr/build/toastr.min.css">

because your node_modules is not available in production. with this plugin you can achieve the desired effect this way:


  const MergeIntoSingleFilePlugin = require('webpack-merge-and-include-globally');
  
  module.exports = {
    ...
    plugins: [
        new MergeIntoSingleFilePlugin({
            files: {
                "vendor.js": [
                    'node_modules/jquery/dist/jquery.min.js',
                    //  will work too
                    //  'node_modules/jquery/**/*.min.js',
                    'node_modules/moment/moment.js',
                    'node_modules/moment/locale/cs.js',
                    'node_modules/moment/locale/de.js',
                    'node_modules/moment/locale/nl.js',
                    'node_modules/toastr/build/toastr.min.js'
                ],
                "vendor.css": [
                    'node_modules/toastr/build/toastr.min.css'
                ]
            }
        }),
    ]

this generates 2 files with merged js and css content, include them into your index.html to take effect:

  <script src="./vendor.js"></script>
  <link rel="stylesheet" href="./vendor.css">

now jQuery, moment and toastr are available globally throughout your application.

Options

files (as object)

Object that maps file names to array of all files (can also be defined by wildcard path) that will be merged together and saved under each file name. For example to merge jquery, classnames and humps into vendor.js, do:

new MergeIntoSingle({
  files: {
    'vendor.js': [
      'node_modules/jquery/**/*.min.js',
      'node_modules/classnames/index.js',
      'node_modules/humps/humps.js'
    ],
    'style.css': [
      'example/test.css'
    ]
  }
})

transform

Object that maps resulting file names to tranform methods that will be applied on merged content before saving. Use to minify / uglify the result. For example to minify the final merge result of vendor.js, do:

new MergeIntoSingle({
  files: { 'vendor.js': [...] },
  transform: {
    'vendor.js': code => require("uglify-js").minify(code).code
  }
})

files (as array)

Alternative way to specify files as array of src & dest, for flexibility to transform and create multiple destination files for same source when you need to generate additional map file for example.

new MergeIntoSingle({
  files: [{
    src:[
      'node_modules/jquery/**/*.min.js',
      'node_modules/classnames/index.js',
      'node_modules/humps/humps.js'
    ],
    dest: code => {
      const min = uglifyJS.minify(code, {sourceMap: {
        filename: 'vendor.js',
        url: 'vendor.js.map'
      }});
      return {
        'vendor.js':min.code,
        'vendor.js.map': min.map
      }
    },

    // also possible:
    //
    // dest: 'vendor.js'
  },{
    src: ['example/test.css'],
    dest: 'style.css'

    // also possible:
    //
    // dest: code => ({
    //   'style.css':new CleanCSS({}).minify(code).styles
    // })
  }]
})

hash

default: false

set true to append version hash before file extension.

you can get names of generated files mapped to original by passing callback function as second argument to plugin:

new MergeIntoSingle({ ... }, filesMap => { ... }),

transformFileName

default: undefined

also you can pass function for change output file name with hash

new MergeIntoSingle({
  ...,
  transformFileName: (fileNameBase, extension, hash) => `${fileName}.[${hash}]${extension}`,
  // bundle.[somehash].js
}),

//or

new MergeIntoSingle({
  ...,
  transformFileName: (fileNameBase, extension, hash) => `${fileNameBase}${extension}?hash=${hash}`,
  // bundle.js?hash=somehash
}),

encoding

default: 'utf-8'

encoding of node.js reading

chunks

default: undefined

array of entry points (strings) for which this plugin should run only

separator

default: '\n'

string used between files when joining them together

Working Example

working example already included in project. to test first install npm i, then run npm run start to see it in action and npm run build to build prod files with vendor file and index.html.