reactjs - 节点 : move files in create-react-app template to root post-install

标签 reactjs npm

我有一个版本的 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/

相关文章:

css - 如果禁用悬停路由器按钮时禁用指针事件

Javascript ES6最佳实践,变量实例化

reactjs - 使用部署在 Apache Tomcat 上的 Create-react-app 创建的 React 网站出现空白屏幕

angular - 如何在 Angular 中异步加载 momentjs 时区数据

node.js - 在 PATH 中使用本地 NPM 依赖项 - 是否有工具可以执行此操作?

unit-testing - 如何对 React 组件的 shouldComponentUpdate 方法进行单元测试

javascript - 如何在 react-router 中为 Link 或 IndexLink 的包装元素设置 activeClassName?

linux - 从 NodeJs 和 npm 完全清理服务器

javascript - 类(模块)未导出

node.js - npm start 和其他脚本不执行任何操作