├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 James Kyle 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # postcss-asset-url 2 | 3 | Map urls to different locations 4 | 5 | ### Install 6 | 7 | ```sh 8 | $ npm install postcss-asset-url 9 | ``` 10 | 11 | ### Usage 12 | 13 | ##### Config 14 | 15 | ```js 16 | var postcss = require("postcss"); 17 | var assetUrl = require("postcss-asset-url"); 18 | 19 | var output = postcss() 20 | .use(assetUrl({ 21 | map: { 22 | image: "/assets/images/", 23 | font: "/assets/font", 24 | icon: "/assets/icon" 25 | } 26 | })) 27 | .process(css) 28 | .css; 29 | ``` 30 | 31 | ##### Input 32 | 33 | ```css 34 | .app { 35 | background-image: url(asset-url("image", "background.png")); 36 | } 37 | 38 | .icon-wrench { 39 | background-image: url(asset-url("icon", "wrench.png")); 40 | } 41 | 42 | @font-face { 43 | font-family: "Open Sans"; 44 | src: url(asset-url("font", "opensans.eot")); 45 | src: url(asset-url("font", "opensans.eot?#iefix")) format("embedded-opentype"), 46 | url(asset-url("font", "opensans.woff2")) format("woff2"), 47 | url(asset-url("font", "opensans.woff")) format("woff"), 48 | url(asset-url("font", "opensans.ttf")) format("truetype"); 49 | font-weight: normal; 50 | font-style: normal; 51 | } 52 | ``` 53 | 54 | ##### Output 55 | 56 | ```css 57 | .app { 58 | background-image: url("/assets/images/background.png")); 59 | } 60 | 61 | .icon-wrench { 62 | background-image: url("/assets/icons/wrench.png")); 63 | } 64 | 65 | @font-face { 66 | font-family: "Open Sans"; 67 | src: url("/assets/fonts/opensans.eot"); 68 | src: url("/assets/fonts/opensans.eot?#iefix") format("embedded-opentype"), 69 | url("/assets/fonts/opensans.woff2") format("woff2"), 70 | url("/assets/fonts/opensans.woff") format("woff"), 71 | url("/assets/fonts/opensans.ttf") format("truetype"); 72 | font-weight: normal; 73 | font-style: normal; 74 | } 75 | ``` 76 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var reduceFunctionCall = require('reduce-function-call'); 2 | var helpers = require('postcss-message-helpers'); 3 | var postcss = require('postcss'); 4 | 5 | var CONTAINS_ASSET_URL = /asset-url\(.*\)/; 6 | var ARGS_SPLITTER = /, */g; 7 | var QUOTES_REPLACER = /['"]/g; 8 | 9 | function parseAssetUrl(str) { 10 | var args = str.split(ARGS_SPLITTER); 11 | 12 | if (args.length !== 2) { 13 | throw new SyntaxError('Wrong number of arguments passed to `asset-url()`. Expected 2, got ' + args.length); 14 | } 15 | 16 | var type = args[0].replace(QUOTES_REPLACER, ''); 17 | var value = args[1].replace(QUOTES_REPLACER, ''); 18 | 19 | return { 20 | type: type, 21 | value: value 22 | }; 23 | } 24 | 25 | function reduceAssetUrl(value, map) { 26 | return reduceFunctionCall(value, 'asset-url', function(str) { 27 | var result = parseAssetUrl(str); 28 | var prefix = map[result.type]; 29 | 30 | if (!prefix) { 31 | throw new SyntaxError('Invalid type for `asset-url()`: ' + result.type); 32 | } 33 | 34 | return prefix + result.value; 35 | }); 36 | } 37 | 38 | function transformRule(node, map) { 39 | var value = node.value; 40 | 41 | if (!value || !CONTAINS_ASSET_URL.test(value)) { 42 | return; 43 | } 44 | 45 | node.value = reduceAssetUrl(value, map); 46 | } 47 | 48 | module.exports = postcss.plugin('postcss-asset-url', function(options) { 49 | options = options || {}; 50 | 51 | var map = options.map; 52 | 53 | return function(style) { 54 | style.walkDecls(function(node) { 55 | helpers.try(function() { 56 | transformRule(node, map); 57 | }); 58 | }); 59 | }; 60 | }); 61 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "postcss-asset-url", 3 | "version": "1.0.0", 4 | "description": "Map urls to different locations", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha test" 8 | }, 9 | "keywords": [ 10 | "postcss", 11 | "css", 12 | "postcss-plugin", 13 | "url", 14 | "assets", 15 | "path" 16 | ], 17 | "author": "James Kyle ", 18 | "license": "MIT", 19 | "dependencies": { 20 | "postcss": "^5.0.15", 21 | "postcss-message-helpers": "^2.0.0", 22 | "reduce-function-call": "^1.0.1" 23 | }, 24 | "devDependencies": { 25 | "mocha": "^2.4.5" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var postcss = require('postcss'); 2 | var assert = require('assert'); 3 | 4 | var assetUrl = require('./'); 5 | 6 | function test(css, options) { 7 | return postcss() 8 | .use(assetUrl(options)) 9 | .process(css) 10 | .css; 11 | } 12 | 13 | describe('postcss-asset-url', function() { 14 | it('should replace asset-url()', function() { 15 | var options = { map: { image: '/path/to/images/' } }; 16 | var input = '.foo { background-image: url(asset-url("image", "background.png")); }'; 17 | var output = '.foo { background-image: url(/path/to/images/background.png); }'; 18 | 19 | assert.equal(test(input, options), output); 20 | }); 21 | 22 | it('should throw with wrong number of args', function() { 23 | assert.throws(function() { 24 | test('.foo { background-image: url(asset-url("arg1")); }'); 25 | }, /Wrong number of arguments passed/); 26 | 27 | assert.throws(function() { 28 | test('.foo { background-image: url(asset-url("arg1", "arg2", "arg3")); }'); 29 | }, /Wrong number of arguments passed/); 30 | }); 31 | 32 | it('should throw with unmapped type', function() { 33 | var options = { map: {} }; 34 | var input = '.foo { background-image: url(asset-url("image", "background.png")); }'; 35 | 36 | assert.throws(function() { 37 | test(input, options); 38 | }, /Invalid type/); 39 | }); 40 | }); 41 | --------------------------------------------------------------------------------