Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove source-map from browser build #1571

Merged
merged 1 commit into from May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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