我对这个问题有一个不同的版本:
我正在构建一个同时使用 Electron 和 Redux 的create-react-app 模板。
我想/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",
....
}
Run Code Online (Sandbox Code Playgroud)
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.');
});
Run Code Online (Sandbox Code Playgroud)
3. 将“node file-copy.js”添加为 package.json 中构建过程的附加步骤
"scripts": {
...
"build": "react-scripts build && node file-copy.js"
...
},
Run Code Online (Sandbox Code Playgroud)
注意:更新 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.
$
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2681 次 |
| 最近记录: |