zoukankan      html  css  js  c++  java
  • cross-env 运行跨平台设置和使用环境变量的脚本

    1.1 cross-env是什么

    运行跨平台设置和使用环境变量的脚本

    1.2 出现原因

    当您使用 NODE_ENV=production, 来设置环境变量时,大多数 Windows 命令提示将会阻塞(报错)。(异常是Windows上的Bash,它使用本机Bash。)换言之,Windows 不支持 NODE_ENV=production 的设置方式。

    1.3 解决

    cross-env 使得您可以使用单个命令,而不必担心为平台正确设置或使用环境变量。这个迷你的包(cross-env)能够提供一个设置环境变量的 scripts,让你能够以 Unix 方式设置环境变量,然后在 Windows 上也能兼容运行。

    1.4 安装
    npm install --save-dev cross-env
    1.5 使用
    {
      "scripts": {
        "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
      }
    }

    NODE_ENV环境变量将由 cross-env 设置 打印 process.env.NODE_ENV === 'production'

    例如,webpack很多都有设置如下:

    // Source maps are resource heavy and can cause out of memory issue for large source files.
    const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';

    此时 该判断在打包的时候,会一直默认是需要map文件的, 其本身是有三种方式处理:

    1: 根目录下创建 .env文件,并增加一句参数

    GENERATE_SOURCEMAP=false

    2: 将改语句改为:

    // Source maps are resource heavy and can cause out of memory issue for large source files.
    // const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
     const shouldUseSourceMap = false;

    3: 就用到我们这个插件,在package.json中创建一个用于不生成sourcemap的打包命令:

     "scripts": {
        "start": "node scripts/start.js",
        "build": "node scripts/build.js",
        "build-p": "cross-env GENERATE_SOURCEMAP=false node scripts/build.js",
        "test": "node scripts/test.js"
      },
  • 相关阅读:
    一些你可能用到的代码
    iOS 键盘下去的方法
    iOS设计模式汇总
    随笔
    Spring cloud config 分布式配置中心 (三) 总结
    Spring cloud config 分布式配置中心(二) 客户端
    Spring cloud config 分布式配置中心(一) 服务端
    jdbcUrl is required with driverClassName spring boot 2.0版本
    JpaRepository接口找不到 spring boot 项目
    解决IntelliJ “Initialization failed for 'https://start.spring.io'
  • 原文地址:https://www.cnblogs.com/ningxin/p/15175645.html
Copyright © 2011-2022 走看看