webpack build 引用本地文件系统中的绝对路径

无音阶

我正在使用 office js 为 MS Word 构建插件。他们为您提供了入门套件,我使用他们的 react-starter 构建了它。它包括一个我几乎保持不变的 webpack 配置。我添加了一个 svg-loader 和一个副本。它包含在下面。

我的问题是,在我为生产构建后, dist 文件夹中的一些编译文件指的是我自己的本地计算机文件系统上的绝对路径。是什么导致了这个问题?

[编辑:我创建了一个新的 office js react 插件项目,看看我是否遇到了同样的问题。是的,它仍然指的是绝对路径。]

(dist/taskpane.js)

reactHotLoaderGlobal?reactHotLoaderGlobal.default:void 0;if(e){var n=void 0!==r?r:t;if(n)if("function"!=typeof n){for(var o in n) if(Object.prototype.hasOwnProperty.call(n,o)){var i=void 0;try{i=n[o]}catch(e){continue}e.register(i,o, "/Users/ current_user/app_folder/another_app_folder/src/taskpane/components/Collections/index.ts" )}}else e.register(n,"module.exports", "/Users/current_user/app_folder/another_app_folder/src/taskpane/components/ Collections/index.ts" )}}()},25522:function(e,t,n){"use strict";var o=this&&this.__createBinding||(Object.create?function(e,t,n, r){void 0===r&&(r=n);var o=Object.getOwnPropertyDescriptor(t,n);o&&!("get"in

(webpack.config.js)

/* eslint-disable no-undef */

const devCerts = require("office-addin-dev-certs");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const webpack = require("webpack");

const urlDev = "https://localhost:3002/";
const urlProd = "https://url-to-prod.com/"; 
async function getHttpsOptions() {
  const httpsOptions = await devCerts.getHttpsServerOptions();
  return { cacert: httpsOptions.ca, key: httpsOptions.key, cert: httpsOptions.cert };
}

module.exports = async (env, options) => {
  const dev = options.mode === "development";
  const buildType = dev ? "dev" : "prod";
  const config = {
    devtool: "source-map",
    entry: {
      polyfill: ["core-js/stable", "regenerator-runtime/runtime"],
      vendor: ["react", "react-dom", "core-js", "@fluentui/react"],
      taskpane: ["react-hot-loader/patch", "./src/taskpane/index.tsx"],
      commands: "./src/commands/commands.ts",
    },
    output: {
      devtoolModuleFilenameTemplate: "webpack:///[resource-path]?[loaders]",
      clean: true,
    },
    resolve: {
      extensions: [".ts", ".tsx", ".html", ".js"],
    },
    module: {
      rules: [
        {
          test: /\.ts$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-typescript"],
            },
          },
        },
        {
          test: /\.tsx?$/,
          use: ["react-hot-loader/webpack", "ts-loader"],
          exclude: /node_modules/,
        },
        {
          test: /\.html$/,
          exclude: /node_modules/,
          use: "html-loader",
        },
        {
          test: /\.(png|jpg|jpeg|gif|ico)$/,
          type: "asset/resource",
          generator: {
            filename: "assets/[name][ext][query]",
          },
        },
        {
          test: /\.svg$/,
          use: [
            {
              loader: "svg-url-loader",
              options: {
                limit: 10000,
              },
            },
          ],
        },
        {
          test: /\.(woff(2)?|ttf|eot)(\?v=\d+\.\d+\.\d+)?$/,
          use: [
            {
              loader: "file-loader",
              options: {
                name: "[name].[ext]",
                outputPath: "fonts/",
              },
            },
          ],
        },
      ],
    },
    plugins: [
      new CopyWebpackPlugin({
        patterns: [
          {
            from: "src/web.config",
            to: "src/web.config",
          },
        ],
      }),
      new CopyWebpackPlugin({
        patterns: [
          {
            from: "assets/*",
            to: "assets/[name][ext][query]",
          },
          {
            from: "assets/icons/*",
            to: "assets/icons/[name][ext][query]",
          },
          {
            from: "manifest*.xml",
            to: "[name]." + buildType + "[ext]",
            transform(content) {
              if (dev) {
                return content;
              } else {
                return content
                  .toString()
                  .replace(new RegExp(urlDev, "g"), urlProd);
              }
            },
          },
        ],
      }),
      new HtmlWebpackPlugin({
        filename: "taskpane.html",
        template: "./src/taskpane/taskpane.html",
        chunks: ["taskpane", "vendor", "polyfills"],
      }),
      new HtmlWebpackPlugin({
        filename: "commands.html",
        template: "./src/commands/commands.html",
        chunks: ["commands"],
      }),
      new webpack.ProvidePlugin({
        Promise: ["es6-promise", "Promise"],
      }),
    ],
    devServer: {
      hot: true,
      headers: {
        "Access-Control-Allow-Origin": "*",
      },
      https: env.WEBPACK_BUILD || options.https !== undefined ? options.https : await getHttpsOptions(),
      port: process.env.npm_package_config_dev_server_port || 3002,
    },
  };

  return config;
};

无音阶

通过从“tsx”规则中删除“react-hot-loader/webpack”,它解决了这个问题。绝对路径不再包含在构建中。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Windows 上 build.gradle 中的绝对路径

Webpack 相对路径 vs 绝对路径

在Webpack中建立相对于项目根目录的绝对路径

如何为 npm run build 使用绝对路径

配置webpack使用绝对路径而不是相对路径

在本地文件系统上的HTML中设置根目录的绝对路径

使绝对路径在本地文件中工作

本地文件对象的绝对路径

开玩笑使用Webpack解析时无法检测到绝对路径

-configuration.output.path:提供的值“ public”不是绝对路径!与Webpack

Webpack2 无法解析:config.context 绝对路径

使用React + Webpack时如何使用绝对路径导入自定义scss?

Webpack / Vue / Vuetify / PWA项目中的绝对路径问题

Typescript / Electron / Webpack模块如何/为什么具有绝对路径?

为什么在webpack的输出属性的入口点和绝对路径中使用相对路径?

为什么本地文件中的相对路径被转换为绝对路径?

使用本地绝对路径读取/下载文件

Webpack 4-无法使用url-loader在src中加载具有绝对路径的图像

引用没有绝对路径的文件夹

如何在Hadoop文件系统中获取绝对路径?

可以在CSharpCodeProvider中设置引用的绝对路径吗?

引用rootURL / hbs中的任何绝对路径

Webpack 错误 - 无法解析“tmp/build...”中的加载程序

在vue的简单Webpack模板中,build.js来自何处?

尝试使用 webpack 构建时多 ./src ./build/ 中的错误

在cpio归档文件中创建绝对路径

在Java中绝对路径打开文件

如何在Java中获取文件的绝对路径

获取Eclipse中当前编辑文件的绝对路径