zoukankan      html  css  js  c++  java
  • webpack 原理 实战 (1) 同步加载

    同步加载

    3.1 安装模块

    cnpm i webpack webpack-cli html-webpack-plugin clean-webpack-plugin -D
    

    3.2 webpack.config.js

    const path = require("path");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    const { CleanWebpackPlugin } = require("clean-webpack-plugin");
    module.exports = {
      mode: "development",
      devtool: "source-map",
      entry: "./src/index.js",
      output: {
        path: path.resolve(__dirname, "dist"),
        filename: "main.js",
      },
      module: {},
      plugins: [
        new CleanWebpackPlugin({ cleanOnceBeforeBuildPatterns: ["**/*"] }),
        new HtmlWebpackPlugin({
          template: "./src/index.html",
          filename: "index.html",
        }),
      ],
      devServer: {},
    };
    

    3.2 index.js

    srcindex.js

    let title = require("./title.js");
    console.log(title);
    

    3.3 title.js

    src itle.js

    module.exports = "title";
    

    3.4 index.html

    srcindex.html

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>webpack</title>
      </head>
      <body></body>
    </html>
    

    3.5 package.json

    package.json

      "scripts": {
        "build": "webpack"
      }
    

    3.6 打包文件

    (() => {
      var modules = ({
        "./src/title.js":
          ((module) => {
            module.exports = "title";
          })
      });
      var cache = {};
      function require(moduleId) {
        if (cache[moduleId]) {
          return cache[moduleId].exports;
        }
        var module = cache[moduleId] = {
          exports: {}
        };
        modules[moduleId](module, module.exports, require);
        return module.exports;
      }
      (() => {
        let title = require("./src/title.js");
        console.log(title);
      })();
    })();
    越努力越幸运
  • 相关阅读:
    解决express不是内部或外部命令
    spring ioc认识
    Filter编码过滤
    call、apply、bind
    js面向对象浅析
    由clientWidth到document
    401
    删除页面中Form下面隐藏的ViewStatue
    asp.net 下载
    day98
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/14386085.html
Copyright © 2011-2022 走看看