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

@wok-cli/task-webpack

v1.0.1

Published

Webpack task for wok-cli

Downloads

4

Readme

Webpack Task

Sharable tasks for Webpack.

Installation

This task requires @wok-cli/core and webpack@^4.0.0 as peer dependency.

npm i @wok-cli/core webpack@^4.0.0 @wok-cli/task-scripts --save-dev

Parameters

| parameter | type | default | note | | -------------- | ------ | --------------- | --------------------------------------------------- | | entry | object | | Webpack entry configuration (1) | | outputFolder | string | | Bundle output folder (1) | | context | string | process.cwd() | Compiler context folder(1) |

  1. Supports environment templates.

Hooks

| name | type | description | | ---------------------- | ---------------------- | ------------------------------------------- | | config:chain | webpack-chain | The default webpack chain instance | | config | object | The resolved webpack configuration object | | completed | stats | Run when compilation ends (single mode) | | completed:watch | stats | Run when compilation ends (watch mode) | | completed:middleware | stats | Run when compilation ends (middleware mode) |

Usage

By default, the task will output sourcemaps in development. Each entry will be stored in the outputFolder with the following pattern: [name].bundle.js.

Example

const $ = require('@wok-cli/core');
const webpackTask = require('@wok-cli/task-webpack');

const webpack = $.task(webpackTask, {
  entry: { main: './src/main.js' },
  outputFolder: 'public',
});

exports.webpack = webpack;

By running the gulp webpack task, webpack will bundle ./src/main.js and save it as ./public/main.bundle.js.

Watching for changes

The returned task has a watch sub-task that triggers the webpack watcher instead of a single compilation.

const $ = require('@wok-cli/core');
const serveTask = require('@wok-cli/task-serve');
const webpackTask = require('@wok-cli/task-webpack');

const webpack = $.task(webpackTask, {
  entry: { main: './src/main.js' },
  outputFolder: 'public',
});

// watch and compile files
exports.watch = webpack.watch;

Reloading the page

To reload the page after each successful compilation you can leverage the completed:watch hook. For example here is how you can trigger a reload with the @wok-cli/server task:

const $ = require('@wok-cli/core');
const serveTask = require('@wok-cli/task-serve');
const webpackTask = require('@wok-cli/task-webpack');

const webpack = $.task(webpackTask, {
  entry: { main: './src/main.js' },
  outputFolder: 'public',
});

const serve = $.task(serveTask, {
  baseDir: ['public'],
});

const reload = serve.reload();

webpack.tap('completed:watch', 'reload', (stats) => {
  if (!stats.hasErrors()) {
    reload();
  }
  return stats;
});

export.serve = $.series(serve, webpack.watch)

Running gulp serve will run a local server watching for file changes and reloading the page after each compilation.

Usage with webpack-dev-middleware

To setup a webpack development middleware, you can use the middleware method. This will return an instance of webpack-dev-middleware to be used in any express-like application.

For example here is an example implementation with the @wok-cli/server task that uses webpack-dev-middleware in development mode:

const $ = require('@wok-cli/core');
const serveTask = require('@wok-cli/task-serve');
const webpackTask = require('@wok-cli/task-webpack');

const webpack = $.task(webpackTask, {
  entry: { main: './src/main.js' },
  outputFolder: 'public',
});

const serve = $.task(serveTask, {
  baseDir: ['public'],
});


server.tap('middlewares', 'webpack', (middlewares) => {
  if ($.env.production === false) {
    middlewares.set('webpack', webpack.middleware());
  }
  return middlewares;
});

export.serve = $.series(serve)

Live Reload and Hot Module Replacement

The above configuration will serve the bundled application via webpack-dev-middleware but to see the changes you still need to refresh manually the page.

To automate the process you can again leverage the built-in reload task of @wok-cli/task-serve attaching a function to the completed:middleware hook:

const $ = require('@wok-cli/core');
const serveTask = require('@wok-cli/task-serve');
const webpackTask = require('@wok-cli/task-webpack');

// ...

const reload = serve.reload();

webpack.tap('completed:middleware', 'reload', (stats) => {
  if (!stats.hasErrors()) {
    reload();
  }
  return stats;
});

server.tap('middlewares', 'webpack', (middlewares) => {
  if ($.env.production === false) {
    middlewares.set('webpack', webpack.middleware());
  }
  return middlewares;
});

export.serve = $.series(serve)

Another popular reload option is Hot Module Replacement. You can enable it by passing an options object with hot: true to the middleware method:

// ...

server.tap('middlewares', 'webpack', (middlewares) => {
  if ($.env.production === false) {
    middlewares.set('webpack', webpack.middleware({ hot: true }));
  }
  return middlewares;
});

export.serve = $.series(serve)

Shorthand signature

For your convenience, the task exposes an utility method asServeMiddleware to quickly integrate it with @wok-cli/task-serve:

// ...
const webpack = $.task(webpackTask, {
  entry: { main: './src/main.js' },
  outputFolder: 'public',
});

const serve = $.task(serveTask, {
  baseDir: ['public'],
});

// run `webpack-dev-middleware`
// and reload on changes
webpack.asServeMiddleware(serve);

// run `webpack-dev-middleware`
// and trigger HMR on changes
webpack.asServeMiddleware(serve, { hot: true });

Babel integration

To setup babel-loader use the config:chain hook:

// ...
const webpack = $.task(webpackTask, {
  entry: { main: './src/main.js' },
  outputFolder: 'public',
});

webpack.tap('config:chain', 'babel', (config) => {
  config.module
    .rule('js')
    .test(/\.m?js$/)
    .use('babel')
    .loader('babel-loader');
  return config;
});