关于前端:Taro项目打包生成不同目录

13次阅读

共计 1507 个字符,预计需要花费 4 分钟才能阅读完成。

家喻户晓,Taro 我的项目能够生成 RN、H5 和各种平台小程序,打包的命令位于 package.json 文件的 scripts 节点中,如下所示。

"scripts": {
        "build:weapp": "taro build --type weapp",
        "build:swan": "taro build --type swan",
        "build:alipay": "taro build --type alipay",
        "build:tt": "taro build --type tt",
        "build:h5": "cross-env CLIENT_ENV=h5 taro build --type h5",
        "build:rn": "cross-env CLIENT_ENV=rn taro build --type rn",
        "dev:weapp": "npm run build:weapp -- --watch",
        "dev:swan": "npm run build:swan -- --watch",
        "dev:alipay": "npm run build:alipay -- --watch",
        "dev:tt": "npm run build:tt -- --watch",
        "dev:h5": "cross-env CLIENT_ENV=h5 npm run build:h5 -- --watch",
        "dev:rn": "cross-env CLIENT_ENV=rn npm run build:rn -- --watch"
    }

当咱们执行打包命令就能够生成不同平台的资源文件:

yarn build:weapp        // 微信小程序
yarn build:swan         // 百度小程序
yarn build:alipay       // 支付宝小程序
yarn build:tt           // 字节小程序
yarn build:jd           // 京东小程序
...

不过,应用下面的命令打包的时候,如果没有配置输入门路,那么默认的输入门路是 dist 目录。如果咱们须要打包到不同目录,那么就须要批改 config/index.js 文件的打包配置。首先,减少一段如下配置:

const outputRootStrtegy = {
  h5: 'dist/h5',
  weapp: 'dist/weapp',
  alipay: 'dist/alipay',
  swan: 'dist/swan',
  jd: 'dist/jd',
  ['undefined']: 'dist'
}
const env = JSON.parse(process.env.npm_config_argv)['cooked'][1].split(':')[1]
const outputRoot = outputRootStrtegy[env]

而后,再批改 config/index.js 文件 config 外面的配置,如下所示。

const outputRootStrtegy = {
  h5: 'dist_h5',
  weapp: 'dist_weapp',
  alipay: 'dist_alipay',
  swan: 'dist_swan',
  ['undefined']: 'dist'
}
const env = JSON.parse(process.env.npm_config_argv)['cooked'][1].split(':')[1]
const outputRoot = outputRootStrtegy[env]

const config = {
  projectName: 'yx',
  date: '2020-12-11',
  designWidth: 750,
  ...   // 省略代码
  sourceRoot: 'src',
  outputRoot: outputRoot,        // 批改为变量
  plugins: {},}

而后,从新执行打包,就能够看到打包时会生成不同的子目录了,如下图所示。

正文完
 0