grunt-webpack
Use webpack with grunt.
Last updated 3 months ago by danez .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install grunt-webpack 
SYNC missed versions from official npm registry.

npm deps test chat

Grunt Webpack

Use Webpack with Grunt.

Requirements

  • Version 4 of grunt-webpack supports webpack 4 as well as version 5 beta.
  • Version 3.2.0 of webpack-dev-server is needed if used (optional).

Install

Install this grunt plugin next to your project's Gruntfile.js. You also need to install webpack yourself, this grunt plugin does not install webpack itself.

yarn add webpack grunt-webpack --dev
// or
// npm i webpack grunt-webpack --save-dev

If you also want to use the webpack-dev-server task you also need to install webpack-dev-server

yarn add webpack-dev-server --dev

Then add this line to your project's Gruntfile.js gruntfile:

const webpackConfig = require('./webpack.config.js');

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    ...,
    webpack: {
      myConfig: webpackConfig,
    },
    ...
  });

  grunt.loadNpmTasks('grunt-webpack');
};

Configuration

webpack-grunt offers two different tasks webpack and webpack-dev-server. Both support all webpack options as can be seen in the webpack documentation. For exceptions and additions see this list.

Both Tasks

progress

Type: bool Default: true (false if no TTY present)

Activates or deactivates the progress output of webpack.

keepalive

Type: bool Default: false (true if watch mode is used and for webpack-dev-server task)

When set to true the grunt process/task will be kept alive after webpack task is finished. This is especially useful for watch and webpack-dev-server as these usually need to run as long as not manually killed.

Webpack Task

watch

Type: bool Default: undefined

Turn on watch mode. This means that after the initial build, webpack will continue to watch for changes in any of the resolved files.

Turning on watch mode also sets the following defaults:

  • Default cache to true
  • Default keepalive to true
  • Default failOnError to false

failOnError

Type: bool Default: true (false if watch mode is used)

Will terminate the grunt process when an error happens if set to true. If set to false the grunt process will not be immediately terminated on error and instead continue to run.

storeStatsTo

Type: string Default: null

When set the stats from webpack will be written to a variable with the name provided in this option. The variable can later be used inside of other grunt tasks with template tags <%= %>.

...
storeStatsTo: "webpackStats"

...

<%= webpackStats.hash %>
...

For more information about grunt template tags have a look at the grunt docs.

Webpack-dev-server Task

The webpack-dev-server options host, hotOnly, inline, port and public which are usually only available in the CLI can also be used in this grunt-plugin.

Examples

Webpack

imported config

This is a simple example that requires the webpack config from the config file. It also disables stats in non 'development' environments and enables watch in development.

const webpackConfig = require('./webpack.config.js');

module.exports = function(grunt) {
  grunt.initConfig({
    webpack: {
      options: {
        stats: !process.env.NODE_ENV || process.env.NODE_ENV === 'development'
      },
      prod: webpackConfig,
      dev: Object.assign({ watch: true }, webpackConfig)
    }
  });

  grunt.loadNpmTasks('grunt-webpack');
};

The webpack task in this example has two targets called prod and dev. They can be renamed to everything besides options. See the grunt docs for more information about targets.

On the command line you can then do the following.

# Run webpack with the `prod` target
> NODE_ENV='production' grunt webpack:prod

# Run webpack with the `dev` target
> grunt webpack:dev

# Run webpack for all targets
> grunt webpack

For more examples and information have a look at the webpack documentation which mostly also applies here besides the noted differences above.

Lazy config loading

In some cases you might want to load the webpack config lazy. This can be done by specifying a function as the config value. The first paramter to this function will be the complete grunt config, which can be used in cases where grunt templates do not work (see below).

const webpackConfig = require('./webpack.config.js');

module.exports = function(grunt) {
  grunt.initConfig({
    webpack: {
      myconfig: () => ({
        entry: path.join(__dirname, "entry"),
        output: {
          path: __dirname,
          filename: "output.js",
        },
      }),
    },
  });

  grunt.loadNpmTasks('grunt-webpack');
};

Grunt templates

grunt-webpack supports grunt templates in all string values in it's configuration.

In the next example we use a template for output.filename.

const webpackConfig = require('./webpack.config.js');

module.exports = function(grunt) {
  grunt.initConfig({
    outputFileName: "output.js",
    webpack: {
      myconfig: {
        entry: path.join(__dirname, "entry"),
        output: {
          path: __dirname,
          filename: "<%= outputFileName %>",
        },
      },
    },
  });

  grunt.loadNpmTasks('grunt-webpack');
};

