溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Vue3.x源碼調(diào)試的實(shí)現(xiàn)

發(fā)布時(shí)間:2021-06-03 17:04:31 來源:億速云 閱讀:330 作者:Leah 欄目:web開發(fā)

這篇文章給大家介紹Vue3.x源碼調(diào)試的實(shí)現(xiàn),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

如何調(diào)試vue3.x的ts源碼

  • 官網(wǎng)說使用 yarn dev 命令就可以對(duì)其進(jìn)行調(diào)試,可是運(yùn)行該命令后,是生成過后的代碼,不能對(duì)其編寫的ts源碼進(jìn)行調(diào)試。

  • 其實(shí)再生成對(duì)應(yīng)的sourcemap文件,便可以原汁原味的調(diào)試。

  • 先看下幾個(gè)截圖:

Vue3.x源碼調(diào)試的實(shí)現(xiàn) 

Vue3.x源碼調(diào)試的實(shí)現(xiàn)

如果這是你想要的調(diào)試效果,下面請(qǐng)看下如何生成sourcemap文件。

生成sourcemap文件

rollup.js中文文檔

// rollup.config.js
export default {
 // 核心選項(xiàng)
 input,   // 必須
 external,
 plugins,

 // 額外選項(xiàng)
 onwarn,

 // danger zone
 acorn,
 context,
 moduleContext,
 legacy

 output: { // 必須 (如果要輸出多個(gè),可以是一個(gè)數(shù)組)
  // 核心選項(xiàng)
  file,  // 必須
  format, // 必須
  name,
  globals,

  // 額外選項(xiàng)
  paths,
  banner,
  footer,
  intro,
  outro,
  sourcemap,
  sourcemapFile,
  interop,

  // 高危選項(xiàng)
  exports,
  amd,
  indent
  strict
 },
};

可以看到output對(duì)象有個(gè)sourcemap屬性,其實(shí)只要配置上這個(gè)就能生成sourcemap文件了。 在vue-next項(xiàng)目中的rollup.config.js文件中,找到createConfig函數(shù)

function createConfig(output, plugins = []) {
 const isProductionBuild =
  process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
 const isGlobalBuild = /\.global(\.prod)?\.js$/.test(output.file)
 const isBunlderESMBuild = /\.esm\.js$/.test(output.file)
 const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.test(output.file)

 if (isGlobalBuild) {
  output.name = packageOptions.name
 }

 const shouldEmitDeclarations =
  process.env.TYPES != null &&
  process.env.NODE_ENV === 'production' &&
  !hasTSChecked

 const tsPlugin = ts({
  check: process.env.NODE_ENV === 'production' && !hasTSChecked,
  tsconfig: path.resolve(__dirname, 'tsconfig.json'),
  cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
  tsconfigOverride: {
   compilerOptions: {
    declaration: shouldEmitDeclarations,
    declarationMap: shouldEmitDeclarations
   },
   exclude: ['**/__tests__']
  }
 })
 // we only need to check TS and generate declarations once for each build.
 // it also seems to run into weird issues when checking multiple times
 // during a single build.
 hasTSChecked = true

 const externals = Object.keys(aliasOptions).filter(p => p !== '@vue/shared')

 output.sourcemap = true // 這句話是新增的
 return {
  input: resolve(`src/index.ts`),
  // Global and Browser ESM builds inlines everything so that they can be
  // used alone.
  external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
  plugins: [
   json({
    namedExports: false
   }),
   tsPlugin,
   aliasPlugin,
   createReplacePlugin(
    isProductionBuild,
    isBunlderESMBuild,
    isGlobalBuild || isBrowserESMBuild
   ),
   ...plugins
  ],
  output,
  onwarn: (msg, warn) => {
   if (!/Circular/.test(msg)) {
    warn(msg)
   }
  }
 }
}

關(guān)于Vue3.x源碼調(diào)試的實(shí)現(xiàn)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI