Webpack ha sh in js - Очистка кеша - PullRequest
0 голосов
/ 19 июня 2020

Я хотел бы добавить ha sh к имени моего js файла, чтобы выполнить очистку кеша. Я прочитал много статей о inte rnet, но не знаю, куда добавить элемент [hash]. Не уверен, с чего начать.

Это ниже моего файла webpack.config.

const devCerts = require("office-addin-dev-certs");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const fs = require("fs");
const webpack = require("webpack");

module.exports = async (env, options) => {
  const dev = options.mode === "development";
  const config = {
    devtool: "source-map",
    entry: {
      polyfill: "@babel/polyfill",
      jquery: "jquery/src/jquery",
      taskpane: "./src/taskpane.ts",
      sentry: "@sentry/browser"
    },
    resolve: {
      extensions: [".ts", ".tsx", ".html", ".js"]
    },
    module: {
      rules: [
        {
          test: /\.ts$/,
          exclude: /node_modules/,
          use: "babel-loader"
        },
        {
          test: /\.tsx?$/,
          exclude: /node_modules/,
          use: "ts-loader"
        },
        {
          test: /\.html$/,
          exclude: /node_modules/,
          use: "html-loader"
        },
        {
          test: /\.(png|jpg|jpeg|gif)$/,
          use: "file-loader"
        }
      ]
    },
    plugins: [
      new CleanWebpackPlugin(),
      new HtmlWebpackPlugin({
        filename: "taskpane.html",
        template: "./src/taskpane.html",
        chunks: ["polyfill", "jquery", "taskpane", "sentry"]
      }),
      new CopyWebpackPlugin([
        {
          to: "taskpane.css",
          from: "./src/taskpane.css"
        }
      ]),
      new CopyWebpackPlugin([
        {
          to: "assets",
          from: "./assets"
        }
      ]),
      new webpack.ProvidePlugin({
        $: "jquery",
        jQuery: "jquery"
      })
    ],
    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 Ответ

1 голос
/ 19 июня 2020

Добавьте это после entry: { ... },

    output: {
      filename: '[name].[hash].js',  // or better [contenthash]
      path: path.resolve(__dirname, 'dist'),
    },
    optimization: {
      runtimeChunk: 'single',
    },

Предполагая, что вы хотите, чтобы Webpack создавал поддиректорию ./dist.

...