Skip to content

Commit 7b12b45

Browse files
authored
Merge pull request #39 from Igorbek/bugfix/issue36
Fix minification issues #36
2 parents e77f358 + a8bee3f commit 7b12b45

11 files changed

+274
-7
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue36-extended.ts 1`] = `
4+
5+
File: issue36-extended.ts
6+
Source code:
7+
8+
declare const styled: any;
9+
10+
export const A = styled.div\`
11+
border: \${'solid'} 10px;
12+
\`
13+
14+
styled.div\`
15+
border: \${'solid'}// comment here
16+
10px;
17+
border: solid// comment here
18+
10px;
19+
\`
20+
21+
styled.div\`
22+
border: \${'solid'}/* comment here
23+
*/10px;
24+
border: \${'solid'}/* comment here
25+
*/ 10px;
26+
\`
27+
28+
29+
TypeScript before transform:
30+
31+
declare const styled: any;
32+
export const A = styled.div \`\\n border: \${"solid"} 10px;\\n\`;
33+
styled.div \`\\n border: \${"solid"}// comment here\\n10px;\\n border: solid// comment here\\n10px;\\n\`;
34+
styled.div \`\\n border: \${"solid"}/* comment here\\n*/10px;\\n border: \${"solid"}/* comment here\\n*/ 10px;\\n\`;
35+
36+
37+
TypeScript after transform:
38+
39+
declare const styled: any;
40+
export const A = styled.div \`border:\${'solid'} 10px;\`;
41+
styled.div \`border:\${'solid'} 10px;border:solid 10px;\`;
42+
styled.div \`border:\${'solid'} 10px;border:\${'solid'} 10px;\`;
43+
44+
45+
46+
`;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue36.tsx 1`] = `
4+
5+
File: issue36.tsx
6+
Source code:
7+
8+
declare const keyframes: any;
9+
declare const styled: any;
10+
11+
const rotate360 = keyframes\`
12+
from {
13+
transform: rotate(0deg);
14+
}
15+
to {
16+
transform: rotate(360deg);
17+
}
18+
\`;
19+
20+
export const StyledDiv = styled.div\`
21+
width: 100px;
22+
height: 100px;
23+
background-color: greenyellow;
24+
animation: \${rotate360} 2s linear infinite;
25+
\`;
26+
27+
28+
TypeScript before transform:
29+
30+
declare const keyframes: any;
31+
declare const styled: any;
32+
const rotate360 = keyframes \`\\n from {\\n transform: rotate(0deg);\\n }\\n to {\\n transform: rotate(360deg);\\n }\\n\`;
33+
export const StyledDiv = styled.div \`\\n width: 100px;\\n height: 100px;\\n background-color: greenyellow;\\n animation: \${rotate360} 2s linear infinite;\\n\`;
34+
35+
36+
TypeScript after transform:
37+
38+
declare const keyframes: any;
39+
declare const styled: any;
40+
const rotate360 = keyframes \`from{transform:rotate(0deg);}to{transform:rotate(360deg);}\`;
41+
export const StyledDiv = styled.div \`width:100px;height:100px;background-color:greenyellow;animation:\${rotate360} 2s linear infinite;\`;
42+
43+
44+
45+
`;

