我有一个版本的 this question有一点扭曲:
我正在构建一个 create-react-app
template使用电子和redux。
我想从 /src/config
移动 webpack 和 babel 的配置文件安装后根目录。但是,添加 "postinstall": "cp /src/config ."
到我的 package.json
文件不起作用。看起来像那个文件而不是 template.json
是添加安装后脚本的正确位置,但出于某种原因,可能是由于模板系统如何管理内部文件结构(?),此脚本在安装后运行但失败。我在 /template/src/config
有配置文件在我发布的包中。
编辑:如果我将安装后脚本更改为 "cp template/src/config/* ."
,它看起来会运行但它不会复制任何文件。
最佳答案
通过使用 ,可以在所有操作系统上可靠地实现构建后的文件复制。 ncp 包和相关的脚本。
react构建后如何复制文件?
1.在'package.json'中添加ncp作为依赖
"dependencies": {
"ncp": "2.0.0",
....
}
2. 创建一个文件复制脚本,它将递归地将文件从源复制到目标
// File name: file-copy.js
var path = require('path');
var ncp = require('ncp').ncp;
ncp.limit = 16;
var srcPath = 'src/config'
var destPath = 'dst/config'
console.log('Copying files...');
ncp(srcPath, destPath, function (err) {
if (err) {
return console.error(err);
}
console.log('Copying files complete.');
});
3. 在 package.json 中添加“node file-copy.js”作为构建过程的附加步骤
"scripts": {
...
"build": "react-scripts build && node file-copy.js"
...
},
注:更新 package.json 后,运行
npm install
一次安装/启用 ncp 包。替代:
npm add ncp
在最新版本的 nodejs 上将更新 package.json 并单步启用 ncp。4. 通过运行“npm run build”来测试构建过程
$ npm run build
> ui@0.1.0 build /Users/macuser1/stack-overflow/ui
> react-scripts build && node file-copy.js
Creating an optimized production build...
Compiled successfully.
File sizes after gzip:
40.32 KB build/static/js/2.db436670.chunk.js
1.24 KB (-20 B) build/static/js/main.02386c69.chunk.js
767 B build/static/js/runtime-main.873e104b.js
...
Copying files...
Copying files complete.
Copying files complete.
$
关于reactjs - 节点 : move files in create-react-app template to root post-install,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59904670/