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"
} )
]
} );
它应该去哪里?
答案 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