src/__tests__/baselines/minification-only/minify-css-to-use-without-transpilation.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TypeScript after transform:
6565
const SpecialCharacters = styled.div \`content:" \${props => props.text} ";color:red;\`;
6666
const Comment = styled.div \`width:100%;color:red;\`;
6767
const Parens = styled.div \`&:hover{color:blue;}color:red;\`;
68-
const UrlComments = styled.div \`color:red; background:red;border:1px solid green;\`;
68+
const UrlComments = styled.div \`color:red;background:red;border:1px solid green;\`;
6969
export {};
7070

7171

src/__tests__/baselines/minification-only/simple.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ TypeScript after transform:
158158
styled.div \`line one{line:two;}\`;
159159
// removes line comments at the end of lines of code
160160
// \`valid line with out comments\`
161-
styled.div \`valid line without comments\`;
161+
styled.div \`valid line with out comments\`;
162162
// preserves multi-line comments starting with /*!
163163
// \`this is a /*! dont ignore me please */ test\`
164164
styled.div \`this is a test\`;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue36-extended.ts 1`] = `
4+
5+
File: issue36-extended.ts
6+
Source code:
7+
8+
declare const styled: any;
9+
10+
export const A = styled.div\`
11+
border: \${'solid'} 10px;
12+
\`
13+
14+
styled.div\`
15+
border: \${'solid'}// comment here
16+
10px;
17+
border: solid// comment here
18+
10px;
19+
\`
20+
21+
styled.div\`
22+
border: \${'solid'}/* comment here
23+
*/10px;
24+
border: \${'solid'}/* comment here
25+
*/ 10px;
26+
\`
27+
28+
29+
TypeScript before transform:
30+
31+
declare const styled: any;
32+
export const A = styled.div \`\\n border: \${"solid"} 10px;\\n\`;
33+
styled.div \`\\n border: \${"solid"}// comment here\\n10px;\\n border: solid// comment here\\n10px;\\n\`;
34+
styled.div \`\\n border: \${"solid"}/* comment here\\n*/10px;\\n border: \${"solid"}/* comment here\\n*/ 10px;\\n\`;
35+
36+
37+
TypeScript after transform:
38+
39+
declare const styled: any;
40+
export const A = styled.div.withConfig({ displayName: "A" }) \`border:\${'solid'} 10px;\`;
41+
styled.div \`border:\${'solid'} 10px;border:solid 10px;\`;
42+
styled.div \`border:\${'solid'} 10px;border:\${'solid'} 10px;\`;
43+
44+
45+
46+
`;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`issue36.tsx 1`] = `
4+
5+
File: issue36.tsx
6+
Source code:
7+
8+
declare const keyframes: any;
9+
declare const styled: any;
10+
11+
const rotate360 = keyframes\`
12+
from {
13+
transform: rotate(0deg);
14+
}
15+
to {
16+
transform: rotate(360deg);
17+
}
18+
\`;
19+
20+
export const StyledDiv = styled.div\`
21+
width: 100px;
22+
height: 100px;
23+
background-color: greenyellow;
24+
animation: \${rotate360} 2s linear infinite;
25+
\`;
26+
27+
28+
TypeScript before transform:
29+
30+
declare const keyframes: any;
31+
declare const styled: any;
32+
const rotate360 = keyframes \`\\n from {\\n transform: rotate(0deg);\\n }\\n to {\\n transform: rotate(360deg);\\n }\\n\`;
33+
export const StyledDiv = styled.div \`\\n width: 100px;\\n height: 100px;\\n background-color: greenyellow;\\n animation: \${rotate360} 2s linear infinite;\\n\`;
34+
35+
36+
TypeScript after transform:
37+
38+
declare const keyframes: any;
39+
declare const styled: any;
40+
const rotate360 = keyframes \`from{transform:rotate(0deg);}to{transform:rotate(360deg);}\`;
41+
export const StyledDiv = styled.div.withConfig({ displayName: "StyledDiv" }) \`width:100px;height:100px;background-color:greenyellow;animation:\${rotate360} 2s linear infinite;\`;
42+
43+
44+
45+
`;

