๐Ÿ“ฆ malash / linaria-issue-1382

๐Ÿ“„ README.md ยท 129 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129# Bad case demo for Linaria babel preset

## How to run

```bash
yarn
yarn start
```

## Bad case

You should checkout the `master` branch. The folder structure looks like this:

```
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ src
โ”‚   โ””โ”€โ”€ index.ts
โ””โ”€โ”€ yarn.lock
```

The `src/index.ts` file contains only one line: `const a: number = 1;`.

The `yarn start` command will run `babel --presets=@babel/typescript,@linaria src/index.ts` and output will be:

```
yarn run v1.22.19
$ babel --presets=@babel/typescript,@linaria src/index.ts
SyntaxError: /Users/malash/Projects/test-goji/src/index.ts: /Users/malash/Projects/test-goji/src/index.ts: Missing initializer in const declaration. (1:7)

> 1 | const a: number = 1;
    |        ^
  2 |
    at constructor (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:356:19)
    at Parser.raise (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:3223:19)
    at Parser.parseVar (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:13267:16)
    at Parser.parseVarStatement (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:13100:10)
    at Parser.parseStatementContent (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:12683:23)
    at Parser.parseStatementLike (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:12588:17)
    at Parser.parseModuleItem (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:12565:17)
    at Parser.parseBlockOrModuleBlockBody (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:13189:36)
    at Parser.parseBlockBody (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:13182:10)
    at Parser.parseProgram (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:12464:10)
    at Parser.parseTopLevel (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:12454:25)
    at Parser.parse (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:14376:10)
    at parse (/Users/malash/Projects/test-goji/node_modules/@babel/parser/lib/index.js:14417:38)
    at parser (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/parser/index.js:41:34)
    at parser.next (<anonymous>)
    at parse (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/parse.js:25:37)
    at parse.next (<anonymous>)
    at evaluateSync (/Users/malash/Projects/test-goji/node_modules/gensync/index.js:251:28)
    at sync (/Users/malash/Projects/test-goji/node_modules/gensync/index.js:89:14)
    at stopHiding - secret - don't use this - v1 (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/errors/rewrite-stack-trace.js:47:12)
    at Object.parseSync (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/parse.js:40:72)
    at parseFile (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/Entrypoint.helpers.js:35:29)
    at /Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/Entrypoint.helpers.js:129:48
    at EventEmitter.perf (/Users/malash/Projects/test-goji/node_modules/@linaria/utils/lib/EventEmitter.js:45:20)
    at getOrParse (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/Entrypoint.helpers.js:129:24)
    at get ast [as ast] (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/Entrypoint.helpers.js:148:14)
    at BaseAction.explodeReexports (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/generators/explodeReexports.js:39:68)
    at explodeReexports.next (<anonymous>)
    at /Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/BaseAction.js:66:78
    at EventEmitter.action (/Users/malash/Projects/test-goji/node_modules/@linaria/utils/lib/EventEmitter.js:25:22)
    at BaseAction.emitAction (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/BaseAction.js:131:39)
    at nextFn (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/BaseAction.js:66:32)
    at processNext (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/BaseAction.js:111:28)
    at Object.next (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/BaseAction.js:120:9)
    at syncActionRunner (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/actionRunner.js:68:95)
    at syncActionRunner (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/actionRunner.js:75:22)
    at syncActionRunner (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform/actions/actionRunner.js:75:22)
    at transformSync (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/transform.js:51:55)
    at PluginPass.pre (/Users/malash/Projects/test-goji/node_modules/@linaria/babel-preset/lib/plugins/babel-transform.js:42:36)
    at transformFile (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/transformation/index.js:73:27)
    at transformFile.next (<anonymous>)
    at run (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/transformation/index.js:24:12)
    at run.next (<anonymous>)
    at Function.<anonymous> (/Users/malash/Projects/test-goji/node_modules/@babel/core/lib/transform-file.js:27:33)
    at Generator.next (<anonymous>)
    at step (/Users/malash/Projects/test-goji/node_modules/gensync/index.js:261:32)
    at /Users/malash/Projects/test-goji/node_modules/gensync/index.js:273:13
    at async.call.result.err.err (/Users/malash/Projects/test-goji/node_modules/gensync/index.js:223:11)
    at /Users/malash/Projects/test-goji/node_modules/gensync/index.js:189:28
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
  code: 'BABEL_PARSE_ERROR',
  reasonCode: 'DeclarationMissingInitializer',
  loc: Position { line: 1, column: 7, index: 7 },
  pos: 7
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
```

## Good case

If you want to see the good case, you should checkout the `good-case` branch. The folder structure looks like this:

```
โ”œโ”€โ”€ package.json
|โ”€โ”€ babel.config.json
โ”œโ”€โ”€ src
โ”‚   โ””โ”€โ”€ index.ts
โ””โ”€โ”€ yarn.lock
```

As you can see, the only difference is that I added a `babel.config.json` file with the following content:

```json
{
  "presets": ["@babel/typescript", "@linaria"]
}
```

and now the `yarn start` command will run `babel src/index.ts` and output will be:

```
yarn run v1.22.19
$ babel src/index.ts
const a = 1;

โœจ  Done in 0.26s.
```

## Linaria 2

This bug is not present in Linaria 2. If you want to see the good case, you should checkout the `linaria-2` branch.

## Conclusion

I have no idea why this is happening, but I think it's a bug in the `@linaria/babel-preset` package. As I understand it, the `@linaria/babel-preset` package should be able to work with the `babel.config.json` file or `babel --presets` option, but it doesn't.