-
Notifications
You must be signed in to change notification settings - Fork 582
/
Copy pathcomponent-file-extensions.ts
188 lines (174 loc) · 4.51 KB
/
component-file-extensions.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { MitosisConfig, Target } from '@/types/config';
import { checkShouldOutputTypeScript } from './output';
const COMPONENT_EXTENSIONS = {
jsx: ['.lite.tsx', '.lite.jsx'],
svelte: ['.svelte'],
};
export const COMPONENT_IMPORT_EXTENSIONS = [COMPONENT_EXTENSIONS.svelte, COMPONENT_EXTENSIONS.jsx]
.flat()
.concat(['.lite']);
export const checkIsSvelteComponentFilePath = (filePath: string) =>
COMPONENT_EXTENSIONS.svelte.some((x) => filePath.endsWith(x));
export const checkIsLiteComponentFilePath = (filePath: string) =>
COMPONENT_EXTENSIONS.jsx.some((x) => filePath.endsWith(x));
export const checkIsMitosisComponentFilePath = (filePath: string) =>
checkIsLiteComponentFilePath(filePath) || checkIsSvelteComponentFilePath(filePath);
/**
* Matches `.svelte`, `.lite.tsx`, `.lite.jsx` files (with optional `.jsx`/`.tsx` extension)
*/
export const INPUT_EXTENSION_REGEX = /\.(svelte|(lite(\.tsx|\.jsx)?))/g;
export const renameComponentFile = ({
path,
target,
options,
}: {
path: string;
target: Target;
options: MitosisConfig;
}) =>
path.replace(
INPUT_EXTENSION_REGEX,
getComponentFileExtensionForTarget({
type: 'filename',
target,
isTypescript: checkShouldOutputTypeScript({ options, target }),
}),
);
/**
* just like `INPUT_EXTENSION_REGEX`, but adds trailing quotes to the end of import paths.
*/
const INPUT_EXTENSION_IMPORT_REGEX = /\.(svelte|(lite(\.tsx|\.jsx)?))(?<quote>['"])/g;
export const renameComponentImport = ({
importPath,
target,
explicitImportFileExtension,
}: {
importPath: string;
target: Target;
explicitImportFileExtension: boolean;
}) => {
return importPath.replace(
INPUT_EXTENSION_IMPORT_REGEX,
`${getComponentFileExtensionForTarget({
type: 'import',
target,
explicitImportFileExtension,
})}$4`,
);
};
export const renameImport = ({
importPath,
target,
explicitImportFileExtension,
}: {
importPath: string;
target: Target;
explicitImportFileExtension: boolean;
}) => {
return importPath.replace(
/\.js(['"])/g,
`${getFileExtensionForTarget({
target,
explicitImportFileExtension,
})}$1`,
);
};
type Args = { target: Target } & (
| {
/**
* Whether we are rendering an import statement or a filename.
*/
type: 'import';
explicitImportFileExtension: boolean;
}
| {
/**
* Whether we are rendering an import statement or a filename.
*/
type: 'filename';
isTypescript: boolean;
}
);
/**
* Provides the correct file extension for a given component. This is used:
* - in `core` to render import statements within other components.
* - in `cli` to render filenames for generated components, and import statements within plain `.js`/`.ts` files.
*/
export const getComponentFileExtensionForTarget = (args: Args): string => {
switch (args.target) {
case 'angular': {
switch (args.type) {
case 'import':
return '.js';
case 'filename':
return args.isTypescript ? '.ts' : '.js';
}
}
case 'alpine':
case 'html':
return '.html';
case 'svelte':
return '.svelte';
case 'swift':
return '.swift';
case 'vue':
return '.vue';
case 'webcomponent':
return '.ts';
case 'lit':
return '.ts';
case 'qwik': {
switch (args.type) {
case 'import':
return '.jsx';
case 'filename':
return args.isTypescript ? '.tsx' : '.jsx';
}
}
case 'solid':
case 'preact':
case 'react':
case 'reactNative':
case 'rsc':
case 'stencil':
switch (args.type) {
case 'import':
// we can't have `.jsx`/`.tsx` extensions in the import paths, so we stick with implicit file extensions.
return args.explicitImportFileExtension ? '.js' : '';
case 'filename':
return args.isTypescript ? '.tsx' : '.jsx';
}
case 'marko':
return '.marko';
default:
return '.js';
}
};
export const getFileExtensionForTarget = ({
target,
explicitImportFileExtension,
}: {
target: Target;
explicitImportFileExtension: boolean;
}): string => {
switch (target) {
case 'angular':
case 'alpine':
case 'html':
case 'svelte':
case 'swift':
case 'vue':
case 'webcomponent':
case 'lit':
case 'qwik':
case 'react':
case 'reactNative':
case 'rsc':
case 'solid':
case 'stencil':
case 'marko':
case 'preact':
default:
return explicitImportFileExtension ? '.js' : '';
}
};