src/__tests__/baselines/minification/minify-css-to-use-without-transpilation.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ TypeScript after transform:
6565
const SpecialCharacters = styled.div.withConfig({ displayName: "SpecialCharacters" }) \`content:" \${props => props.text} ";color:red;\`;
6666
const Comment = styled.div.withConfig({ displayName: "Comment" }) \`width:100%;color:red;\`;
6767
const Parens = styled.div.withConfig({ displayName: "Parens" }) \`&:hover{color:blue;}color:red;\`;
68-
const UrlComments = styled.div.withConfig({ displayName: "UrlComments" }) \`color:red; background:red;border:1px solid green;\`;
68+
const UrlComments = styled.div.withConfig({ displayName: "UrlComments" }) \`color:red;background:red;border:1px solid green;\`;
6969
export {};
7070

7171

src/__tests__/baselines/minification/simple.ts.baseline

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ TypeScript after transform:
158158
styled.div \`line one{line:two;}\`;
159159
// removes line comments at the end of lines of code
160160
// \`valid line with out comments\`
161-
styled.div \`valid line without comments\`;
161+
styled.div \`valid line with out comments\`;
162162
// preserves multi-line comments starting with /*!
163163
// \`this is a /*! dont ignore me please */ test\`
164164
styled.div \`this is a test\`;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
declare const styled: any;
2+
3+
export const A = styled.div`
4+
border: ${'solid'} 10px;
5+
`
6+
7+
styled.div`
8+
border: ${'solid'}// comment here
9+
10px;
10+
border: solid// comment here
11+
10px;
12+
`
13+
14+
styled.div`
15+
border: ${'solid'}/* comment here
16+
*/10px;
17+
border: ${'solid'}/* comment here
18+
*/ 10px;
19+
`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
declare const keyframes: any;
2+
declare const styled: any;
3+
4+
const rotate360 = keyframes`
5+
from {
6+
transform: rotate(0deg);
7+
}
8+
to {
9+
transform: rotate(360deg);
10+
}
11+
`;
12+
13+
export const StyledDiv = styled.div`
14+
width: 100px;
15+
height: 100px;
16+
background-color: greenyellow;
17+
animation: ${rotate360} 2s linear infinite;
18+
`;

src/minify.ts

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

4-
type State = ';' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | '/$' | '//$' | '/*' | '/**' | '/*$' | '/*$*';
5-
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State } | void;
4+
type State = ';' | ';$' | 'x' | ' ' | '\n' | '"' | '(' | '\'' | '/' | '//' | ';/' | ';//' | '/$' | '//$' | '/*' | '/**' | ';/*' | ';/**' | '/*$' | '/*$*';
5+
type ReducerResult = { emit?: string; skipEmit?: boolean; state?: State; } | void;
66
type StateMachine = {
77
[K in State]: {
88
next?(ch: string): ReducerResult;
@@ -19,6 +19,18 @@ const stateMachine: StateMachine = {
1919
next(ch) {
2020
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
2121
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true }
22+
if (ch == '/') return { state: ';/', skipEmit: true }
23+
if (isSymbol(ch)) return;
24+
return { state: 'x' }
25+
},
26+
flush() {
27+
return { state: ';$' }
28+
}
29+
},
30+
';$': { // after placeholder
31+
next(ch) {
32+
if (ch == '\'' || ch == '"' || ch == '(') return { state: ch }
33+
if (ch == ' ' || ch == '\n' || ch == '\r') return { skipEmit: true, state: ' ' } // we may need a space
2234
if (ch == '/') return { state: '/', skipEmit: true }
2335
if (isSymbol(ch)) return;
2436
return { state: 'x' }
@@ -76,6 +88,26 @@ const stateMachine: StateMachine = {
7688
}
7789
},
7890
'//': {
91+
next(ch) {
92+
if (ch == '\n') return { state: ' ', skipEmit: true }
93+
return { skipEmit: true };
94+
},
95+
flush(last) {
96+
if (last) return { skipEmit: true }
97+
return { state: '//$', emit: '//' }
98+
}
99+
},
100+
';/': {
101+
next(ch) {
102+
if (ch == '/') return { state: ';//', skipEmit: true }
103+
if (ch == '*') return { state: ';/*', skipEmit: true }
104+
return { state: ';', emit: '/' + ch }
105+
},
106+
flush() {
107+
return { state: '/$', emit: '/' }
108+
}
109+
},
110+
';//': {
79111
next(ch) {
80112
if (ch == '\n') return { state: ';', skipEmit: true }
81113
return { skipEmit: true };
@@ -108,6 +140,22 @@ const stateMachine: StateMachine = {
108140
return { state: '/*', skipEmit: true }
109141
}
110142
},
143+
';/*': {
144+
next(ch) {
145+
if (ch == '*') return { state: ';/**', skipEmit: true }
146+
return { skipEmit: true };
147+
},
148+
flush(last) {
149+
if (last) return { skipEmit: true }
150+
return { state: '/*$', emit: '/*' }
151+
}
152+
},
153+
';/**': {
154+
next(ch) {
155+
if (ch == '/') return { state: ';', skipEmit: true }
156+
return { state: ';/*', skipEmit: true }
157+
}
158+
},
111159
'/*$': {
112160
next(ch) {
113161
if (ch == '*') return { state: '/*$*', skipEmit: true };
@@ -141,7 +189,7 @@ function createMinifier(): (next: string, last?: boolean) => string {
141189
minified += ch;
142190
}
143191
}
144-
192+
145193
let pos = 0;
146194
let len = next.length;
147195
while (pos < len) {

0 commit comments

Comments
 (0)