Skip to content

Commit

Permalink
Merge pull request #1909 from romainmenke/no-work-result-vs-lazy-resu…
Browse files Browse the repository at this point in the history
…lt--inventive-pygmy-marmoset-5bbea7d7dd

Ensure that `NoWorkResult` and `LazyResult` have the same outcome.
  • Loading branch information
ai committed Jan 4, 2024
2 parents a0d9f10 + 3c2fa2a commit c751e11
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/map-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MapGenerator {
this.root = root
this.opts = opts
this.css = cssString
this.originalCSS = cssString
this.usesFileUrls = !this.mapOpts.from && this.mapOpts.absolute

this.memoizedFileURLs = new Map()
Expand Down Expand Up @@ -74,7 +75,7 @@ class MapGenerator {
}
}
} else if (this.css) {
this.css = this.css.replace(/(\n)?\/\*#[\S\s]*?\*\/$/gm, '')
this.css = this.css.replace(/\n*?\/\*#[\S\s]*?\*\/$/gm, '')
}
}

Expand Down Expand Up @@ -276,7 +277,7 @@ class MapGenerator {
}
})
} else {
let input = new Input(this.css, this.opts)
let input = new Input(this.originalCSS, this.opts)
if (input.map) this.previousMaps.push(input.map)
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/no-work-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class NoWorkResult {
if (generatedMap) {
this.result.map = generatedMap
}
} else {
map.clearAnnotation();
this.result.css = map.css;
}
}

Expand Down
74 changes: 74 additions & 0 deletions test/no-work-result.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import postcss = require('../lib/postcss.js')
import stringify = require('../lib/stringify.js')
import { spy } from 'nanospy'
import { SourceMapGenerator } from 'source-map-js'
import { test } from 'uvu'
import { equal, instance, is, not, throws, type } from 'uvu/assert'

import NoWorkResult from '../lib/no-work-result.js'
import parse from '../lib/parse.js'
import Processor from '../lib/processor.js'

let processor = new Processor()
Expand Down Expand Up @@ -98,4 +101,75 @@ test('prints its object type', () => {
is(Object.prototype.toString.call(result), '[object NoWorkResult]')
})

test('no work result matches lazy result', async () => {
let source = '.foo { color: red }\n';

let noWorkResult = await postcss([]).process(source, {
from: 'foo.css',
map: false
});

let lazyResult = await postcss([]).process(source, {
from: 'foo.css',
map: false,
syntax: { parse, stringify }
});

equal(noWorkResult.css, lazyResult.css);
})

// https://github.com/postcss/postcss/issues/1911
test('no work result matches lazy result when map is true', async () => {
let source = '.foo { color: red }\n';

let noWorkResult = await postcss([]).process(source, {
from: 'foo.css',
map: true
});

equal(noWorkResult.css, '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEiLCJmaWxlIjoiZm9vLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIi5mb28geyBjb2xvcjogcmVkIH1cbiJdfQ== */');

let lazyResult = await postcss([]).process(source, {
from: 'foo.css',
map: true,
syntax: { parse, stringify }
});

equal(lazyResult.css, '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxXQUFXIiwiZmlsZSI6ImZvby5jc3MiLCJzb3VyY2VzQ29udGVudCI6WyIuZm9vIHsgY29sb3I6IHJlZCB9XG4iXX0= */');
})

test('no work result matches lazy result when the source contains an inline source map', async () => {
let source = '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxXQUFXIiwiZmlsZSI6ImZvby5jc3MiLCJzb3VyY2VzQ29udGVudCI6WyIuZm9vIHsgY29sb3I6IHJlZCB9XG4iXX0= */\n';

let noWorkResult = await postcss([]).process(source, {
from: 'foo.css',
map: false
});

let lazyResult = await postcss([]).process(source, {
from: 'foo.css',
map: false,
syntax: { parse, stringify }
});

equal(noWorkResult.css, lazyResult.css);
})

test('no work result matches lazy result when map is true and the source contains an inline source map', async () => {
let source = '.foo { color: red }\n\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImZvby5jc3MiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxXQUFXIiwiZmlsZSI6ImZvby5jc3MiLCJzb3VyY2VzQ29udGVudCI6WyIuZm9vIHsgY29sb3I6IHJlZCB9XG4iXX0= */\n';

let lazyResult = await postcss([]).process(source, {
from: 'bar.css',
map: true,
syntax: { parse, stringify }
});

let noWorkResult = await postcss([]).process(source, {
from: 'bar.css',
map: true
});

equal(noWorkResult.css, lazyResult.css);
})

test.run()

0 comments on commit c751e11

Please sign in to comment.