|
1 | 1 | const fs = require('fs') |
2 | 2 | const path = require('path') |
| 3 | +const SourceMapConsumer = require('source-map').SourceMapConsumer |
3 | 4 | const nukecss = require('../lib/nuke') |
4 | 5 |
|
5 | 6 | describe('nuke.js', () => { |
@@ -120,4 +121,39 @@ describe('nuke.js', () => { |
120 | 121 | expect(result).to.contain('.totally-unused') |
121 | 122 | }) |
122 | 123 | }) |
| 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 | + }) |
123 | 159 | }) |
0 commit comments