vite.config.ts 2.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2
import path from 'path'

3 4
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
fxy060608's avatar
fxy060608 已提交
5 6 7 8
import replace from '@rollup/plugin-replace'

import { isCustomElement } from '../uni-shared'

fxy060608's avatar
fxy060608 已提交
9 10 11 12 13 14 15
const moduleAlias = require('module-alias')
moduleAlias.addAlias(
  '@vue/babel-plugin-jsx',
  path.join(__dirname, 'lib/babel-plugin-jsx')
)
const vueJsx = require('@vitejs/plugin-vue-jsx')

fxy060608's avatar
fxy060608 已提交
16 17 18 19
function resolve(file: string) {
  return path.resolve(__dirname, file)
}

20
export default defineConfig({
fxy060608's avatar
fxy060608 已提交
21 22 23 24
  root: '.',
  define: {
    global: 'window',
    __DEV__: `(process.env.NODE_ENV !== 'production')`,
fxy060608's avatar
fxy060608 已提交
25
    __TEST__: false,
26
    __PLATFORM__: JSON.stringify('h5'),
fxy060608's avatar
fxy060608 已提交
27
  },
fxy060608's avatar
fxy060608 已提交
28 29 30 31 32 33 34 35 36 37 38 39
  resolve: {
    alias: [
      {
        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',
fxy060608's avatar
fxy060608 已提交
40
        replacement: resolve('../uni-core/src'),
fxy060608's avatar
fxy060608 已提交
41 42 43 44 45
      },
      {
        find: '@dcloudio/uni-components',
        replacement: resolve('../uni-components/src/index.ts'),
      },
fxy060608's avatar
fxy060608 已提交
46 47 48 49
      {
        find: '@dcloudio/uni-platform',
        replacement: resolve('./src/platform/index.ts'),
      },
fxy060608's avatar
fxy060608 已提交
50 51
    ],
  },
52 53 54 55 56 57 58 59
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement,
        },
      },
    }),
fxy060608's avatar
fxy060608 已提交
60
    vueJsx({ optimize: true, isCustomElement }),
61 62 63 64 65
  ],
  build: {
    minify: false,
    assetsDir: '.',
    rollupOptions: {
66
      input: path.resolve(__dirname, 'src/index.ts'),
fxy060608's avatar
fxy060608 已提交
67 68 69 70 71 72 73 74 75 76 77 78
      external(source) {
        if (
          ['vue', 'vue-router', '@vue/shared', '@dcloudio/uni-shared'].includes(
            source
          )
        ) {
          return true
        }
        if (source.startsWith('@dcloudio/uni-h5/style')) {
          return true
        }
      },
79 80 81
      preserveEntrySignatures: 'strict',
      plugins: [
        replace({
82
          values: {
fxy060608's avatar
fxy060608 已提交
83 84 85 86 87 88
            // extend: `/*#__PURE__*/ extend`,
            // defineOnApi: `/*#__PURE__*/ defineOnApi`,
            // defineOffApi: `/*#__PURE__*/ defineOffApi`,
            // defineTaskApi: `/*#__PURE__*/ defineTaskApi`,
            // defineSyncApi: `/*#__PURE__*/ defineSyncApi`,
            // defineAsyncApi: `/*#__PURE__*/ defineAsyncApi`,
89 90
          },
          preventAssignment: true,
91 92 93
        }),
      ],
      output: {
94
        dir: path.resolve(__dirname, 'dist'),
95
        format: 'es',
96
        manualChunks: undefined,
97 98 99 100 101 102 103 104 105
        entryFileNames: 'uni-h5.esm.js',
        assetFileNames(assetInfo) {
          if (assetInfo.name === 'style.css') {
            return 'uni-h5.css'
          }
          return 'assets/[name]-[hash][extname]'
        },
      },
    },
fxy060608's avatar
fxy060608 已提交
106
  },
107
})