Skip to content

Commit

Permalink
Merge pull request #1571 from barak007/source-map-browser
Browse files Browse the repository at this point in the history
Remove source-map from browser build
  • Loading branch information
ai committed May 5, 2021
2 parents a81cf0d + de6f33c commit b158dd5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
6 changes: 4 additions & 2 deletions lib/input.js
Expand Up @@ -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')
Expand All @@ -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 {
Expand Down Expand Up @@ -43,7 +45,7 @@ class Input {
}
}

if (pathAvailable) {
if (pathAvailable && sourceMapAvailable) {
let map = new PreviousMap(this.css, opts)
if (map.text) {
this.map = map
Expand Down Expand Up @@ -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`)
}
}

Expand Down
13 changes: 8 additions & 5 deletions lib/map-generator.js
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -282,7 +285,7 @@ class MapGenerator {
generate() {
this.clearAnnotation()

if (pathAvailable && this.isMap()) {
if (pathAvailable && sourceMapAvailable && this.isMap()) {
return this.generateMap()
}

Expand Down
18 changes: 11 additions & 7 deletions lib/previous-map.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand 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)
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -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": {
Expand Down

0 comments on commit b158dd5

Please sign in to comment.