vite.config.ts 3.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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'

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 已提交
25
      __UNI_FEATURE_LONGPRESS__: JSON.stringify(true),
fxy060608's avatar
fxy060608 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    },
    preventAssignment: true,
  }),

  jscc({
    values: {
      // 该插件限制了不能以__开头
      _NODE_JS_: 0,
    },
    // 忽略 pako 内部条件编译
    exclude: [resolve('../../node_modules/pako/**')],
  }),
]

export default defineConfig({
  root: __dirname,
  define: {
    global: 'window',
    __DEV__: true,
    __TEST__: false,
fxy060608's avatar
fxy060608 已提交
46
    __PLATFORM__: JSON.stringify('app'),
fxy060608's avatar
fxy060608 已提交
47
    __NODE_JS__: false,
fxy060608's avatar
fxy060608 已提交
48
    __APP_VIEW__: true,
fxy060608's avatar
fxy060608 已提交
49 50 51 52 53
    __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 已提交
54 55 56
  },
  resolve: {
    alias: [
fxy060608's avatar
fxy060608 已提交
57 58
      {
        find: 'vue',
fxy060608's avatar
fxy060608 已提交
59
        replacement: resolve('../uni-app-vue/src/view/index.ts'),
fxy060608's avatar
fxy060608 已提交
60
      },
fxy060608's avatar
fxy060608 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      {
        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 已提交
87 88 89 90 91 92 93 94 95
  css: {
    postcss: {
      plugins: [
        require('autoprefixer')({
          overrideBrowserslist: ['Android > 4.4', 'iOS >= 10'],
        }),
      ],
    },
  },
fxy060608's avatar
fxy060608 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  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 已提交
111
      entry: path.resolve(__dirname, 'src/view/index.ts'),
fxy060608's avatar
fxy060608 已提交
112 113 114 115
      formats: ['umd'],
    },
    assetsDir: '.',
    rollupOptions: {
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120 121 122 123 124 125
      // output: {
      //   globals: {
      //     vue: 'Vue',
      //   },
      // },
      // external(source) {
      //   if (['vue'].includes(source)) {
      //     return true
      //   }
      // },
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131 132 133 134 135
      preserveEntrySignatures: 'strict',
      plugins: rollupPlugins,
      onwarn: (msg, warn) => {
        if (!String(msg).includes('external module "vue" but never used')) {
          warn(msg)
        }
      },
    },
  },
})