gpt4 book ai didi

javascript - 如何复制文件夹中的文件进行构建?

转载 作者:行者123 更新时间:2023-12-02 21:32:15 24 4
gpt4 key购买 nike

我的 icons 文件夹中有几张图像:

enter image description here

并且想要将所有 png 文件复制到 build 文件夹。我试过:

new CopyWebpackPlugin([
{
from: "src/manifest.json",
transform: function(content, path) {
// generates the manifest file using the package.json informations
return Buffer.from(
JSON.stringify({
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString())
})
);
}
},
{ from: "icons", to: "./"}
]),

但是它不起作用。我究竟做错了什么?

更新

配置文件的内容:

var webpack = require("webpack"),
path = require("path"),
fileSystem = require("fs"),
env = require("./utils/env"),
CleanWebpackPlugin = require("clean-webpack-plugin").CleanWebpackPlugin,
CopyWebpackPlugin = require("copy-webpack-plugin"),
HtmlWebpackPlugin = require("html-webpack-plugin"),
WriteFilePlugin = require("write-file-webpack-plugin");

// load the secrets
var alias = {};

var secretsPath = path.join(__dirname, ("secrets." + env.NODE_ENV + ".js"));

var fileExtensions = ["jpg", "jpeg", "png", "gif", "eot", "otf", "svg", "ttf", "woff", "woff2"];

if (fileSystem.existsSync(secretsPath)) {
alias["secrets"] = secretsPath;
}

var options = {
mode: process.env.NODE_ENV || "development",
entry: {
popup: path.join(__dirname, "src", "js", "popup.js"),
options: path.join(__dirname, "src", "js", "options.js"),
background: path.join(__dirname, "src", "js", "background.js")
},
output: {
path: path.join(__dirname, "build"),
filename: "[name].bundle.js"
},
module: {
rules: [
{
test: /\.css$/,
loader: "style-loader!css-loader",
exclude: /node_modules/
},
{
test: new RegExp('.(' + fileExtensions.join('|') + ')$'),
loader: "file-loader?name=[name].[ext]",
exclude: /node_modules/
},
{
test: /\.html$/,
loader: "html-loader",
exclude: /node_modules/
}
]
},
resolve: {
alias: alias
},
plugins: [
// clean the build folder
new CleanWebpackPlugin(),
// expose and write the allowed env vars on the compiled bundle
new webpack.EnvironmentPlugin(["NODE_ENV"]),
new CopyWebpackPlugin([{
from: "src/manifest.json",
transform: function (content, path) {
// generates the manifest file using the package.json informations
return Buffer.from(JSON.stringify({
description: process.env.npm_package_description,
version: process.env.npm_package_version,
...JSON.parse(content.toString())
}))
}
},
{ from: "icons", to: "./"}
]),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "popup.html"),
filename: "popup.html",
chunks: ["popup"]
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "options.html"),
filename: "options.html",
chunks: ["options"]
}),
new HtmlWebpackPlugin({
template: path.join(__dirname, "src", "background.html"),
filename: "background.html",
chunks: ["background"]
}),
new WriteFilePlugin()
]
};

if (env.NODE_ENV === "development") {
options.devtool = "cheap-module-eval-source-map";
}

module.exports = options;

最佳答案

文件加载器应该适合您的用例,而不是尝试 CopyWebpackPlugin

module.exports = {
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
loader: 'file-loader',
options: {
name: 'build/[name].[ext]',
},
},
],
},
};

https://webpack.js.org/loaders/file-loader/

更新:

尝试这个代码沙箱,对我来说只需一些小的调整即可:

https://codesandbox.io/s/2rgs4

这是我使用上面的配置得到的构建输出:

enter image description here

关于javascript - 如何复制文件夹中的文件进行构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60592582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com