From 4b217fba0095f4c68e6fc3e407f500c40c58c616 Mon Sep 17 00:00:00 2001 From: majik Date: Thu, 11 Apr 2019 09:07:53 +0800 Subject: [PATCH] Feature/relative url (#307) * resrouce relative url * add url pathname for require.toUrl (fix webview url) * rest resrouce relative url * fix resource url * ignore .vscode --- .gitignore | 3 ++- packages/ide/src/fill/electron.ts | 14 +++++++++----- packages/vscode/src/fill/storageDatabase.ts | 3 ++- scripts/webpack.general.config.js | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index d2ee77a3..ffaa56b4 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ dist out .DS_Store release -.cache \ No newline at end of file +.vscode +.cache diff --git a/packages/ide/src/fill/electron.ts b/packages/ide/src/fill/electron.ts index bbc1f48b..399cda82 100644 --- a/packages/ide/src/fill/electron.ts +++ b/packages/ide/src/fill/electron.ts @@ -45,7 +45,8 @@ const newCreateElement = (tagName: K): HT }, set: (value: string): void => { if (value) { - value = value.replace(/file:\/\//g, "/resource"); + const resourceBaseUrl = location.pathname.replace(/\/$/, "") + "/resource"; + value = value.replace(/file:\/\//g, resourceBaseUrl); } oldSrc!.set!.call(img, value); }, @@ -66,7 +67,8 @@ const newCreateElement = (tagName: K): HT }, set: (value: string): void => { if (value) { - value = value.replace(/file:\/\//g, "/resource"); + const resourceBaseUrl = location.pathname.replace(/\/$/, "") + "/resource"; + value = value.replace(/file:\/\//g, resourceBaseUrl); } oldInnerHtml!.set!.call(style, value); }, @@ -80,7 +82,8 @@ const newCreateElement = (tagName: K): HT if (sheet && !overridden) { const oldInsertRule = sheet.insertRule; sheet.insertRule = (rule: string, index?: number): void => { - rule = rule.replace(/file:\/\//g, "/resource"); + const resourceBaseUrl = location.pathname.replace(/\/$/, "") + "/resource"; + rule = rule.replace(/file:\/\//g, resourceBaseUrl); oldInsertRule.call(sheet, rule, index); }; overridden = true; @@ -145,8 +148,9 @@ const newCreateElement = (tagName: K): HT (view as any).send = (channel: string, ...args: any[]): void => { // tslint:disable-line no-any if (args[0] && typeof args[0] === "object" && args[0].contents) { // TODO - args[0].contents = (args[0].contents as string).replace(/"(file:\/\/[^"]*)"/g, (m1) => `"/resource${m1}"`); - args[0].contents = (args[0].contents as string).replace(/"vscode-resource:([^"]*)"/g, (m, m1) => `"/resource${m1}"`); + const resourceBaseUrl = location.pathname.replace(/\/$/, "") + "/resource"; + args[0].contents = (args[0].contents as string).replace(/"(file:\/\/[^"]*)"/g, (m1) => `"${resourceBaseUrl}${m1}"`); + args[0].contents = (args[0].contents as string).replace(/"vscode-resource:([^"]*)"/g, (m, m1) => `"${resourceBaseUrl}${m1}"`); args[0].contents = (args[0].contents as string).replace(/style-src vscode-core-resource:/g, "style-src 'self'"); } if (view.contentWindow) { diff --git a/packages/vscode/src/fill/storageDatabase.ts b/packages/vscode/src/fill/storageDatabase.ts index 37b627c7..94343e18 100644 --- a/packages/vscode/src/fill/storageDatabase.ts +++ b/packages/vscode/src/fill/storageDatabase.ts @@ -28,7 +28,8 @@ class StorageDatabase implements workspaceStorage.IStorageDatabase { } this.triggerFlush(WillSaveStateReason.SHUTDOWN); - navigator.sendBeacon(`/resource${this.path}`, this.content); + const resourceBaseUrl = location.pathname.replace(/\/$/, "") + "/resource"; + navigator.sendBeacon(`${resourceBaseUrl}/${this.path}`, this.content); }); } diff --git a/scripts/webpack.general.config.js b/scripts/webpack.general.config.js index 4da6ca88..17a056ba 100644 --- a/scripts/webpack.general.config.js +++ b/scripts/webpack.general.config.js @@ -21,7 +21,7 @@ module.exports = (options = {}) => ({ // they are parsed as URIs and will throw errors if not fully formed. // The !! prefix causes it to ignore other loaders (doesn't work). search: "require\\.toUrl\\(", - replace: "location.protocol + '//' + location.host + '/' + require('!!file-loader?name=[path][name].[ext]!' + ", + replace: "location.protocol + '//' + location.host + location.pathname.replace(/\\/$/,'') + '/' + require('!!file-loader?name=[path][name].[ext]!' + ", flags: "g", }, { search: "require\\.__\\$__nodeRequire",