Taro: 在watch的时候如何压缩代码?

Created on 29 Jan 2019  ·  9Comments  ·  Source: NervJS/taro

在dev.js配置uglify和csso并没什么用

// dev.js
 ...
  plugins: {
    babel: {
      sourceMap: true,
      presets: [
        'env'
      ],
      plugins: [
        'transform-decorators-legacy',
        'transform-class-properties',
        'transform-object-rest-spread'
      ]
    },
    uglify: {
      enable: true,
    },
    csso: {
      enable: true,
    }
  },
...

Taro CLI 1.2.10 environment info:
System:
OS: macOS High Sierra 10.13.6
Shell: 5.3 - /bin/zsh
Binaries:
Node: 11.4.0 - ~/.nvm/versions/node/v11.4.0/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 6.5.0 - ~/.nvm/versions/node/v11.4.0/bin/npm
npmPackages:
@tarojs/async-await: 1.2.10 => 1.2.10
@tarojs/components: 1.2.10 => 1.2.10
@tarojs/plugin-babel: 1.2.10 => 1.2.10
@tarojs/plugin-csso: 1.2.10 => 1.2.10
@tarojs/plugin-sass: 1.2.10 => 1.2.10
@tarojs/plugin-uglifyjs: 1.2.10 => 1.2.10
@tarojs/router: 1.2.10 => 1.2.10
@tarojs/taro: 1.2.10 => 1.2.10
@tarojs/taro-alipay: 1.2.10 => 1.2.10
@tarojs/taro-h5: 1.2.10 => 1.2.10
@tarojs/taro-swan: 1.2.10 => 1.2.10
@tarojs/taro-tt: 1.2.10 => 1.2.10
@tarojs/taro-weapp: 1.2.10 => 1.2.10
@tarojs/webpack-runner: 1.2.10 => 1.2.10
eslint-config-taro: 1.2.10 => 1.2.10
eslint-plugin-taro: 1.2.10 => 1.2.10
nervjs: ^1.3.9 => 1.3.9

Most helpful comment

1219

$ NODE_ENV=production taro build --type weapp --watch

All 9 comments

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

1219

$ NODE_ENV=production taro build --type weapp --watch

1219

$ NODE_ENV=production taro build --type weapp --watch

thx

@LeeeoZhang 请问下,这是改动的哪里的文件

1219

$ NODE_ENV=production taro build --type weapp --watch

@luckyadam 这样 环境变量就设置为 production 了,因为项目里有些会根据环境变量动态生成图片、接口等前缀的,有没有其他解决办法?

1219

$ NODE_ENV=production taro build --type weapp --watch

@luckyadam 这样 环境变量就设置为 production 了,因为项目里有些会根据环境变量动态生成图片、接口等前缀的,有没有其他解决办法?

@BackToHappyBear
cross-env 可以设置node环境变量,通过多个config里的 env 去实现

// config/index.js
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  if (process.env.NODE_ENV === 'preBuild') {
    return merge({}, config, require('./pre-build'))
  }
  return merge({}, config, require('./prod'))

1219

$ NODE_ENV=production taro build --type weapp --watch

@luckyadam 这样 环境变量就设置为 production 了,因为项目里有些会根据环境变量动态生成图片、接口等前缀的,有没有其他解决办法?

@BackToHappyBear
cross-env 可以设置node环境变量,通过多个config里的 env 去实现

// config/index.js
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  if (process.env.NODE_ENV === 'preBuild') {
    return merge({}, config, require('./pre-build'))
  }
  return merge({}, config, require('./prod'))

关键是除了环境变量为 production 的情况,其余情况即使配置了代码压缩也不会生效的

1219

$ NODE_ENV=production taro build --type weapp --watch

@luckyadam 这样 环境变量就设置为 production 了,因为项目里有些会根据环境变量动态生成图片、接口等前缀的,有没有其他解决办法?

@BackToHappyBear
cross-env 可以设置node环境变量,通过多个config里的 env 去实现

// config/index.js
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  if (process.env.NODE_ENV === 'preBuild') {
    return merge({}, config, require('./pre-build'))
  }
  return merge({}, config, require('./prod'))

关键是除了环境变量为 production 的情况,其余情况即使配置了代码压缩也不会生效的

以 pre-build为例

// package.json
  "preBuild:weh5": "cross-env TARO_ENV=h5 NODE_ENV=preBuild npm run build:h5",
// config/pre-build.js
env: {
    NODE_ENV: '"preBuild"',
    XXX: '"XXX"'
  },

这样就能在代码中用到环境变量。
node_env 如果不在config/xxx.js中配置 其实在编译时不存在 node_env 的。要把两个环境变量分开,一个是 package.json 命令中的,一个是config/xxx 下的env 设置的

1219

$ NODE_ENV=production taro build --type weapp --watch

@luckyadam 这样 环境变量就设置为 production 了,因为项目里有些会根据环境变量动态生成图片、接口等前缀的,有没有其他解决办法?

@BackToHappyBear
cross-env 可以设置node环境变量,通过多个config里的 env 去实现

// config/index.js
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  if (process.env.NODE_ENV === 'preBuild') {
    return merge({}, config, require('./pre-build'))
  }
  return merge({}, config, require('./prod'))

关键是除了环境变量为 production 的情况,其余情况即使配置了代码压缩也不会生效的

以 pre-build为例

// package.json
  "preBuild:weh5": "cross-env TARO_ENV=h5 NODE_ENV=preBuild npm run build:h5",
// config/pre-build.js
env: {
    NODE_ENV: '"preBuild"',
    XXX: '"XXX"'
  },

这样就能在代码中用到环境变量。
node_env 如果不在config/xxx.js中配置 其实在编译时不存在 node_env 的。要把两个环境变量分开,一个是 package.json 命令中的,一个是config/xxx 下的env 设置的

我明白你的意思,现在是这样,下面设置了环境变量,且在 config/dev.js 下配置 ugilfy: { enable: true }, 但是运行起来是无用的,代码并未压缩,我希望 npm run dev:weapp 的时候代码也压缩
"dev:weapp": "cross-env NODE_ENV=dev npm run build:weapp"

Was this page helpful?
0 / 5 - 0 ratings