Webpack如何使用$translatePartialLoader进行缓存破坏以支持angular-translate?

3
"webpack": "^2.7.0"

我正在尝试在我们的翻译文件中添加哈希,以便在部署时进行缓存清除。我已经成功提取了JSON并添加了哈希,将其输出到一个文件夹中,并且一切都很好。
但是,在构建后,我的未哈希JSON仍然位于它们原始文件夹中。我知道我们不需要为JSON添加加载器,因为它已经有处理导入的方法,所以我的问题是如何清除已经处理过的JSON?
我的文件夹结构如下:
src/
   app/
     module-name/
        /translations
         en.json
         fn.json
     module-name/
        /translations
         en.json
         fn.json
     //ect...

我使用CopyWebpackPlugin获取json和hash,可能有我遗漏的选项可以清除处理过的文件?还是我方法错误了。
const webpack = require('webpack');
const conf = require('./gulp.conf');
const path = require('path');

const VersionFile = require('webpack-version-file');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const pkg = require('../package.json');
const autoprefixer = require('autoprefixer');

module.exports = {
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader',
        enforce: 'pre'
      },
      {
        test: /\.(css|scss)$/,
        loaders: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: 'css-loader?minimize!resolve-url-loader!sass-loader?sourceMap!postcss-loader'
        })
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/,
        loader: 'file-loader',
        options: {
          regExp: /\/([a-z0-9]+)\/[a-z0-9]+\.json$/,
          name: '[name]-[hash].[ext]'
        }
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loaders: [
          'ng-annotate-loader',
          'babel-loader'
        ]
      },
      {
        test: /\.html$/,
        loaders: [
          'html-loader'
        ]
      }
    ]
  },
  plugins: [
    new webpack.optimize.OccurrenceOrderPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    new HtmlWebpackPlugin({
      template: conf.path.src('index.html')
    }),
    new webpack.optimize.UglifyJsPlugin({
      output: {comments: false},
      compress: {unused: true, dead_code: true, warnings: false} // eslint-disable-line camelcase
    }),
    new ExtractTextPlugin('index-[contenthash].css'),
    new webpack.optimize.CommonsChunkPlugin({name: 'vendor'}),
    new webpack.LoaderOptionsPlugin({
      options: {
        postcss: () => [autoprefixer]
      }
    }),
    new webpack.HashedModuleIdsPlugin(),
    new CopyWebpackPlugin([{
      from: 'src/app/**/*.json',
      to: 'translations/[name]-[hash].[ext]'
    }]),
    new VersionFile({
      output: `${conf.paths.dist}/version.txt`,
      verbose: true
    })
  ],
  output: {
    path: path.join(process.cwd(), conf.paths.dist),
    filename: '[name]-[hash].js'
  },
  entry: {
    app: [`./${conf.path.src('app/app.module.js')}`],
    vendor: Object.keys(pkg.dependencies)
  },
  node: {
    fs: 'empty',
    /* eslint-disable camelcase */
    child_process: 'empty'
  }
};

简化问题,我该如何在JSON文件中添加哈希?以下代码似乎没有起作用。

   {
       test: /\.json$/,
       loader: 'file-loader',
       options: {
         name: '[name]-[hash].[ext]'
       }
   }

翻译同时出现在两个位置的示例

编辑:

看起来我的JSON加载器无法捕获翻译文件,因为它们是这样动态导入的:

  $translateProvider.useLoader('$translatePartialLoader', {
    urlTemplate: 'app/{part}/translations/{lang}.json'
  });

你处理过类似这样的案例吗?

在CopyWebpackPlugin中,您可以指定路径to: path.join(process.cwd(), conf.path.dist, 'translations/[name]-[hash].[ext]'),它将为插件提供绝对路径而不是每个条目的当前相对路径。这将在您的dist目录的根目录下创建一个translations文件夹。 - aquiseb
是的,我可以很好地创建带有哈希值的翻译文件夹,您的方法会删除旧条目吗? - Joe Warner
@astenmies 谢谢您的关注。 - Joe Warner
为了删除旧的条目,您可以直接删除dist文件夹并重新构建。 - aquiseb
不,所以复制的JSON仍然保持在它们原来的路径中,如屏幕截图所示。当然,我可以执行类似于“-rm /dist/app”的命令,但我希望它可以在构建步骤中完成,如果不行也没关系。 - Joe Warner
有一个名为 rimraf 的 npm 包,可以执行 rm -rf。您可以使用它与 fs 一起迭代文件 参见循环遍历文件 - aquiseb
1个回答

2

您在这里的主要目标是在发布新版本时告诉浏览器这是一个新文件,我们可以很容易地做到这一点,而不必强制webpack知道正在使用哪些文件。

在webpack配置文件中添加以下内容:

const pkg = require('../package.json');
//...
new webpack.DefinePlugin({
  __VERSION__: JSON.stringify(pkg.version)
})

当您添加翻译文件时,这会让浏览器知道是否有新版本可用,并应该更新翻译文件。

$translateProvider.useLoader('$translatePartialLoader', {
   urlTemplate: `app/{part}/translations/{lang}.json?v=${__VERSION__}`
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接