Skip to content

ecliptic/webpack-blocks-utils

Repository files navigation

webpack-blocks-utils

Tiny helpers for webpack-blocks.

NPM Version CircleCI JavaScript Style Guide

Version compatibility:

  • webpack-blocks v0.x -> webpack-blocks-utils v1.x (@latest)
  • webpack-blocks v1.x -> webpack-blocks-utils v2.x (@next)

Installation

yarn add --dev webpack-blocks-utils

or

npm install --save-dev webpack-blocks-utils

Usage

setTarget(target)

Sets target

Example usage:

setTarget('electron-renderer')

setNode(node)

Sets node

Example usage:

setNode({
  __dirname: false,
  __filename: false,
})

setExternals(externals)

Sets externals

Example usage:

setExternals(['firebase', 'electron-debug', 'debug'])

envVar(key, value, configSetters)

Applies an array of webpack blocks only if process.env[key] matches the given value.

envVar('ELECTRON', 'true', [
  setTarget('electron-main'),
  setExternals(['firebase', 'electron-debug', 'debug']),
  // ...
]),

noParse(regexes)

Sets module.noParse

Example usage:

noParse([
  // Don't parse localforage because it's pre-built
  new RegExp('node_modules/localforage/dist/localforage.js'),
]),

Copy Plugin

The copy, copyPattern, and copyOptions blocks wrap copy-webpack-plugin.

The most basic way to use the plugin is to import copy and use it to define simple from, to pairs that are added to the list of patterns used when the plugin is instantiated.

import {createConfig, entryPoint, setOutput} from '@webpack-blocks/webpack2'
import babel from '@webpack-blocks/babel6'
import {copy} from 'webpack-blocks-utils'

export default createConfig([
  entryPoint(['babel-polyfill', './src/Main.js']),
  setOutput('./build/bundle.js'),
  babel(),
  copy('assets/robots.txt', 'robots.txt'),
  copy('assets/favicon.ico', 'favicon.ico'),
])

This would result in the following copy plugin config:

new CopyWebpackPlugin([
  {from: 'assets/robots.txt', to: 'robots.txt'},
  {from: 'assets/favicon.ico', to: 'favicon.ico'},
])

Advanced patterns

If you need to use more advanced patterns, described here, use the copyPattern function:

import {createConfig} from '@webpack-blocks/webpack2'
import {copyPattern} from 'webpack-blocks-utils'

export default createConfig([
  copyPattern({
    context: 'from/directory',
    from: '**/*',
    to: '/absolute/path',
  }),
])

Options

If you need to set options, use the copyOptions function:

import {createConfig} from '@webpack-blocks/webpack2'
import {copyOptions} from 'webpack-blocks-utils'

export default createConfig([
  copyOptions({copyUnmodified: true}),
])

See the options list here.

Html Plugin

import {createConfig, entryPoint, setOutput} from '@webpack-blocks/webpack2'
import babel from '@webpack-blocks/babel6'
import html from './src/webpack-block-html'

export default createConfig([
  entryPoint(['babel-polyfill', './src/Main.js']),
  setOutput('./build/bundle.js'),
  babel(),
  html({template: 'assets/index.html'}),
])

Options

See the options list here.

Defaults:

  • filename: 'index.html'
  • template: 'templates/index.html'
  • showErrors: false

License

This project is licensed under MIT.