在webpack中使用babel-loader时如何指定cacheDirectory选项?

时间:2015-04-21 07:58:57

标签: webpack babeljs

babel-loader中有cacheDirectory个选项。我无法弄清楚如何在以下webpack设置中使用它:

var compiler = webpack( {
    context: path.resolve( __dirname + "/../../" + rootModuleDir + "/" + modules[ module ] ),
    entry: "./index.jsx",
    resolve: {
        root: path.resolve( __dirname + "/../../assets/js/lib/react" ),
        extensions: [ "", ".js", ".jsx" ]
    },
    output: {
        path: targetDir,
        filename: modules[ module ] + ".js"
    },
    module: {
        loaders: [
            { test: /\.jsx?$/, exclude: /node_modules/, loader: "babel-loader?optional=runtime" }
        ]
    },
    plugins: [
        //new webpack.optimize.UglifyJsPlugin(),
        new webpack.SourceMapDevToolPlugin( {
            filename: "[file].map"
        } )
    ]
} );

它应该去哪里?

3 个答案:

答案 0 :(得分:18)

您可以将它添加到babel-loader配置中,如下所示:

loaders: [
    {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader?optional=runtime&cacheDirectory"
    }
]

注意,您不应添加=true,这是不必要的,并将cacheDirectory设置为使用名为true的目录。参考:using cacheDirectory fails with Error

您还可以使用query属性:

rules: [
    {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader",
        query: {
            optional: "runtime",
            cacheDirectory: true
        }
    }
]

使用query属性时,指定true将启用该选项,并指定字符串值将启用该选项并将其配置为使用该目录名称。参考:query parameters

答案 1 :(得分:2)

将其添加到加载程序字符串中,如下所示:

module: {
    loaders: [
        { test: /\.jsx?$/, exclude: /node_modules/, loader: "babel-loader?optional=runtime&cacheDirectory=true" }
    ]
},

答案 2 :(得分:0)

您可以这样做-

module.exports = {
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader?+cacheDirectory'
      }
    ]
  } 
};


它将缓存在node_modules/.cache/babel-loader文件夹中。

文件将这样存储-

0065e42bfc1ab4c292dfa71c6a0e924b59ed6a37.json.gz 538205e2b3bffa535d934c3d8f2feee4566bfc00.json.gz b17d9d4b73726f4ba7c5797a9959546fb3d72d69.json.gz

每个JS文件的缓存文件都不同。

在此处参考文档-https://webpack.js.org/loaders/babel-loader/#options