Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
modules.forEach(module => {
if (!/\.css$/.test(module)) return;
const css = css_map.get(module);
const { code, map } = extract_sourcemap(css, module);
parts.push(code);
if (map) {
const lines = codec.decode(map.mappings);
if (combined_map.sources.length > 0 || combined_map.names.length > 0) {
lines.forEach(line => {
line.forEach(segment => {
// adjust source index
segment[1] += combined_map.sources.length;
// adjust name index
if (segment[4]) segment[4] += combined_map.names.length;
});
});
}
combined_map.sources.push(...map.sources);
combined_map.sourcesContent.push(...map.sourcesContent);
combined_map.names.push(...map.names);
function fixMappings( mappings ) {
if ( typeof mappings !== 'string' ) {
return '';
}
mappings = decode( mappings );
mappings[ 0 ].forEach( ( line, index ) => {
// If the line has only one segment with only one item,
// just drop the whole line.
if ( line && line.length === 1 ) {
mappings[ 0 ].splice( index, 1 );
}
} );
return encode( mappings );
}
export default function check ( magicString, ast ) {
const code = magicString.toString();
try {
parse( code, {
ecmaVersion: 8,
sourceType: 'module'
});
} catch ( err ) {
const map = magicString.generateMap();
const { line, column } = err.loc;
const snippet = code.slice( Math.max( 0, err.pos - 35 ), Math.min( code.length, err.pos + 35 ) );
const mappings = decode( map.mappings );
const segments = mappings[ line - 1 ];
for ( let i = 0; i < segments.length; i += 1 ) {
const segment = segments[i];
if ( segment[0] >= column ) {
const sourceCodeLine = segment[2];
const sourceCodeColumn = segment[3];
err.message = `Butternut generated invalid JS: code in source file near (${sourceCodeLine + 1}:${sourceCodeColumn}) became\n...${snippet}...`;
try {
const repro = createRepro( magicString.original, ast, sourceCodeLine, sourceCodeColumn );
if ( repro ) err.repro = repro;
} catch (err) {
// do nothing
}
function Link ( map, sources ) {
if ( !map ) throw new Error( 'Cannot generate a sourcemap if non-sourcemap-generating transformers are used' );
this.sources = sources;
this.names = map.names;
this.mappings = decode( map.mappings );
}
export default function decodeMappings ( mappings ) {
let checksum = crc32( mappings );
if ( !cache[ checksum ] ) {
cache[ checksum ] = decode( mappings );
}
return cache[ checksum ];
}
let result = '';
const mappings = [];
const sources = [];
const sourcesContent = [];
for (let chunk of cssLookup.values()) {
if (!chunk.code) continue;
result += chunk.code + '\n';
if (chunk.map) {
const i = sources.length;
sources.push(chunk.map.sources[0]);
sourcesContent.push(chunk.map.sourcesContent[0]);
const decoded = decode(chunk.map.mappings);
if (i > 0) {
decoded.forEach(line => {
line.forEach(segment => {
segment[1] = i;
});
});
}
mappings.push(...decoded);
}
}
const writer = new CssWriter(result, {
sources,
sourcesContent,
function trace(loc, map) {
const mappings = decode(map.mappings);
const segments = mappings[loc.line - 1];
for (let i = 0; i < segments.length; i += 1) {
const segment = segments[i];
if (segment[0] === loc.column) {
const [, sourceIndex, line, column] = segment;
const source = map.sources[sourceIndex].slice(2);
return { source, line: line + 1, column };
}
}
return null;
}
function addNewLine( code, map, banner ) {
map = Object.assign( {}, map );
code = code.replace( banner, `${ banner }\n` );
const mappings = decodeSourceMap( map.mappings );
let codeStart = banner.match( /\n/g );
codeStart = codeStart ? codeStart.length + 1 : 1;
let whitespaceAtStart = code.replace( `${ banner }\n`, '' ).match( /^(\s)+/g );
whitespaceAtStart = whitespaceAtStart ? whitespaceAtStart.length : 0;
mappings.unshift( [] );
if ( Array.isArray( mappings[ codeStart ] ) && mappings[ codeStart ].length ) {
const offset = mappings[ codeStart ][ 0 ][ 0 ] - whitespaceAtStart;
mappings[ codeStart ].forEach( ( segment ) => {
segment[ 0 ] -= offset;
} );
}
return getMap( this, sourceMapByPath ).then( map => {
if ( !map ) return null;
this.map = map;
let decodingStart = process.hrtime();
this.mappings = decode( map.mappings );
let decodingTime = process.hrtime( decodingStart );
this._stats.decodingTime = 1e9 * decodingTime[0] + decodingTime[1];
const sourcesContent = map.sourcesContent || [];
const sourceRoot = resolve( dirname( this.file ), map.sourceRoot || '' );
this.sources = map.sources.map( ( source, i ) => {
return new Node({
file: source ? resolve( sourceRoot, source ) : null,
content: sourcesContent[i]
});
});
const promises = this.sources.map( node => node.load( sourcesContentByPath, sourceMapByPath ) );
return Promise.all( promises );