代码之家  ›  专栏  ›  技术社区  ›  Itsik Mauyhas

react.js-使用自定义路径运行npm run build

  •  5
  • Itsik Mauyhas  · 技术社区  · 6 年前

    跑步 npm run build 命令打开 react-create-app project在所有文件(如映射文件)中创建生成文件夹和一些默认路径:

    {"version":3,"sources":["../static/js/main.500cb0d9.js" ...
    

    我可以更改生成文件夹中的所有路径以匹配我的路径吗,例如

    {"version":3,"sources":["mywebsite/web/static/js/main.500cb0d9.js" ...
    

    package.json包:

    {
      "name": "reactTest",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "jquery": "^3.3.1",
        "moment": "^2.22.1",
        "react": "^16.4.1",
        "react-datepicker": "^1.5.0",
        "react-dom": "^16.4.1",
        "react-month-picker": "^1.3.7",
        "react-scripts": "1.1.4"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      }
    }
    
    1 回复  |  直到 6 年前
        1
  •  9
  •   Patrick Hund    6 年前

    您可以使用以下两种方法之一设置根路径,以便在生产环境中为React应用程序提供服务:

    一。通过在 包.json 文件

    通知行5:

    {
      "name": "reactTest",
      "version": "0.1.0",
      "private": true,
      "homepage": "mywebsite/web",
      "dependencies": {
        "jquery": "^3.3.1",
        "moment": "^2.22.1",
        "react": "^16.4.1",
        "react-datepicker": "^1.5.0",
        "react-dom": "^16.4.1",
        "react-month-picker": "^1.3.7",
        "react-scripts": "1.1.4"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test --env=jsdom",
        "eject": "react-scripts eject"
      }
    }
    

    ( see documentation )

    2。使用公共URL环境变量

    当运行 建造 job,在其前面添加env变量,如下所示:

    PUBLIC_URL=mywebsite/web npm run build
    

    ( see documentation )

    它是做什么的?

    这些方法将 更改源映射文件中的路径,这些路径将始终是相对的,但是 使您能够使用您选择的绝对路径将React应用程序部署到您的web服务器。

    它将在生成的 index.html索引 要绝对,根据您设置的值:

    <!doctype html>
    <html lang="en">
    
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
        <meta name="theme-color" content="#000000">
        <link rel="manifest" href="mywebsite/web/manifest.json">
        <link rel="shortcut icon" href="mywebsite/web/favicon.ico">
        <title>React App</title>
        <link href="mywebsite/web/static/css/main.c17080f1.css" rel="stylesheet">
    </head>
    
    <body>
        <noscript>You need to enable JavaScript to run this app.</noscript>
        <div id="root"></div>
        <script type="text/javascript" src="mywebsite/web/static/js/main.dbfd0e89.js"></script>
    </body>
    
    </html>