Outlook 插件 JS- NPM 运行构建丢失的 JS 文件

时间:2021-06-18 19:06:24

标签: npm webpack build babeljs

我正在按照以下教程创建 Outlook 插件项目:- https://docs.microsoft.com/en-us/office/dev/add-ins/quickstarts/outlook-quickstart?tabs=yeomangenerator 在开发过程中,我添加了一些额外的 JavaScript 文件(如 helper.js、settings.js),其中包含一些在本地运行时可以正常工作的常用和辅助函数, 现在,当我运行“npm run build”命令以生成要部署在服务器上的项目的已发布版本时,这些文件丢失了,因此已发布的项目由于缺少功能而无法运行。 下面是我的项目。

project structure

missing helper and setting folder

下面是我的 webpack.config.js 样板代码

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"],
      taskpane: "./src/taskpane/taskpane.js",
      commands: "./src/commands/commands.js",
      landing: "./src/landing/landing.js"
    },
    resolve: {
      extensions: [".html", ".js"]
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader", 
            options: {
              presets: ["@babel/preset-env"]
            }
          }
        },
        {
          test: /\.html$/,
          exclude: /node_modules/,
          use: "html-loader"
        },
        {
          test: /\.(png|jpg|jpeg|gif)$/,
          loader: "file-loader",
          options: {
            name: '[path][name].[ext]',          
          }
        }
      ]
    },
    plugins: [
      new CleanWebpackPlugin(),
      new HtmlWebpackPlugin({
        filename: "taskpane.html",
        template: "./src/taskpane/taskpane.html",
        chunks: ["polyfill", "taskpane"]
      }),
      new CopyWebpackPlugin({
        patterns: [
        {
          to: "taskpane.css",
          from: "./src/taskpane/taskpane.css"
        },
        {
          to: "[name]." + buildType + ".[ext]",
          from: "manifest*.xml",
          transform(content) {
            if (dev) {
              return content;
            } else {
              return content.toString().replace(new RegExp(urlDev, "g"), urlProd);
            }
          }
        }
      ]}),
      new HtmlWebpackPlugin({
        filename: "commands.html",
        template: "./src/commands/commands.html",
        chunks: ["polyfill", "commands"]
      }),
      new HtmlWebpackPlugin({
        filename: "landing.html",
        template: "./src/landing/landing.html",
        chunks: ["polyfill", "dialog"]
      })
    ],
    devServer: {
      headers: {
        "Access-Control-Allow-Origin": "*"
      },      
      https: (options.https !== undefined) ? options.https : await devCerts.getHttpsServerOptions(),
      port: process.env.npm_package_config_dev_server_port || 3000
    }
  };

  return config;
};

你能帮忙吗

1 个答案:

答案 0 :(得分:0)

终于找到解决方案了,我们需要通过

包含自定义/helper js文件
  1. 将函数标记为导出并使其在需要的主文件上需要 &

  2. 然后使用导出的函数和

  3. 一旦我们运行“npm run build”函数就可以作为主文件的一部分使用了 下面是相同的例子

    //子文件夹中的自定义或帮助文件 示例.js 导出函数 sampleFunc() { //一些代码enter code here }

    taskpane.js // 需要用到的主文件
    const samJs = require("./../helpers/sample"); // 无扩展 //调用函数 var 数据 = samJs.sampleFunc