Skip to content

Commit

Permalink
perf: replace concat-with-sourcemaps with custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed Jun 8, 2020
1 parent 91846bc commit a1b04d2
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/utils/concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { SourceMapGenerator, RawSourceMap } from "source-map";
import { Extracted } from "../loaders/types";
import { mm } from "./sourcemap";

interface Concatenated {
css: string;
map: RawSourceMap;
}

export default async function (extracted: Extracted[]): Promise<Concatenated> {
const sm = new SourceMapGenerator({ file: "" });
const content = [];
let offset = 0;

for await (const { css, map } of extracted) {
content.push(css);

const consumer = await mm(map).toConsumer();
if (!consumer) continue;

consumer.eachMapping(m =>
sm.addMapping({
generated: { line: offset + m.generatedLine, column: m.generatedColumn },
original: { line: m.originalLine, column: m.originalColumn },
source: m.source,
name: m.name,
}),
);

if (consumer.sourcesContent) {
for (let i = 0; i < consumer.sources.length; i++) {
sm.setSourceContent(consumer.sources[i], consumer.sourcesContent[i]);
}
}

consumer.destroy();
offset += css.split("\n").length;
}

return {
css: content.join("\n"),
map: sm.toJSON(),
};
}

0 comments on commit a1b04d2

Please sign in to comment.