最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WindiCSS實(shí)現(xiàn)加載windi.config.ts配置文件詳解

 更新時(shí)間:2023年02月07日 10:37:15   作者:AFine970  
這篇文章主要為大家介紹了WindiCSS實(shí)現(xiàn)加載windi.config.ts配置文件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

背景

我們知道WindiCSS的配置文件既支持js后綴也支持ts后綴,即:windi.config.jswindi.config.ts

我們?cè)?vscode 安裝的WindiCSS IntelliSense也支持讀取多種后綴格式的配置文件。vscode 基于 electron 實(shí)現(xiàn),electron 底層是一個(gè)node.js + v8的集成終端,支持運(yùn)行 js 格式的代碼,WindiCSS IntelliSense最終打包出來的運(yùn)行的代碼也是 js 格式的代碼

WindiCSS IntelliSense是怎么實(shí)現(xiàn) js 文件加載 ts 文件的呢?

這個(gè)問題困惑了我大半天,正好最近在寫腳手架,需要支持加載 ts 后綴的配置文件

于是下定決心,把這個(gè)問題搞清楚,最后終于在源碼里找到了答案

解惑

WindiCSS IntelliSense源碼

  • 我們將WindiCSS IntelliSense源碼直接clone下來
$ git clone https://github.com/windicss/windicss-intellisense.git
  • 找到讀取配置文件的核心代碼
<!--src\lib\index.ts-->
// ...
async init() {
    try {
      const config = await this.loadConfig();
      this.processor = new Processor(
        config
      ) as Processor;
      this.attrPrefix = this.processor.config('attributify.prefix') as
        | string
        | undefined;
      this.variants = this.processor.resolveVariants();
      this.colors = flatColors(
        this.processor.theme('colors', {}) as colorObject
      );
      this.register();
    } catch (error) {
      Log.error(error);
    }
  }
  // ...
  • 關(guān)鍵實(shí)現(xiàn)
<!--src\lib\index.ts-->
// ...
import { loadConfig } from 'unconfig';
// ...
async loadConfig(file?: string) {
    if(!workspace.workspaceFolders) return;
    const { config, sources } = await loadConfig<Config>({
      sources: [
        {
          files: 'windi.config',
          extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs'],
        },
        {
          files: 'tailwind.config',
          extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs'],
        },
      ],
      merge: false,
      cwd: workspace.workspaceFolders[0].uri.fsPath,
    });
    Log.info(`Loading Config File: ${sources}`);
    return config;
  }
// ...

從關(guān)鍵實(shí)現(xiàn)的代碼上看,我們就找到了答案:使用unconfig來實(shí)現(xiàn)加載不同后綴的文件

看到這里,我們還是沒有搞懂:為什么js能加載ts,我們繼續(xù)深入了解一下unconfig的內(nèi)部實(shí)現(xiàn)

unconfig源碼

  • 我們將unconfig源碼直接clone下來
git clone https://github.com/antfu/unconfig.git
  • 核心代碼
<!--src\index.ts-->
// ...
import jiti from 'jiti'
// ...
async function loadConfigFile<T>(filepath: string, source: LoadConfigSource<T>): Promise<LoadConfigResult<T> | undefined> {
  let config: T | undefined
  let parser = source.parser || 'auto'
  let bundleFilepath = filepath
  let code: string | undefined
  async function read() {
    if (code == null)
      code = await fs.readFile(filepath, 'utf-8')
    return code
  }
  if (source.transform) {
    const transformed = await source.transform(await read(), filepath)
    if (transformed) {
      bundleFilepath = join(dirname(filepath), `__unconfig_${basename(filepath)}`)
      await fs.writeFile(bundleFilepath, transformed, 'utf-8')
      code = transformed
    }
  }
  if (parser === 'auto') {
    try {
      config = JSON.parse(await read())
      parser = 'json'
    }
    catch {
      parser = 'require'
    }
  }
  try {
    if (!config) {
      if (typeof parser === 'function') {
        config = await parser(filepath)
      }
      else if (parser === 'require') {
        config = await jiti(filepath, {
          interopDefault: true,
          cache: false,
          requireCache: false,
          v8cache: false,
          esmResolve: true,
        })(bundleFilepath)
      }
      else if (parser === 'json') {
        config = JSON.parse(await read())
      }
    }
    if (!config)
      return
    const rewritten = source.rewrite
      ? await source.rewrite(config, filepath)
      : config
    if (!rewritten)
      return undefined
    return {
      config: rewritten,
      sources: [filepath],
    }
  }
  catch (e) {
    if (source.skipOnError)
      return
    throw e
  }
  finally {
    if (bundleFilepath !== filepath)
      await fs.unlink(bundleFilepath).catch()
  }
}
// ...
  • 把核心代碼進(jìn)行精簡(jiǎn),找到關(guān)鍵實(shí)現(xiàn)
