vite.config.ts 3.8 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

fxy060608's avatar
fxy060608 已提交
11
import { cssTarget } from '@dcloudio/uni-cli-shared'
fxy060608's avatar
fxy060608 已提交
12
import { isH5CustomElement } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26

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 已提交
27
      __UNI_FEATURE_LONGPRESS__: JSON.stringify(true),
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36
    },
    preventAssignment: true,
  }),
  jscc({
    values: {
      // 该插件限制了不能以__开头
      _NODE_JS_: 0,
    },
    // 忽略 pako 内部条件编译
fxy060608's avatar
fxy060608 已提交
37
    exclude: [/pako/ as unknown as string],
fxy060608's avatar
fxy060608 已提交
38
  }),
fxy060608's avatar
fxy060608 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52
  babel({
    babelHelpers: 'bundled',
    extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'],
    presets: [
      [
        '@babel/preset-env',
        {
          corejs: 2,
          useBuiltIns: 'usage',
          targets: ['ios >= 10'],
        },
      ],
    ],
  }),
fxy060608's avatar
fxy060608 已提交
53 54 55 56 57 58
]

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