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 );
}
while ( i-- ) {
line = this.node.mappings[i];
resolved[i] = result = [];
for ( j = 0; j < line.length; j += 1 ) {
applySegment( line[j], result );
}
}
let tracingTime = process.hrtime( tracingStart );
this._stats.tracingTime = 1e9 * tracingTime[0] + tracingTime[1];
// Encode mappings
let encodingStart = process.hrtime();
let mappings = encode( resolved );
let encodingTime = process.hrtime( encodingStart );
this._stats.encodingTime = 1e9 * encodingTime[0] + encodingTime[1];
let includeContent = options.includeContent !== false;
return new SourceMap({
file: basename( this.node.file ),
sources: allSources.map( source => slash( relative( options.base || dirname( this.node.file ), source ) ) ),
sourcesContent: allSources.map( source => includeContent ? this.sourcesContentByPath[ source ] : null ),
names: allNames,
mappings
});
},
});
return source;
});
let source = new Link( map, sources );
bundleSourcemapChain.forEach( map => {
source = new Link( map, [ source ] );
});
const { names, mappings } = source.traceMappings();
// we re-use the `map` object because it has convenient toString/toURL methods
map.sourcesContent = modules.map( module => module.originalCode );
map.mappings = encode( mappings );
map.names = names;
return map;
}
// 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);
mappings.push(...lines);
}
});
if (parts.length > 0) {
combined_map.mappings = codec.encode(mappings);
combined_map.sources = combined_map.sources.map(source => path.relative(asset_dir, source).replace(/\\/g, '/'));
return {
code: parts.join('\n'),
map: combined_map
};
}
return null;
}
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
}
unit => {
t.deepEqual(unit, {
contents: 'a;\nadd;\nb;\n',
path: 'src/foo.js',
moduleId: 'foo',
sourceMap: {
version: 3,
file: 'src/foo.js',
sources: [ 'src/foo.js' ],
sourcesContent: [ 'a;b;\n' ],
names: [],
mappings: encode([
[ [ 0, 0, 0, 0 ], [ 1, 0, 0, 0 ], [ 2, 0, 0, 0 ] ],
[ [ 0, 0, 0, 0 ] ],
[ [ 0, 0, 0, 3 ], [ 1, 0, 0, 3 ], [ 2, 0, 0, 3 ] ]
])
}
})
},
t.fail
if (i > 0) {
decoded.forEach(line => {
line.forEach(segment => {
segment[1] = i;
});
});
}
mappings.push(...decoded);
}
}
const writer = new CssWriter(result, {
sources,
sourcesContent,
mappings: encode(mappings)
}, this.warn);
css(writer);
}
}
};
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;
} );
}
map.mappings = encodeSourceMap( mappings );
return {
code,
map
};
}