<!--src\index.ts-->
// ...
import jiti from 'jiti'
// ...
async function loadConfigFile<T>(filepath: string, source: LoadConfigSource<T>): Promise<LoadConfigResult<T> | undefined> {
// ...
    try {
    if (!config) {
      if (typeof parser === 'function') {
        config = await parser(filepath)
      }
      else if (parser === 'require') {
        config = await jiti(filepath, {
          interopDefault: true,
          cache: false,
          requireCache: false,
          v8cache: false,
          esmResolve: true,
        })(bundleFilepath)
      }
      else if (parser === 'json') {
        config = JSON.parse(await read())
      }
    }
    // ...
}
// ...

從關(guān)鍵實(shí)現(xiàn)的代碼上看,我們就找到了答案:使用jiti來實(shí)現(xiàn) js 文件加載 ts 文件時(shí),動(dòng)態(tài)編譯 ts 文件并返回結(jié)果。

jiti文檔中這么描述:Runtime typescript and ESM support for Node.js (CommonJS),我們可以更加粗暴地理解為require-ts

為了讓大家更好地理解unconfig的工作流程,樓主根據(jù)上述的unconfig核心代碼,整理出一個(gè)unconfig核心工作原理流程圖

關(guān)于js文件如何加載ts文件的疑惑得以解開

代碼實(shí)踐

看過沒練過,等于沒看過 - B 站夢(mèng)覺教游泳

我們?cè)趯懩_手架的時(shí)候可以直接使用unconfig讀取配置文件即可,例如:讀取vite.config.ts可以這么實(shí)現(xiàn)

import { loadConfig } from 'unconfig'
const { config } = await loadConfig({
  sources: [
    {
      files: 'vite.config',
      async rewrite(config) {
        return await (typeof config === 'function' ? config() : config)
      },
    },
  ]
})

以上就是WindiCSS實(shí)現(xiàn)加載windi.config.ts配置文件詳解的詳細(xì)內(nèi)容,更多關(guān)于WindiCSS加載windi.config.ts的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 微信小程序(十四)button組件詳細(xì)介紹

    微信小程序(十四)button組件詳細(xì)介紹

    這篇文章主要介紹了 微信小程序button組件詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • WebAssembly使用方法研究

    WebAssembly使用方法研究

    這篇文章主要為大家介紹了WebAssembly使用方法研究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 手寫實(shí)現(xiàn)JS中的new

    手寫實(shí)現(xiàn)JS中的new

    這篇文章主要介紹JS中的new,new 運(yùn)算符創(chuàng)建一個(gè)用戶定義的對(duì)象類型的實(shí)例或具有構(gòu)造函數(shù)的內(nèi)置對(duì)象的實(shí)例。下面我們一起來看看我呢很臟具體內(nèi)容的詳細(xì)介紹,需要的朋友可以參考一下
    2021-11-11
  • 微信小程序 使用騰訊地圖SDK詳解及實(shí)現(xiàn)步驟

    微信小程序 使用騰訊地圖SDK詳解及實(shí)現(xiàn)步驟

    這篇文章主要介紹了微信小程序 使用騰訊地圖SDK詳解及實(shí)現(xiàn)步驟的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Project?Reference優(yōu)化TypeScript編譯性能示例

    Project?Reference優(yōu)化TypeScript編譯性能示例

    這篇文章主要為大家介紹了Project?Reference優(yōu)化TypeScript編譯性能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • 微信小程序 參數(shù)傳遞詳解

    微信小程序 參數(shù)傳遞詳解

    這篇文章主要介紹了微信小程序 參數(shù)傳遞詳解的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • js 標(biāo)簽語法使用詳情

    js 標(biāo)簽語法使用詳情

    這篇文章主要介紹了js 標(biāo)簽語法使用,在 JavaScript 中提供了標(biāo)簽語句,用于標(biāo)記指定的代碼塊,便于跳轉(zhuǎn)到指定的位置。本文來記錄一下標(biāo)簽語句的使用方法,需要的朋友可以參考一下
    2021-10-10
  • 前端可視化搭建組件值與聯(lián)動(dòng)實(shí)現(xiàn)詳解

    前端可視化搭建組件值與聯(lián)動(dòng)實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了前端可視化搭建組件值與聯(lián)動(dòng)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • TypeScript的類型指令單行注釋詳解

    TypeScript的類型指令單行注釋詳解

    這篇文章主要為大家介紹了TypeScript的類型指令單行注釋詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • JS遞歸遍歷查詢是否有權(quán)限示例詳解

    JS遞歸遍歷查詢是否有權(quán)限示例詳解

    這篇文章主要為大家介紹了JS遞歸遍歷查詢是否有權(quán)限示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07

最新評(píng)論

壶关县| 柞水县| 武清区| 绵竹市| 文昌市| 富平县| 临泉县| 论坛| 辽阳县| 安新县| 双鸭山市| 嘉定区| 剑河县| 同心县| 西丰县| 浦县| 弥勒县| 大邑县| 青河县| 苍梧县| 西城区| 新野县| 拜泉县| 寻乌县| 唐河县| 乐至县| 延津县| 乌苏市| 建宁县| 牟定县| 长岛县| 怀集县| 邓州市| 贵溪市| 尚志市| 固原市| 锡林浩特市| 甘谷县| 沧源| 阜平县| 大余县|