UNPKG

1.86 kBJavaScriptView Raw
1const {warn} = require('./log');
2
3// stub core Node.js modules based on https://github.com/webpack/node-libs-browser/blob/master/index.js
4// no need stub for following modules, they got same name on npm package
5//
6// assert
7// buffer
8// events
9// punycode
10// process
11// string_decoder
12// url
13// util
14
15// fail on following core modules has no stub
16const UNAVAIABLE_CORE_MODULES = [
17 'child_process',
18 'cluster',
19 'dgram',
20 'dns',
21 // 'fs',
22 'net',
23 'readline',
24 'repl',
25 'tls',
26 'worker_thread'
27];
28
29const EMPTY_MODULE = 'define(function(){return {};});';
30
31module.exports = function(moduleId, resolve) {
32 // with subfix -browserify
33 if (['crypto', 'https', 'os', 'path', 'stream', 'timers', 'tty', 'vm'].indexOf(moduleId) !== -1) {
34 return {name: moduleId, location: resolve(`${moduleId}-browserify`)};
35 }
36
37 if (moduleId === 'domain') {
38 warn('core Node.js module "domain" is deprecated');
39 return {name: 'domain', location: resolve('domain-browser')};
40 }
41
42 if (moduleId === 'http') {
43 return {name: 'http', location: resolve('stream-http')};
44 }
45
46 if (moduleId === 'querystring') {
47 return {name: 'querystring', location: resolve('querystring-browser-stub')};
48 }
49
50 if (moduleId === 'sys') {
51 warn('core Node.js module "sys" is deprecated, the stub is disabled in CLI bundler due to conflicts with "util"');
52 return EMPTY_MODULE;
53 }
54
55 if (moduleId === 'zlib') {
56 return {name: 'zlib', location: resolve('browserify-zlib')};
57 }
58
59 if (moduleId === 'fs') {
60 return {name: 'fs', location: resolve('fs-browser-stub')};
61 }
62
63 if (UNAVAIABLE_CORE_MODULES.indexOf(moduleId) !== -1) {
64 return EMPTY_MODULE;
65 }
66
67 // https://github.com/defunctzombie/package-browser-field-spec
68 // {"module-a": false}
69 // replace with special placeholder __ignore__
70 if (moduleId === '__ignore__') {
71 return EMPTY_MODULE;
72 }
73};