See https://github.com/kalaschnik/multipurpose-webpack-config for my vanialla JS config
- Dev Server (
npm start
) - Top-Level await (through
webpack
, this requires (at least)"target": "es2017", "module": "es2022"
intsconfig.json
) - Single Webpack Config for Dev and Production
- Auto-switch mode to production when using
npm run build
- source-maps-enabled on dev — disabled in prod
- Auto-switch mode to production when using
- static assets sit in /public and get copied to dist on build (CRA style)
- Using HtmlWebpackPlugin with templates for multiple entry points and outputs
- CSS in
<style>
tags to reduce requests - PostCSS support (w/ native CSS nesting enabled)
- Markdown support using
remark-html
- Inline SVG support (using
asset/source
) - Image support (using
asset/resource
)
Show
Note that this is only the initial setup I started with. Check out the project’s config files for the current setup!
- Init npm
npm init -y
- Install dependencies
webpack
core bundlerwebpack-cli
run webpack commandswebpack-dev-server
liver server watching file changestypescript
install specific ts version for current versionts-loader
tells webpack how to convert ts to js
npm i -D webpack webpack-cli webpack-dev-server typescript ts-loader
- Create project structure
mkdir src && touch src/index.ts && touch webpack.config.js && tsc --init
-
Set your desired JS
target
intsconfig.json
whichwebpack
will respect -
Set
// "outDir": "./",
to"outDir": "./dist/",
-
Add the following to your
webpack.config.js
(from the docs)
const path = require('path');
const path = require('path');
module.exports = {
entry: './src/index.ts',
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
devtool: 'source-map',
};
-
Enable source-maps (for debugging) in
tsconfig.json
:"sourceMap": true,
-
Add an
index.html
in your root pointing to<script src="dist/bundle.js"></script>
-
Setup
scripts
inpackage.json
:
"scripts": {
"start": "webpack serve",
"build": "webpack"
},