Skip to content

Commit

Permalink
workaround for node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
xsro committed Dec 5, 2021
1 parent c90309d commit 13b9df8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"editor.formatOnSave": true,
"markdownlint.config": {
"MD033": false
}
},
"typescript.tsdk": "node_modules/typescript/lib"
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
"process": "^0.11.10",
"rimraf": "^3.0.2",
"ts-loader": "^9.2.6",
"typescript": "^4.4.4",
"typescript": "^4.6.0-dev.20211205",
"vscode-test": "^1.4.1",
"vscode-uri": "^3.0.2",
"webpack": "^5.60.0",
Expand All @@ -400,4 +400,4 @@
"email": "[email protected]"
},
"license": "MIT"
}
}
16 changes: 16 additions & 0 deletions src/test/suite/download.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assert = require("assert");
import { downloadFromMultiSources } from "../../utils/downloadFile";

suite("test download", async function () {
test("test download with fetch", async function () {
const list = [
'https://cdn.jsdelivr.net/gh/MicrosoftDocs/cpp-docs@master/docs/assembler/masm/assume.md',
'https://raw.githubusercontent.com/MicrosoftDocs/cpp-docs/master/docs/assembler/masm/assume.md',
'https://gitee.com/dosasm/cpp-docs/raw/master/docs/assembler/masm/assume.md'
];
for (const url of list) {
const str = await downloadFromMultiSources([url]);
assert.ok(str, url);
}
});
});
2 changes: 1 addition & 1 deletion src/utils/downloadFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* API for downloading information from web
*/
import { logger } from "./logger";
import fetch from 'node-fetch';
import { fetch } from "./fetch";

export async function downloadFromMultiSources(urls: string[]): Promise<string | undefined> {
for (const url of urls) {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

export const fetch =
process.platform
? (...args: any[]) => import(
/* webpackIgnore: true */'node-fetch').then((module) => {
console.log(module);
return (module.default as any)(...args);
}
)
: window.fetch;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "node12",
"target": "es2017",
"outDir": "dist",
"lib": [
Expand Down
4 changes: 1 addition & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'use strict';

const path = require('path');
const webpack = require('webpack');
/** @typedef {import('webpack').Configuration} WebpackConfig **/

/** @type WebpackConfig */
Expand Down Expand Up @@ -51,8 +52,6 @@ const config = {
}
};

const webpack = require('webpack');

/** @type WebpackConfig */
const webExtensionConfig = {
mode: 'none', // this leaves the source code as close as possible to the original (when packaging we set this to 'production')
Expand All @@ -70,7 +69,6 @@ const webExtensionConfig = {
mainFields: ['browser', 'module', 'main'], // look for `browser` entry point in imported node modules
extensions: ['.ts', '.js'], // support ts-files and js-files
alias: {
// 'node-fetch': './browser-fetch'
// provides alternate implementation for node module and source files
},
fallback: {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3284,10 +3284,10 @@ type-is@^1.6.16:
media-typer "0.3.0"
mime-types "~2.1.24"

typescript@^4.4.4:
version "4.5.2"
resolved "https://registry.npmmirror.com/typescript/download/typescript-4.5.2.tgz#8ac1fba9f52256fdb06fb89e4122fa6a346c2998"
integrity sha512-5BlMof9H1yGt0P8/WF+wPNw6GfctgGjXp5hkblpyT+8rkASSmkUKMXrxR0Xg8ThVCi/JnHQiKXeBaEwCeQwMFw==
typescript@^4.6.0-dev.20211205:
version "4.6.0-dev.20211205"
resolved "https://registry.npmmirror.com/typescript/download/typescript-4.6.0-dev.20211205.tgz#38a5a152e432442faa1f9593220e3b8abaf1726a"
integrity sha512-OThM062cY8geGPmZFgih9QNtslG04mUgJYFNlGAkqZKBGOOS84gEdV5omwIi09q7viDSabIlyFd45YiIhUjaLA==

unbox-primitive@^1.0.1:
version "1.0.1"
Expand Down

0 comments on commit 13b9df8

Please sign in to comment.