-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bundler): stub core Node.js modules just like browserify and web…
…pack
- Loading branch information
Showing
5 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
const logger = require('aurelia-logging').getLogger('StubNodejs'); | ||
|
||
// stub core Node.js modules based on https://github.com/webpack/node-libs-browser/blob/master/index.js | ||
// no need stub for following modules, they got same name on npm package | ||
// | ||
// assert | ||
// buffer | ||
// events | ||
// punycode | ||
// process | ||
// string_decoder | ||
// url | ||
// util (note: got small problem on ./support/isBuffer, read util package.json browser field) | ||
|
||
// fail on following core modules has no stub | ||
const UNAVAIABLE_CORE_MODULES = [ | ||
'child_process', | ||
'cluster', | ||
'dgram', | ||
'dns', | ||
'fs', | ||
'net', | ||
'readline', | ||
'repl', | ||
'tls' | ||
]; | ||
|
||
const EMPTY_MODULE = 'define(function(){});'; | ||
|
||
// note all paths here assumes local node_modules folder | ||
// TODO use require.resolve to get correct node_modules folder. (to support yarn workspaces for instance) | ||
module.exports = function(moduleId) { | ||
// with subfix -browserify | ||
if (['crypto', 'https', 'os', 'path', 'stream', 'timers', 'tty', 'vm'].indexOf(moduleId) !== -1) { | ||
return {name: moduleId, path: `../node_modules/${moduleId}-browserify`}; | ||
} | ||
|
||
if (moduleId === 'domain') { | ||
logger.warn('core Node.js module "domain" is deprecated'); | ||
return {name: 'domain', path: '../node_modules/domain-browser'}; | ||
} | ||
|
||
if (moduleId === 'http') { | ||
return {name: 'http', path: '../node_modules/stream-http'}; | ||
} | ||
|
||
if (moduleId === 'querystring') { | ||
// using querystring-es3 next version 1.0.0-0 | ||
return {name: 'querystring', path: '../node_modules/querystring-es3'}; | ||
} | ||
|
||
if (moduleId === 'sys') { | ||
logger.warn('core Node.js module "sys" is deprecated, the stub is disabled in CLI bundler due to conflicts with "util"'); | ||
} | ||
|
||
if (moduleId === 'zlib') { | ||
return {name: 'zlib', path: '../node_modules/browserify-zlib'}; | ||
} | ||
|
||
if (UNAVAIABLE_CORE_MODULES.indexOf(moduleId) !== -1) { | ||
logger.warn(`No avaiable stub for core Node.js module "${moduleId}", stubbed with empty module`); | ||
return EMPTY_MODULE; | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const stubCoreNodejsModule = require('../../../lib/build/stub-core-nodejs-module'); | ||
|
||
describe('StubCoreNodejsModule', () => { | ||
it('stubs some core module with subfix -browserify', () => { | ||
expect(stubCoreNodejsModule('os')).toEqual({ | ||
name: 'os', | ||
path: '../node_modules/os-browserify' | ||
}); | ||
}); | ||
|
||
it('ignores sys', () => { | ||
expect(stubCoreNodejsModule('sys')).toBeUndefined(); | ||
}); | ||
|
||
it('stubs empty module for some core module', () => { | ||
expect(stubCoreNodejsModule('fs')).toBe('define(function(){});'); | ||
}); | ||
}); |