35
loading...
This website collects cookies to deliver better user experience
src/index.js
and will be bundled to dist/main.js
. This is very convenient and promotes convention over configuration but it does not use webpack's full potential.WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value.
Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/
webpack --help
. This command will show you a list of arguments and how to use them. The following execution mimics the default (zero config) behaviour of webpack:webpack --entry=./src/index.js --output-path=./dist --output-filename=main.js
webpack ./src/index.js -o ./dist
webpack.config.js
is being used. You can create it by using the npx webpack init
command or by writing it yourself:const path = require("path");
const config = {
entry: "./src/index.js",
mode: "development",
module: {
rules: [
{
exclude: /(node_modules)/,
test: /\.(js|jsx)$/i,
loader: "babel-loader"
}
]
},
output: {
path: path.resolve(__dirname, "dist")
},
plugins: []
};
module.exports = config;
require
and module.exports
. Make sure that your package.json
does not define "type": "module"
, otherwise you will receive the following error:[webpack-cli] ReferenceError: require is not defined
package.json
file specifies "type": "module"
and you want to make use of ECMAScript modules, then you can also modernize your webpack configuration:import path from "path";
const config = {
entry: "./src/index.js",
mode: "development",
module: {
rules: [
{
exclude: /(node_modules)/,
test: /\.(js|jsx)$/i,
loader: "babel-loader"
}
]
},
output: {
path: path.resolve("./dist")
},
plugins: []
};
export default config;
.ts
does not correspond to the standard .js
extension, webpack has to be informed about this via the --config
argument:webpack --config webpack.config.ts
import path from "path";
import { Configuration } from "webpack";
const config: Configuration = {
entry: "./src/index.js",
mode: "development",
module: {
rules: [
{
exclude: /(node_modules)/,
test: /\.[tj]sx?$/,
loader: "babel-loader"
}
]
},
output: {
path: path.resolve(__dirname, "./dist")
},
plugins: [],
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx"]
}
};
export default config;
webpack-cli
, webpack also supports a programmatic interface. This allows you to compile your frontend code on a Node.js server. Here is an example:import express from "express";
import { webpack } from "webpack";
import webpackConfig, { webappDir } from "../webpack.config.js";
export function useWebpack(app: express.Express) {
const webpackCompiler = webpack(webpackConfig);
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");
app.use(webpackDevMiddleware(webpackCompiler));
app.use(webpackHotMiddleware(webpackCompiler));
app.use(express.static(webappDir));
}
webpack.config.js
file, you can also pass a configuration object to the webpack
API.