Vite小知识

62 阅读1分钟
  • 分享你不熟悉的vite知识,增加你的知识面,了解的当复习一下

Vite 中处理静态资源的方法

  • url

  • raw

  • work/work inline

文件/src/test.ts

type Test = { age: string } 

export const test:Test = { age: 18 }

获取文件url

文件/src/url.ts

import test from './test?url'

console.log(test) 
// 打印 /src/test.ts  

获取文件源代码字符串

文件/src/raw.ts

import test from './raw?url'  
console.log(test) 
//  打印  
//  type Test = { age: string }
//  export const test:Test = { age: 18 }

导入脚本作为 Worker

文件/src/worker.ts

function timeCount() {   
  i = i + 1;
  postMessage(i); 
}  
timeCount()   

文件/src/main.ts

import Worker from './worker?worker'  
const worker = new Worker()  
worker.onmessage = (e) => {   
  console.log(e.data) 
}  
// 打印 
// 0、1、2、3、4、5、6、7、8、9