Skip to main content

Webpack configuration

Webpack is Packer's stable bundler path. Use Packer.webpack.createApplicationConfiguration(options) from webpack.config.js.

const path = require('path');
const Packer = require('@ekz/packer');

module.exports = Packer.webpack.createApplicationConfiguration({
entry: {
'my-app': './src/index.tsx'
},
output: {
path: 'dist',
publicPath: '/my-app/'
},
resolve: {
alias: {
react: path.resolve(__dirname, 'node_modules/react'),
'react-dom': path.resolve(__dirname, 'node_modules/react-dom')
}
},
devServer: {
proxy: [{
context: ['/'],
target: 'http://localhost:8080',
changeOrigin: true
}]
}
});

Defaults

OptionDefault
Entry{ main: './src/index.js' }
Output pathdist
Public path/
Dev server port9000
Dev server hot reloadtrue
Dev server compressiontrue
Dev server headersAccess-Control-Allow-Origin: *
Asset pathsassets/js/, assets/css/, assets/static/
Production filenamesContent hash when --mode=production

Common options

OptionPurpose
entryWebpack entry object or value. Use this for TSX apps, multi-entry apps, or non-default entry files.
output.pathOutput directory relative to the app root.
output.publicPathPublic URL prefix for emitted assets.
assetPaths.jsDirectory prefix for emitted JS files.
assetPaths.cssDirectory prefix for emitted CSS files.
assetPaths.staticDirectory prefix for emitted images, fonts, SVG, and other static assets.
useHashInFileNamesEnables content hashes in production filenames. Set to false for predictable filenames.
htmlOptions passed to html-webpack-plugin. Set to null to disable HTML generation.
resolveMerged into Webpack resolve. Packer always adds @root./src unless you override it.
devServerMerged into webpack-dev-server config.
module.exports = Packer.webpack.createApplicationConfiguration({
entry: {
main: './src/index.tsx',
admin: './src/admin.tsx'
},
output: {
path: 'build',
publicPath: '/my-app/'
},
assetPaths: {
js: 'static/js/',
css: 'static/css/',
static: 'static/media/'
},
html: {
template: 'public/index.html',
filename: 'index.html'
}
});

Development server

devServer is merged with Packer's defaults: port 9000, hot reload, compression, and Access-Control-Allow-Origin: *.

module.exports = Packer.webpack.createApplicationConfiguration({
devServer: {
port: 3000,
proxy: [{
context: ['/api'],
target: 'http://localhost:8080',
changeOrigin: true
}]
}
});

TypeScript config

Packer looks for tsconfig.json in the application root. When found, it enables ts-loader and ForkTsChecker.

If your tsconfig.json lives elsewhere, set tsconfigPath:

module.exports = Packer.webpack.createApplicationConfiguration({
tsconfigPath: 'src/main/web/tsconfig.json'
});

Without a discoverable TypeScript config, .tsx files fall through to Babel, which can emit jsxDEV and crash in production.

Babel and file extensions

Packer uses Babel for JS/JSX, and for TS/TSX when no TypeScript config is found.

OptionPurpose
babelEnvTargetsTargets passed to @babel/preset-env. Defaults to > 0.25%, not dead.
babelPresetsAdditional Babel presets appended after Packer defaults.
babelPluginsAdditional Babel plugins appended after Packer defaults.
babelOptionsExtra babel-loader options merged over Packer defaults.
fileExtensionsExtensions Webpack resolves. Defaults to .js, .jsx, .ts, .tsx.
module.exports = Packer.webpack.createApplicationConfiguration({
babelEnvTargets: {
browsers: ['last 2 Chrome versions']
},
babelPlugins: ['babel-plugin-example'],
fileExtensions: ['.web.tsx', '.tsx', '.ts', '.jsx', '.js']
});

Plugins, loaders, and globals

Use these escape hatches when the built-in rules are not enough.

OptionPurpose
pluginsAdditional Webpack plugins appended after Packer's plugins.
loadersAdditional module rules appended after Packer's loader rules.
provideValues passed to Webpack ProvidePlugin.
defineValues passed to Webpack DefinePlugin.
externalsWebpack externals config.
targetWebpack target. Defaults to web.
nodeWebpack node options.
const webpack = require('webpack');

module.exports = Packer.webpack.createApplicationConfiguration({
define: {
__APP_VERSION__: JSON.stringify('1.0.0')
},
provide: {
process: 'process/browser'
},
plugins: [new webpack.IgnorePlugin({resourceRegExp: /^\.\/locale$/})],
loaders: [{
test: /\.txt$/,
type: 'asset/source'
}]
});

Optimization

OptionPurpose
splitChunksWebpack optimization.splitChunks. Defaults to { chunks: 'all', automaticNameDelimiter: '.' }.
terserOptionsOptions passed to terser-webpack-plugin.
miniCssExtractPluginOptionsOptions merged into mini-css-extract-plugin.
devtoolDevelopment source map setting. Defaults to eval-cheap-module-source-map; production disables devtool.
enableProgressPluginEnables Webpack ProgressPlugin. Defaults to true.
eslintOptions merged into eslint-webpack-plugin.
module.exports = Packer.webpack.createApplicationConfiguration({
splitChunks: false,
terserOptions: {
terserOptions: {
compress: {
drop_console: true
}
}
},
eslint: {
failOnWarning: false
},
enableProgressPlugin: false
});

Library builds

For UMD/library output, use Packer.webpack.createLibraryConfiguration(name, options) instead. Application defaults such as the HTML plugin and split chunks are adjusted for library mode.

module.exports = Packer.webpack.createLibraryConfiguration('MyLibrary', {
entry: {
index: './src/index.ts'
}
});