Skip to content

Commit

Permalink
fix sourcemap #17
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Oct 5, 2023
1 parent f370dce commit f8cfe2e
Show file tree
Hide file tree
Showing 15 changed files with 6,950 additions and 208 deletions.
118 changes: 75 additions & 43 deletions dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,6 @@
return `data:application/json,${encodeURIComponent(JSON.stringify(this.toJSON()))}`;
}
toJSON() {
// console.error({
//
// version: this.#version,
// sources: this.#sources.slice(),
// mappings: [...this.#map.values()]
// });
const mappings = [];
let i = 0;
for (; i <= this.#line; i++) {
Expand Down Expand Up @@ -712,19 +706,22 @@
}
}
function doRender(data, options = {}) {
options = {
...(options.minify ?? true ? {
indent: '',
newLine: '',
removeComments: true
} : {
indent: ' ',
newLine: '\n',
compress: false,
removeComments: false,
}), sourcemap: false, colorConvert: true, expandNestingRules: false, preserveLicense: false, ...options
};
const startTime = performance.now();
const errors = [];
options = Object.assign(options.minify ?? true ? {
indent: '',
newLine: '',
removeComments: true
} : {
indent: ' ',
newLine: '\n',
compress: false,
removeComments: false,
}, { colorConvert: true, expandNestingRules: false, preserveLicense: false }, options);
const sourcemap = options.sourcemap ? new SourceMap : null;
const cache = Object.create(null);
const result = {
code: renderAstNode(options.expandNestingRules ? expand(data) : data, options, sourcemap, {
ind: 0,
Expand All @@ -737,20 +734,47 @@
}
return acc + curr.val;
}
return acc + renderToken(curr, options, reducer, errors);
}, 0), errors, stats: {
return acc + renderToken(curr, options, cache, reducer, errors);
}, cache), errors, stats: {
total: `${(performance.now() - startTime).toFixed(2)}ms`
}
};
if (options.output != null) {
// @ts-ignore
options.output = options.resolve(options.output, options.cwd).absolute;
}
if (sourcemap != null) {
result.map = sourcemap.toJSON();
// @ts-ignore
// result.map.sources = result.map.sources?.map(s => <string>options?.resolve(s, <string>options?.cwd)?.relative)
}
return result;
}
function updateSourceMap(node, options, cache, sourcemap, position, str) {
if ([4 /* NodeType.RuleNodeType */, 3 /* NodeType.AtRuleNodeType */].includes(node.typ)) {
let src = node.loc?.src ?? '';
let output = options.output ?? '';
if (src !== '') {
if (!(src in cache)) {
// @ts-ignore
cache[src] = options.resolve(src, options.cwd ?? '').relative;
}
}
if (!(output in cache)) {
// @ts-ignore
cache[output] = options.resolve(output, options.cwd).relative;
}
// @ts-ignore
sourcemap.add({ src: cache[output], sta: { ...position } }, {
...node.loc,
// @ts-ignore
src: options.resolve(cache[src], options.cwd).relative
});
}
update(position, str);
}
// @ts-ignore
function renderAstNode(data, options, sourcemap, position, errors, reducer, level = 0, indents = []) {
function renderAstNode(data, options, sourcemap, position, errors, reducer, cache, level = 0, indents = []) {
if (indents.length < level + 1) {
indents.push(options.indent.repeat(level));
}
Expand All @@ -767,35 +791,19 @@
return !options.removeComments || (options.preserveLicense && data.val.startsWith('/*!')) ? data.val : '';
case 2 /* NodeType.StyleSheetNodeType */:
return data.chi.reduce((css, node) => {
const str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, level, indents);
const str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level, indents);
if (str === '') {
return css;
}
if (css === '') {
if (sourcemap != null) {
if ([4 /* NodeType.RuleNodeType */, 3 /* NodeType.AtRuleNodeType */].includes(node.typ)) {
let src = node.loc?.src ?? '';
if (src !== '') {
// @ts-ignore
src = options.resolve(src, options.cwd ?? '').relative;
}
sourcemap.add({ src: '', sta: { ...position } }, { ...node.loc, src });
}
update(position, str);
updateSourceMap(node, options, cache, sourcemap, position, str);
}
return str;
}
if (sourcemap != null) {
update(position, options.newLine);
if ([4 /* NodeType.RuleNodeType */, 3 /* NodeType.AtRuleNodeType */].includes(node.typ)) {
let src = node.loc?.src ?? '';
if (src !== '') {
// @ts-ignore
src = options.resolve(src, options.cwd ?? '').relative;
}
sourcemap.add({ src: '', sta: { ...position } }, { ...node.loc, src });
}
update(position, str);
updateSourceMap(node, options, cache, sourcemap, position, str);
}
return `${css}${options.newLine}${str}`;
}, '');
Expand Down Expand Up @@ -826,7 +834,7 @@
str = `${data.val === '' ? '' : options.indent || ' '}${data.val};`;
}
else {
str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, level + 1, indents);
str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
}
if (css === '') {
return str;
Expand All @@ -846,7 +854,7 @@
}
return '';
}
function renderToken(token, options = {}, reducer, errors) {
function renderToken(token, options = {}, cache = Object.create(null), reducer, errors) {
if (reducer == null) {
reducer = function (acc, curr) {
if (curr.typ == exports.EnumToken.CommentTokenType && options.removeComments) {
Expand All @@ -855,12 +863,12 @@
}
return acc + curr.val;
}
return acc + renderToken(curr, options, reducer, errors);
return acc + renderToken(curr, options, cache, reducer, errors);
};
}
switch (token.typ) {
case exports.EnumToken.BinaryExpressionTokenType:
return renderToken(token.l) + (token.op == exports.EnumToken.Add ? ' + ' : (token.op == exports.EnumToken.Sub ? ' - ' : (token.op == exports.EnumToken.Mul ? '*' : '/'))) + renderToken(token.r);
return renderToken(token.l, options, cache) + (token.op == exports.EnumToken.Add ? ' + ' : (token.op == exports.EnumToken.Sub ? ' - ' : (token.op == exports.EnumToken.Mul ? '*' : '/'))) + renderToken(token.r, options, cache);
case exports.EnumToken.Add:
return ' + ';
case exports.EnumToken.Sub:
Expand Down Expand Up @@ -955,7 +963,7 @@
case exports.EnumToken.FrequencyTokenType:
case exports.EnumToken.ResolutionTokenType:
if (token.val.typ == exports.EnumToken.BinaryExpressionTokenType) {
const result = renderToken(token.val);
const result = renderToken(token.val, options, cache);
if (!('unit' in token)) {
return result;
}
Expand Down Expand Up @@ -1034,6 +1042,28 @@
return '';
}
case exports.EnumToken.UrlTokenTokenType:
if (token.typ == exports.EnumToken.UrlTokenTokenType) {
if (options.output != null) {
if (!('original' in token)) {
// do not modify original token
token = { ...token };
Object.defineProperty(token, 'original', { enumerable: false, writable: false, value: token.val });
}
// @ts-ignore
if (!(token.original in cache)) {
let output = options.output ?? '';
const key = output + 'abs';
if (!(key in cache)) {
// @ts-ignore
cache[key] = options.dirname(options.resolve(output, options.cwd).absolute);
}
// @ts-ignore
cache[token.original] = options.resolve(token.original, cache[key]).relative;
}
// @ts-ignore
token.val = cache[token.original];
}
}
case exports.EnumToken.AtRuleTokenType:
case exports.EnumToken.HashTokenType:
case exports.EnumToken.PseudoClassTokenType:
Expand Down Expand Up @@ -5427,13 +5457,15 @@
return doRender(data, Object.assign(options, {
load,
resolve,
dirname,
cwd: options.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
}));
}
async function parse(iterator, opt = {}) {
return doParse(iterator, Object.assign(opt, {
load,
resolve,
dirname,
cwd: opt.cwd ?? self.location.pathname.endsWith('/') ? self.location.pathname : dirname(self.location.pathname)
}));
}
Expand Down
Loading

0 comments on commit f8cfe2e

Please sign in to comment.