最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - vite replace an import statement - Stack Overflow

programmeradmin2浏览0评论

I have a vue file

// myComponent.vue
import { something } from 'some-module'
...

I want to replace this import statement into

import { something } from '@/utils/myModule'

in case when running the vitest mand. Do we have some plugin which I can use to get the above result?

I have a vue file

// myComponent.vue
import { something } from 'some-module'
...

I want to replace this import statement into

import { something } from '@/utils/myModule'

in case when running the vitest mand. Do we have some plugin which I can use to get the above result?

Share Improve this question edited Feb 8, 2022 at 11:56 coure2011 asked Feb 8, 2022 at 11:38 coure2011coure2011 42.5k87 gold badges225 silver badges361 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Ok its working

// vite.config.js
    alias: [
          {
            find: /some-module/,
            replacement: fileURLToPath(new URL('./src/utils/someModuleFake.ts', import.meta.url)),
          },
          {
            find: '@',
            replacement: fileURLToPath(new URL('./src', import.meta.url))
          },
        ],

you can use jsconfig.json file to achieve this.

{
  "pilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/utils/*": ["ponentpath/*"],
    }
  }
}

vite-plugin-filter-replace can solve this problem. Add follow config in vite.config.ts

import filterReplace from 'vite-plugin-filter-replace';
export default {
    plugins: [filterReplace([{
        filter: /\.vue$/,
        replace: {
            from: /some-module/g,
            to: '@/utils/myModule'
        },   
    }], {
       enforce: 'pre',
       apply: 'build'
    }
    )],
};
发布评论

评论列表(0)

  1. 暂无评论