2023-01-11 webpack 在package.json中type="module"模式下的配置

// 主要是__dirname, __filename字段的获取
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

import HtmlWebpackPlugin from 'html-webpack-plugin';

export default {
  target: "web",
  entry: "./src/index.js",
  output: {
    path: path.join(__dirname, "/dist"),
    publicPath: "/",
    filename: "index.js",
  },
  resolve: {
    extensions: [".ts", ".tsx", ".json", ".js"],
  },
  module: {
    rules: [],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(__dirname, "/public/index.html"),
      scriptLoading: "blocking",
      inject: "head",
    }),
  ],
  devServer: {
    static: {
      // https://webpack.docschina.org/configuration/dev-server/#directory
      // 告诉服务器从哪里提供内容。只有在你希望提供静态文件时才需要这样做。static.publicPath 将会被用来决定应该从哪里提供 bundle,并具有优先级。
      directory: path.join(__dirname, "public"),
      // 告诉服务器在哪个 URL 上提供 static.directory 的内容。 (静态资源)
      // 配置了下面的内容的话, 可以访问: http://localhost:8080/dist/webpack.html  拿到的会是没有注入代码的原生静态代码
      publicPath: "/dist",
    },
    hot: true,
    port: "8080",
    open: true,
    proxy: {
      "/api": {
        target: "",
        changeOrigin: true,
        pathRewrite: {
          "^/api": "",
        },
      },
    },
  },
};

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容