From d59311e3f62118f882a8555dc7804168b6a531ed Mon Sep 17 00:00:00 2001 From: chenwenxiu Date: Fri, 30 Jun 2017 11:12:59 +0800 Subject: [PATCH] add test case, improved coverage to 100% --- fixtures/not-standard/expect.css | 10 +++++++--- fixtures/not-standard/style.css | 10 +++++++--- index.js | 13 ++++++++----- index.test.js | 19 +++++++++++++++++++ 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/fixtures/not-standard/expect.css b/fixtures/not-standard/expect.css index 14c5f4a..cf2bdd7 100644 --- a/fixtures/not-standard/expect.css +++ b/fixtures/not-standard/expect.css @@ -1,4 +1,8 @@ a { - background-image: url("b.png?___inline"); - border-image: url(c.png?__Inline); -} \ No newline at end of file + background-image: url("icon.png?___inline"); + border-image: url(icon.png?__Inline); +} + +a { + background: black url('icon.png?__inline', ); +} diff --git a/fixtures/not-standard/style.css b/fixtures/not-standard/style.css index 14c5f4a..cf2bdd7 100644 --- a/fixtures/not-standard/style.css +++ b/fixtures/not-standard/style.css @@ -1,4 +1,8 @@ a { - background-image: url("b.png?___inline"); - border-image: url(c.png?__Inline); -} \ No newline at end of file + background-image: url("icon.png?___inline"); + border-image: url(icon.png?__Inline); +} + +a { + background: black url('icon.png?__inline', ); +} diff --git a/index.js b/index.js index db336d5..8af43a5 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ const postcss = require('postcss') const parseValue = require('postcss-value-parser') const Datauri = require('datauri') const { dirname, resolve } = require('path') +const fs = require('fs') module.exports = postcss.plugin('__inline', () => (root, result) => { const cssDirname = dirname(root.source.input.file) @@ -12,13 +13,15 @@ module.exports = postcss.plugin('__inline', () => (root, result) => { if (node.type === 'function' && node.value === 'url') { if (node.nodes.length === 1) { const url = node.nodes[0] - if (url.type === 'word' || url.type === 'string') { - const urlValue = url.value - const match = urlValue.match(/^(.+)\?__inline$/) - if (match) { - const file = resolve(cssDirname, match[1]) + const urlValue = url.value + const match = urlValue.match(/^(.+)\?__inline$/) + if (match) { + const file = resolve(cssDirname, match[1]) + if (fs.existsSync(file)) { url.value = new Datauri(file).content processed = true + } else { + throw new Error('File: ' + file + ' not exists') } } } diff --git a/index.test.js b/index.test.js index d2bf0db..7addfbf 100644 --- a/index.test.js +++ b/index.test.js @@ -14,3 +14,22 @@ it('should inline binary files', () => Promise.all([ ]).then(([result, expectCss]) => { expect(result.css).toBe(expectCss) })) + +it('file not exists', () => { + readFile('fixtures/not-exists/style.css', 'utf8').then( + code => postcss([__inline]).process(code, { + from: 'fixtures/not-exists/style.css' + })) + .catch((e) => {}) +}) + +it('not standard', () => Promise.all([ + readFile('fixtures/not-standard/style.css', 'utf8').then( + code => postcss([__inline]).process(code, { + from: 'fixtures/not-standard/style.css' + }) + ), + readFile('fixtures/not-standard/expect.css', 'utf8') +]).then(([result, expectCss]) => { + expect(result.css).toBe(expectCss) +}))