简体   繁体   English

如何设置Webpack自定义输入和输出

[英]How to set up webpack custom entry and output

我正在学习webpack4。有没有一种方法可以设置自定义条目和输出,因为似乎没有教程可以解释这一点。

You can try the following link: 您可以尝试以下链接:

https://www.valentinog.com/blog/webpack-4-tutorial/#webpack_4_overriding_the_defaults_entryoutput https://www.valentinog.com/blog/webpack-4-tutorial/#webpack_4_overriding_the_defaults_entryoutput

Quote from the link: 从链接引用:

I love webpack 4 zero conf but how about overriding the default entry point? 我喜欢webpack 4零配置,但是如何覆盖默认入口点呢? And the default output? 和默认输出?

Configure them in package.json! 在package.json中配置它们!

An example from the link: 链接中的示例:

"scripts": {
  "dev": "webpack --mode development ./foo/src/js/index.js --output ./foo/main.js",
  "build": "webpack --mode production ./foo/src/js/index.js --output ./foo/main.js"
}

Hope this helps. 希望这可以帮助。

You can configure entry and output in webpack.config.js . 您可以在webpack.config.js中配置条目和输出。 In example configuration below I have added entry as a string whereas you can add as an object and array as well and in a similar way you can add output key as a string, as follows: 在下面的示例配置中,我将条目添加为string而您也可以将其添加为objectarray ,并且可以类似的方式将输出键添加为字符串,如下所示:

var path = require('path');

module.exports = {
    resolve: {
      extensions: ['.js', '.jsx']
    },
    mode: 'development',
    entry: './app/main.js',
    cache: true,
    output: {
        path: __dirname,
        filename: './resources/script.js'
    },
    module: {
        rules: [
            {
                test: path.join(__dirname, '.'),
                exclude: /(node_modules)/,
                loader: 'babel-loader',
                query: {
                    cacheDirectory: true,
                    presets: ['es2015', 'react']
                }
            }
        ]
    }
};

Below is the package.json to work with npm dependencies: 以下是可与npm依赖项一起使用的package.json

{
  "name": "reactjs",
  "version": "1.0.0",
  "description": "React Js",
  "keywords": [
    "react"
  ],
  "author": "Arpit Aggarwal",
  "dependencies": {
    "axios": "^0.18.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-router-dom": "^4.2.2",
    "webpack": "^4.2.0",
    "webpack-cli": "^2.0.9",
    "lodash": "^4.17.5"
  },
  "scripts": {
    "build": "webpack",
    "watch": "webpack --watch -d"
  },
  "devDependencies": {
    "babel-core": "^6.18.2",
    "babel-loader": "^7.1.4",
    "babel-polyfill": "^6.16.0",
    "babel-preset-es2015": "^6.18.0",
    "babel-preset-react": "^6.16.0"
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM