Skip to content

Commit cf4cbae

Browse files
committed
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.
1 parent c51e66a commit cf4cbae

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ function extract (chunk) {
5454
if (!node.arguments) return
5555
if (!node.arguments[0]) return
5656
if (node.arguments[0].value !== 'insert-css') return
57+
if (!node.parent.arguments || !node.parent.arguments[0]) return
58+
5759
css.push(node.parent.arguments[0].value)
5860
node.parent.update('0')
5961
}

test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,26 @@ test('css-extract', function (t) {
2929
})
3030
}
3131
})
32+
33+
t.test('should leave non-matching insert-css statements inline', function (t) {
34+
t.plan(4)
35+
36+
browserify(path.join(__dirname, 'manual-insert-source.js'))
37+
.transform('sheetify/transform')
38+
.plugin(cssExtract, { out: readCss })
39+
.bundle(readJs)
40+
41+
function readCss () {
42+
return bl(function (err, data) {
43+
t.ifError(err, 'no error')
44+
t.equal(String(data), '', 'no css extracted')
45+
})
46+
}
47+
48+
function readJs (err, data) {
49+
t.ifError(err, 'no error')
50+
const source = fs.readFileSync(path.join(__dirname, 'manual-insert-source.js'), 'utf8')
51+
t.ok(String(data).indexOf(String(source)) !== -1, 'source is still in built bundle')
52+
}
53+
})
3254
})

test/manual-insert-source.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var insertCss = require('insert-css')
2+
3+
insertCss('.foo{color: purple}')

0 commit comments

Comments
 (0)