vite.config.ts 3.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8
import path from 'path'

import { defineConfig } from 'vite'

import jscc from 'rollup-plugin-jscc'
import replace from '@rollup/plugin-replace'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
fxy060608's avatar
fxy060608 已提交
9
import babel from '@rollup/plugin-babel'
fxy060608's avatar
fxy060608 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

import { isCustomElement } from '@dcloudio/uni-shared'

function resolve(file: string) {
  return path.resolve(__dirname, file)
}

const rollupPlugins = [
  replace({
    values: {
      defineOnApi: `/*#__PURE__*/ defineOnApi`,
      defineOffApi: `/*#__PURE__*/ defineOffApi`,
      defineTaskApi: `/*#__PURE__*/ defineTaskApi`,
      defineSyncApi: `/*#__PURE__*/ defineSyncApi`,
      defineAsyncApi: `/*#__PURE__*/ defineAsyncApi`,
      __IMPORT_META_ENV_BASE_URL__: 'import.meta.env.BASE_URL', //直接使用import.meta.env.BASE_URL会被vite替换成'/'
fxy060608's avatar
fxy060608 已提交
26
      __UNI_FEATURE_LONGPRESS__: JSON.stringify(true),
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32 33 34 35 36 37
    },
    preventAssignment: true,
  }),
  jscc({
    values: {
      // 该插件限制了不能以__开头
      _NODE_JS_: 0,
    },
    // 忽略 pako 内部条件编译
    exclude: [resolve('../../node_modules/pako/**')],
  }),
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
  babel({
    babelHelpers: 'bundled',
    extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'],
    presets: [
      [
        '@babel/preset-env',
        {
          corejs: 2,
          useBuiltIns: 'usage',
          targets: ['ios >= 10'],
        },
      ],
    ],
  }),
fxy060608's avatar
fxy060608 已提交
52 53 54 55 56 57 58 59
]

export default defineConfig({
  root: __dirname,
  define: {
    global: 'window',
    __DEV__: true,
    __TEST__: false,
fxy060608's avatar
fxy060608 已提交
60
    __PLATFORM__: JSON.stringify('app'),
fxy060608's avatar
fxy060608 已提交
61
    __NODE_JS__: false,
fxy060608's avatar
fxy060608 已提交
62
    __APP_VIEW__: true,
fxy060608's avatar
fxy060608 已提交
63 64 65 66 67
    __UNI_FEATURE_I18N_EN__: true,
    __UNI_FEATURE_I18N_ES__: true,
    __UNI_FEATURE_I18N_FR__: true,
    __UNI_FEATURE_I18N_ZH_HANS__: true,
    __UNI_FEATURE_I18N_ZH_HANT__: true,
fxy060608's avatar
fxy060608 已提交
68
    __IMPORT_META_ENV_BASE_URL__: JSON.stringify(''),
fxy060608's avatar
fxy060608 已提交
69 70 71
  },
  resolve: {
    alias: [
fxy060608's avatar
fxy060608 已提交
72 73
      {
        find: 'vue',
fxy060608's avatar
fxy060608 已提交
74
        replacement: resolve('../uni-app-vue/src/view/index.ts'),
fxy060608's avatar
fxy060608 已提交
75
      },
fxy060608's avatar
fxy060608 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
      {
        find: '@dcloudio/uni-api',
        replacement: resolve('../uni-api/src/index.ts'),
      },
      {
        find: '@dcloudio/uni-vue',
        replacement: resolve('../uni-vue/src/index.ts'),
      },
      {
        find: '@dcloudio/uni-core',
        replacement: resolve('../uni-core/src'),
      },
      {
        find: '@dcloudio/uni-components/style',
        replacement: resolve('../uni-components/style'),
      },
      {
        find: '@dcloudio/uni-components',
        replacement: resolve('../uni-components/src/index.ts'),
      },
      {
        find: '@dcloudio/uni-platform',
        replacement: resolve('./src/platform/index.ts'),
      },
    ],
  },
fxy060608's avatar
fxy060608 已提交
102 103 104 105 106 107 108 109 110
  css: {
    postcss: {
      plugins: [
        require('autoprefixer')({
          overrideBrowserslist: ['Android > 4.4', 'iOS >= 10'],
        }),
      ],
    },
  },
fxy060608's avatar
fxy060608 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement,
        },
      },
    }),
    vueJsx({ optimize: true, isCustomElement }),
  ],
  build: {
    minify: false,
    lib: {
      name: 'uni-app-view',
      fileName: 'uni-app-view',
fxy060608's avatar
fxy060608 已提交
126
      entry: path.resolve(__dirname, 'src/view/index.ts'),
fxy060608's avatar
fxy060608 已提交
127 128 129 130
      formats: ['umd'],
    },
    assetsDir: '.',
    rollupOptions: {
fxy060608's avatar
fxy060608 已提交
131 132 133 134 135 136 137 138 139 140
      // output: {
      //   globals: {
      //     vue: 'Vue',
      //   },
      // },
      // external(source) {
      //   if (['vue'].includes(source)) {
      //     return true
      //   }
      // },
fxy060608's avatar
fxy060608 已提交
141 142 143 144 145 146 147 148 149 150
      preserveEntrySignatures: 'strict',
      plugins: rollupPlugins,
      onwarn: (msg, warn) => {
        if (!String(msg).includes('external module "vue" but never used')) {
          warn(msg)
        }
      },
    },
  },
})