1
- import { expect , test } from 'vitest'
1
+ import path from 'node:path'
2
+ import { describe , expect , test } from 'vitest'
2
3
import type { InternalModuleFormat } from 'rolldown'
3
4
import { resolveConfig } from '../../config'
4
- import { buildOxcPlugin } from '../../plugins/oxc'
5
+ import { buildOxcPlugin , transformWithOxc } from '../../plugins/oxc'
5
6
import { PartialEnvironment } from '../../baseEnvironment'
6
7
7
8
async function createBuildOxcPluginRenderChunk ( target : string ) {
@@ -26,10 +27,142 @@ async function createBuildOxcPluginRenderChunk(target: string) {
26
27
}
27
28
}
28
29
29
- test ( 'should inject helper for worker iife from esm' , async ( ) => {
30
- const renderChunk = await createBuildOxcPluginRenderChunk ( 'es2015' )
31
- const result = await renderChunk (
32
- `(function() {
30
+ describe ( 'transformWithOxc' , ( ) => {
31
+ test ( 'correctly overrides TS configuration and applies automatic transform' , async ( ) => {
32
+ const jsxImportSource = 'bar'
33
+ const result = await transformWithOxc (
34
+ 'const foo = () => <></>' ,
35
+ path . resolve (
36
+ import . meta. dirname ,
37
+ './fixtures/oxc-tsconfigs/jsx-preserve/baz.jsx' ,
38
+ ) ,
39
+ {
40
+ jsx : {
41
+ runtime : 'automatic' ,
42
+ importSource : jsxImportSource ,
43
+ } ,
44
+ } ,
45
+ )
46
+ expect ( result ?. code ) . toContain ( `${ jsxImportSource } /jsx-runtime` )
47
+ expect ( result ?. code ) . toContain ( '/* @__PURE__ */' )
48
+ } )
49
+
50
+ test ( 'correctly overrides TS configuration and preserves code' , async ( ) => {
51
+ const foo = 'const foo = () => <></>'
52
+ const result = await transformWithOxc (
53
+ foo ,
54
+ path . resolve (
55
+ import . meta. dirname ,
56
+ './fixtures/oxc-tsconfigs/jsx-react-jsx/baz.jsx' ,
57
+ ) ,
58
+ {
59
+ jsx : 'preserve' ,
60
+ } ,
61
+ )
62
+ expect ( result ?. code ) . toContain ( foo )
63
+ } )
64
+
65
+ test ( 'correctly overrides TS configuration and transforms code' , async ( ) => {
66
+ const jsxFactory = 'h' ,
67
+ jsxFragment = 'bar'
68
+ const result = await transformWithOxc (
69
+ 'const foo = () => <></>' ,
70
+ path . resolve (
71
+ import . meta. dirname ,
72
+ './fixtures/oxc-tsconfigs/jsx-complex-options/baz.jsx' ,
73
+ ) ,
74
+ {
75
+ jsx : {
76
+ runtime : 'classic' ,
77
+ pragma : jsxFactory ,
78
+ pragmaFrag : jsxFragment ,
79
+ } ,
80
+ } ,
81
+ )
82
+ expect ( result ?. code ) . toContain (
83
+ `/* @__PURE__ */ ${ jsxFactory } (${ jsxFragment } , null)` ,
84
+ )
85
+ } )
86
+
87
+ describe ( 'useDefineForClassFields' , async ( ) => {
88
+ const transformClassCode = async ( target : string , tsconfigDir : string ) => {
89
+ const result = await transformWithOxc (
90
+ `
91
+ class foo {
92
+ bar = 'bar'
93
+ }
94
+ ` ,
95
+ path . resolve ( import . meta. dirname , tsconfigDir , './bar.ts' ) ,
96
+ { target } ,
97
+ )
98
+ return result ?. code
99
+ }
100
+
101
+ const [
102
+ defineForClassFieldsTrueTransformedCode ,
103
+ defineForClassFieldsTrueLowerTransformedCode ,
104
+ defineForClassFieldsFalseTransformedCode ,
105
+ ] = await Promise . all ( [
106
+ transformClassCode ( 'esnext' , './fixtures/oxc-tsconfigs/use-define-true' ) ,
107
+ transformClassCode ( 'es2021' , './fixtures/oxc-tsconfigs/use-define-true' ) ,
108
+ transformClassCode ( 'esnext' , './fixtures/oxc-tsconfigs/use-define-false' ) ,
109
+ ] )
110
+
111
+ test ( 'target: esnext and tsconfig.target: esnext => true' , async ( ) => {
112
+ const actual = await transformClassCode (
113
+ 'esnext' ,
114
+ './fixtures/oxc-tsconfigs/target-esnext' ,
115
+ )
116
+ expect ( actual ) . toBe ( defineForClassFieldsTrueTransformedCode )
117
+ } )
118
+
119
+ test ( 'target: es2021 and tsconfig.target: esnext => true' , async ( ) => {
120
+ const actual = await transformClassCode (
121
+ 'es2021' ,
122
+ './fixtures/oxc-tsconfigs/target-esnext' ,
123
+ )
124
+ expect ( actual ) . toBe ( defineForClassFieldsTrueLowerTransformedCode )
125
+ } )
126
+
127
+ test ( 'target: es2021 and tsconfig.target: es2021 => false' , async ( ) => {
128
+ const actual = await transformClassCode (
129
+ 'es2021' ,
130
+ './fixtures/oxc-tsconfigs/target-es2021' ,
131
+ )
132
+ expect ( actual ) . toBe ( defineForClassFieldsFalseTransformedCode )
133
+ } )
134
+
135
+ test ( 'target: esnext and tsconfig.target: es2021 => false' , async ( ) => {
136
+ const actual = await transformClassCode (
137
+ 'esnext' ,
138
+ './fixtures/oxc-tsconfigs/target-es2021' ,
139
+ )
140
+ expect ( actual ) . toBe ( defineForClassFieldsFalseTransformedCode )
141
+ } )
142
+
143
+ test ( 'target: es2022 and tsconfig.target: es2022 => true' , async ( ) => {
144
+ const actual = await transformClassCode (
145
+ 'es2022' ,
146
+ './fixtures/oxc-tsconfigs/target-es2022' ,
147
+ )
148
+ expect ( actual ) . toBe ( defineForClassFieldsTrueTransformedCode )
149
+ } )
150
+
151
+ test ( 'target: es2022 and tsconfig.target: undefined => false' , async ( ) => {
152
+ const actual = await transformClassCode (
153
+ 'es2022' ,
154
+ './fixtures/oxc-tsconfigs/empty' ,
155
+ )
156
+ expect ( actual ) . toBe ( defineForClassFieldsFalseTransformedCode )
157
+ } )
158
+ } )
159
+ } )
160
+
161
+ describe ( 'renderChunk' , ( ) => {
162
+ test ( 'should inject helper for worker iife from esm' , async ( ) => {
163
+ const renderChunk = await createBuildOxcPluginRenderChunk ( 'es2015' )
164
+ const result = await renderChunk (
165
+ `(function() {
33
166
34
167
"use strict";
35
168
@@ -41,9 +174,9 @@ test('should inject helper for worker iife from esm', async () => {
41
174
42
175
//#endregion
43
176
})();` ,
44
- 'iife' ,
45
- )
46
- expect ( result ) . toMatchInlineSnapshot ( `
177
+ 'iife' ,
178
+ )
179
+ expect ( result ) . toMatchInlineSnapshot ( `
47
180
"(function() {
48
181
"use strict";var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
49
182
@@ -56,12 +189,12 @@ test('should inject helper for worker iife from esm', async () => {
56
189
})();
57
190
"
58
191
` )
59
- } )
192
+ } )
60
193
61
- test ( 'should inject helper for worker iife from cjs' , async ( ) => {
62
- const renderChunk = await createBuildOxcPluginRenderChunk ( 'es2015' )
63
- const result = await renderChunk (
64
- `(function() {
194
+ test ( 'should inject helper for worker iife from cjs' , async ( ) => {
195
+ const renderChunk = await createBuildOxcPluginRenderChunk ( 'es2015' )
196
+ const result = await renderChunk (
197
+ `(function() {
65
198
66
199
67
200
//#region src/index.js
@@ -72,9 +205,9 @@ test('should inject helper for worker iife from cjs', async () => {
72
205
73
206
//#endregion
74
207
})();` ,
75
- 'iife' ,
76
- )
77
- expect ( result ) . toMatchInlineSnapshot ( `
208
+ 'iife' ,
209
+ )
210
+ expect ( result ) . toMatchInlineSnapshot ( `
78
211
"(function() {var babelHelpers=function(exports){function t(e,t,n,r,i,a,o){try{var s=e[a](o),c=s.value}catch(e){return void n(e)}s.done?t(c):Promise.resolve(c).then(r,i)}function n(e){return function(){var n=this,r=arguments;return new Promise(function(i,a){var o=e.apply(n,r);function s(e){t(o,i,a,s,c,\`next\`,e)}function c(e){t(o,i,a,s,c,\`throw\`,e)}s(void 0)})}}return exports.asyncToGenerator=n,exports}({});
79
212
80
213
//#region src/index.js
@@ -86,4 +219,5 @@ test('should inject helper for worker iife from cjs', async () => {
86
219
})();
87
220
"
88
221
` )
222
+ } )
89
223
} )
0 commit comments