Skip to content

Commit 95a2a06

Browse files
committed
feat: more polyfill
1 parent 4734931 commit 95a2a06

3 files changed

Lines changed: 64 additions & 14 deletions

File tree

index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,29 @@ module.exports = class GopeedPolyfillPlugin {
5151
apply(compiler) {
5252
const filter = createAliasFilter(this.options);
5353

54-
compiler.options.plugins.push(new compiler.webpack.ProvidePlugin(filter({
55-
Buffer: [require.resolve('buffer/'), 'Buffer'],
56-
process: require.resolve('process/browser'),
57-
})));
54+
compiler.options.plugins.push(
55+
new compiler.webpack.NormalModuleReplacementPlugin(/^node:/, resource => {
56+
resource.request = resource.request.replace(/^node:/, '');
57+
}),
58+
new compiler.webpack.ProvidePlugin(filter({
59+
Buffer: [require.resolve('buffer/'), 'Buffer'],
60+
process: require.resolve('process/browser'),
61+
TextEncoder: [require.resolve('text-encoding-utf-8'), 'TextEncoder'],
62+
TextDecoder: [require.resolve('text-encoding-utf-8'), 'TextDecoder'],
63+
})),
64+
);
5865

5966
compiler.options.resolve.fallback = {
6067
...filter({
6168
assert: require.resolve('assert/'),
6269
buffer: require.resolve('buffer/'),
6370
constants: require.resolve('constants-browserify'),
64-
crypto: require.resolve('./modules/crypto/index.js'),
71+
crypto: require.resolve('crypto-browserify'),
72+
/* eslint-disable-next-line camelcase */
73+
child_process: false,
6574
domain: require.resolve('domain-browser'),
6675
events: require.resolve('events/'),
76+
fs: false,
6777
http: require.resolve('./modules/http/index.js'),
6878
https: require.resolve('https-browserify'),
6979
os: require.resolve('os-browserify/browser'),
@@ -83,7 +93,7 @@ module.exports = class GopeedPolyfillPlugin {
8393
sys: require.resolve('util/'),
8494
timers: require.resolve('timers-browserify'),
8595
tty: require.resolve('tty-browserify'),
86-
url: require.resolve('url/'),
96+
url: require.resolve('./modules/url/index.js'),
8797
util: require.resolve('util/'),
8898
vm: require.resolve('./modules/vm/index.js'),
8999
zlib: require.resolve('browserify-zlib'),

modules/url/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var url = require("url/");
2+
3+
function isURLInstance(fileURLOrPath) {
4+
return fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin;
5+
}
6+
7+
function getPathFromURLPosix(url) {
8+
if (url.hostname !== "") {
9+
throw new TypeError('File URL host must be "localhost" or empty on darwin');
10+
}
11+
var pathname = url.pathname;
12+
for (var n = 0; n < pathname.length; n++) {
13+
if (pathname[n] === "%") {
14+
var third = pathname.codePointAt(n + 2) | 0x20;
15+
if (pathname[n + 1] === "2" && third === 102) {
16+
throw new TypeError(
17+
"File URL path must not include encoded / characters",
18+
);
19+
}
20+
}
21+
}
22+
return decodeURIComponent(pathname);
23+
}
24+
25+
function fileURLToPath(path) {
26+
if (typeof path === "string") {
27+
path = url.parse(path);
28+
} else if (!isURLInstance(path)) {
29+
throw new TypeError(
30+
'The "path" argument must be of type string or an instance of URL. Received ' +
31+
path,
32+
);
33+
}
34+
if (path.protocol !== "file:") {
35+
throw new TypeError("The URL must be of scheme file");
36+
}
37+
return getPathFromURLPosix(path);
38+
}
39+
40+
module.exports = { ...url, fileURLToPath };

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "gopeed-polyfill-webpack-plugin",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Polyfill Node.js core modules in Webpack.",
55
"repository": "GopeedLab/gopeed-polyfill-webpack-plugin",
66
"author": {
7-
"name": "monkeyWie",
8-
"email": "liwei-8466@qq.com"
7+
"name": "Levi",
8+
"email": "liwei2633@gmail.com"
99
},
1010
"license": "MIT",
1111
"main": "index.js",
@@ -30,29 +30,29 @@
3030
"assert": "^2.1.0",
3131
"browserify-zlib": "^0.2.0",
3232
"buffer": "^6.0.3",
33+
"builtin-status-codes": "^3.0.0",
3334
"console-browserify": "^1.2.0",
3435
"constants-browserify": "^1.0.0",
3536
"crypto-browserify": "^3.12.0",
3637
"domain-browser": "^4.22.0",
3738
"events": "^3.3.0",
3839
"https-browserify": "^1.0.0",
40+
"inherits": "^2.0.4",
3941
"os-browserify": "^0.3.0",
4042
"path-browserify": "^1.0.1",
4143
"process": "^0.11.10",
4244
"punycode": "^2.3.0",
4345
"querystring-es3": "^0.2.1",
4446
"readable-stream": "^3.6.0",
45-
"builtin-status-codes": "^3.0.0",
46-
"inherits": "^2.0.4",
47-
"xtend": "^4.0.2",
4847
"stream-browserify": "^3.0.0",
4948
"string_decoder": "^1.3.0",
49+
"text-encoding-utf-8": "^1.0.2",
5050
"timers-browserify": "^2.0.12",
5151
"tty-browserify": "^0.0.1",
5252
"type-fest": "^4.4.0",
5353
"url": "^0.11.3",
5454
"util": "^0.12.5",
55-
"vm-browserify": "^1.1.2"
55+
"xtend": "^4.0.2"
5656
},
5757
"devDependencies": {
5858
"ava": "^5.3.1",
@@ -71,4 +71,4 @@
7171
"modules/**"
7272
]
7373
}
74-
}
74+
}

0 commit comments

Comments
 (0)