Skip to content

Commit 39043f3

Browse files
committed
Fix line comment minifying and add test for createGlobalStyle
1 parent cb3e1bb commit 39043f3

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

src/__tests__/baselines/minification/minify-css-in-helpers.ts.baseline

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Source code:
77

88
declare const keyframes: any;
99
declare const css: any;
10+
declare const createGlobalStyle: any;
1011

1112
declare const theColor: any;
1213

@@ -20,26 +21,38 @@ Source code:
2021
color: \${theColor};
2122
\`;
2223

24+
const MyRedBody = createGlobalStyle\`
25+
body {
26+
background-color: red; // comments
27+
\${color} // comments
28+
// it will be ignored, but still emitted \${color}
29+
}
30+
\`;
31+
2332
export {};
2433

2534

2635
TypeScript before transform:
2736

2837
declare const keyframes: any;
2938
declare const css: any;
39+
declare const createGlobalStyle: any;
3040
declare const theColor: any;
3141
const key = keyframes \`\\n to {\\n transform: rotate(360deg);\\n }\\n\`;
3242
const color = css \`\\n color: \${theColor};\\n\`;
43+
const MyRedBody = createGlobalStyle \`\\n body {\\n background-color: red; // comments\\n \${color} // comments\\n // it will be ignored, but still emitted \${color}\\n }\\n\`;
3344
export {};
3445

3546

3647
TypeScript after transform:
3748

3849
declare const keyframes: any;
3950
declare const css: any;
51+
declare const createGlobalStyle: any;
4052
declare const theColor: any;
4153
const key = keyframes \`to{transform:rotate(360deg);}\`;
4254
const color = css \`color:\${theColor};\`;
55+
const MyRedBody = createGlobalStyle \`body{background-color:red;\${color}//\${color}\\n}\`;
4356
export {};
4457

4558

src/__tests__/baselines/minification/minify-single-line-comments-with-interpolations.ts.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ TypeScript after transform:
8181
const Test4 = styled.div.withConfig({ displayName: "Test4" }) \`width:100%;//\${'red'}\`;
8282
const Test5 = styled.div.withConfig({ displayName: "Test5" }) \`width:100%;//\${'red'}\${'blue'}\`;
8383
const Test6 = styled.div.withConfig({ displayName: "Test6" }) \`background:url("https://google.com");width:100%;\${'green'}//\${'red'}\${'blue'}\`;
84-
const Test7 = styled.div.withConfig({ displayName: "Test7" }) \`background:url("https://google.com");width:\${p => p.props.width};\${'green'}//\${'red'}\${'blue'}height:\${p => p.props.height};\`;
84+
const Test7 = styled.div.withConfig({ displayName: "Test7" }) \`background:url("https://google.com");width:\${p => p.props.width};\${'green'}//\${'red'}\${'blue'}\\nheight:\${p => p.props.height};\`;
8585
const Test8 = styled.dev.withConfig({ displayName: "Test8" }) \`color:/*\${'red'}*/blue;\`;
86-
const Test9 = styled.dev.withConfig({ displayName: "Test9" }) \`color://\${'red'}blue\`;
86+
const Test9 = styled.dev.withConfig({ displayName: "Test9" }) \`color://\${'red'}\\nblue\`;
8787
export {};
8888

8989

src/__tests__/fixtures/minification/minify-css-in-helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare const keyframes: any;
22
declare const css: any;
3+
declare const createGlobalStyle: any;
34

45
declare const theColor: any;
56

@@ -13,4 +14,12 @@ const color = css`
1314
color: ${theColor};
1415
`;
1516

17+
const MyRedBody = createGlobalStyle`
18+
body {
19+
background-color: red; // comments
20+
${color} // comments
21+
// it will be ignored, but still emitted ${color}
22+
}
23+
`;
24+
1625
export {};

src/minify.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as ts from 'typescript';
22
import { isNoSubstitutionTemplateLiteral, isTemplateExpression } from './ts-is-kind';
33

4-
type State = ';' | 'x' | ' ' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
4+
type State = ';' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
55
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State } | void;
66
type StateMachine = {
77
[K in State]: {
@@ -41,6 +41,15 @@ const stateMachine: StateMachine = {
4141
return { state: 'x', emit: ' ' + ch };
4242
}
4343
},
44+
'\n': { // may need new line
45+
next(ch) {
46+
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch, emit: '\n' + ch }
47+
if (ch == ' ' || ch == '\n' || ch == '\r') return { state: '\n', skipEmit: true }
48+
if (ch == '/') return { state: '/', emit: '\n' }
49+
if (isSymbol(ch)) return { state: ';', emit: '\n' + ch };
50+
return { state: 'x', emit: '\n' + ch };
51+
}
52+
},
4453
"'": {
4554
next(ch) {
4655
if (ch == '\'') return { state: ';' };
@@ -79,7 +88,7 @@ const stateMachine: StateMachine = {
7988
'/$': { },
8089
'//$': {
8190
next(ch) {
82-
if (ch == '\n') return { state: ';', skipEmit: true }
91+
if (ch == '\n') return { state: '\n', skipEmit: true }
8392
return { skipEmit: true };
8493
}
8594
},
@@ -148,14 +157,6 @@ function createMinifier(): (next: string, last?: boolean) => string {
148157
}
149158
}
150159

151-
function *minifierFn() {
152-
let nextResult = '';
153-
while (true) {
154-
const next = yield nextResult;
155-
nextResult = next;
156-
}
157-
}
158-
159160
export function minifyTemplate(templateLiteral: ts.TemplateLiteral) {
160161
const minifier = createMinifier();
161162

0 commit comments

Comments
 (0)