创建js webpack配置

时间:2018-09-03 23:45:48

标签: webpack

如何为js文件正确创建webpack配置文件?

enter image description here

我需要为文件夹src / Components / Auth / js / modules创建Webpack配置文件(内部文件夹具有JS文件)

1 个答案:

答案 0 :(得分:0)

From wikipedia

  

Webpack是一个开源JavaScript模块捆绑器。其主要目的   是捆绑JavaScript文件以便在浏览器中使用。

npm install --save-dev webpack

npm install --save-dev webpack-cli

在项目目录的根目录中创建webpack.config.js文件

webpack.config.js

const path = require('path') // This is common.js

console.log('*** webpack started loading. ***');

/*
   webpack config object takes two parameters.
   1. entry: firstFile which need to be loaded.
   2. output:
      -path: directory name so it can store fully minified version of .js 
             files. need to specifiy fully qualified path.
      -filename: file name of minified file.
*/
  const config = {
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: 'bundle.js',
  },
}

console.log('*** webpack loading completed. ***');

module.exports = config; // this is common.js

现在可以从您的终端机类型'webpack'

运行

有关更多信息,下面是package.json文件。

{
  "name": "hello_webpack",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "webpack": "^4.19.0",
    "webpack-cli": "^3.1.0",
    "path": "^0.12.7"
  },
  "dependencies": {

  }
}

For Basic Example click here