pax_global_header00006660000000000000000000000064123304632350014513gustar00rootroot0000000000000052 comment=0daa0d675907c2a43d808fb58edacdf2f83c6934 uglify-save-license-0.4.1/000077500000000000000000000000001233046323500153705ustar00rootroot00000000000000uglify-save-license-0.4.1/.gitignore000066400000000000000000000002631233046323500173610ustar00rootroot00000000000000actual/ # Node # https://github.com/github/gitignore/blob/master/Node.gitignore lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results npm-debug.log node_modules uglify-save-license-0.4.1/.jshintrc000066400000000000000000000001431233046323500172130ustar00rootroot00000000000000{ "camelcase": true, "indent": 2, "quotmark": "single", "browser": false, "node": true } uglify-save-license-0.4.1/.npmignore000066400000000000000000000000551233046323500173670ustar00rootroot00000000000000.travis.yml .jshintrc Gruntfile.coffee test/ uglify-save-license-0.4.1/.travis.yml000066400000000000000000000001721233046323500175010ustar00rootroot00000000000000language: node_js node_js: - "0.10" - "0.11" before_script: - npm install -g grunt-cli notifications: email: falseuglify-save-license-0.4.1/Gruntfile.coffee000066400000000000000000000035441233046323500205060ustar00rootroot00000000000000module.exports = (grunt) -> 'use strict' require('load-grunt-tasks') grunt semver = require 'semver' pkg = grunt.file.readJSON 'package.json' getNextVersion = -> currentVer = pkg.version semver.inc currentVer, 'patch' grunt.initConfig jshint: options: jshintrc: '.jshintrc' main: files: src: ['uglify-save-license.js'] test: files: src: '<%= nodeunit.all %>' clean: test: src: ['test/actual/*.js'] replace: version: options: prefix: ' v' preservePrefix: true patterns: [ match: pkg.version replacement: getNextVersion() ] files: 'uglify-save-license.js': ['uglify-save-license.js'] year: options: prefix: '2013 - ' preservePrefix: true patterns: [ match: "#{ new Date().getFullYear() - 1 }" replacement: "#{ new Date().getFullYear() }" ] files: 'uglify-save-license.js': ['uglify-save-license.js'] uglify: options: preserveComments: -> args = grunt.util.toArray arguments require('./uglify-save-license.js') args... fixture: files: [ expand: true cwd: 'test/fixtures' src: '{,*/}*.js' dest: 'test/actual' ] nodeunit: options: reporter: 'default' all: ['test/test.js'] watch: main: files: ['uglify-save-license.js'] tasks: ['build'] release: {} grunt.registerTask 'test', [ 'jshint' 'clean' 'uglify' 'nodeunit' ] grunt.registerTask 'build', ['replace', 'test'] grunt.registerTask 'default', ['build', 'watch'] grunt.registerTask 'publish', ['build', 'release:patch'] uglify-save-license-0.4.1/LICENSE000066400000000000000000000021051233046323500163730ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2013 - 2014 Shinnosuke Watanabe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. uglify-save-license-0.4.1/README.md000066400000000000000000000121411233046323500166460ustar00rootroot00000000000000# uglify-save-license [![NPM version](https://badge.fury.io/js/uglify-save-license.svg)](http://badge.fury.io/js/uglify-save-license) [![Build Status](https://travis-ci.org/shinnn/uglify-save-license.svg)](https://travis-ci.org/shinnn/uglify-save-license) [![devDependency Status](https://david-dm.org/shinnn/uglify-save-license/dev-status.svg)](https://david-dm.org/shinnn/uglify-save-license#info=devDependencies) A support module for [UglifyJS](http://lisperator.net/uglifyjs/) to detect and preserve license comments ```javascript // Backbone.js 1.1.2 // (c) 2010-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors // Backbone may be freely distributed under the MIT license. // For all details and documentation: // http://backbonejs.org (function(root, factory) { // Set up Backbone appropriately for the environment. Start with AMD. if (typeof define === 'function' && define.amd) { define(['underscore', 'jquery', 'exports'], function(_, $, exports) { //... ``` ↓ ```javascript // Backbone.js 1.1.2 // (c) 2010-2014 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors // Backbone may be freely distributed under the MIT license. // For all details and documentation: // http://backbonejs.org !function(a,b){if("function"==typeof define&&define.amd)define(["underscore","jquery","exports"],function(c,d,e){a.Backbone=b(a,e,c,d)});else if("undefined"!=typeof exports){... ``` ## Overview This module enables us to preserve license comments when using UglifyJS. Even if the license statement is in multiple line comments, or the comment has no directive such as `@license` and `/*!`, this module keeps them readable. ## Installation Install with [npm](https://npmjs.org/). (Make sure you have installed [Node](http://nodejs.org/download/).) ``` npm install --save-dev uglify-save-license ``` ## Usage First of all, load `uglify-save-license` module. ```javascript var saveLicense = require('uglify-save-license'); ``` ### Use with [UglifyJS](https://github.com/mishoo/UglifyJS2) Pass this module to the [`comments` option](https://github.com/mishoo/UglifyJS2#keeping-comments-in-the-output). ```javascript var result = UglifyJS.minify('file1.js', { output: { comments: saveLicense } }); ``` ### Use with [grunt-contrib-uglify](https://github.com/gruntjs/grunt-contrib-uglify) Pass this module to the [`preserveComments` option](https://github.com/gruntjs/grunt-contrib-uglify#preservecomments). ```javascript grunt.initConfig({ uglify: { my_target: { options: { preserveComments: saveLicense }, src: ['src/app.js'], dest: 'dest/app.min.js' } } }); ``` ## How it works *uglify-save-license* checks each [comment token](http://lisperator.net/uglifyjs/ast#tokens) of a JavaScript file. The comment will be regarded as a license statement and preserved after compression, if it meets at least one of the following requirements: 1. The comment is in the *first* line of a file. 2. [The regexp for license statement](./uglify-save-license.js#L7) matches the string of the comment. It matches, for example, `MIT` and `Copyright`. 3. There is a comment at the *previous* line, and it matches 1. 2. or 3. ## Examples ### CLI tool example #### Main script (`uglify-example.js`) ```javascript #!/usr/bin/env node var UglifyJS = require('uglify-js'), saveLicense = require('uglify-save-license'); var minified = UglifyJS.minify(process.argv[2], { output: { comments: saveLicense } }).code; console.log(minified); ``` #### Target file ```javascript // First line // (c) 2014 John <- contains '(c)' // The previous line is preserved // This line won't be preserved. (function(win, doc) { var str = 'Hello World! :' + doc.title; // This line will not, too. console.log(str); }(window, document)); ``` #### Command ``` node uglify-example.js ``` #### Output ```javascript // First line // (c) 2014 John <- contains '(c)' // The previous line is preserved !function(o,l){var n="Hello World! :"+l.title;console.log(n)}(window,document); ``` ### [Gruntfile.coffee](http://gruntjs.com/getting-started#the-gruntfile) example ```coffeescript module.exports = (grunt) -> grunt.loadNpmTasks 'grunt-contrib-uglify' grunt.loadNpmTasks 'grunt-contrib-concat' grunt.loadNpmTasks 'grunt-contrib-clean' grunt.initConfig uglify: target: options: preserveComments: require 'uglify-save-license' files: [ expand: true flatten: true cwd: 'path/to/src' src: ['**/*.js'] dest: 'tmp/' ] concat: js: src: ['tmp/*.js'] dest: 'path/to/build/app.js' clean: tmpdir: ['tmp'] grunt.registerTask 'default' ['uglify', 'concat', 'clean'] ``` ## Acknowledgements *uglify-save-license* is inspired by [grunt-license-saver](https://github.com/kyo-ago/grunt-license-saver) and I used it as reference. Thanks, [kyo-ago](https://github.com/kyo-ago). ## License Copyright (c) 2013 - 2014 [Shinnosuke Watanabe](https://github.com/shinnn) Licensed under [the MIT license](./LICENSE). uglify-save-license-0.4.1/package.json000066400000000000000000000023241233046323500176570ustar00rootroot00000000000000{ "name": "uglify-save-license", "version": "0.4.1", "description": "License detector for UglifyJS", "main": "./uglify-save-license.js", "scripts": { "test": "grunt build" }, "repository": { "type": "git", "url": "https://github.com/shinnn/uglify-save-license.git" }, "author": { "name": "Shinnosuke Watanabe", "url": "https://github.com/shinnn" }, "readmeFilename": "README.md", "licenses": [ { "type": "MIT", "url": "https://github.com/shinnn/uglify-save-license/blob/master/LICENSE" } ], "keywords": [ "uglify", "compression", "minification", "comment", "license", "copyright", "detection", "preservation", "banner" ], "bugs": { "url": "https://github.com/shinnn/uglify-save-license/issues" }, "homepage": "https://github.com/shinnn/uglify-save-license", "devDependencies": { "grunt": "^0.4.4", "load-grunt-tasks": "^0.4.0", "grunt-contrib-jshint": "^0.10.0", "grunt-contrib-watch": "^0.6.1", "grunt-contrib-uglify": "^0.4.0", "grunt-contrib-clean": "^0.5.0", "grunt-contrib-nodeunit": "^0.3.3", "grunt-release": "^0.7.0", "grunt-replace": "^0.7.7", "semver": "^2.2.1" } } uglify-save-license-0.4.1/test/000077500000000000000000000000001233046323500163475ustar00rootroot00000000000000uglify-save-license-0.4.1/test/expected/000077500000000000000000000000001233046323500201505ustar00rootroot00000000000000uglify-save-license-0.4.1/test/expected/block-series.js000066400000000000000000000000451233046323500230670ustar00rootroot00000000000000/* First block */ /* Second block */ uglify-save-license-0.4.1/test/expected/first-block.js000066400000000000000000000000221233046323500227170ustar00rootroot00000000000000/* First block */ uglify-save-license-0.4.1/test/expected/first-line.js000066400000000000000000000000161233046323500225570ustar00rootroot00000000000000// First line uglify-save-license-0.4.1/test/expected/line-after-newline.js000066400000000000000000000000001233046323500241610ustar00rootroot00000000000000uglify-save-license-0.4.1/test/expected/line-series.js000066400000000000000000000000351233046323500227230ustar00rootroot00000000000000// First line // Second line uglify-save-license-0.4.1/test/expected/regexp/000077500000000000000000000000001233046323500214425ustar00rootroot00000000000000uglify-save-license-0.4.1/test/expected/regexp/at_cc_on.js000066400000000000000000000000751233046323500235470ustar00rootroot00000000000000// @cc_on directive // @cc_on This line should be preserved. uglify-save-license-0.4.1/test/expected/regexp/at_preserve.js000066400000000000000000000001031233046323500243110ustar00rootroot00000000000000// @preserve directive // @preserve This line should be preserved. uglify-save-license-0.4.1/test/expected/regexp/copyright.js000066400000000000000000000001071233046323500240060ustar00rootroot00000000000000// Copyright string // Copyright This line should be preserved. uglify-save-license-0.4.1/test/expected/regexp/copyright_mark.js000066400000000000000000000000701233046323500250170ustar00rootroot00000000000000// Copyright mark // (c) This line should be preserved. uglify-save-license-0.4.1/test/expected/regexp/mit.js000066400000000000000000000000671233046323500225740ustar00rootroot00000000000000// MIT license // (MIT) This line should be preserved. uglify-save-license-0.4.1/test/expected/separated-block-license.js000066400000000000000000000000531233046323500251640ustar00rootroot00000000000000/* First block */ /* Second block (MIT) */ uglify-save-license-0.4.1/test/expected/separated-block.js000066400000000000000000000000221233046323500235400ustar00rootroot00000000000000/* First block */ uglify-save-license-0.4.1/test/expected/separated-line-license.js000066400000000000000000000000411233046323500250160ustar00rootroot00000000000000// First line // (c) Second line uglify-save-license-0.4.1/test/expected/separated-line.js000066400000000000000000000000161233046323500234000ustar00rootroot00000000000000// First line uglify-save-license-0.4.1/test/expected/start_with_bang/000077500000000000000000000000001233046323500233275ustar00rootroot00000000000000uglify-save-license-0.4.1/test/expected/start_with_bang/block_bang.js000066400000000000000000000001111233046323500257370ustar00rootroot00000000000000// Start with bang (block comment) /*! This line should be preserved. */ uglify-save-license-0.4.1/test/expected/start_with_bang/line_bang.js000066400000000000000000000001141233046323500255770ustar00rootroot00000000000000// Start with bang (line comment) // Any comments should not exist below. uglify-save-license-0.4.1/test/fixtures/000077500000000000000000000000001233046323500202205ustar00rootroot00000000000000uglify-save-license-0.4.1/test/fixtures/block-series.js000066400000000000000000000000451233046323500231370ustar00rootroot00000000000000/* First block */ /* Second block */ uglify-save-license-0.4.1/test/fixtures/first-block.js000066400000000000000000000000221233046323500227670ustar00rootroot00000000000000/* First block */ uglify-save-license-0.4.1/test/fixtures/first-line.js000066400000000000000000000000161233046323500226270ustar00rootroot00000000000000// First line uglify-save-license-0.4.1/test/fixtures/line-after-newline.js000066400000000000000000000000421233046323500242370ustar00rootroot00000000000000 // First line after a line break uglify-save-license-0.4.1/test/fixtures/line-series.js000066400000000000000000000000351233046323500227730ustar00rootroot00000000000000// First line // Second line uglify-save-license-0.4.1/test/fixtures/regexp/000077500000000000000000000000001233046323500215125ustar00rootroot00000000000000uglify-save-license-0.4.1/test/fixtures/regexp/at_cc_on.js000066400000000000000000000000751233046323500236170ustar00rootroot00000000000000// @cc_on directive // @cc_on This line should be preserved.uglify-save-license-0.4.1/test/fixtures/regexp/at_preserve.js000066400000000000000000000001031233046323500243610ustar00rootroot00000000000000// @preserve directive // @preserve This line should be preserved.uglify-save-license-0.4.1/test/fixtures/regexp/copyright.js000066400000000000000000000001101233046323500240500ustar00rootroot00000000000000// Copyright string // Copyright This line should be preserved. uglify-save-license-0.4.1/test/fixtures/regexp/copyright_mark.js000066400000000000000000000000711233046323500250700ustar00rootroot00000000000000// Copyright mark // (c) This line should be preserved. uglify-save-license-0.4.1/test/fixtures/regexp/mit.js000066400000000000000000000000701233046323500226360ustar00rootroot00000000000000// MIT license // (MIT) This line should be preserved. uglify-save-license-0.4.1/test/fixtures/separated-block-license.js000066400000000000000000000000541233046323500252350ustar00rootroot00000000000000/* First block */ /* Second block (MIT) */ uglify-save-license-0.4.1/test/fixtures/separated-block.js000066400000000000000000000000701233046323500236130ustar00rootroot00000000000000/* First block */ /* This block should not be saved */ uglify-save-license-0.4.1/test/fixtures/separated-line-license.js000066400000000000000000000000421233046323500250670ustar00rootroot00000000000000// First line // (c) Second line uglify-save-license-0.4.1/test/fixtures/separated-line.js000066400000000000000000000000611233046323500234500ustar00rootroot00000000000000// First line // This line should not be saved. uglify-save-license-0.4.1/test/fixtures/start_with_bang/000077500000000000000000000000001233046323500233775ustar00rootroot00000000000000uglify-save-license-0.4.1/test/fixtures/start_with_bang/block_bang.js000066400000000000000000000001121233046323500260100ustar00rootroot00000000000000// Start with bang (block comment) /*! This line should be preserved. */ uglify-save-license-0.4.1/test/fixtures/start_with_bang/line_bang.js000066400000000000000000000001641233046323500256540ustar00rootroot00000000000000// Start with bang (line comment) // Any comments should not exist below. //! This line should not be preserved. uglify-save-license-0.4.1/test/test.js000066400000000000000000000007631233046323500176720ustar00rootroot00000000000000'use strict'; var path = require('path'); var grunt = require('grunt'); var files = grunt.file.expandMapping ( '{,*/,*/*/}*.js', 'test/expected', { cwd: 'test/actual' } ); function exportTests (map) { var basename = path.basename(map.src); exports[basename] = function (test) { var actual = grunt.file.read(map.src); var expected = grunt.file.read(map.dest); test.strictEqual( actual, expected ); test.done(); }; } files.forEach(exportTests); uglify-save-license-0.4.1/uglify-save-license.js000066400000000000000000000016051233046323500216030ustar00rootroot00000000000000// uglify-save-license.js v0.4.1 // Copyright (c) 2013 - 2014 Shinnosuke Watanabe // Licensed uder the MIT license 'use strict'; var licenseRegexp = /@preserve|@cc_on|\bMIT\b|\bMPL\b|\bGPL\b|\bBSD\b|\bISCL\b|\(c\)|License|Copyright/mi; // number of line where license comment appeared last var prevCommentLine = 0; // name of the file minified last var prevFile = ''; module.exports = function saveLicense(node, comment) { if (comment.file !== prevFile) { prevCommentLine = 0; } var isLicense = licenseRegexp.test(comment.value) || (comment.type === 'comment2' && comment.value.charAt(0) === '!') || comment.line === 1 || comment.line === prevCommentLine + 1; if (isLicense) { prevCommentLine = comment.line; } else { prevCommentLine = 0; } prevFile = comment.file; return isLicense; };