Skip to content

Commit 72c9303

Browse files
committed
(test): ensure styled-components works with TSDX
- well, babel-plugin-styled-components fails... but we have a TODO to make it work at least! - ensure styled template tags get converted to regular functions - add a build-withBabel fixture (deps): add styled-components, babel-plugin-styled-components - and it depends on react-dom, react-is - and needed react-dom and @types/react-dom for the fixture code too - and @types/styled-components for TS usage
1 parent 7c89be6 commit 72c9303

File tree

8 files changed

+360
-2
lines changed

8 files changed

+360
-2
lines changed

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,25 @@
107107
"@types/ora": "^3.2.0",
108108
"@types/ps-tree": "^1.1.0",
109109
"@types/react": "^16.9.11",
110+
"@types/react-dom": "^16.9.5",
110111
"@types/rollup-plugin-json": "^3.0.2",
111112
"@types/rollup-plugin-sourcemaps": "^0.4.2",
112113
"@types/sade": "^1.6.0",
113114
"@types/semver": "^7.1.0",
115+
"@types/styled-components": "^5.0.1",
114116
"autoprefixer": "^9.7.4",
117+
"babel-plugin-styled-components": "^1.10.7",
115118
"cssnano": "^4.1.10",
116119
"doctoc": "^1.4.0",
117120
"husky": "^4.2.2",
118121
"pretty-quick": "^2.0.0",
119122
"ps-tree": "^1.2.0",
120123
"react": "^16.8.6",
124+
"react-dom": "^16.13.0",
125+
"react-is": "^16.13.0",
121126
"rollup-plugin-postcss": "^2.5.0",
122127
"semver": "^7.1.1",
128+
"styled-components": "^5.0.1",
123129
"tiny-invariant": "^1.1.0",
124130
"tiny-warning": "^1.0.3"
125131
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
plugins: [
3+
'styled-components'
4+
]
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"scripts": {
3+
"build": "tsdx build"
4+
},
5+
"name": "build-withbabel",
6+
"license": "MIT"
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import './styled.tsx';
2+
3+
export const sum = (a: number, b: number) => {
4+
if ('development' === process.env.NODE_ENV) {
5+
console.log('fuck');
6+
}
7+
return a + b;
8+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import styled from 'styled-components';
4+
5+
const Title = styled.h1`
6+
/* this comment should be removed */
7+
font-size: 1.5em;
8+
text-align: center;
9+
color: palevioletred;
10+
`;
11+
12+
ReactDOM.render(<Title />, document.getElementById('test'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"lib": ["dom", "esnext"],
5+
"declaration": true,
6+
"sourceMap": true,
7+
"rootDir": "./src",
8+
"strict": true,
9+
"noImplicitAny": true,
10+
"strictNullChecks": true,
11+
"strictFunctionTypes": true,
12+
"strictPropertyInitialization": true,
13+
"noImplicitThis": true,
14+
"alwaysStrict": true,
15+
"noUnusedLocals": true,
16+
"noUnusedParameters": true,
17+
"noImplicitReturns": true,
18+
"noFallthroughCasesInSwitch": true,
19+
"moduleResolution": "node",
20+
"baseUrl": "./",
21+
"paths": {
22+
"*": ["src/*", "node_modules/*"]
23+
},
24+
"jsx": "react",
25+
"esModuleInterop": true
26+
},
27+
"include": ["src", "types"],
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const shell = require('shelljs');
2+
3+
const util = require('../util');
4+
5+
shell.config.silent = false;
6+
7+
const testDir = 'integration';
8+
const fixtureName = 'build-withBabel';
9+
const stageName = `stage-integration-${fixtureName}`;
10+
11+
describe('integration :: tsdx build :: .babelrc.js', () => {
12+
beforeAll(() => {
13+
util.teardownStage(stageName);
14+
util.setupStageWithFixture(testDir, stageName, fixtureName);
15+
});
16+
17+
it('should convert styled-components template tags', () => {
18+
let output = shell.exec('node ../dist/index.js build');
19+
expect(output.code).toBe(0);
20+
21+
// from styled.h1` to styled.h1(
22+
output = shell.grep(/styled.h1\(/, ['dist/build-withbabel.*.js']);
23+
expect(output.code).toBe(0);
24+
});
25+
26+
// TODO: make this test work by allowing customization of plugin order
27+
it.skip('should remove comments in the CSS', () => {
28+
// the "should be removed" comment shouldn't be there (gets error code)
29+
const output = shell.grep(/should be removed/, [
30+
'dist/build-withbabel.*.js',
31+
]);
32+
expect(output.code).toBe(1);
33+
});
34+
35+
it('should compile files into a dist directory', () => {
36+
expect(shell.test('-f', 'dist/index.js')).toBeTruthy();
37+
expect(
38+
shell.test('-f', 'dist/build-withbabel.cjs.development.js')
39+
).toBeTruthy();
40+
expect(
41+
shell.test('-f', 'dist/build-withbabel.cjs.production.min.js')
42+
).toBeTruthy();
43+
expect(shell.test('-f', 'dist/build-withbabel.esm.js')).toBeTruthy();
44+
45+
expect(shell.test('-f', 'dist/index.d.ts')).toBeTruthy();
46+
});
47+
48+
afterAll(() => {
49+
util.teardownStage(stageName);
50+
});
51+
});

0 commit comments

Comments
 (0)