4 * Copyright 2015 Google Inc. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * https://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 var through = require('through2');
22 var escodegen = require('escodegen');
23 var acorn = require('acorn');
25 function uniffe(contents) {
29 var ast = acorn.parse(contents, {
35 escodegen.attachComments(ast, comments, tokens);
37 if (ast.body[0].expression === undefined ||
38 ast.body[0].expression.callee === undefined) {
42 var rootProgram = ast.body[0].expression.callee.body;
44 rootProgram.type = 'Program';
46 rootProgram.body = rootProgram.body.slice(1);
47 // attach all leading comments from outside iffe
48 rootProgram.leadingComments = ast.body[0].leadingComments;
50 return escodegen.generate(rootProgram, {comment: true});
53 module.exports = function() {
54 return through.obj(function(file, enc, cb) {
55 if (file.isBuffer()) {
56 file.contents = new Buffer(uniffe(file.contents.toString(enc)), enc);