├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '10' 4 | - '8' 5 | - '6' 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const through = require('through2'); 3 | const _ = require('lodash'); 4 | const rework = require('rework'); 5 | const PluginError = require('plugin-error'); 6 | const applySourceMap = require('vinyl-sourcemaps-apply'); 7 | 8 | const lastIsObject = _.flowRight(_.isPlainObject, _.last); 9 | 10 | module.exports = (...args) => { 11 | const options = Object.assign({}, lastIsObject(args) ? args.pop() : {}); 12 | const plugins = args; 13 | 14 | return through.obj((file, enc, cb) => { 15 | if (file.isNull()) { 16 | cb(null, file); 17 | return; 18 | } 19 | 20 | if (file.isStream()) { 21 | cb(new PluginError('gulp-rework', 'Streaming not supported')); 22 | return; 23 | } 24 | 25 | try { 26 | const ret = rework(file.contents.toString(), {source: file.path}); 27 | plugins.forEach(ret.use.bind(ret)); 28 | 29 | if (file.sourceMap) { 30 | options.sourcemap = true; 31 | options.inputSourcemap = false; 32 | options.sourcemapAsObject = true; 33 | } 34 | 35 | const result = ret.toString(options); 36 | if (file.sourceMap) { 37 | file.contents = Buffer.from(result.code); 38 | result.map.file = file.relative; 39 | result.map.sources = result.map.sources.map(() => file.relative); 40 | applySourceMap(file, result.map); 41 | } else { 42 | file.contents = Buffer.from(result); 43 | } 44 | 45 | cb(null, file); 46 | } catch (err) { 47 | cb(new PluginError('gulp-rework', err, {fileName: err.filename || file.path})); 48 | } 49 | }); 50 | }; 51 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-rework", 3 | "version": "4.0.0", 4 | "description": "Preprocess CSS with Rework", 5 | "license": "MIT", 6 | "repository": "sindresorhus/gulp-rework", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=6" 14 | }, 15 | "scripts": { 16 | "test": "xo && mocha" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "gulpplugin", 23 | "rework", 24 | "style", 25 | "stylesheet", 26 | "css", 27 | "preprocess", 28 | "preprocessor", 29 | "rework", 30 | "compile", 31 | "transform", 32 | "manipulation" 33 | ], 34 | "dependencies": { 35 | "lodash": "^4.8.2", 36 | "plugin-error": "^1.0.1", 37 | "rework": "^1.0.0", 38 | "through2": "^2.0.0", 39 | "vinyl-sourcemaps-apply": "^0.2.1" 40 | }, 41 | "devDependencies": { 42 | "mocha": "^5.1.1", 43 | "rework-plugin-at2x": "^1.0.1", 44 | "source-map": "^0.6.1", 45 | "vinyl": "^2.1.0", 46 | "xo": "*" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Deprecated 2 | 3 | The Rework project is no longer maintained. 4 | 5 | --- 6 | 7 | # gulp-rework [![Build Status](https://travis-ci.org/sindresorhus/gulp-rework.svg?branch=master)](https://travis-ci.org/sindresorhus/gulp-rework) 8 | 9 | > Preprocess CSS with [Rework](https://github.com/reworkcss/rework) 10 | 11 | *Issues with the output should be reported on the Rework [issue tracker](https://github.com/reworkcss/rework/issues).* 12 | 13 | 14 | ## Install 15 | 16 | ``` 17 | $ npm install --save-dev gulp-rework 18 | ``` 19 | 20 | 21 | ## Usage 22 | 23 | ```js 24 | const gulp = require('gulp'); 25 | const rework = require('gulp-rework'); 26 | const at2x = require('rework-plugin-at2x'); 27 | 28 | gulp.task('default', () => 29 | gulp.src('src/app.css') 30 | .pipe(rework(at2x(), {sourcemap: true})) 31 | .pipe(gulp.dest('dist')) 32 | ); 33 | ``` 34 | 35 | 36 | ## API 37 | 38 | The `compress` option from Rework is intentionally missing. A separate task like [gulp-csso](https://github.com/ben-eb/gulp-csso) will do a better job. 39 | 40 | ### rework(plugin, plugin, ..., [options]) 41 | 42 | Plugins are supplied as arguments.
43 | Optionally supply an object with options as the last argument. 44 | 45 | ### options 46 | 47 | Type: `Object` 48 | 49 | #### sourcemap 50 | 51 | Type: `boolean`
52 | Default: `false` 53 | 54 | 55 | ## License 56 | 57 | MIT © [Sindre Sorhus](https://sindresorhus.com) 58 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /* eslint-env mocha */ 3 | const assert = require('assert'); 4 | const at2x = require('rework-plugin-at2x'); 5 | const Vinyl = require('vinyl'); 6 | const sourcemap = require('source-map'); 7 | const rework = require('.'); 8 | 9 | it('should preprocess CSS using Rework', cb => { 10 | const stream = rework(at2x()); 11 | 12 | stream.on('data', data => { 13 | assert.equal( 14 | data.contents.toString(), 15 | '.logo {\n background-image: url(\'component.png\');\n width: 289px;\n height: 113px;\n}\n\n@media (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (-webkit-min-device-pixel-ratio: 1.5), (min-device-pixel-ratio: 1.5), (min-resolution: 144dpi), (min-resolution: 1.5dppx) {\n .logo {\n background-image: url("component@2x.png");\n background-size: contain;\n }\n}' 16 | ); 17 | cb(); 18 | }); 19 | 20 | stream.end(new Vinyl({ 21 | contents: Buffer.from('.logo{background-image:url(\'component.png\') at-2x;width:289px;height:113px;}') 22 | })); 23 | }); 24 | 25 | it('should support Source Map', cb => { 26 | const stream = rework(at2x(), {sourcemap: true}); 27 | 28 | stream.on('data', data => { 29 | assert.equal( 30 | data.contents.toString(), 31 | '.logo {\n background-image: url(\'component.png\');\n width: 289px;\n height: 113px;\n}\n\n@media (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (-webkit-min-device-pixel-ratio: 1.5), (min-device-pixel-ratio: 1.5), (min-resolution: 144dpi), (min-resolution: 1.5dppx) {\n .logo {\n background-image: url("component@2x.png");\n background-size: contain;\n }\n}\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInNvdXJjZS5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7RUFBTTtFQUE0QztFQUFZIiwic291cmNlc0NvbnRlbnQiOlsiLmxvZ297YmFja2dyb3VuZC1pbWFnZTp1cmwoJ2NvbXBvbmVudC5wbmcnKSBhdC0yeDt3aWR0aDoyODlweDtoZWlnaHQ6MTEzcHg7fSJdfQ== */' 32 | ); 33 | cb(); 34 | }); 35 | 36 | stream.end(new Vinyl({ 37 | contents: Buffer.from('.logo{background-image:url(\'component.png\') at-2x;width:289px;height:113px;}') 38 | })); 39 | }); 40 | 41 | function identityPlugin() { 42 | return () => {}; 43 | } 44 | 45 | it('should support gulp-sourcemap', cb => { 46 | const map = new sourcemap.SourceMapGenerator({file: 'src/styles/logo.css'}); 47 | map.addMapping({ 48 | generated: {line: 1, column: 0}, 49 | source: 'src/styles/mixin.scss', 50 | original: {line: 33, column: 2}, 51 | name: 'abc' 52 | }); 53 | 54 | const file = new Vinyl({ 55 | path: '/Users/me/code/src/styles/logo.css', 56 | base: '/Users/me/code', 57 | contents: Buffer.from('.logo{width:289px;}') 58 | }); 59 | file.sourceMap = map.toJSON(); 60 | 61 | const stream = rework(identityPlugin()); 62 | 63 | stream.on('data', data => { 64 | try { 65 | assert.equal( 66 | data.contents.toString(), 67 | '.logo {\n width: 289px;\n}' 68 | ); 69 | 70 | const map = new sourcemap.SourceMapConsumer(data.sourceMap); 71 | assert.deepEqual( 72 | map.originalPositionFor({line: 2, column: 2}), 73 | {line: 33, column: 2, source: 'src/styles/mixin.scss', name: 'abc'}); 74 | 75 | cb(); 76 | } catch (err) { 77 | cb(err); 78 | } 79 | }); 80 | 81 | stream.write(file); 82 | }); 83 | 84 | it('should not override given options', cb => { 85 | const map = new sourcemap.SourceMapGenerator({file: 'src/styles/logo.css'}); 86 | const file = new Vinyl({ 87 | path: 'src/styles/logo.css', 88 | contents: Buffer.from('.logo{width:289px;}') 89 | }); 90 | file.sourceMap = map.toJSON(); 91 | const options = {}; 92 | 93 | const stream = rework(identityPlugin(), options); 94 | 95 | stream.on('data', () => { 96 | try { 97 | assert.deepEqual( 98 | options, {} 99 | ); 100 | 101 | cb(); 102 | } catch (err) { 103 | cb(err); 104 | } 105 | }); 106 | 107 | stream.write(file); 108 | }); 109 | --------------------------------------------------------------------------------