generate-file-webpack-plugin

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript maintenance

A general-purpose Webpack plugin (v1.0.1) for generating arbitrary files during the build process. Supports file paths (relative or absolute), content as strings, Buffers, Promises, or functions returning these. Works with Webpack 4.x (5.x untested) and Node.js 12+. Features include debug mode with console output. Less opinionated than similar plugins like `webpack-manifest-plugin` or `html-webpack-plugin`, allowing any file content generation. Last updated in 2020, currently in maintenance mode with no active development.

error TypeError: generate is not a function
cause Importing the package incorrectly, e.g., using `import generate from 'generate-file-webpack-plugin'` in an ESM context or using destructuring without default.
fix
Use const generate = require('generate-file-webpack-plugin');
error Error: Plugin could not be instantiated. Did you use 'new' keyword?
cause Using `new generate(options)` instead of calling `generate(options)` as a factory function.
fix
Remove the new keyword: generate(options)
error Module not found: Error: Can't resolve 'generate-file-webpack-plugin'
cause Package not installed or missing from `package.json`.
fix
Run npm install generate-file-webpack-plugin --save-dev
gotcha Do not use `new generate()` – it is a factory function, not a constructor. Using `new` will cause the plugin to be applied twice.
fix Use `generate(options)` directly without `new`.
deprecated The named export `GenerateFileWebpackPlugin` is deprecated and will be removed in a future version. Use `GenerateFilePlugin` instead.
fix Use `const { GenerateFilePlugin } = require('generate-file-webpack-plugin');`.
gotcha Relative file paths are resolved relative to webpack's `output.path`, not the project root.
fix Use absolute paths or ensure `output.path` is set correctly.
gotcha The `content` option as a function receives no arguments; attempting to access `compilation` or other webpack internals will fail.
fix If you need compilation data, use a different plugin like `webpack-sources` or hook into compilation directly.
gotcha When using `debug: true`, the plugin logs to console via `console.log()`, which may interfere with CI log outputs.
fix Set `debug: false` in production builds.
npm install generate-file-webpack-plugin
yarn add generate-file-webpack-plugin
pnpm add generate-file-webpack-plugin

Demonstrates generating two files: a JSON file with version info and a text file with build timestamp using a function.

const generate = require('generate-file-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  output: {
    path: __dirname + '/dist',
    filename: 'bundle.js'
  },
  plugins: [
    generate({
      file: 'version.json',
      content: JSON.stringify({ version: '1.0.0', buildTime: Date.now() })
    }),
    generate({
      file: 'about.txt',
      content: () => `Built at ${new Date().toISOString()}`
    })
  ]
};