鸿蒙版taro编译配置

编译配置存放于项目根目录下的 config 目录中,只要确保 config/index.js 或者 config/index.ts 文件存在,可以作为用户自定义编译配置导出即可。你也可以选择拆分成三个文件(具体见默认配置):

  • index.js 是通用配置

  • dev.js 是项目预览时的配置

  • prod.js 是项目打包时的配置

config/index.js

import { defineConfig, type UserConfigExport } from '@tarojs/cli'
​
import devConfig from './dev'
import prodConfig from './prod'
import os from 'os'
import path from 'path'
​
// https://taro-docs.jd.com/docs/next/config#defineconfig-辅助函数
export default defineConfig<'vite'>(async (merge, { command, mode }) => {
  const baseConfig: UserConfigExport<'vite'> = {
​
    projectName: 'taro-ohos',
    date: '2025-5-16',
    designWidth: 750,
    deviceRatio: {
      640: 2.34 / 2,
      750: 1,
      375: 2,
      828: 1.81 / 2
    },
    sourceRoot: 'src',
    outputRoot: 'dist',
    plugin: ['@tarojs/plugin-platform-harmony-cpp', {
     harmony: {
        // 当前仅支持使用 Vite 编译鸿蒙应用
        compiler: 'vite',
        projectPath: path.join(os.homedir(), '../tarooh'),
        // Taro 项目编译到对应鸿蒙模块名,默认为 entry
        hapName: 'entry',
      },
    }],
​
    defineConstants: {
    },
    copy: {
      patterns: [
      ],
      options: {
      }
    },
    framework: 'react',
    compiler: 'vite',
​
    mini: {
      postcss: {
        pxtransform: {
          enable: true,
          config: {
​
          }
        },
        cssModules: {
          enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
          config: {
            namingPattern: 'module', // 转换模式,取值为 global/module
            generateScopedName: '[name]__[local]___[hash:base64:5]'
          }
        }
      },
    },
    // plugins: [],
​
    h5: {
      publicPath: '/',
      staticDirectory: 'static',
​
      miniCssExtractPluginOption: {
        ignoreOrder: true,
        filename: 'css/[name].[hash].css',
        chunkFilename: 'css/[name].[chunkhash].css'
      },
      postcss: {
        autoprefixer: {
          enable: true,
          config: {}
        },
        cssModules: {
          enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
          config: {
            namingPattern: 'module', // 转换模式,取值为 global/module
            generateScopedName: '[name]__[local]___[hash:base64:5]'
          }
        }
      },
    },
    rn: {
      appName: 'taroDemo',
      postcss: {
        cssModules: {
          enable: false, // 默认为 false,如需使用 css modules 功能,则设为 true
        }
      }
    },
  }
​
  process.env.BROWSERSLIST_ENV = process.env.NODE_ENV
​
  if (process.env.NODE_ENV === 'development') {
    // 本地开发构建配置(不混淆压缩)
    return merge({}, baseConfig, devConfig)
  }
  // 生产构建配置(默认开启压缩混淆等)
  return merge({}, baseConfig, prodConfig)
})
​

完毕,

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