Angular 2 + Zone.js + Common js module: IF stateme

2019-05-25 02:43发布

问题:

I tried a ton of checks. This definitely is an anomaly. I have an Angular 2 service that loads a @type definition (typescript 2) which in turn loads a commmon.js module (visionmedia/debug). Inside the common.js module I have a simple if statement that triggers an error even if the condition is false and code should not be executed. The Angular app uses system.js to load modules.

Regular Code if (false) { console.log('This code is not executed') } Normal behavior, nothing happens

Anomaly Code: if (false) { exports.humanize = require('ms'); } It triggers Error: zone.js:101 GET http://localhost:8002/ms.js 404 (Not Found)

The error itself is valid. Indeed that script is not to be found. What is deeply wrong is that it should not appear in the first place. It should be blocked by the false if condition. It seems to me that zone.js somehow parses the the instruction even if the case is false. What can I do to avoid this? I need to check wheter to require one path or another depending if the same script is called on server or on frontend.

Bigger picture inside the CJS module:

// Trying to detect if environment is node.js
// In backend (no zones) everything works as expected
// In frontend, the require('ms') statement is executed event if condition is false. 
// I checked manually if process is defined, it's not. 
// Event the basic `false` condition also fails to block code.
if (typeof process === 'undefined') { 
    exports.humanize = require('node_modules/ms/index.js');
    console.log('Browser');
} else {
    exports.humanize = require('ms'); // If I comment this code works as intended
    console.log('Node');
}

回答1:

Do you use systemjs? I had a similar situation with import-statements in multi-line comment-blocks. SystemJS uses a regular expression to detect import statements. Maybe it uses the same approach for detecting export statements.

It is not a problem within zone.js since zonejs just executes tasks. The task itself is triggered somewhere else.

EDIT (answer to comment):

I dont think you should conditionally execute export-statements.

Maybe this helps:

var myExport;
var myRequired;

if (something) {
  myExport = function() {
    console.log('exported this');
  };
  myRequired = require('something');
}
else {
  myExport = function() {
    console.log('exported something else');
  };
  myRequired = require('something-else');
}

exports.myExport = myExport;
exports.someMore = myRequired;


回答2:

Issue solved. It looks like zone.js has nothing to do with this issue. I have received help from system.js comunity. Check this answer.

@guybedford

This is the way that dependency analysis in SystemJS works via static parsing of CommonJS requires - it is the only way to make CommonJS support asynchronous loading. Try adding:

System.config({
    map: {
        ms: '@empty'
    }
});