Skip to content

Commit

Permalink
feat: support source maps
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Mar 3, 2017
1 parent e0d9607 commit ed966de
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/nuke.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
const _ = require('lodash')
const postcss = require('postcss')
const plugin = require('./plugin')

function nuke(sources, css, opts) {
function toPostcssOpts(opts) {
if (opts.sourceMap) {
const mainOpts = _.pick(opts.sourceMap, ['from', 'to'])
const map = _.pick(opts.sourceMap, ['inline'])
return Object.assign(mainOpts, {map})
}
}

function nuke(sources, css, opts = {}) {
const processor = postcss([plugin(sources, opts)])
return processor.process(css).css
const processed = processor.process(css, toPostcssOpts(opts))
return opts.sourceMap ?
{css: processed.css, map: JSON.parse(processed.map.toString())} :
processed.css
}

module.exports = nuke
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"semantic-release": "^6.3.2",
"sinon": "^1.17.7",
"sinon-chai": "^2.8.0",
"source-map": "^0.5.6",
"xo": "patrickhulce/xo#master"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ html .something > foobar[something=x] #xb {
}
}

.something {
.unused, .something {
color: white;
}
36 changes: 36 additions & 0 deletions test/nuke.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs')
const path = require('path')
const SourceMapConsumer = require('source-map').SourceMapConsumer
const nukecss = require('../lib/nuke')

describe('nuke.js', () => {
Expand Down Expand Up @@ -120,4 +121,39 @@ describe('nuke.js', () => {
expect(result).to.contain('.totally-unused')
})
})

context('when sourceMaps are enabled', () => {
const filePath = 'file://' + path.join(__dirname, '/fixtures/content.html')
const cssContent = fs.readFileSync(path.join(__dirname, '/fixtures/content.css'), 'utf8')
const sourceMap = {from: 'content.css', to: 'content.css', inline: false}
const result = nukecss(filePath, cssContent, {sourceMap})

function findLineAndColumn(css, string) {
const lines = css.split('\n')
const line = lines.findIndex(l => l.includes(string)) + 1
if (line === -1) {
throw new Error(`could not find string ${string}`)
}

const column = lines[line - 1].indexOf(string) + 1
return {line, column}
}

it('should return an object', () => {
expect(result).to.be.an('object')
expect(result).to.have.property('css')
expect(result).to.have.property('map')
})

it('should produce a valid sourcemap', () => {
const string = '.something {'
const realLocation = findLineAndColumn(cssContent, string)
const location = findLineAndColumn(result.css, string)
const consumer = new SourceMapConsumer(result.map)

const sourceMapPosition = consumer.originalPositionFor(location)
expect(sourceMapPosition.line).to.be.greaterThan(location.line)
expect(sourceMapPosition.line).to.equal(realLocation.line)
})
})
})

0 comments on commit ed966de

Please sign in to comment.