From f30e1f269f079a71bd677da595d3c155b14fddb3 Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Sat, 6 Jul 2019 13:50:56 +0200 Subject: [PATCH 1/4] Add failing test for external map file path creation --- index.js | 6 ++---- lib/getMapfile.js | 6 ++++++ test/map.js | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 lib/getMapfile.js diff --git a/index.js b/index.js index f96c07f..8d79228 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ const reporter = require('postcss-reporter/lib/formatter')() const argv = require('./lib/args') const depGraph = require('./lib/depGraph') +const getMapfile = require('./lib/getMapfile') let input = argv._ const { dir, output } = argv @@ -221,10 +222,7 @@ function css(css, file) { tasks.push(fs.outputFile(options.to, result.css)) if (result.map) { - const mapfile = options.to.replace( - path.extname(options.to), - `${path.extname(options.to)}.map` - ) + const mapfile = getMapfile(options.to) tasks.push(fs.outputFile(mapfile, result.map)) } } else process.stdout.write(result.css, 'utf8') diff --git a/lib/getMapfile.js b/lib/getMapfile.js new file mode 100644 index 0000000..5060574 --- /dev/null +++ b/lib/getMapfile.js @@ -0,0 +1,6 @@ +'use strict' +const path = require('path') + +module.exports = function getMapfile(p) { + return p.replace(path.extname(p), `${path.extname(p)}.map`) +} diff --git a/test/map.js b/test/map.js index 53880d8..663d505 100644 --- a/test/map.js +++ b/test/map.js @@ -5,6 +5,8 @@ import cli from './helpers/cli.js' import tmp from './helpers/tmp.js' import read from './helpers/read.js' +import getMapfile from '../lib/getMapfile' + test('inline maps are generated by default', async t => { const output = tmp('output.css') @@ -53,3 +55,20 @@ test('--no-map disables internal sourcemaps', async t => { t.notRegex(await read(output), /\/*# sourceMappingURL=/) }) + +test('mapFile path is property resolved', async t => { + const paths = [ + { + input: '/foo/bar.css/baz/index.css', + want: '/foo/bar.css/baz/index.css.map' + }, + { + input: '/foo/bar.sss/baz/index.sss', + want: '/foo/bar.sss/baz/index.sss.map' + } + ] + + for (const p of paths) { + t.is(getMapfile(p.input), p.want) + } +}) From 69050d7576ebb5010bc2432374fe9e1612b33c3d Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Sat, 6 Jul 2019 13:55:34 +0200 Subject: [PATCH 2/4] Write passing map file implementation Can we just append + '.map'? --- lib/getMapfile.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/getMapfile.js b/lib/getMapfile.js index 5060574..625897a 100644 --- a/lib/getMapfile.js +++ b/lib/getMapfile.js @@ -2,5 +2,7 @@ const path = require('path') module.exports = function getMapfile(p) { - return p.replace(path.extname(p), `${path.extname(p)}.map`) + const ext = path.extname(p) + const base = path.basename(p) + return p.replace(base, base.replace(ext, `${ext}.map`)) } From 0da0cc84fbf2a9b500a68987d0b3ea8665679558 Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Sat, 6 Jul 2019 14:22:50 +0200 Subject: [PATCH 3/4] Add additional failing example --- test/map.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/map.js b/test/map.js index 663d505..b944cd5 100644 --- a/test/map.js +++ b/test/map.js @@ -65,6 +65,10 @@ test('mapFile path is property resolved', async t => { { input: '/foo/bar.sss/baz/index.sss', want: '/foo/bar.sss/baz/index.sss.map' + }, + { + input: '/foo/bar.css/baz/bar.css', + want: '/foo/bar.css/baz/bar.css.map' } ] From ee41093db26c7b03f9a7924d82df902ff9f192ad Mon Sep 17 00:00:00 2001 From: Bret Comnes Date: Sat, 6 Jul 2019 14:26:35 +0200 Subject: [PATCH 4/4] Simplify implementation --- lib/getMapfile.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/getMapfile.js b/lib/getMapfile.js index 625897a..b9dd441 100644 --- a/lib/getMapfile.js +++ b/lib/getMapfile.js @@ -1,8 +1,4 @@ 'use strict' -const path = require('path') - module.exports = function getMapfile(p) { - const ext = path.extname(p) - const base = path.basename(p) - return p.replace(base, base.replace(ext, `${ext}.map`)) + return `${p}.map` }