mirror of
https://github.com/hanzoai/postcss.git
synced 2026-09-24 17:01:56 +00:00
88 lines
2.8 KiB
JavaScript
88 lines
2.8 KiB
JavaScript
import { Base64 } from 'js-base64';
|
|
import mozilla from 'source-map';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
|
|
export default class PreviousMap {
|
|
constructor(css, opts) {
|
|
this.loadAnnotation(css);
|
|
this.inline = this.startWith(this.annotation, 'data:');
|
|
|
|
let prev = opts.map ? opts.map.prev : undefined;
|
|
let text = this.loadMap(opts.from, prev);
|
|
if ( text ) this.text = text;
|
|
}
|
|
|
|
consumer() {
|
|
if ( !this.consumerCache ) {
|
|
this.consumerCache = new mozilla.SourceMapConsumer(this.text);
|
|
}
|
|
return this.consumerCache;
|
|
}
|
|
|
|
withContent() {
|
|
return !!(this.consumer().sourcesContent &&
|
|
this.consumer().sourcesContent.length > 0);
|
|
}
|
|
|
|
startWith(string, start) {
|
|
if ( !string ) return false;
|
|
return string.substr(0, start.length) === start;
|
|
}
|
|
|
|
loadAnnotation(css) {
|
|
let match = css.match(/\/\*\s*# sourceMappingURL=(.*)\s*\*\//);
|
|
if ( match ) this.annotation = match[1].trim();
|
|
}
|
|
|
|
decodeInline(text) {
|
|
let uri = 'data:application/json,';
|
|
let base64 = 'data:application/json;base64,';
|
|
|
|
if ( this.startWith(text, uri) ) {
|
|
return decodeURIComponent( text.substr(uri.length) );
|
|
|
|
} else if ( this.startWith(text, base64) ) {
|
|
return Base64.decode( text.substr(base64.length) );
|
|
|
|
} else {
|
|
let encoding = text.match(/data:application\/json;([^,]+),/)[1];
|
|
throw new Error('Unsupported source map encoding ' + encoding);
|
|
}
|
|
}
|
|
|
|
loadMap(file, prev) {
|
|
if ( prev === false ) return false;
|
|
|
|
if ( prev ) {
|
|
if ( typeof prev === 'string' ) {
|
|
return prev;
|
|
} else if ( prev instanceof mozilla.SourceMapConsumer ) {
|
|
return mozilla.SourceMapGenerator
|
|
.fromSourceMap(prev).toString();
|
|
} else if ( prev instanceof mozilla.SourceMapGenerator ) {
|
|
return prev.toString();
|
|
} else if ( typeof prev === 'object' && prev.mappings ) {
|
|
return JSON.stringify(prev);
|
|
} else {
|
|
throw new Error('Unsupported previous source map format: ' +
|
|
prev.toString());
|
|
}
|
|
|
|
} else if ( this.inline ) {
|
|
return this.decodeInline(this.annotation);
|
|
|
|
} else if ( this.annotation ) {
|
|
let map = this.annotation;
|
|
if ( file ) map = path.join(path.dirname(file), map);
|
|
|
|
this.root = path.dirname(map);
|
|
if ( fs.existsSync && fs.existsSync(map) ) {
|
|
return fs.readFileSync(map, 'utf-8').toString().trim();
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|