Giter VIP home page Giter VIP logo

copy-pkg-json-webpack-plugin's Introduction

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'}}
    })
  ]
}

Options

Options are passed as arguments to the new CopyPkgJsonPlugin({ options }) constructor and must be an object containing either a remove key with an an array containing properties you want to remove as strings and/or a replace key with an object property 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(
  {  remove: [/*...*/], replace: {/*...*/} }, 
  /* 'OPTIONAL/PATH/TO/pckJSON/DIRECTORY' */
 )

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 simply copy package.json to the dist file 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 but not replace any just pass in this key:

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

And vice-versa if you wish to replace but not remove any items:

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

copy-pkg-json-webpack-plugin's People

Contributors

pmsandhu avatar zelphir avatar

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.