美文网首页
Webpack v4 笔记

Webpack v4 笔记

作者: yingjieg | 来源:发表于2019-06-24 14:00 被阅读0次

    At its core, webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles.
    本质上,webpack是针对现代JavaScript程序的静态模块打包工具。当webpack处理你的app时,内部会根据你项目中所有模块的需要,生成一个或多个依赖关系图。

    Entry

    An entry point indicates which module webpack should use to begin building out its internal dependency graph. webpack will figure out which other modules and libraries that entry point depends on (directly and indirectly).

    By default its value is ./src/index.js, but you can specify a different (or multiple entry points) by configuring the entry property in the webpack configuration. For example:

    入口点指定了webpack用哪个模块来发起内部依赖的构建。webpack会分析出入口点所依赖的模块和库。

    module.exports = {
      entry: './path/to/my/entry/file.js'
    };
    

    Output

    The output property tells webpack where to emit the bundles it creates and how to name these files. It defaults to ./dist/main.js for the main output file and to the ./dist folder for any other generated file.

    output属性告诉webpack提交打包后的文件去指定路径。

    const path = require('path');
    
    module.exports = {
      entry: './path/to/my/entry/file.js',
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'my-first-webpack.bundle.js'
      }
    };
    

    Loaders

    Out of the box, webpack only understands JavaScript and JSON files. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.

    At a high level, loaders have two properties in your webpack on configuration:

    1. The test property identifies which file or files should be transformed.
    2. The use property indicates which loader should be used to do the transforming.

    webpack只能处理JavaScript与JSON文件。Loaders允许webpack处理其它类型的文件并将它们转换为可以被理解的模块。
    test 属性指定了哪些文件可以被转换。
    use 属性指定了转换时所需要的loader。

    const path = require('path');
    
    module.exports = {
      output: {
        filename: 'my-first-webpack.bundle.js'
      },
      module: {
        rules: [
          { test: /\.txt$/, use: 'raw-loader' }
        ]
      }
    };
    

    The configuration above has defined a rules property for a single module with two required properties: test and use. This tells webpack's compiler the following:

    "Hey webpack compiler, when you come across a path that resolves to a '.txt' file inside of a require() / import statement, use the raw-loader to transform it before you add it to the bundle."

    Plugins

    While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management and injection of environment variables.

    const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
    const webpack = require('webpack'); //to access built-in plugins
    
    module.exports = {
      module: {
        rules: [
          { test: /\.txt$/, use: 'raw-loader' }
        ]
      },
      plugins: [
        new HtmlWebpackPlugin({template: './src/index.html'})
      ]
    };
    

    Mode

    By setting the mode parameter to either development, production or none, you can enable webpack's built-in optimizations that correspond to each environment. The default value is production.

    module.exports = {
      mode: 'production'
    };
    

    相关文章

      网友评论

          本文标题:Webpack v4 笔记

          本文链接:https://www.haomeiwen.com/subject/atvnqctx.html