From 17f02466c27abd4c933bea509df77085cc0b3ec1 Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Tue, 11 Aug 2015 12:46:13 -0400 Subject: [PATCH] Add Uniffe to reduce closure count xref #1309 --- gulpfile.js | 4 +++- package.json | 4 +++- utils/uniffe.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 utils/uniffe.js diff --git a/gulpfile.js b/gulpfile.js index dc85d01e..c0964192 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -24,6 +24,7 @@ var gulp = require('gulp'); var fs = require('fs'); var merge = require('merge-stream'); var $ = require('gulp-load-plugins')(); +var uniffe = require('./utils/uniffe.js'); var del = require('del'); var vinylPaths = require('vinyl-paths'); var runSequence = require('run-sequence'); @@ -217,11 +218,12 @@ gulp.task('scripts', ['jscs', 'jshint'], function() { 'src/ripple/ripple.js' ]; return gulp.src(sources) + .pipe(uniffe()) .pipe($.sourcemaps.init()) // Concatenate Scripts .pipe($.concat('material.js')) .pipe($.iife({ - useStrict: false, + useStrict: true, })) .pipe(gulp.dest('./dist')) // Minify Scripts diff --git a/package.json b/package.json index 17a75d64..5d735970 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,14 @@ "author": "Google", "repository": "google/material-design-lite", "devDependencies": { + "acorn": "^2.2.0", "apache-server-configs": "^2.7.1", "browser-sync": "^2.2.3", "chai": "^2.0.0", "chai-jquery": "^2.0.0", "del": "^1.1.1", "drool": "^0.2.1", + "escodegen": "^1.6.1", "gulp": "^3.8.11", "gulp-autoprefixer": "^2.0.0", "gulp-cache": "^0.2.6", @@ -61,7 +63,7 @@ "require-dir": "^0.3.0", "run-sequence": "^1.0.2", "swig": "^1.4.2", - "through2": "^0.6.3", + "through2": "^2.0.0", "vinyl-paths": "^1.0.0" }, "engines": { diff --git a/utils/uniffe.js b/utils/uniffe.js new file mode 100644 index 00000000..da6ccab0 --- /dev/null +++ b/utils/uniffe.js @@ -0,0 +1,60 @@ +/** + * + * Material Design Lite + * Copyright 2015 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + * + */ +'use strict'; + +var through = require('through2'); +var escodegen = require('escodegen'); +var acorn = require('acorn'); + +function uniffe(contents) { + var comments = []; + var tokens = []; + + var ast = acorn.parse(contents, { + ranges: true, + onComment: comments, + onToken: tokens + }); + + escodegen.attachComments(ast, comments, tokens); + + if (ast.body[0].expression.callee === undefined) { + return contents; + } + + var rootProgram = ast.body[0].expression.callee.body; + + rootProgram.type = 'Program'; + // drop use strict + rootProgram.body = rootProgram.body.slice(1); + // attach all leading comments from outside iffe + rootProgram.leadingComments = ast.body[0].leadingComments; + + return escodegen.generate(rootProgram, {comment: true}); +} + +module.exports = function() { + return through.obj(function(file, enc, cb) { + if (file.isBuffer()) { + file.contents = new Buffer(uniffe(file.contents.toString(enc)), enc); + } + + cb(null, file); + }); +};