Skip to content

Commit

Permalink
Exempt code containing goog.base from wrapper.
Browse files Browse the repository at this point in the history
When @provideGoog is in the code, that is a sign that the user sent JS prefixed with the goog.base code and they don't need the loadModule wrapper.
  • Loading branch information
johnjbarton authored Dec 1, 2017
1 parent ae87aba commit 52b8d46
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/googmodule.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
var path = require('path');

// If the code has goog.module we need to wrap it.
var MODULE_REGEXP = /(^|;)\s*goog\.module\s*\(\s*['""]/m;
// If the code already has goog.base no wrapper even if goog.module.
var GOOGBASE_REGEXP = /^ \* @provideGoog *$/m;

var createPreprocesor = function(logger, /* config.basePath */ basePath) {
var log = logger.create('preprocessor.googmodule');

return function(content, file, done) {
if (path.extname(file.originalPath) !== '.js' ||
// Only check the first 10k chars, for performance reasons, to avoid
// degenerate behaviour on very large JS files. That's enough to allow
// for the Apache license, docs, and similar headers.
!MODULE_REGEXP.test(content.substring(0, 10000))) {
if (path.extname(file.originalPath) !== '.js') {
return done(content);
}
// Only check the first 10k chars, for performance reasons, to avoid
// degenerate behaviour on very large JS files. That's enough to allow
// for the Apache license, docs, and similar headers.
var first10k = content.substring(0, 10000);
if (!MODULE_REGEXP.test(first10k) || GOOGBASE_REGEXP.test(first10k) {
return done(content);
}

Expand Down

0 comments on commit 52b8d46

Please sign in to comment.