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

@wildpeaks/webpack-config-node

v5.16.0

Published

Webpack 4 base config generator for bundling Node apps

Downloads

27

Readme

Webpack Config: Node

Github Release

Generates a Webpack 4 config for Node applications written in Typescript.


Example

package.json:

"scripts": {

	// Build for production mode
	"build": "webpack --mode production"

},
"dependencies": {

	// This package
	"@wildpeaks/webpack-config-node": "...",

	// Peer dependencies
	"typescript": "...",
	"webpack": "...",
	"webpack-cli": "..."
}

webpack.config.js:

'use strict';
const {join} = require('path');
const getConfig = require('@wildpeaks/webpack-config-node');

module.exports = function(_env, {mode = 'production'} = {}) {
	return getConfig({
		mode,
		entry: {
			myapp: './src/myapp.ts'
		},
		rootFolder: __dirname,
		outputFolder: join(__dirname, 'dist')
	});
};

Options


mode: String

Default: production.

Use production to optimize the output (JS files are minified).

See Mode in the Webpack documentation.


entry: Object

Webpack entries.

Default: {}

Example:

{
	app1: './src/entry1.ts',
	app2: './src/entry2.ts',
	app3: './src/entry3.ts'
}

See Entry Points in the Webpack documentation.


jsFilename: String

Custom filename for JS bundles.

By default, files are named [name].js (development mode) or [hash].[name].js (production mode).

For example, this generates assets/scripts/app1.js:

{
	"entry": {
		"app1": "./src/example.ts"
	},
	"jsFilename": "assets/scripts/[name].js"
}

jsChunkFilename: String

Custom filename for JS chunks.

By default, files are named chunk.[id].js (development mode) or [hash].chunk.[id].js (production mode).

For example, this generates assets/scripts/chunk.0.js:

{
	"jsChunkFilename": "assets/scripts/chunk.[id].js"
}

rootFolder: String

Absolute path to the root folder context. Defaults to the process current working directory.

Examples: "C:/Example" or /usr/share/www/example

See context in the Webpack documentation.


outputFolder: String

Absolute path to the output folder, where files are emitted.

Defaults to subfolder dist in rootFolder.

Example: "C:/Example/dist" or /usr/share/www/example/dist

See output.path in the Webpack documentation.


publicPath: String

Path prepended to url references.

Default: "/"

Example: "/mysite/"

See publicPath in the Webpack documentation.


copyPatterns: Object[]

Additional files & folders to copy as-is, despite not being referenced by import or require.

Default: [].

Examples:

// Copy a directory:
// "models/example.ext" is copied to "assets/example.ext"
{from: 'models', to: 'assets'}

// Copy specific files:
// "extras/models/example.gltf" is copied to "assets/extras/models/example.gltf"
{from: 'extras/**/*.gltf', to: 'assets'}

// Copy specific files:
// "extras/models/example.gltf" is copied to "assets/models/example.gltf"
{from: '**/*.gltf', to: 'assets', context: 'extras'}

// Ignore some files
{from: 'textures', to: 'assets', ignore: ['Thumbs.db']}

See patterns in the copy-webpack-plugin documentation.


sourcemaps: Boolean

Use true to generate sourcemaps for scripts, false to skip them.

Default: true


skipHashes: Boolean

If true, mode "production" won't add hashes in filenames.

Default: false


skipReset: Boolean

If true, it will not empty the output folder at the start. This is useful if you have multiple configs at the same time and are emptying the output folder before starting Webpack.

Default: false.


Exclude specific modules

Target node already excludes native modules like fs from the bundles, meaning the output script will contain require("fs") calls.

You can exclude additional modules using externals:

'use strict';
const {join} = require('path');
const getConfig = require('@wildpeaks/webpack-config-node');

module.exports = function(_env, {mode = 'production'} = {}) {
	const config = getConfig({
		mode,
		entry: {
			myapp: './src/myapp.ts'
		},
		rootFolder: __dirname,
		outputFolder: join(__dirname, 'dist')
	});

	// This generates `require("mymodule")` in the output
	// instead of copying the source of mymodule in the bundle.
	config.externals = {
		mymodule: "commonjs mymodule"
	};

	return config;
};

You could also use package webpack-node-externals to exclude the entire node_modules folder, but it's best to bundle as much of the dependencies as possible to optimize for runtime.


Babel

Note that it intentionally doesn't use Babel because Typescript itself can already take care of transpiling to ES2017 + ES Modules, and Webpack converts the ES Modules. This greatly reduces the number of dependencies and avoids limitations of the Typescript plugin for Babel.