• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

原生Webpack的html-webpack-plugin配置js,css

武飞扬头像
Meta.Qing
帮助3

原生webpack如何从外部引入js和css呢?

index.html代码

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.  
    <title><%= htmlWebpackPlugin.options.title %></title>
  8.  
    <% for (var i in htmlWebpackPlugin.options.css) { %>
  9.  
    <link rel="stylesheet" type="text/css" href="<%= htmlWebpackPlugin.options.css[i] %>">
  10.  
     
  11.  
    <% } %>
  12.  
    </head>
  13.  
    <body>
  14.  
    <div id="#app"></div>
  15.  
    <% for (var i in htmlWebpackPlugin.options.js) { %>
  16.  
    <script type="text/javascript" src="<%= htmlWebpackPlugin.options.js[i] %>"></script>
  17.  
    <% } %>
  18.  
    </body>
  19.  
    </html>

webpack.config.js 配置

  1.  
    const path = require("path");
  2.  
    var HtmlWebpackPlugin = require("html-webpack-plugin");
  3.  
    const webpack = require("webpack");
  4.  
    module.exports = {
  5.  
    entry: "./src/index.js",
  6.  
    mode: "production", //根据环境自行配置
  7.  
    output: {
  8.  
    filename: "bundle.js",
  9.  
    path: path.resolve(__dirname, "dist"),
  10.  
    },
  11.  
    devServer: {
  12.  
    // 热部署
  13.  
    hot: true,
  14.  
    },
  15.  
    plugins: [
  16.  
    new HtmlWebpackPlugin({
  17.  
    title: "Meat.Qing",
  18.  
    template: "index.html",
  19.  
    inject: true, //注入关键
  20.  
    js: ["https://unpkg.com/react@17.0.1/umd/react.production.min.js"], //从外部导入js
  21.  
    css: ["https://unpkg.com/antd@4.14.0/dist/antd.min.css"], //从外部导入css
  22.  
    }),
  23.  
    new webpack.HotModuleReplacementPlugin(),
  24.  
    ],
  25.  
    };

 demo代码链接:https://download.csdn.net/download/qq_20173195/86511769

解压之后执行

  1.  
    npm install
  2.  
    npm run start

到dist目录 右键浏览器打开index.html即可,

F12打开浏览器控制台,查看元素面板即可看到效果

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfeiicj
系列文章
更多 icon
同类精品
更多 icon
继续加载