-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nodeRequire): export nodeRequire utility from compiler
- Loading branch information
1 parent
03decfa
commit 10ea2fb
Showing
5 changed files
with
142 additions
and
91 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
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 |
---|---|---|
@@ -1,62 +1,66 @@ | ||
/** | ||
* Node builtin modules as of v14.5.0 | ||
*/ | ||
export const NODE_BUILTINS = [ | ||
'_http_agent', | ||
'_http_client', | ||
'_http_common', | ||
'_http_incoming', | ||
'_http_outgoing', | ||
'_http_server', | ||
'_stream_duplex', | ||
'_stream_passthrough', | ||
'_stream_readable', | ||
'_stream_transform', | ||
'_stream_wrap', | ||
'_stream_writable', | ||
'_tls_common', | ||
'_tls_wrap', | ||
'assert', | ||
'async_hooks', | ||
'buffer', | ||
'child_process', | ||
'cluster', | ||
'console', | ||
'constants', | ||
'crypto', | ||
'dgram', | ||
'dns', | ||
'domain', | ||
'events', | ||
'fs', | ||
'fs/promises', | ||
'http', | ||
'http2', | ||
'https', | ||
'inspector', | ||
'module', | ||
'net', | ||
'os', | ||
'path', | ||
'perf_hooks', | ||
'process', | ||
'punycode', | ||
'querystring', | ||
'readline', | ||
'repl', | ||
'stream', | ||
'string_decoder', | ||
'sys', | ||
'timers', | ||
'tls', | ||
'trace_events', | ||
'tty', | ||
'url', | ||
'util', | ||
'v8', | ||
'vm', | ||
'worker_threads', | ||
'zlib', | ||
]; | ||
|
||
export default class Module { | ||
static get builtinModules() { | ||
// as of node v14.2.0 | ||
return [ | ||
'_http_agent', | ||
'_http_client', | ||
'_http_common', | ||
'_http_incoming', | ||
'_http_outgoing', | ||
'_http_server', | ||
'_stream_duplex', | ||
'_stream_passthrough', | ||
'_stream_readable', | ||
'_stream_transform', | ||
'_stream_wrap', | ||
'_stream_writable', | ||
'_tls_common', | ||
'_tls_wrap', | ||
'assert', | ||
'async_hooks', | ||
'buffer', | ||
'child_process', | ||
'cluster', | ||
'console', | ||
'constants', | ||
'crypto', | ||
'dgram', | ||
'dns', | ||
'domain', | ||
'events', | ||
'fs', | ||
'fs/promises', | ||
'http', | ||
'http2', | ||
'https', | ||
'inspector', | ||
'module', | ||
'net', | ||
'os', | ||
'path', | ||
'perf_hooks', | ||
'process', | ||
'punycode', | ||
'querystring', | ||
'readline', | ||
'repl', | ||
'stream', | ||
'string_decoder', | ||
'sys', | ||
'timers', | ||
'tls', | ||
'trace_events', | ||
'tty', | ||
'url', | ||
'util', | ||
'v8', | ||
'vm', | ||
'worker_threads', | ||
'zlib', | ||
]; | ||
return NODE_BUILTINS; | ||
} | ||
} |
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,72 @@ | ||
import type { Diagnostic } from '../../declarations'; | ||
import { IS_NODE_ENV } from './environment'; | ||
import { loadTypeScriptDiagnostic, catchError } from '@utils'; | ||
import ts from 'typescript'; | ||
|
||
export const nodeRequire = (id: string) => { | ||
const results = { | ||
module: undefined as any, | ||
id, | ||
diagnostics: [] as Diagnostic[], | ||
}; | ||
|
||
if (IS_NODE_ENV) { | ||
try { | ||
const fs: typeof import('fs') = require('fs'); | ||
const path: typeof import('path') = require('path'); | ||
|
||
results.id = path.resolve(id); | ||
|
||
// ensure we cleared out node's internal require() cache for this file | ||
delete require.cache[results.id]; | ||
|
||
// let's override node's require for a second | ||
// don't worry, we'll revert this when we're done | ||
require.extensions['.ts'] = (module: NodeModuleWithCompile, fileName: string) => { | ||
let sourceText = fs.readFileSync(fileName, 'utf8'); | ||
|
||
if (fileName.endsWith('.ts')) { | ||
// looks like we've got a typed config file | ||
// let's transpile it to .js quick | ||
const tsResults = ts.transpileModule(sourceText, { | ||
fileName, | ||
compilerOptions: { | ||
module: ts.ModuleKind.CommonJS, | ||
moduleResolution: ts.ModuleResolutionKind.NodeJs, | ||
esModuleInterop: true, | ||
target: ts.ScriptTarget.ES2017, | ||
allowJs: true, | ||
}, | ||
}); | ||
sourceText = tsResults.outputText; | ||
|
||
results.diagnostics.push(...tsResults.diagnostics.map(loadTypeScriptDiagnostic)); | ||
} else { | ||
// quick hack to turn a modern es module | ||
// into and old school commonjs module | ||
sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1'); | ||
} | ||
|
||
try { | ||
module._compile(sourceText, fileName); | ||
} catch (e) { | ||
catchError(results.diagnostics, e); | ||
} | ||
}; | ||
|
||
// let's do this! | ||
results.module = require(results.id); | ||
|
||
// all set, let's go ahead and reset the require back to the default | ||
require.extensions['.ts'] = undefined; | ||
} catch (e) { | ||
catchError(results.diagnostics, e); | ||
} | ||
} | ||
|
||
return results; | ||
}; | ||
|
||
interface NodeModuleWithCompile extends NodeModule { | ||
_compile(code: string, filename: string): any; | ||
} |
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