From 70376d1825887feac0551339907bef66aa8773ec Mon Sep 17 00:00:00 2001 From: lackhurt Date: Sun, 15 Jun 2014 12:46:01 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=9F=E6=88=90=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gruntfile.js | 73 ++++++++++++++++++++++++++++ LICENSE-MIT | 22 +++++++++ README.md | 89 +++++++++++++++++++++++++++++++++++ package.json | 41 ++++++++++++++++ tasks/css_import.js | 50 ++++++++++++++++++++ test/css_import_test.js | 48 +++++++++++++++++++ test/expected/custom_options | 1 + test/expected/default_options | 1 + test/fixtures/123 | 1 + test/fixtures/testing | 1 + 10 files changed, 327 insertions(+) create mode 100644 Gruntfile.js create mode 100644 LICENSE-MIT create mode 100644 README.md create mode 100644 package.json create mode 100644 tasks/css_import.js create mode 100644 test/css_import_test.js create mode 100644 test/expected/custom_options create mode 100644 test/expected/default_options create mode 100644 test/fixtures/123 create mode 100644 test/fixtures/testing diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..429c056 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,73 @@ +/* + * css-import + * https://github.com/lackhurt/gruntjs + * + * Copyright (c) 2014 lackhurt + * Licensed under the MIT license. + */ + +'use strict'; + +module.exports = function(grunt) { + + // Project configuration. + grunt.initConfig({ + jshint: { + all: [ + 'Gruntfile.js', + 'tasks/*.js', + '<%= nodeunit.tests %>' + ], + options: { + jshintrc: '.jshintrc' + } + }, + + // Before generating any new files, remove any previously-created files. + clean: { + tests: ['tmp'] + }, + + // Configuration to be run (and then tested). + css_import: { + default_options: { + options: { + }, + files: { + 'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123'] + } + }, + custom_options: { + options: { + separator: ': ', + punctuation: ' !!!' + }, + files: { + 'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123'] + } + } + }, + + // Unit tests. + nodeunit: { + tests: ['test/*_test.js'] + } + + }); + + // Actually load this plugin's task(s). + grunt.loadTasks('tasks'); + + // These plugins provide necessary tasks. + grunt.loadNpmTasks('grunt-contrib-jshint'); + grunt.loadNpmTasks('grunt-contrib-clean'); + grunt.loadNpmTasks('grunt-contrib-nodeunit'); + + // Whenever the "test" task is run, first clean the "tmp" dir, then run this + // plugin's task(s), then test the result. + grunt.registerTask('test', ['clean', 'css_import', 'nodeunit']); + + // By default, lint and run all tests. + grunt.registerTask('default', ['jshint', 'test']); + +}; diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..514a708 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2014 lackhurt + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..891f6e3 --- /dev/null +++ b/README.md @@ -0,0 +1,89 @@ +# css-import + +> Concat the css file by "@import". + +## Getting Started +This plugin requires Grunt `~0.4.5` + +If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command: + +```shell +npm install css-import --save-dev +``` + +Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript: + +```js +grunt.loadNpmTasks('css-import'); +``` + +## The "css_import" task + +### Overview +In your project's Gruntfile, add a section named `css_import` to the data object passed into `grunt.initConfig()`. + +```js +grunt.initConfig({ + css_import: { + options: { + // Task-specific options go here. + }, + your_target: { + // Target-specific file lists and/or options go here. + }, + }, +}); +``` + +### Options + +#### options.separator +Type: `String` +Default value: `', '` + +A string value that is used to do something with whatever. + +#### options.punctuation +Type: `String` +Default value: `'.'` + +A string value that is used to do something else with whatever else. + +### Usage Examples + +#### Default Options +In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.` + +```js +grunt.initConfig({ + css_import: { + options: {}, + files: { + 'dest/default_options': ['src/testing', 'src/123'], + }, + }, +}); +``` + +#### Custom Options +In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!` + +```js +grunt.initConfig({ + css_import: { + options: { + separator: ': ', + punctuation: ' !!!', + }, + files: { + 'dest/default_options': ['src/testing', 'src/123'], + }, + }, +}); +``` + +## Contributing +In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/). + +## Release History +_(Nothing yet)_ diff --git a/package.json b/package.json new file mode 100644 index 0000000..725458b --- /dev/null +++ b/package.json @@ -0,0 +1,41 @@ +{ + "name": "css-import", + "description": "Concat the css file by \"@import\".", + "version": "0.1.0", + "homepage": "https://github.com/lackhurt/gruntjs", + "author": { + "name": "lackhurt", + "email": "pangjn@yeah.net" + }, + "repository": { + "type": "git", + "url": "git://github.com/lackhurt/gruntjs.git" + }, + "bugs": { + "url": "https://github.com/lackhurt/gruntjs/issues" + }, + "licenses": [ + { + "type": "MIT", + "url": "https://github.com/lackhurt/gruntjs/blob/master/LICENSE-MIT" + } + ], + "engines": { + "node": ">= 0.8.0" + }, + "scripts": { + "test": "grunt test" + }, + "devDependencies": { + "grunt-contrib-jshint": "^0.9.2", + "grunt-contrib-clean": "^0.5.0", + "grunt-contrib-nodeunit": "^0.3.3", + "grunt": "~0.4.5" + }, + "peerDependencies": { + "grunt": "~0.4.5" + }, + "keywords": [ + "gruntplugin" + ] +} \ No newline at end of file diff --git a/tasks/css_import.js b/tasks/css_import.js new file mode 100644 index 0000000..1fa11d5 --- /dev/null +++ b/tasks/css_import.js @@ -0,0 +1,50 @@ +/* + * css-import + * https://github.com/lackhurt/gruntjs + * + * Copyright (c) 2014 lackhurt + * Licensed under the MIT license. + */ + +'use strict'; + +module.exports = function(grunt) { + + // Please see the Grunt documentation for more information regarding task + // creation: http://gruntjs.com/creating-tasks + + grunt.registerMultiTask('css_import', 'Concat the css file by "@import".', function() { + // Merge task-specific and/or target-specific options with these defaults. + var options = this.options({ + punctuation: '.', + separator: ', ' + }); + + // Iterate over all specified file groups. + this.files.forEach(function(f) { + // Concat specified files. + var src = f.src.filter(function(filepath) { + // Warn on and remove invalid source files (if nonull was set). + if (!grunt.file.exists(filepath)) { + grunt.log.warn('Source file "' + filepath + '" not found.'); + return false; + } else { + return true; + } + }).map(function(filepath) { + // Read file source. + return grunt.file.read(filepath); + }).join(grunt.util.normalizelf(options.separator)); + + // Handle options. + src += options.punctuation; + + // Write the destination file. + grunt.file.write(f.dest, src); + + // Print a success message. + grunt.log.writeln('File "' + f.dest + '" created.'); + }); + }); + +}; diff --git a/test/css_import_test.js b/test/css_import_test.js new file mode 100644 index 0000000..55e4a70 --- /dev/null +++ b/test/css_import_test.js @@ -0,0 +1,48 @@ +'use strict'; + +var grunt = require('grunt'); + +/* + ======== A Handy Little Nodeunit Reference ======== + https://github.com/caolan/nodeunit + + Test methods: + test.expect(numAssertions) + test.done() + Test assertions: + test.ok(value, [message]) + test.equal(actual, expected, [message]) + test.notEqual(actual, expected, [message]) + test.deepEqual(actual, expected, [message]) + test.notDeepEqual(actual, expected, [message]) + test.strictEqual(actual, expected, [message]) + test.notStrictEqual(actual, expected, [message]) + test.throws(block, [error], [message]) + test.doesNotThrow(block, [error], [message]) + test.ifError(value) +*/ + +exports.css_import = { + setUp: function(done) { + // setup here if necessary + done(); + }, + default_options: function(test) { + test.expect(1); + + var actual = grunt.file.read('tmp/default_options'); + var expected = grunt.file.read('test/expected/default_options'); + test.equal(actual, expected, 'should describe what the default behavior is.'); + + test.done(); + }, + custom_options: function(test) { + test.expect(1); + + var actual = grunt.file.read('tmp/custom_options'); + var expected = grunt.file.read('test/expected/custom_options'); + test.equal(actual, expected, 'should describe what the custom option(s) behavior is.'); + + test.done(); + }, +}; diff --git a/test/expected/custom_options b/test/expected/custom_options new file mode 100644 index 0000000..e597128 --- /dev/null +++ b/test/expected/custom_options @@ -0,0 +1 @@ +Testing: 1 2 3 !!! \ No newline at end of file diff --git a/test/expected/default_options b/test/expected/default_options new file mode 100644 index 0000000..5f8b72f --- /dev/null +++ b/test/expected/default_options @@ -0,0 +1 @@ +Testing, 1 2 3. \ No newline at end of file diff --git a/test/fixtures/123 b/test/fixtures/123 new file mode 100644 index 0000000..703ca85 --- /dev/null +++ b/test/fixtures/123 @@ -0,0 +1 @@ +1 2 3 \ No newline at end of file diff --git a/test/fixtures/testing b/test/fixtures/testing new file mode 100644 index 0000000..0a90125 --- /dev/null +++ b/test/fixtures/testing @@ -0,0 +1 @@ +Testing \ No newline at end of file