For plugins we cannot support grunt template interpolation, as plugins are class instances which we cannot modify during runtime without breaking them. If you need to use template in a string option to a plugin, you can use lazy config loading and use the supplied config. You can also use grunt inside directly to access utility methods:

const webpackConfig = require('./webpack.config.js');

module.exports = function(grunt) {
  grunt.initConfig({
    name: "Webpack",
    pkg: {
      copyright: '<%= name %>',
      version: '6.55.345',
    },
    webpack: {
      myconfig: (config) => ({
        entry: path.join(__dirname, "entry"),
        output: {
          path: __dirname,
          filename: "output.js",
        },
        plugins: [
          new webpack.BannerPlugin({
            banner: `/*! ${config.pkg.copyright} - Version ${config.pkg.version} dated ${grunt.template.today()} */`,
            raw: true,
          }),
        ],
      }),
    },
  });

  grunt.loadNpmTasks('grunt-webpack');
};

Troubleshooting

Circular reference detected (.plugins)

If you encounter this message it means that one of the plugins which are present in your config have circular references. This is not a bug in the plugin and is totally fine for webpack, but sadly grunt cannot handle these.

To workaround this problem use lazy config loading, by wrapping your config inside a function.

const webpackConfig = require('./webpack.config.js');

module.exports = function(grunt) {
  grunt.initConfig({
    webpack: {
      myconfig: () => webpackConfig,
    },
  });

  grunt.loadNpmTasks('grunt-webpack');
};

Maintainers


Daniel Tschinder

Juho Vepsäläinen

Joshua Wiens

Kees Kluskens

Sean Larkin

Current Tags

  • 4.0.2                                ...           latest (3 months ago)
  • 3.0.0-beta.2                                ...           next (4 years ago)

60 Versions

  • 4.0.2                                ...           3 months ago
  • 4.0.1                                ...           3 months ago
  • 4.0.0                                ...           3 months ago
  • 3.1.3                                ...           2 years ago
  • 3.1.2                                ...           2 years ago
  • 3.1.1                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           3 years ago
  • 3.0.0-beta.2                                ...           4 years ago
  • 3.0.0-beta.1                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 2.0.0-beta.6                                ...           4 years ago
  • 2.0.0-beta.5                                ...           4 years ago
  • 2.0.0-beta.4                                ...           4 years ago
  • 2.0.0-beta.3                                ...           4 years ago
  • 2.0.0-beta.2                                ...           4 years ago
  • 2.0.0-beta.1                                ...           4 years ago
  • 1.0.18                                ...           4 years ago
  • 1.0.17                                ...           4 years ago
  • 1.0.16                                ...           4 years ago
  • 1.0.15                                ...           4 years ago
  • 1.0.14                                ...           4 years ago
  • 1.0.13                                ...           4 years ago
  • 1.0.12                                ...           4 years ago
  • 1.0.11                                ...           5 years ago
  • 1.0.10                                ...           5 years ago
  • 1.0.8                                ...           6 years ago
  • 1.0.7                                ...           6 years ago
  • 1.0.6                                ...           6 years ago
  • 1.0.5                                ...           7 years ago
  • 1.0.4                                ...           7 years ago
  • 1.0.3                                ...           7 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
  • 0.11.1                                ...           7 years ago
  • 0.11.0                                ...           7 years ago
  • 0.10.5                                ...           7 years ago
  • 0.10.4                                ...           7 years ago
  • 0.10.3                                ...           7 years ago
  • 0.10.2                                ...           7 years ago
  • 0.10.1                                ...           8 years ago
  • 0.10.0                                ...           8 years ago
  • 0.9.6                                ...           8 years ago
  • 0.9.5                                ...           8 years ago
  • 0.9.4                                ...           8 years ago
  • 0.9.3                                ...           8 years ago
  • 0.9.1                                ...           8 years ago
  • 0.9.0                                ...           8 years ago
  • 0.4.0                                ...           8 years ago
  • 0.3.1                                ...           8 years ago
  • 0.3.0                                ...           8 years ago
  • 0.2.2                                ...           8 years ago
  • 0.2.1                                ...           8 years ago
  • 0.2.0                                ...           8 years ago
  • 0.1.1                                ...           8 years ago
  • 0.1.0                                ...           8 years ago

Copyright 2014 - 2016 © taobao.org |