簡體   English   中英

如何使用 npm serve 命令提供紗線構建文件?

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

基本上,我的任務是編譯一個 js 文件並在http://localhost:5000/final.js提供它

我有以下腳本。

目前的問題

  • console.log 似乎打印亂序。
  • 能夠切換目錄並運行yarn build ,但似乎無法提供文件

這是源代碼:

#!/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);
}

這是上面腳本的輸出

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