Skip to content

Commit 3e68416

Browse files
committed
feat: enable source map support
1 parent d27eeec commit 3e68416

6 files changed

Lines changed: 98 additions & 22 deletions

File tree

lib/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const esprima = require('esprima')
66
const nukecss = require('nukecss')
77
const sources = require('webpack-sources')
88

9-
const ConcatSource = sources.ConcatSource
9+
const RawSource = sources.RawSource
10+
const SourceMapSource = sources.SourceMapSource
1011
const ReplaceSource = sources.ReplaceSource
1112

1213
const CSS_LOADER_PATTERN = /exports\.push\(\[module\./
@@ -48,9 +49,23 @@ class NukeCssPlugin {
4849
}
4950

5051
onCssAsset(compilation, name, asset, sources) {
51-
const content = asset.source()
52-
const nuked = nukecss(sources, content)
53-
compilation.assets[name] = new ConcatSource(nuked)
52+
const nukeCssOpts = {}
53+
const input = asset.source()
54+
55+
let inputSourceMap
56+
if (this._options.sourceMap) {
57+
inputSourceMap = asset.map()
58+
nukeCssOpts.sourceMap = {
59+
from: name,
60+
to: name,
61+
inline: _.get(this._options, 'sourceMap.inline', false),
62+
}
63+
}
64+
65+
const nuked = nukecss(sources, input, nukeCssOpts)
66+
compilation.assets[name] = this._options.sourceMap && nuked.map ?
67+
new SourceMapSource(nuked.css, name, nuked.map, input, inputSourceMap) :
68+
new RawSource(nuked.css || nuked)
5469
}
5570

5671
onJsAsset(compilation, name, asset, sources) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dependencies": {
4141
"esprima": "^3.1.3",
4242
"lodash": "^4.17.4",
43-
"nukecss": "^1.0.0",
43+
"nukecss": "^1.5.0",
4444
"webpack-sources": "^0.1.4"
4545
},
4646
"devDependencies": {
@@ -56,6 +56,7 @@
5656
"semantic-release": "^6.3.2",
5757
"sinon": "^1.17.7",
5858
"sinon-chai": "^2.8.0",
59+
"source-map": "^0.5.6",
5960
"style-loader": "^0.13.1",
6061
"webpack": "^2.2.1",
6162
"xo": "github:patrickhulce/xo#master"

test/fixtures/entry.extracted.css

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
/* thrown away */
1+
/* thrown away comment */
22
@import "~bootstrap/dist/css/bootstrap.css";
33

44
.fa {
55
content: 'fa';
66
}
77

8+
.my-favorite-class {
9+
background: blue;
10+
}
11+
12+
.fa-other {
13+
content: 'foo';
14+
}
15+
816
.fa-address-book-o {
917
content: 'fa';
1018
}
@@ -16,11 +24,3 @@
1624
.fa-table {
1725
content: 'fa';
1826
}
19-
20-
.fa-other {
21-
content: 'foo';
22-
}
23-
24-
.my-favorite-class {
25-
background: blue;
26-
}

test/fixtures/webpack.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
const NukeCssPlugin = require('../../lib')
2+
const webpack = require('webpack')
23
const ExtractTextPlugin = require('extract-text-webpack-plugin')
34

45
module.exports = {
6+
devtool: 'source-map',
57
entry: `${__dirname}/entry.js`,
68
output: {filename: 'out.js', path: `${__dirname}/dist`},
79
module: {
810
rules: [
911
{test: /\.(svg|eot|woff2?|ttf)/, use: 'file-loader'},
1012
{test: /\.extracted.css$/, use: ExtractTextPlugin.extract({
11-
use: ['css-loader']
13+
use: ['css-loader?sourceMap']
1214
}), include: __dirname},
1315
{test: /\.css$/, exclude: /.extracted.css/, use: ['style-loader', 'css-loader'], include: __dirname},
1416
],
1517
},
1618
plugins: [
1719
new ExtractTextPlugin('out.css'),
18-
new NukeCssPlugin(),
20+
new NukeCssPlugin({sourceMap: true}),
21+
new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
1922
]
2023
}

test/index.test.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const fs = require('fs')
22
const _ = require('lodash')
33
const webpack = require('webpack')
4+
const SourceMapConsumer = require('source-map').SourceMapConsumer
45

56
describe('NukeCssPlugin', function () {
67
let fileStats
@@ -14,7 +15,7 @@ describe('NukeCssPlugin', function () {
1415
return {
1516
filename,
1617
stats: fs.statSync(fullPath),
17-
content: fs.readFileSync(fullPath, 'utf8'),
18+
content: /\.(css|js)/.test(filename) && fs.readFileSync(fullPath, 'utf8'),
1819
}
1920
})
2021
.keyBy('filename')
@@ -32,6 +33,17 @@ describe('NukeCssPlugin', function () {
3233
})
3334
}
3435

36+
function findLineAndColumn(css, string) {
37+
const lines = css.split('\n')
38+
const line = lines.findIndex(l => l.includes(string)) + 1
39+
if (line === -1) {
40+
throw new Error(`could not find string ${string}`)
41+
}
42+
43+
const column = lines[line - 1].indexOf(string) + 1
44+
return {line, column}
45+
}
46+
3547
before(function (done) {
3648
this.timeout(10000)
3749
testWithConfig(baseConfig, done)
@@ -46,4 +58,15 @@ describe('NukeCssPlugin', function () {
4658
expect(fileStats['out.css'].content).to.contain('.fa-address-book-o')
4759
expect(fileStats['out.css'].content).to.not.contain('.my-favorite-class')
4860
})
61+
62+
it('should generate a source map', function () {
63+
expect(fileStats).to.have.property('out.css.map')
64+
65+
const newContent = fileStats['out.css'].content
66+
const newLocation = findLineAndColumn(newContent, '.fa-table {')
67+
const consumer = new SourceMapConsumer(fileStats['out.css.map'].content)
68+
const oldLocation = consumer.originalPositionFor(newLocation)
69+
expect(oldLocation).to.have.property('source').that.include('entry.extracted.css')
70+
expect(oldLocation).to.have.property('line', 24)
71+
})
4972
})

yarn.lock

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -923,12 +923,18 @@ dateformat@^1.0.11:
923923
get-stdin "^4.0.1"
924924
meow "^3.3.0"
925925

926-
debug@2, debug@2.2.0, debug@^2.1.1, debug@^2.2.0, debug@~2.2.0:
926+
debug@2, debug@2.2.0, debug@^2.2.0, debug@~2.2.0:
927927
version "2.2.0"
928928
resolved "https://registry.yarnpkg.com/debug/-/debug-2.2.0.tgz#f87057e995b1a1f6ae6a4960664137bc56f039da"
929929
dependencies:
930930
ms "0.7.1"
931931

932+
debug@^2.1.1, debug@^2.6.1:
933+
version "2.6.1"
934+
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.1.tgz#79855090ba2c4e3115cc7d8769491d58f0491351"
935+
dependencies:
936+
ms "0.7.2"
937+
932938
decamelize@^1.0.0, decamelize@^1.1.1, decamelize@^1.1.2:
933939
version "1.2.0"
934940
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
@@ -1670,7 +1676,7 @@ glob-parent@^2.0.0:
16701676
dependencies:
16711677
is-glob "^2.0.0"
16721678

1673-
glob@7.0.5, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5:
1679+
glob@7.0.5, glob@^7.0.5:
16741680
version "7.0.5"
16751681
resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.5.tgz#b4202a69099bbb4d292a7c1b95b6682b67ebdc95"
16761682
dependencies:
@@ -1691,6 +1697,17 @@ glob@^5.0.15:
16911697
once "^1.3.0"
16921698
path-is-absolute "^1.0.0"
16931699

1700+
glob@^7.0.0, glob@^7.0.3, glob@^7.1.1:
1701+
version "7.1.1"
1702+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8"
1703+
dependencies:
1704+
fs.realpath "^1.0.0"
1705+
inflight "^1.0.4"
1706+
inherits "2"
1707+
minimatch "^3.0.2"
1708+
once "^1.3.0"
1709+
path-is-absolute "^1.0.0"
1710+
16941711
globals@^9.14.0:
16951712
version "9.16.0"
16961713
resolved "https://registry.yarnpkg.com/globals/-/globals-9.16.0.tgz#63e903658171ec2d9f51b1d31de5e2b8dc01fb80"
@@ -2588,6 +2605,10 @@ ms@0.7.1:
25882605
version "0.7.1"
25892606
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"
25902607

2608+
ms@0.7.2:
2609+
version "0.7.2"
2610+
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.2.tgz#ae25cf2512b3885a1d95d7f037868d8431124765"
2611+
25912612
multimatch@^2.1.0:
25922613
version "2.1.0"
25932614
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b"
@@ -2774,12 +2795,16 @@ npmlog@^1.2.1:
27742795
are-we-there-yet "~1.0.0"
27752796
gauge "~1.2.0"
27762797

2777-
nukecss@^1.0.0:
2778-
version "1.0.0"
2779-
resolved "https://registry.yarnpkg.com/nukecss/-/nukecss-1.0.0.tgz#80b16bd28d1b5b7e41b1232f78e462381497badf"
2798+
nukecss@^1.5.0:
2799+
version "1.5.0"
2800+
resolved "https://registry.yarnpkg.com/nukecss/-/nukecss-1.5.0.tgz#4957804f28dda2dbc173ea14fcf8601249224eed"
27802801
dependencies:
2802+
debug "^2.6.1"
2803+
esprima "^3.1.3"
2804+
glob "^7.1.1"
27812805
gonzales-pe "^4.0.3"
27822806
lodash "^4.17.4"
2807+
postcss "^5.2.15"
27832808

27842809
num2fraction@^1.2.2:
27852810
version "1.2.2"
@@ -3276,6 +3301,15 @@ postcss@^5.0.10, postcss@^5.0.11, postcss@^5.0.12, postcss@^5.0.13, postcss@^5.0
32763301
source-map "^0.5.6"
32773302
supports-color "^3.2.3"
32783303

3304+
postcss@^5.2.15:
3305+
version "5.2.15"
3306+
resolved "https://registry.yarnpkg.com/postcss/-/postcss-5.2.15.tgz#a9e8685e50e06cc5b3fdea5297273246c26f5b30"
3307+
dependencies:
3308+
chalk "^1.1.3"
3309+
js-base64 "^2.1.9"
3310+
source-map "^0.5.6"
3311+
supports-color "^3.2.3"
3312+
32793313
prelude-ls@~1.1.2:
32803314
version "1.1.2"
32813315
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"

0 commit comments

Comments
 (0)