Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Material Design Lite |
| 4 | * Copyright 2015 Google Inc. All rights reserved. |
| 5 | * |
| 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 |
| 9 | * |
| 10 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 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 |
| 17 | * |
| 18 | */ |
| 19 | 'use strict'; |
| 20 | |
| 21 | var through = require('through2'); |
| 22 | var escodegen = require('escodegen'); |
| 23 | var acorn = require('acorn'); |
| 24 | |
| 25 | function uniffe(contents) { |
| 26 | var comments = []; |
| 27 | var tokens = []; |
| 28 | |
| 29 | var ast = acorn.parse(contents, { |
| 30 | ranges: true, |
| 31 | onComment: comments, |
| 32 | onToken: tokens |
| 33 | }); |
| 34 | |
| 35 | escodegen.attachComments(ast, comments, tokens); |
| 36 | |
| 37 | if (ast.body[0].expression === undefined || |
| 38 | ast.body[0].expression.callee === undefined) { |
| 39 | return contents; |
| 40 | } |
| 41 | |
| 42 | var rootProgram = ast.body[0].expression.callee.body; |
| 43 | |
| 44 | rootProgram.type = 'Program'; |
| 45 | // drop use strict |
| 46 | rootProgram.body = rootProgram.body.slice(1); |
| 47 | // attach all leading comments from outside iffe |
| 48 | rootProgram.leadingComments = ast.body[0].leadingComments; |
| 49 | |
| 50 | return escodegen.generate(rootProgram, {comment: true}); |
| 51 | } |
| 52 | |
| 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); |
| 57 | } |
| 58 | |
| 59 | cb(null, file); |
| 60 | }); |
| 61 | }; |