上一篇:Theia架構
構建你自己的IDE
本指南將教你如何構建你自己的Theia應用。
必要條件
你需要安裝node 10版本(譯者:事實上最新的node穩定版即可):
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash nvm install 10
以及yarn:
npm install -g yarn
還需要確保已安裝python 2.x,可通過python --version來檢查。
安裝
首先請創建一個空目錄,然后切換到這個目錄下:
mkdir my-app cd my-app
在這個目錄下創建package.json:
{ "private": true, "dependencies": { "typescript": "latest", "@theia/typescript": "next", "@theia/navigator": "next", "@theia/terminal": "next", "@theia/outline-view": "next", "@theia/preferences": "next", "@theia/messages": "next", "@theia/git": "next", "@theia/file-search": "next", "@theia/markers": "next", "@theia/preview": "next", "@theia/callhierarchy": "next", "@theia/merge-conflicts": "next", "@theia/search-in-workspace": "next", "@theia/json": "next", "@theia/textmate-grammars": "next", "@theia/mini-browser": "next" }, "devDependencies": { "@theia/cli": "next" } }
簡而言之,Theia應用程序和擴展包都是Node.js包。每一個包都包含一個package.json文件,里面列出了包的一些元數據,如name、version、運行時和構建時的依賴關系等。
- name和version被省略了,因為我們不打算將它作為一個依賴項來使用。同時它被標記為private,因為不打算將它發布為一個獨立的Node.js包。
- 我們在dependencies中列出了所有運行時依賴的擴展包,如@theia/navigator。
- 有些擴展包需要額外的工具來進行安裝,例如,@theia/python需要Python Language Server來安裝。此時你需要參考相應的文檔。
- 可以在這里查看所有已發布的擴展包。
- 我們將@theis/cli列為構建時的依賴項,它提供了構建和運行應用程序的腳本。
構建
首先,安裝所有的依賴項。
yarn
然后,使用Theia CLI來構建應用程序。
yarn theia build
yarn在我們應用程序的上下文中查找由@theia/cli提供的theia可執行文件,然后使用theia執行build命令。這可能需要一些時間,因為默認情況下應用程序會在production模式下進行構建,即它會進行模糊處理和最小化處理。
運行
構建完成之后,我們就可以啟動應用程序:
yarn theia start
你可以在命令的第一個參數中指定一個workspace路徑,--hostname和--port選項用來指定部署的主機名和端口號。例如下面的命令在指定的位置和端口號上打開/workspace:
yarn theia start /my-workspace --hostname 0.0.0.0 --port 8080
在終端中,你應該看到Theia應用程序已經啟動並監聽:
打開瀏覽器並輸入上面顯示的地址,你就可以打開應用程序了。
故障排除
通過代理構建本地依賴項
如果你通過代理運行yarn命令,在構建本地依賴項時有可能會遇到一些問題(如onigurma),例如下面的這個錯誤:
[4/4] Building fresh packages... [1/9] XXXXX [2/9] XXXXX [3/9] XXXXX [4/9] XXXXX error /theiaide/node_modules/XXXXX: Command failed. Exit code: 1 Command: node-gyp rebuild Arguments: Directory: /theiaide/node_modules/XXXXX Output: gyp info it worked if it ends with ok gyp info using node-gyp@3.8.0 gyp info using node@8.15.0 | linux | x64 gyp http GET https://nodejs.org/download/release/v8.15.0/node-v8.15.0-headers.tar.gz gyp WARN install got an error, rolling back install gyp ERR! configure error gyp ERR! stack Error: read ECONNRESET gyp ERR! stack at TLSWrap.onread (net.js:622:25) gyp ERR! System Linux 3.10.0-862.11.6.el7.x86_64 gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" gyp ERR! cwd /theiaide/node_modules/XXXXX gyp ERR! node -v v8.15.0
這是因為node-gyp在system/NPM的代理設置中不工作。如果遇到這種情況,可以通過錯誤堆棧中提供的鏈接下載node-headers文件(如上面例子中的https://nodejs.org/download/release/v8.15.0/node-v8.15.0-headers.tar.gz),然后使用下面的命令進行構建:
npm_config_tarball=/path/to/node-v8.15.0-headers.tar.gz yarn install