diff --git a/lib/input.js b/lib/input.js index c66c3dc2d..83becf5ef 100644 --- a/lib/input.js +++ b/lib/input.js @@ -2,6 +2,7 @@ let { fileURLToPath, pathToFileURL } = require('url') let { resolve, isAbsolute } = require('path') +let { SourceMapConsumer, SourceMapGenerator } = require('source-map') let { nanoid } = require('nanoid/non-secure') let terminalHighlight = require('./terminal-highlight') @@ -10,6 +11,7 @@ let PreviousMap = require('./previous-map') let fromOffsetCache = Symbol('fromOffset cache') +let sourceMapAvailable = Boolean(SourceMapConsumer && SourceMapGenerator) let pathAvailable = Boolean(resolve && isAbsolute) class Input { @@ -43,7 +45,7 @@ class Input { } } - if (pathAvailable) { + if (pathAvailable && sourceMapAvailable) { let map = new PreviousMap(this.css, opts) if (map.text) { this.map = map @@ -168,7 +170,7 @@ class Input { result.file = fileURLToPath(fromUrl) } else { // istanbul ignore next - throw new Error(`file: protocol is not available in this PostCSS build`); + throw new Error(`file: protocol is not available in this PostCSS build`) } } diff --git a/lib/map-generator.js b/lib/map-generator.js index b35fc32cc..e9afd39b1 100644 --- a/lib/map-generator.js +++ b/lib/map-generator.js @@ -2,8 +2,9 @@ let { dirname, resolve, relative, sep } = require('path') let { pathToFileURL } = require('url') -let mozilla = require('source-map') +let { SourceMapConsumer, SourceMapGenerator } = require('source-map') +let sourceMapAvailable = Boolean(SourceMapConsumer && SourceMapGenerator) let pathAvailable = Boolean(dirname && resolve && relative && sep) class MapGenerator { @@ -99,7 +100,7 @@ class MapGenerator { let map if (this.mapOpts.sourcesContent === false) { - map = new mozilla.SourceMapConsumer(prev.text) + map = new SourceMapConsumer(prev.text) if (map.sourcesContent) { map.sourcesContent = map.sourcesContent.map(() => null) } @@ -206,7 +207,9 @@ class MapGenerator { return pathToFileURL(node.source.input.from).toString() } else { // istanbul ignore next - throw new Error('`map.absolute` option is not available in this PostCSS build') + throw new Error( + '`map.absolute` option is not available in this PostCSS build' + ) } } else { return this.toUrl(this.path(node.source.input.from)) @@ -215,7 +218,7 @@ class MapGenerator { generateString() { this.css = '' - this.map = new mozilla.SourceMapGenerator({ file: this.outputFile() }) + this.map = new SourceMapGenerator({ file: this.outputFile() }) let line = 1 let column = 1 @@ -282,7 +285,7 @@ class MapGenerator { generate() { this.clearAnnotation() - if (pathAvailable && this.isMap()) { + if (pathAvailable && sourceMapAvailable && this.isMap()) { return this.generateMap() } diff --git a/lib/previous-map.js b/lib/previous-map.js index 4928e1c23..7269c27db 100644 --- a/lib/previous-map.js +++ b/lib/previous-map.js @@ -2,7 +2,7 @@ let { existsSync, readFileSync } = require('fs') let { dirname, join } = require('path') -let mozilla = require('source-map') +let { SourceMapConsumer, SourceMapGenerator } = require('source-map') function fromBase64(str) { if (Buffer) { @@ -30,7 +30,7 @@ class PreviousMap { consumer() { if (!this.consumerCache) { - this.consumerCache = new mozilla.SourceMapConsumer(this.text) + this.consumerCache = new SourceMapConsumer(this.text) } return this.consumerCache } @@ -48,11 +48,15 @@ class PreviousMap { } getAnnotationURL(sourceMapString) { - return sourceMapString.match(/\/\*\s*# sourceMappingURL=((?:(?!sourceMappingURL=).)*)\*\//)[1].trim() + return sourceMapString + .match(/\/\*\s*# sourceMappingURL=((?:(?!sourceMappingURL=).)*)\*\//)[1] + .trim() } loadAnnotation(css) { - let annotations = css.match(/\/\*\s*# sourceMappingURL=(?:(?!sourceMappingURL=).)*\*\//gm) + let annotations = css.match( + /\/\*\s*# sourceMappingURL=(?:(?!sourceMappingURL=).)*\*\//gm + ) if (annotations && annotations.length > 0) { // Locate the last sourceMappingURL to avoid picking up @@ -107,9 +111,9 @@ class PreviousMap { } return map } - } else if (prev instanceof mozilla.SourceMapConsumer) { - return mozilla.SourceMapGenerator.fromSourceMap(prev).toString() - } else if (prev instanceof mozilla.SourceMapGenerator) { + } else if (prev instanceof SourceMapConsumer) { + return SourceMapGenerator.fromSourceMap(prev).toString() + } else if (prev instanceof SourceMapGenerator) { return prev.toString() } else if (this.isMap(prev)) { return JSON.stringify(prev) diff --git a/package.json b/package.json index 9ef50b3f2..c0bced9ad 100644 --- a/package.json +++ b/package.json @@ -130,12 +130,13 @@ "colorette": false, "fs": false, "path": false, - "url": false + "url": false, + "source-map": false }, "size-limit": [ { "path": "lib/postcss.js", - "limit": "30 KB" + "limit": "23 KB" } ], "jest": {