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

copy-pkg-json-webpack-plugin

v0.0.40

Published

A webpack plugin to copy package.json file with desired key props into build directory

Downloads

5,924

Readme

CopyPackageJsonPlugin

This is a plugin to copy and edit your package.json file to your webpack distribution/production bundle. This is useful for updating the version number of your package and only including the necessary information in your package.json bundle that consumer of your application/package need.

Install

$ npm install copy-pkg-json-webpack-plugin --save-dev

Usage

// webpack.config.js
const CopyPkgJsonPlugin = require("copy-pkg-json-webpack-plugin")
module.exports = {
  entry: '//...',
  output: '//...',
  plugins: [
    new CopyPkgJsonPlugin({
      remove: ['devDependencies'],
      replace: {scripts: {start: 'node index.js'}}
    })
  ]
}
// webpack.config.js
const CopyPkgJsonPlugin = require("copy-pkg-json-webpack-plugin")
const pkg = require('./package.json')
module.exports = {
  entry: '//...',
  output: '//...',
  plugins: [
    new CopyPkgJsonPlugin({      
      new: {
        name: pkg.name,
        version: '1.2.0',
        description: pkg.description,
        repository: pkg.repository,
        peerDependencies: {
          react: 'latest',
          'react-dom': 'latest'
        },
        devDependencies: {
          colors: 'latest'
        } 
      }
    })
  ]
}

Options

Options are passed as arguments to the new CopyPkgJsonPlugin({ options }) constructor and must be an object containing either a new key with an object containing the key/value pairs you wish to populate your new package.json with, a remove key with an array containing properties you want to remove from your existing package.json as strings and/or a replace key with an object containing the key/value pairs you wish to replace from your original package.json with. You may optionally pass in the absolute path string to the directory containing your package.json file. The plugin defaults to process.cwd() path. See the NOTE section below for more information on specifying the package.json directory path.

new CopyPkgJsonPlugin(
  { new: {/*...*/} },
  /* OR */
  {  remove: [/*...*/], replace: {/*...*/} }, 
  /* 'OPTIONAL/PATH/TO/pckJSON/DIRECTORY' */
 )

New

The new key expects an object with the standard package.json fields that you wish to be made available in your final bundle's package.json

plugins : [
  new CopyPackageJsonPlugin({
    new: {
      name: 'my-package',
      version: '3.0.1',
      description: 'my package to publish to npm',
      author: 'Mario Lopez',
      license: 'MIT',
      private: false,
      peerDependencies: { d3: 'latest'}
    }
  })
] 

Remove

The remove key expects an array with property names from your original package.json file that you wish to not be included in the copied package.json

plugins : [
  new CopyPackageJsonPlugin({
    remove: ['scripts', 'jest', 'devDependencies]']
  })
] 

Replace

The replace key expects an object with property names from your original package.json file that you wish to replace with different properties

plugins : [
  new CopyPackageJsonPlugin({
    replace: {
      version: '1.2.1',
      author: 'Mario Lopez',
      scripts: { start: 'node index.js', test: 'echo' }    
    }
  })
] 

By default this will merge keys by one level deep. So using the example above if your original package.json also included other scripts such as build command that script will be included in the copied package.json

{
  "scripts": {
    "start": "node index.js", 
    "test":"echo", 
    "build": "webpack src" 
  }
}   

If you do not want the existing keys to be merged and want to overwrite them you must add the the key to the remove option first

plugins : [
  new CopyPackageJsonPlugin({
    remove: ['scripts'],
    replace: {
      scripts: { start: 'node index.js', test: 'echo' }    
    }
  })
] 

Note

This plugin assumes that your package.json is in the root directory of the node processes current working directory ie. process.cwd(). If your package.json is located elsewhere, you may optionally pass the absolute path of the directory where your package.json resides, as a second argument to new CopyPkgJsonPlugin constructor. NOTE: that you must pass in a first argument in order to pass in the context; if you wish to simply copy your existing package.json you can pass in an empty object :

plugins: [ 
  new CopyPkgJsonPlugin({}, 'src/app/hello') 
]

If you wish to create a brand new package.json to be copied to the final dist bundle folder, pass an object with the key new which contains the key value pairs that you would like to be in your package.json:

plugins: [
  new CopyPkgJsonPlugin({ new: {
    name: 'state-manager-lib',
    description: 'state management library',
    main: 'index.js',
    version: '2.0',
    license: 'MIT',
    dependencies: { lodash: 'latest', redux: 'latest' },
    devDependencies: { 'chalk': 'latest', 'state-invariant': 'latest' }
  }}) 
]

If you wish to simply copy your existing package.json your to the final dist bundle folder without any modifications and your package.json is located in the node processes current working directory ie process.cwd(), simply call the constructor without arguments and your package.json is located at the root :

plugins: [
  new CopyPkgJsonPlugin() 
]

If you which to remove an item from your existing package.json but not replace any just pass in this key:

plugins: [ 
  new CopyPkgJsonPlugin({ remove: ['engines','devDependencies'] }) 
]

And vice-versa if you wish to replace in an existing package.json but not remove any items:

plugins: [ 
  new CopyPkgJsonPlugin({ replace: { author: 'Mario Lopez' } }) 
]