Skip to content

Commit fbb0963

Browse files
ajoslinahdinosaur
authored andcommitted
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 b55076a commit fbb0963

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ function extract (chunk) {
5757
if (!node.arguments) return
5858
if (!node.arguments[0]) return
5959
if (node.arguments[0].value !== 'insert-css') return
60+
if (!node.parent.arguments || !node.parent.arguments[0]) return
61+
6062
css.push(node.parent.arguments[0].value)
6163
node.parent.update('0')
6264
}

test/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,25 @@ 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+
.plugin(cssExtract, { out: readCss })
38+
.bundle(readJs)
39+
40+
function readCss () {
41+
return bl(function (err, data) {
42+
t.ifError(err, 'no error')
43+
t.equal(String(data), '', 'no css extracted')
44+
})
45+
}
46+
47+
function readJs (err, data) {
48+
t.ifError(err, 'no error')
49+
const source = fs.readFileSync(path.join(__dirname, 'manual-insert-source.js'), 'utf8')
50+
t.ok(String(data).indexOf(String(source)) !== -1, 'source is still in built bundle')
51+
}
52+
})
3253
})

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)