@@ -5,6 +5,8 @@ import { rollup } from 'rollup';
5
5
import { SourceMapConsumer } from 'source-map' ;
6
6
import jsonPlugin from '@rollup/plugin-json' ;
7
7
8
+ import { getCode } from '../../../util/test' ;
9
+
8
10
import babelPlugin from '..' ;
9
11
10
12
process . chdir ( __dirname ) ;
@@ -51,15 +53,14 @@ async function generate(input, babelOptions = {}, generateOptions = {}, rollupOp
51
53
...rollupOptions
52
54
} ) ;
53
55
54
- const {
55
- output : [ generated ]
56
- } = await bundle . generate ( Object . assign ( { format : 'cjs' } , generateOptions ) ) ;
57
-
58
- return generated ;
56
+ return getCode ( bundle , {
57
+ format : 'cjs' ,
58
+ ...generateOptions
59
+ } ) ;
59
60
}
60
61
61
62
test ( 'runs code through babel' , async ( t ) => {
62
- const { code } = await generate ( 'fixtures/basic/main.js' ) ;
63
+ const code = await generate ( 'fixtures/basic/main.js' ) ;
63
64
t . false ( code . includes ( 'const' ) ) ;
64
65
t . is (
65
66
code ,
@@ -72,17 +73,17 @@ console.log("the answer is ".concat(answer));
72
73
} ) ;
73
74
74
75
test ( 'adds helpers' , async ( t ) => {
75
- const { code } = await generate ( 'fixtures/class/main.js' ) ;
76
+ const code = await generate ( 'fixtures/class/main.js' ) ;
76
77
t . true ( code . includes ( 'function _classCallCheck' ) ) ;
77
78
} ) ;
78
79
79
80
test ( 'adds helpers in loose mode' , async ( t ) => {
80
- const { code } = await generate ( 'fixtures/class-loose/main.js' ) ;
81
+ const code = await generate ( 'fixtures/class-loose/main.js' ) ;
81
82
t . true ( code . includes ( 'function _inherits' ) ) ;
82
83
} ) ;
83
84
84
85
test ( 'does not babelify excluded code' , async ( t ) => {
85
- const { code } = await generate ( 'fixtures/exclusions/main.js' , { exclude : '**/foo.js' } ) ;
86
+ const code = await generate ( 'fixtures/exclusions/main.js' , { exclude : '**/foo.js' } ) ;
86
87
// eslint-disable-next-line no-template-curly-in-string
87
88
t . false ( code . includes ( '${foo()}' ) ) ;
88
89
t . true ( code . includes ( '=> 42' ) ) ;
@@ -98,7 +99,15 @@ console.log("the answer is ".concat(foo()));
98
99
} ) ;
99
100
100
101
test ( 'generates sourcemap by default' , async ( t ) => {
101
- const { code, map } = await generate ( 'fixtures/class/main.js' , { } , { sourcemap : true } ) ;
102
+ const bundle = await rollup ( {
103
+ input : 'fixtures/class/main.js' ,
104
+ plugins : [ babelPlugin ( { babelHelpers : 'bundled' } ) ]
105
+ } ) ;
106
+
107
+ const {
108
+ output : [ { code, map } ]
109
+ } = await bundle . generate ( { format : 'cjs' , sourcemap : true } ) ;
110
+
102
111
const target = 'log' ;
103
112
const smc = new SourceMapConsumer ( map ) ;
104
113
const loc = getLocation ( code , code . indexOf ( target ) ) ;
@@ -121,21 +130,15 @@ test('works with proposal-decorators (#18)', async (t) => {
121
130
} ) ;
122
131
123
132
test ( 'checks config per-file' , async ( t ) => {
124
- const bundle = await rollup ( {
125
- input : 'fixtures/checks/main.js' ,
126
- plugins : [ babelPlugin ( { babelHelpers : 'bundled' } ) ]
127
- } ) ;
128
- const {
129
- output : [ { code } ]
130
- } = await bundle . generate ( { output : { format : 'esm' } } ) ;
133
+ const code = await generate ( 'fixtures/checks/main.js' , { } , { format : 'esm' } ) ;
131
134
t . true ( code . includes ( 'class Foo' ) ) ;
132
135
t . true ( code . includes ( 'var Bar' ) ) ;
133
136
t . false ( code . includes ( 'class Bar' ) ) ;
134
137
} ) ;
135
138
136
139
test ( 'allows transform-runtime to be used instead of bundled helpers' , async ( t ) => {
137
140
const warnings = [ ] ;
138
- const { code } = await generate (
141
+ const code = await generate (
139
142
'fixtures/runtime-helpers/main.js' ,
140
143
{ babelHelpers : 'runtime' } ,
141
144
{ } ,
@@ -167,7 +170,7 @@ module.exports = Foo;
167
170
168
171
test ( 'allows transform-runtime to inject esm version of helpers' , async ( t ) => {
169
172
const warnings = [ ] ;
170
- const { code } = await generate (
173
+ const code = await generate (
171
174
'fixtures/runtime-helpers-esm/main.js' ,
172
175
{ babelHelpers : 'runtime' } ,
173
176
{
@@ -209,7 +212,7 @@ test('allows transform-runtime to be used instead of bundled helpers, but throws
209
212
} ) ;
210
213
211
214
test ( 'allows using external-helpers plugin in combination with @babel/plugin-external-helpers' , async ( t ) => {
212
- const { code } = await generate ( 'fixtures/external-helpers/main.js' , {
215
+ const code = await generate ( 'fixtures/external-helpers/main.js' , {
213
216
babelHelpers : 'external'
214
217
} ) ;
215
218
t . false ( code . includes ( 'function _classCallCheck' ) ) ;
@@ -234,7 +237,7 @@ module.exports = main;
234
237
} ) ;
235
238
236
239
test ( 'correctly renames helpers (#22)' , async ( t ) => {
237
- const { code } = await generate ( 'fixtures/named-function-helper/main.js' ) ;
240
+ const code = await generate ( 'fixtures/named-function-helper/main.js' ) ;
238
241
t . false ( code . includes ( 'babelHelpers_get get' ) , 'helper was incorrectly renamed' ) ;
239
242
} ) ;
240
243
@@ -247,17 +250,17 @@ test('runs preflight check correctly in absence of class transformer (#23)', asy
247
250
} ) ;
248
251
249
252
test ( 'produces valid code with typeof helper' , async ( t ) => {
250
- const { code } = await generate ( 'fixtures/typeof/main.js' ) ;
253
+ const code = await generate ( 'fixtures/typeof/main.js' ) ;
251
254
t . false ( code . includes ( 'var typeof' ) ) ;
252
255
} ) ;
253
256
254
257
test ( 'handles babelrc with ignore option used' , async ( t ) => {
255
- const { code } = await generate ( 'fixtures/ignored-file/main.js' ) ;
258
+ const code = await generate ( 'fixtures/ignored-file/main.js' ) ;
256
259
t . true ( code . includes ( 'class Ignored' ) ) ;
257
260
} ) ;
258
261
259
262
test ( 'transpiles only files with default extensions' , async ( t ) => {
260
- const { code } = await generate (
263
+ const code = await generate (
261
264
'fixtures/extensions-default/main.js' ,
262
265
{ } ,
263
266
{ } ,
@@ -274,7 +277,7 @@ test('transpiles only files with default extensions', async (t) => {
274
277
} ) ;
275
278
276
279
test ( 'transpiles only files with whitelisted extensions' , async ( t ) => {
277
- const { code } = await generate ( 'fixtures/extensions-custom/main.js' , {
280
+ const code = await generate ( 'fixtures/extensions-custom/main.js' , {
278
281
extensions : [ '.js' , '.other' ]
279
282
} ) ;
280
283
t . true ( code . includes ( 'class Es ' ) , 'should not transpile .es' ) ;
@@ -365,9 +368,8 @@ test('supports customizing the loader', async (t) => {
365
368
input : 'fixtures/basic/main.js' ,
366
369
plugins : [ customBabelPlugin ( { babelHelpers : 'bundled' } ) ]
367
370
} ) ;
368
- const {
369
- output : [ { code } ]
370
- } = await bundle . generate ( { format : 'cjs' } ) ;
371
+ const code = await getCode ( bundle ) ;
372
+
371
373
t . true ( code . includes ( '// Generated by some custom loader' ) , 'adds the custom comment' ) ;
372
374
t . true ( code . includes ( 'console.foobaz' ) , 'runs the plugin' ) ;
373
375
} ) ;
@@ -401,9 +403,8 @@ test('supports overriding the plugin options in custom loader', async (t) => {
401
403
input : 'fixtures/basic/main.js' ,
402
404
plugins : [ customBabelPlugin ( { babelHelpers : 'bundled' } ) ]
403
405
} ) ;
404
- const {
405
- output : [ { code } ]
406
- } = await bundle . generate ( { format : 'cjs' } ) ;
406
+ const code = await getCode ( bundle ) ;
407
+
407
408
t . false (
408
409
code . includes ( '// Generated by some custom loader' ) ,
409
410
'does not add the comment to ignored file'
@@ -412,7 +413,7 @@ test('supports overriding the plugin options in custom loader', async (t) => {
412
413
} ) ;
413
414
414
415
test ( 'uses babel plugins passed in to the rollup plugin' , async ( t ) => {
415
- const { code } = await generate ( 'fixtures/basic/main.js' , {
416
+ const code = await generate ( 'fixtures/basic/main.js' , {
416
417
plugins : [ [ replaceConsoleLogProperty , { replace : 'foobaz' } ] ]
417
418
} ) ;
418
419
t . true ( code . includes ( 'console.foobaz' ) ) ;
@@ -427,8 +428,7 @@ test('can be used as an input plugin while transforming the output', async (t) =
427
428
} )
428
429
]
429
430
} ) ;
430
- const {
431
- output : [ { code } ]
432
- } = await bundle . generate ( { format : 'cjs' } ) ;
431
+ const code = await getCode ( bundle ) ;
432
+
433
433
t . false ( code . includes ( 'const' ) ) ;
434
434
} ) ;
0 commit comments