Skip to content

Commit 6d7257c

Browse files
authored
(fix): change plugin order to make styled-components/macro work (#644)
- if babel-plugin-macros is first, macros get applied first - babel-plugin-styled-components says it should be placed first, but unfortunately all user plugins are added after TSDX plugins - unknown how changing this ordering could impact lots of code out there, but could be very breaking. - moving to a preset instead would mean this is more in user control - but we can change it so macros are added first and so one can use styled-components/macro instead as a workaround - based on babel-plugin-styled-component's code, docs, and output in detail, I think its ordering conflict may be with babel-plugin-annotate-pure-calls, which so happens to be the first plugin in babelPluginTsdx - maybe this should be last given that other plugins can change functions etc (test): update styled-component template tag test to reflect the slightly different tag due to the usage of the macro (fix/test): comment removal should use toBeFalsy, not toBeTruthy, since it's removed - this was a bug I introduced when adding the grep helper; just added it the same everywhere but this was the one place that was testing to get an error code - fix the comment so it doesn't say error code anymore either - since this test was skipped, I didn't pick up that it was wrong until it ran now
1 parent da4b189 commit 6d7257c

4 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/babelPluginTsdx.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
6767
// pragma: customOptions.jsx || 'h',
6868
// pragmaFrag: customOptions.jsxFragment || 'Fragment',
6969
// },
70+
{ name: 'babel-plugin-macros' },
7071
{ name: 'babel-plugin-annotate-pure-calls' },
7172
{ name: 'babel-plugin-dev-expression' },
7273
customOptions.format !== 'cjs' && {
@@ -86,9 +87,6 @@ export const babelPluginTsdx = babelPlugin.custom(() => ({
8687
name: '@babel/plugin-transform-regenerator',
8788
async: false,
8889
},
89-
{
90-
name: 'babel-plugin-macros',
91-
},
9290
isTruthy(customOptions.extractErrors) && {
9391
name: './errors/transformErrorMessages',
9492
},

test/integration/fixtures/build-withBabel/.babelrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ module.exports = {
44
'./test-babel-preset'
55
],
66
plugins: [
7-
'styled-components',
87
['@babel/plugin-transform-runtime', { helpers: false }],
98
]
109
}

test/integration/fixtures/build-withBabel/src/styled.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import styled from 'styled-components';
1+
import styled from 'styled-components/macro';
22

33
export const Title = styled.h1`
44
/* this comment should be removed */

test/integration/tsdx-build-withBabel.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ describe('integration :: tsdx build :: .babelrc.js', () => {
1919
const output = execWithCache('node ../dist/index.js build');
2020
expect(output.code).toBe(0);
2121

22-
// from styled.h1` to styled.h1(
23-
const matched = grep(/styled.h1\(/, ['dist/build-withbabel.*.js']);
22+
// from styled.h1` to styled.h1.withConfig(
23+
const matched = grep(/styled.h1.withConfig\(/, [
24+
'dist/build-withbabel.*.js',
25+
]);
2426
expect(matched).toBeTruthy();
2527
});
2628

27-
// TODO: make this test work by allowing customization of plugin order
28-
it.skip('should remove comments in the CSS', () => {
29+
// TODO: make styled-components work with its Babel plugin and not just its
30+
// macro by allowing customization of plugin order
31+
it('should remove comments in the CSS', () => {
2932
const output = execWithCache('node ../dist/index.js build');
3033
expect(output.code).toBe(0);
3134

32-
// the "should be removed" comment shouldn't be there (gets error code)
35+
// the comment "should be removed" should no longer be there
3336
const matched = grep(/should be removed/, ['dist/build-withbabel.*.js']);
34-
expect(matched).toBeTruthy();
37+
expect(matched).toBeFalsy();
3538
});
3639

3740
it('should add an import of regeneratorRuntime', () => {

0 commit comments

Comments
 (0)