0.4.0 • Published 11 months ago

@cdoublev/webpack-parts v0.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
11 months ago

Webpack parts

  1. About
  2. Installation
  3. Configuration

About

@cdoublev/wepback-parts simplifies creating common Webpack configurations.

It exposes functions taking an optional object of a minimal set of parameters with sane default values, returning one or more parts of a Webpack configuration (to merge with webpack-merge).

Example (webpack.config.js)

  const HtmlWebpackPlugin = require('html-webpack-plugin')
  const { merge } = require('webpack-merge')
  const parts = require('@cdoublev/webpack-parts')

  module.exports = merge(
    { mode: 'development' },
    parts.extractFiles(),
    parts.extractCss(),
    parts.loadJs(),
    parts.serve())

Installation

  npm i @cdoublev/webpack-parts

@cdoublev/webpack-parts can be safely used with Webpack v5 in the active LTS version of NodeJS.

Configuration

buildRender

OptionDefault
filename'render.js'
mode'production'

buildRender() bundles a JavaScript entry point and imported modules without CSS assets. This entry point should be located in a server directory at the root of the project.

It is intented for bundling a server or static render script.

externalize

OptionDefault
keep[]

externalize() excludes all native and installed modules from the bundle but keep the given module names.

It is intended for bundling a server application that will import externalized modules with require().

extractCss

OptionTypeDefault
chunkFilename'[name]-[contenthash].[ext]'
excludeundefined
filename'[name]-[contenthash].[ext]'
includeundefined
modules{ localIdentName: '[name]_[local]_[hash:base64:5]' }

extractCss() resolves CSS files imported in JavaScript files, transpiles their PostCSS syntax and features, generates locally scoped CSS class names (CSS modules), and emits the corresponding files into the output directory.

Note: hot module replacement requires not defining filename and chunkFilename using [hash], [chunkhash], or [contenthash].

extractFiles

OptionTypeDefault
filenameString'[hash][ext][query]'

extractFiles() resolves multimedia files (images, fonts, pdf) imported from other files, and creates a copy in the output directory.

hotModuleReload

hotModuleReload() enables hot module replacement.

It is automatically enabled with serve() (webpack-dev-server) therefore it is intented to be used in the client configuration of a server side rendered application.

loadJs

OptionDefault
includeundefined
excludeundefined

loadJs() forwards the JavaScript entry points and imported modules to babel, so that it transpiles the syntax and polyfills the interfaces.

loadJs() also gathers common modules into a single common.js file, and extracts the Webpack runtime into a separate runtime.js file.

serve

OptionDefault
domainhttps://webpack.js.org/configuration/dev-server/#websocketurl (hostname)'localhost'
port8080
watchwatch

serve() runs an HTTP server serving the bundled application loaded in src/index.html with hot module replacement enabled.

watch

OptionDefault
poll200
aggregateTimeout200
ignored/node_modules/

watch() enables files watching and automatic recompilation on change.