简体   繁体   English

如何使用 npm serve 命令提供纱线构建文件?

[英]How to serve yarn build file with npm serve command?

Basically, my task is to compile a js file and serve it at http://localhost:5000/final.js基本上,我的任务是编译一个 js 文件并在http://localhost:5000/final.js提供它

I have the following script.我有以下脚本。

Current issues目前的问题

  • The console.log seems printing out of order. console.log 似乎打印乱序。
  • Able to switch dir and run yarn build , but it seems not able to serve file能够切换目录并运行yarn build ,但似乎无法提供文件

Here is the source code:这是源代码:

#!/usr/bin/env node

const frontendDir =
  "/my/frontend";
const jsDir =
  "/my/frontend/build/static/js";

// util
const util = require("util");
// exec
const exec = util.promisify(require("child_process").exec);

// async func
async function runcmd(cmd) {
  try {
    const { stdout, stderr } = await exec(cmd);
    // need output
    console.log("stdout:", stdout);
    // need error
    console.log("stderr:", stderr);
  } catch (err) {
    console.error(err);
  }
}

try {
  // go to front end dir
  process.chdir(frontendDir);
  console.log("Switch to dir: " + process.cwd());

  // yarn build
  runcmd("yarn build");

  // go to file dir
  process.chdir(jsDir);
  console.log("Switch to dir: " + process.cwd());

  // find that js file and copy it, rename it
  runcmd("cp main.*.js final.js");

  // serve at /my/frontend/build/static/js with url http://localhost:5000/final.js
  runcmd("serve .");
} catch (err) {
  console.log("chdir: " + err);
}

Here is the output from the script above这是上面脚本的输出

Switch to dir: /my/frontend
Switch to dir: /my/frontend/build/static/js
stdout: 
stderr: 
stdout: yarn run v1.21.1
$ react-app-rewired build && cpr ./build/ ../build/frontend/ -o
Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  210.3 KB  build/static/js/main.c1e6b0e9.js

The project was built assuming it is hosted at ./.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.

Find out more about deployment here:



Done in 13.91s.

stderr:
// https://stackoverflow.com/questions/41037042/nodejs-wait-for-exec-in-function
const exec = require("child_process").exec;

function os_func() {
  this.execCommand = function(cmd) {
    return new Promise((resolve, reject) => {
      exec(cmd, (error, stdout, stderr) => {
        if (error) {
          reject(error);
          return;
        }
        resolve(stdout);
      });
    });
  };
}
const os = new os_func();

process.chdir(frontendDir);
os.execCommand("yarn buildlocal")
  .then(() => {
    console.log("@ done yarn build");
    process.chdir(jsDir);
    os.execCommand("cp main.*.js docman.js").then(() => {
      console.log("@ done copy and serve at port 5000");
      os.execCommand("serve -l 5000 .").then(() => {});
    });
  })
  .catch(err => {
    console.log("os >>>", err);
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM