簡體   English   中英

如何在 Rollup 中導入本地文件系統路徑而不是 node_module?

[英]How to import local file system path instead of node_module in Rollup?

出於某種原因,我必須編寫這樣的代碼:

import { something } from '/Users/my-user-name/my-working-dir/my-package/src/somefile.ts';

Rollup 看到/Users認為這是一個node_modules ,但不是。

我找不到任何與此相關的匯總插件。

現在,我已經寫了一個匯總插件來解決這個問題,但我之前沒有寫任何插件,我不知道我這樣做是對還是錯,但輸出正是我想要的:

function fixLocalImport() {
  return {
    name: 'fix-local-import', // this name will show up in warnings and errors
    resolveId(source, importer) {
      if (source.startsWith('/Users')) {
        return source;
      }
      return null; // other ids should be handled as usually
    },
    load(id) {
      return null; // other ids should be handled as usually
    }
  };
}

我做錯了什么嗎?

Rollup 不會自動處理絕對 URL,因為它們根據上下文(網站服務器的根目錄、文件系統的根目錄或項目的根目錄)引用不同的內容。 在這里編寫插件是最好的解決方案,盡管您不需要覆蓋“加載”鈎子。

function fixLocalImport() {
  return {
    name: 'fix-local-import',
    resolveId(source, importer) {
      if (source.startsWith('/Users')) {
        return source;
      }
      return null;
    },
  };
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM