Skip to content

Commit e773e0c

Browse files
committed
fix(minify): remove useless span expression
1 parent e088adb commit e773e0c

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-styled-components-plugin",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A styled-components plugin to minify & set componentId & displayName for typescript",
55
"author": "acrazing <[email protected]>",
66
"main": "lib/index.js",

src/createStyledComponentsTransformer.spec.ts

+37
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,43 @@ const testCases: TestCase[] = [
156156
bar: foo; \`;other\` foo: bar; \``,
157157
output: `css \`foo:bar;bar:foo;\`;\nkeyframes \`foo:bar;bar:foo;\`;\ncreateGlobalStyle \`foo:bar;bar:foo;\`;\ninjectGlobal \`foo:bar;bar:foo;\`;\nother \` foo: bar; \`;`,
158158
},
159+
{
160+
name: 'minify comments',
161+
input: `css\`
162+
// comment
163+
// \${"span in comment"}
164+
// this should be removed
165+
title: "";
166+
value: \${"value"};
167+
second: "";
168+
// second comment
169+
// no span
170+
third: \${"third"};
171+
/* comment block
172+
value: \${"block comment"};
173+
*/
174+
forth: \${"forth"};
175+
end: "end with space";
176+
\``,
177+
output: `css \`title:"";value:\${"value"};second:"";third:\${"third"};forth:\${"forth"};end:"end with space";\`;`,
178+
},
179+
{
180+
name: 'minify as single span',
181+
input: `css\`
182+
// comment 1
183+
// comment \${comment}
184+
title: value;
185+
// comment \${3}
186+
title: value;
187+
\`;css\`
188+
// comment \${1}
189+
title: value;
190+
\`;css\`
191+
title: value;
192+
// comment: \${2}
193+
\``,
194+
output: `css \`title:value;title:value;\`;\ncss \`title:value;\`;\ncss \`title:value;\`;`,
195+
},
159196
];
160197

161198
describe('createStyledComponentsTransformer', () => {

src/minifyTemplate.ts

+26-18
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,30 @@ export function minifyTemplate(node: ts.TaggedTemplateExpression) {
153153
cookedValues.push(span.literal.text);
154154
});
155155
}
156-
const [spans] = minifyCookedValues(cookedValues);
157-
return ts.createTaggedTemplate(
158-
node.tag,
159-
node.typeArguments,
160-
ts.isNoSubstitutionTemplateLiteral(node.template)
161-
? ts.createNoSubstitutionTemplateLiteral(spans[0])
162-
: ts.createTemplateExpression(
163-
ts.createTemplateHead(spans[0]),
164-
node.template.templateSpans.map((span, index) => {
165-
return ts.createTemplateSpan(
166-
span.expression,
167-
index === spans.length - 2
168-
? ts.createTemplateTail(spans[index + 1])
169-
: ts.createTemplateMiddle(spans[index + 1]),
170-
);
171-
}),
172-
),
173-
);
156+
const [spans, indicates] = minifyCookedValues(cookedValues);
157+
let newTemplate: ts.TemplateLiteral;
158+
if (ts.isNoSubstitutionTemplateLiteral(template)) {
159+
newTemplate = ts.createNoSubstitutionTemplateLiteral(spans[0] || '');
160+
} else {
161+
const templateSpans = template.templateSpans.slice();
162+
indicates.forEach((expressionIndex, iteration) => {
163+
templateSpans.splice(expressionIndex - iteration, 1);
164+
});
165+
if (templateSpans.length === 0) {
166+
newTemplate = ts.createNoSubstitutionTemplateLiteral(spans[0] || '');
167+
} else {
168+
newTemplate = ts.createTemplateExpression(
169+
ts.createTemplateHead(spans[0]),
170+
templateSpans.map((span, index) => {
171+
return ts.createTemplateSpan(
172+
span.expression,
173+
index === spans.length - 2
174+
? ts.createTemplateTail(spans[index + 1])
175+
: ts.createTemplateMiddle(spans[index + 1]),
176+
);
177+
}),
178+
);
179+
}
180+
}
181+
return ts.createTaggedTemplate(node.tag, node.typeArguments, newTemplate);
174182
}

0 commit comments

Comments
 (0)