美文网首页
webpack学习(三)--plugins,entry,outp

webpack学习(三)--plugins,entry,outp

作者: kim_jin | 来源:发表于2019-10-23 10:33 被阅读0次

我们先使用两个插件来看一下插件到底是什么?

html-webpack-plugin

使用:这个插件运行时在打包结束的时候进行的

  • 先在项目中安装插件,npm install html-webpack-plugin -D
  • webpack.config.js中进行相关插件的配置
const path = require('path'); // node 中的内置模块路径
const HtmlWebpackPlugins = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.js', // 打包的入口文件
  plugins: [
    new HtmlWebpackPlugins({
      template: 'src/index.html;' // 当打包的时候生成文件的时候,按照template配置的html进行生成
    }),
  ],
  output: {
    filename: 'bindle.js', // 打包后输出的文件的名字
    path: path.resolve(__dirname, 'dist') // 绝对路径,__dirname是webpack.config.js所在的路径的位置,输出的文件放在bundle的文件夹下
  }
};

html-webpack-plugin作用:会在打包结束后,自动生成一个html文件,并将打包生成的js自动引入到html中,当我们配置了template参数,会有一点区别,在打包生成html的时候,会根据指定的模板进行生成,并将打包生成的bindle.js注入到html文件中

clean-webpack-plugin

const path = require('path'); // node 中的内置模块路径
const HtmlWebpackPlugins = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.js', // 打包的入口文件
  plugins: [
    new HtmlWebpackPlugins({
      template: 'src/index.html;' // 当打包的时候生成文件的时候,按照template配置的html进行生成
    }),
  ],
  output: {
    filename: 'bindle.js', // 打包后输出的文件的名字
    path: path.resolve(__dirname, 'dist') // 绝对路径,__dirname是webpack.config.js所在的路径的位置,输出的文件放在bundle的文件夹下
  }
};

这个插件的作用就是在再次打包的时候dist文件夹中的内容清空

插件:在webpack运行到某个时刻的时候,帮我们做一些事情

Entry 和output

基础
const path = require('path'); // node 中的内置模块路径
const HtmlWebpackPlugins = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: './src/index.js', // 打包的入口文件
  plugins: [
    new HtmlWebpackPlugins({
      template: 'src/index.html;' // 当打包的时候生成文件的时候,按照template配置的html进行生成
    }),
  ],
  output: {
    filename: 'bindle.js', // 打包后输出的文件的名字
    path: path.resolve(__dirname, 'dist') // 绝对路径,__dirname是webpack.config.js所在的路径的位置,输出的文件放在bundle的文件夹下
  }
};

这是最近简单的打包,打包成一个简单的文件,并引入到html

当我们想有两个入口文件的时候
const path = require('path'); // node 中的内置模块路径
const HtmlWebpackPlugins = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

module.exports = {
  mode: 'development',
  entry: {
    main: 'src/index.js',
    sub: 'src/index.js'
  },
 
  plugins: [
    new HtmlWebpackPlugins({
      template: 'src/index.html;'
    }),
    new CleanWebpackPlugin(['dist '])
  ],
  output: {
    filename: '[name].js', // 打包后输出的文件的名字,占位符
    path: path.resolve(__dirname, 'dist') // 绝对路径,__dirname是webpack.config.js所在的路径的位置,输出的文件放在bundle的文件夹下
  }
};

执行打包操作的时候,会将main.jssub.js注入到html文件中

我们期望在注入html的时候,在js文件前添加一个地址

  output: {
    publicPath:'http://www.baidu.com',
    filename: '[name].js', // 打包后输出的文件的名字,占位符
    path: path.resolve(__dirname, 'dist') // 绝对路径,__dirname是webpack.config.js所在的路径的位置,输出的文件放在bundle的文件夹下
  }

相关文章

  • webpack学习(三)--plugins,entry,outp

    我们先使用两个插件来看一下插件到底是什么? html-webpack-plugin 使用:这个插件运行时在打包结束...

  • Webpack打包工具

    webpack的核心概念:Entry,Output,Loaders,Plugins。webpack的默认配置文件是...

  • webpack(3.0)打包工具简介

    webpack的核心概念:Entry,Output,Loaders,Plugins。webpack的默认配置文件是...

  • 【webpack】webpack配置的 entry 和 outp

    entry :页面中的入口文件。也就是打包从哪个文件开始。 它有三种类型的值:字符串、数组、对象 字符串:指定从这...

  • webpack 浅谈系列之 Loader

    webpack 拥有六大核心部分:Entry、Output、Loaders、Plugins、Mode、Browse...

  • test2

    webpack的配置及基本运行原理 能分清loader和plugins区别 核心概念:entry,output,l...

  • test

    webpack的配置及基本运行原理 能分清loader和plugins区别 核心概念:entry,output,l...

  • Webpack之基础篇

    核心概念 entry、output、loader、plugins、mode entry entry含义:用来指定打...

  • Webpack相关

    一、概念 1、基本组成:entry、output、loader、plugins entry: 入口起点(entry...

  • webpack learning note

    核心概念 入口(entry)、输出(output)、loader、插件(plugins) 配置 entry 指定入...

网友评论

      本文标题:webpack学习(三)--plugins,entry,outp

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