From 395139908c7923b8c37b3d6616f579cbe110c85d Mon Sep 17 00:00:00 2001 From: Andrew Joslin Date: Tue, 22 Mar 2016 09:44:37 -0700 Subject: [PATCH] Skip non-matching insert-css statements This allows people to still use `require('insert-css')` in their code. if it doesn't match the expected format, it just won't be extracted from the javascript bundle. --- index.js | 2 ++ test/index.js | 21 +++++++++++++++++++++ test/manual-insert-source.js | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 test/manual-insert-source.js diff --git a/index.js b/index.js index 59432f8..173f874 100644 --- a/index.js +++ b/index.js @@ -54,6 +54,8 @@ function extract (chunk) { if (!node.arguments) return if (!node.arguments[0]) return if (node.arguments[0].value !== 'insert-css') return + if (!node.parent.arguments || !node.parent.arguments[0]) return + css.push(node.parent.arguments[0].value) node.parent.update('0') } diff --git a/test/index.js b/test/index.js index 9d1736f..fc23d81 100644 --- a/test/index.js +++ b/test/index.js @@ -29,4 +29,25 @@ test('css-extract', function (t) { }) } }) + + t.test('should leave non-matching insert-css statements inline', function (t) { + t.plan(4) + + browserify(path.join(__dirname, 'manual-insert-source.js')) + .plugin(cssExtract, { out: readCss }) + .bundle(readJs) + + function readCss () { + return bl(function (err, data) { + t.ifError(err, 'no error') + t.equal(String(data), '', 'no css extracted') + }) + } + + function readJs (err, data) { + t.ifError(err, 'no error') + const source = fs.readFileSync(path.join(__dirname, 'manual-insert-source.js'), 'utf8') + t.ok(String(data).indexOf(String(source)) !== -1, 'source is still in built bundle') + } + }) }) diff --git a/test/manual-insert-source.js b/test/manual-insert-source.js new file mode 100644 index 0000000..3b9771a --- /dev/null +++ b/test/manual-insert-source.js @@ -0,0 +1,3 @@ +var insertCss = require('insert-css') + +insertCss('.foo{color: purple}')