Skip to content

Commit ed966de

Browse files
committed
feat: support source maps
1 parent e0d9607 commit ed966de

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

lib/nuke.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
const _ = require('lodash')
12
const postcss = require('postcss')
23
const plugin = require('./plugin')
34

4-
function nuke(sources, css, opts) {
5+
function toPostcssOpts(opts) {
6+
if (opts.sourceMap) {
7+
const mainOpts = _.pick(opts.sourceMap, ['from', 'to'])
8+
const map = _.pick(opts.sourceMap, ['inline'])
9+
return Object.assign(mainOpts, {map})
10+
}
11+
}
12+
13+
function nuke(sources, css, opts = {}) {
514
const processor = postcss([plugin(sources, opts)])
6-
return processor.process(css).css
15+
const processed = processor.process(css, toPostcssOpts(opts))
16+
return opts.sourceMap ?
17+
{css: processed.css, map: JSON.parse(processed.map.toString())} :
18+
processed.css
719
}
820

921
module.exports = nuke

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"semantic-release": "^6.3.2",
6363
"sinon": "^1.17.7",
6464
"sinon-chai": "^2.8.0",
65+
"source-map": "^0.5.6",
6566
"xo": "patrickhulce/xo#master"
6667
}
6768
}

test/fixtures/content.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ html .something > foobar[something=x] #xb {
2222
}
2323
}
2424

25-
.something {
25+
.unused, .something {
2626
color: white;
2727
}

test/nuke.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs')
22
const path = require('path')
3+
const SourceMapConsumer = require('source-map').SourceMapConsumer
34
const nukecss = require('../lib/nuke')
45

56
describe('nuke.js', () => {
@@ -120,4 +121,39 @@ describe('nuke.js', () => {
120121
expect(result).to.contain('.totally-unused')
121122
})
122123
})
124+
125+
context('when sourceMaps are enabled', () => {
126+
const filePath = 'file://' + path.join(__dirname, '/fixtures/content.html')
127+
const cssContent = fs.readFileSync(path.join(__dirname, '/fixtures/content.css'), 'utf8')
128+
const sourceMap = {from: 'content.css', to: 'content.css', inline: false}
129+
const result = nukecss(filePath, cssContent, {sourceMap})
130+
131+
function findLineAndColumn(css, string) {
132+
const lines = css.split('\n')
133+
const line = lines.findIndex(l => l.includes(string)) + 1
134+
if (line === -1) {
135+
throw new Error(`could not find string ${string}`)
136+
}
137+
138+
const column = lines[line - 1].indexOf(string) + 1
139+
return {line, column}
140+
}
141+
142+
it('should return an object', () => {
143+
expect(result).to.be.an('object')
144+
expect(result).to.have.property('css')
145+
expect(result).to.have.property('map')
146+
})
147+
148+
it('should produce a valid sourcemap', () => {
149+
const string = '.something {'
150+
const realLocation = findLineAndColumn(cssContent, string)
151+
const location = findLineAndColumn(result.css, string)
152+
const consumer = new SourceMapConsumer(result.map)
153+
154+
const sourceMapPosition = consumer.originalPositionFor(location)
155+
expect(sourceMapPosition.line).to.be.greaterThan(location.line)
156+
expect(sourceMapPosition.line).to.equal(realLocation.line)
157+
})
158+
})
123159
})

0 commit comments

Comments
 (0)