Skip to content

Commit

Permalink
Fixing Node frontend.
Browse files Browse the repository at this point in the history
I needed an indirect way to reference 'require' without tripping up Webpack. My previous code didn't work properly.
  • Loading branch information
John Vilk committed Aug 18, 2016
1 parent 231bbfc commit c324416
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion Grunttasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ function getWebpackConfig(target: string, optimize: boolean = false): webpack.Co
* @param target release, dev, or fast-dev
*/
function getKarmaConfig(target: string, singleRun = false, browsers = ['Chrome']): karma.ConfigOptions {
return {
return <any> {
// base path, that will be used to resolve files and exclude
basePath: '.',
frameworks: ['jasmine'],
Expand All @@ -149,6 +149,7 @@ function getKarmaConfig(target: string, singleRun = false, browsers = ['Chrome']
autoWatch: true,
browsers: browsers,
captureTimeout: 60000,
concurrency: 1,
// Avoid hardcoding and cross-origin issues.
proxies: {
'/': 'http://localhost:8000/'
Expand Down
13 changes: 13 additions & 0 deletions src/global_require.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* The only way I could figure out how to get a handle on Node's 'require'
* function without confusing webpack. Using a Function constructor doesn't work,
* as require() isn't in its scope!
*
* Isolating in its own module so it doesn't mess up mangling in other modules.
*/
export default function getGlobalRequire(): Function {
const reqVar = eval('typeof(require)!=="undefined"?require:null');
return reqVar ? reqVar : function(moduleName: string): any {
throw new Error(`Cannot find module ${moduleName}`);
};
}
17 changes: 11 additions & 6 deletions src/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import ThreadPool from './threadpool';
import logging = require('./logging');
import JDKInfo = require('../vendor/java_home/jdk.json');
import global = require('./global');
import getGlobalRequire from './global_require';
declare var RELEASE: boolean;
if (typeof RELEASE === 'undefined') global.RELEASE = false;

Expand Down Expand Up @@ -103,6 +104,10 @@ class JVM {
private jitDisabled: boolean = false;
private dumpJITStats: boolean = false;

// Get the environment's require variable, indirectly.
// Hidden from webpack and other builders, as it confuses them.
private globalRequire: Function = null;

public static isReleaseBuild(): boolean {
return typeof(RELEASE) !== 'undefined' && RELEASE;
}
Expand Down Expand Up @@ -528,6 +533,9 @@ class JVM {
*/
private evalNativeModule(mod: string): any {
"use strict"; // Prevent eval from being terrible.
if (!this.globalRequire) {
this.globalRequire = getGlobalRequire();
}
var rv: any;
/**
* Called by the native method file. Registers the package's native
Expand All @@ -538,10 +546,7 @@ class JVM {
}
// Provide the natives with the Doppio API, if needed.
const DoppioJVM = require('./doppiojvm'),
globalRequire = global['require'],
savedRequire = typeof(globalRequire) !== 'undefined' ? globalRequire : function(moduleName: string): any {
throw new Error(`Cannot find module ${moduleName}`);
};
globalRequire = this.globalRequire;

/**
* An emulation of CommonJS require() for the modules.
Expand Down Expand Up @@ -570,9 +575,9 @@ class JVM {
case 'pako/lib/zlib/adler32':
return adler32;
case 'crypto':
return util.are_in_browser() ? null : savedRequire('crypto');
return util.are_in_browser() ? null : globalRequire('crypto');
default:
return savedRequire(name);
return globalRequire(name);
}
}
/**
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"src/fd_state.ts",
"src/gLong.ts",
"src/global.ts",
"src/global_require.ts",
"src/heap.ts",
"src/interfaces.ts",
"src/jar.ts",
Expand Down

0 comments on commit c324416

Please sign in to comment.