Olá pessoal, tudo certo?
Depois de ter feito o curso da Alura sobre webpack, decidi ler a documentação. Cheguei em um ponto do qual ensina como injetar uma variável para ser acessada em runtime.
plugins: [
new webpack.DefinePlugin({
SERVICE_URL: JSON.stringify(url),
}),
],
Porém na hora de dar um npm start
aparece o seguinte erro webpack.DefinePlugin is not a constructor
Para tornar o projeto disponível no navegador, estou usando o webpack-dev-server
. Vou adicionar aqui meu package.json e webpack.config.js
PACKAGE.JSON
{
"name": "learning-webpack",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack --config webpack.config.js",
"build": "webpack --env production && webpack --env development",
"build-prod": "webpack --env production",
"build-dev": "webpack --env development",
"watch": "webpack --watch",
"start": "webpack serve --open",
"server": "node server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"express": "^4.17.1",
"html-webpack-plugin": "^5.3.1",
"webpack": "^5.26.0",
"webpack-cli": "^4.5.0",
"webpack-dev-middleware": "^4.1.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"lodash": "^4.17.21"
}
}
WEBPACK.CONFIG.JS
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { webpack } = require('webpack');
module.exports = (env) => {
const folderName = env.production ? 'build-prod' : 'build-dev';
const url = env.production ? 'www.prod.com' : 'www.dev.com';
return {
mode: 'development',
entry: {
index: './src/index.js',
print: './src/print.js',
},
devtool: 'inline-source-map',//To show error bundles
devServer: {
contentBase: './dist',//the location that devServer should look for
},
plugins: [
new webpack.DefinePlugin({
SERVICE_URL: JSON.stringify(url),
}),
new HtmlWebpackPlugin({title: 'Caching'}),
],
output: {
filename: '[name].[contenthash].bundle.js',
path: path.resolve(__dirname, folderName),
clean: true, //It cleans '/dist' folder before a new build
publicPath: '/', //The publicPath will be used within our server script as well in order to make sure files are served correctly on http://localhost:3000. The next step is setting up our custom express server
},
optimization: {
moduleIds: 'deterministic',//despite any new local dependencies, our vendor hash should stay consistent between builds:
runtimeChunk: 'single', //to create a single runtime bundle for all chunks
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendors',
chunks: 'all'
},
},
},
},
}
}
Procurei soluções na internet, mas não tive muito sucesso.
Agradeço desde já qualquer ajuda ou comentário!