pnp-package-json-webpack-plugin

A webpack plugin to generate package.json file with external modules as dependencies

Usage no npm install needed!

<script type="module">
  import pnpPackageJsonWebpackPlugin from 'https://cdn.skypack.dev/pnp-package-json-webpack-plugin';
</script>

README

npm node deps downloads

Plug'n'Play package.json webpack plugin

A webpack plugin to generate a package.json file with external modules as dependencies

Inspired by generate-package-json-webpack-plugin

Install

yarn add --dev pnp-package-json-webpack-plugin

Usage

This plugin generates a package.json asset from modules marked in externals with pinned versions using resolutions from the PnP API. This allows for lightweight, mostly-stable builds in Yarn workspace environments.

In your webpack configuration:

const PnpPackageJsonPlugin = require('pnp-package-json-webpack-plugin');

const basePackageValues = {
  name: 'my-nodejs-module',
  version: '1.0.0',
  main: './index.js',
  engines: {
    node: '>= 14.0.0',
  },
};

module.exports = {
  // ...
  plugins: [
    new PnpPackageJsonPlugin({
      basePackageValues,
    }),
  ],
};

Please be aware that the file created should not be viewed as a replacement for a lockfile such as yarn.lock. The version of each external pinned by the plugin will be stable, but it offers no control over child dependencies installed later.

Configuration

basePackageValues

Type: Object|Function Default:

{
  "name": "",
  "version": "0.0.0",
  "description": "",
  "main": "main.js"
}

Specifies an object or function customizing the outputted package.json.

Object

webpack.config.js

module.exports = {
  plugins: [
    new PnpPackageJsonPlugin({
      basePackageValues: {
        name: 'my-app',
        version: '1.0.0',
        main: 'out.js',
      };
    }),
  ],
};

Function

webpack.config.js

module.exports = {
  plugins: [
    new PnpPackageJsonPlugin({
      basePackageValues(compilation) {
        const { runtimeChunk } = compilation.entrypoints.get('my-app');
        return {
          name: 'my-app',
          version: '1.0.0',
          main: runtimeChunk.files[0],
        };
      },
    }),
  ],
};

outputPath

Type: String Default: .

Specifies a filesystem path where the generated package.json will be placed.

webpack.config.js

module.exports = {
  plugins: [
    new PnpPackageJsonPlugin({
      // plugin will generate <output.path>/package/package.json
      outputPath: 'package',
    }),
  ],
};