Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Webpack-dev-server configure para una biblioteca

Tengo un problema con el servidor de desarrollo de paquetes web porque parece que no puedo probar mi biblioteca porque parece estar vacía en el navegador web. Un ejemplo sencillo:

mi src/index.js

 function test() { console.log("test"); } export { test }

Mi paquete.json

 { "name": "testpackage", "version": "0.0.1", "description": "", "module": "dist/index_bundle.js", "scripts": { "build": "webpack", "start": "webpack serve --open", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "html-webpack-plugin": "^5.3.2", "webpack": "^5.48.0", "webpack-cli": "^4.7.2", "webpack-dev-server": "^3.11.2" }, }

Mi webpack.config.js

 const path = require("path") const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: path.resolve(__dirname, "src/index.js"), output: { path: path.resolve(__dirname, "dist"), filename: "index_bundle.js", library: "$", libraryTarget: "umd", }, devtool: 'source-map', devServer: { contentBase: './dist', port: 9000 }, plugins: [ new HtmlWebpackPlugin({ title: 'Test', }), ], mode: "development", }

Si inicio el servidor de desarrollo con npm run start , aparece la página en blanco. Pero el objeto $ parece estar vacío al intentar acceder a él a través de la consola. Mirando la fuente de la página, el script de compilación parece haber sido generado y colocado cuidadosamente en una etiqueta de script.

Si construyo el paquete y hago una página html con una etiqueta de secuencia de comandos que apunta al archivo creado en la carpeta dist/, el paquete simplemente funciona.

¿Que me estoy perdiendo aqui?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Parece un error de webpack-dev-server . Actualizado a "webpack-dev-server": "^4.0.0" , funciona bien.

contentBase se cambia a la opción static , puede encontrarla en la guía de migración

P.ej

src/index.js

 function test() { console.log("test"); } export { test };

webpack.config.js :

 const path = require("path"); const HtmlWebpackPlugin = require("html-webpack-plugin"); module.exports = { entry: path.resolve(__dirname, "src/index.js"), output: { path: path.resolve(__dirname, "dist"), filename: "index_bundle.js", library: { name: "$", type: "umd", }, }, devtool: "source-map", devServer: { static: "./dist", port: 9000, }, plugins: [new HtmlWebpackPlugin({ title: "Test" })], mode: "development", };

package.json :

 { "name": "68712902", "main": "dist/index_bundle.js", "scripts": { "build": "webpack", "start": "webpack serve --open" }, "devDependencies": { "html-webpack-plugin": "^5.3.2", "webpack": "^5.51.1", "webpack-cli": "^4.8.0", "webpack-dev-server": "^4.0.0" } }

Después de ejecutar npm start , verifique la biblioteca en el navegador:

ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!