Skip to content

Commit 9f2e8a6

Browse files
committed
use getCode helper where possible
1 parent 5a16788 commit 9f2e8a6

File tree

2 files changed

+90
-69
lines changed

2 files changed

+90
-69
lines changed

packages/babel/test/as-input-plugin.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { rollup } from 'rollup';
55
import { SourceMapConsumer } from 'source-map';
66
import jsonPlugin from '@rollup/plugin-json';
77

8+
import { getCode } from '../../../util/test';
9+
810
import babelPlugin from '..';
911

1012
process.chdir(__dirname);
@@ -51,15 +53,14 @@ async function generate(input, babelOptions = {}, generateOptions = {}, rollupOp
5153
...rollupOptions
5254
});
5355

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+
});
5960
}
6061

6162
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');
6364
t.false(code.includes('const'));
6465
t.is(
6566
code,
@@ -72,17 +73,17 @@ console.log("the answer is ".concat(answer));
7273
});
7374

7475
test('adds helpers', async (t) => {
75-
const { code } = await generate('fixtures/class/main.js');
76+
const code = await generate('fixtures/class/main.js');
7677
t.true(code.includes('function _classCallCheck'));
7778
});
7879

7980
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');
8182
t.true(code.includes('function _inherits'));
8283
});
8384

8485
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' });
8687
// eslint-disable-next-line no-template-curly-in-string
8788
t.false(code.includes('${foo()}'));
8889
t.true(code.includes('=> 42'));
@@ -98,7 +99,15 @@ console.log("the answer is ".concat(foo()));
9899
});
99100

100101
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+
102111
const target = 'log';
103112
const smc = new SourceMapConsumer(map);
104113
const loc = getLocation(code, code.indexOf(target));
@@ -121,21 +130,15 @@ test('works with proposal-decorators (#18)', async (t) => {
121130
});
122131

123132
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' });
131134
t.true(code.includes('class Foo'));
132135
t.true(code.includes('var Bar'));
133136
t.false(code.includes('class Bar'));
134137
});
135138

136139
test('allows transform-runtime to be used instead of bundled helpers', async (t) => {
137140
const warnings = [];
138-
const { code } = await generate(
141+
const code = await generate(
139142
'fixtures/runtime-helpers/main.js',
140143
{ babelHelpers: 'runtime' },
141144
{},
@@ -167,7 +170,7 @@ module.exports = Foo;
167170

168171
test('allows transform-runtime to inject esm version of helpers', async (t) => {
169172
const warnings = [];
170-
const { code } = await generate(
173+
const code = await generate(
171174
'fixtures/runtime-helpers-esm/main.js',
172175
{ babelHelpers: 'runtime' },
173176
{
@@ -209,7 +212,7 @@ test('allows transform-runtime to be used instead of bundled helpers, but throws
209212
});
210213

211214
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', {
213216
babelHelpers: 'external'
214217
});
215218
t.false(code.includes('function _classCallCheck'));
@@ -234,7 +237,7 @@ module.exports = main;
234237
});
235238

236239
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');
238241
t.false(code.includes('babelHelpers_get get'), 'helper was incorrectly renamed');
239242
});
240243

@@ -247,17 +250,17 @@ test('runs preflight check correctly in absence of class transformer (#23)', asy
247250
});
248251

249252
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');
251254
t.false(code.includes('var typeof'));
252255
});
253256

254257
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');
256259
t.true(code.includes('class Ignored'));
257260
});
258261

259262
test('transpiles only files with default extensions', async (t) => {
260-
const { code } = await generate(
263+
const code = await generate(
261264
'fixtures/extensions-default/main.js',
262265
{},
263266
{},
@@ -274,7 +277,7 @@ test('transpiles only files with default extensions', async (t) => {
274277
});
275278

276279
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', {
278281
extensions: ['.js', '.other']
279282
});
280283
t.true(code.includes('class Es '), 'should not transpile .es');
@@ -365,9 +368,8 @@ test('supports customizing the loader', async (t) => {
365368
input: 'fixtures/basic/main.js',
366369
plugins: [customBabelPlugin({ babelHelpers: 'bundled' })]
367370
});
368-
const {
369-
output: [{ code }]
370-
} = await bundle.generate({ format: 'cjs' });
371+
const code = await getCode(bundle);
372+
371373
t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment');
372374
t.true(code.includes('console.foobaz'), 'runs the plugin');
373375
});
@@ -401,9 +403,8 @@ test('supports overriding the plugin options in custom loader', async (t) => {
401403
input: 'fixtures/basic/main.js',
402404
plugins: [customBabelPlugin({ babelHelpers: 'bundled' })]
403405
});
404-
const {
405-
output: [{ code }]
406-
} = await bundle.generate({ format: 'cjs' });
406+
const code = await getCode(bundle);
407+
407408
t.false(
408409
code.includes('// Generated by some custom loader'),
409410
'does not add the comment to ignored file'
@@ -412,7 +413,7 @@ test('supports overriding the plugin options in custom loader', async (t) => {
412413
});
413414

414415
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', {
416417
plugins: [[replaceConsoleLogProperty, { replace: 'foobaz' }]]
417418
});
418419
t.true(code.includes('console.foobaz'));
@@ -427,8 +428,7 @@ test('can be used as an input plugin while transforming the output', async (t) =
427428
})
428429
]
429430
});
430-
const {
431-
output: [{ code }]
432-
} = await bundle.generate({ format: 'cjs' });
431+
const code = await getCode(bundle);
432+
433433
t.false(code.includes('const'));
434434
});

packages/babel/test/as-output-plugin.js

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import test from 'ava';
44
import { rollup } from 'rollup';
55
import { SourceMapConsumer } from 'source-map';
66

7+
import { getCode } from '../../../util/test';
8+
79
import babelPlugin from '..';
810

911
process.chdir(__dirname);
@@ -44,31 +46,32 @@ function replaceConsoleLogProperty({ types: t }) {
4446
}
4547

4648
async function generate(input, babelOptions = {}, generateOptions = {}, rollupOptions = {}) {
47-
const bundle = await rollup(Object.assign({ input }, rollupOptions));
48-
const {
49-
output: [generated]
50-
} = await bundle.generate({
49+
const bundle = await rollup({
50+
input,
51+
...rollupOptions
52+
});
53+
54+
return getCode(bundle, {
5155
format: 'cjs',
5256
plugins: [babelPlugin.generated(babelOptions)],
5357
...generateOptions
5458
});
55-
return generated;
5659
}
5760

5861
test('allows running the plugin on the output via output options', async (t) => {
59-
const { code } = await generate('fixtures/basic/main.js', {
62+
const code = await generate('fixtures/basic/main.js', {
6063
presets: ['@babel/env']
6164
});
6265
t.false(code.includes('const'));
6366
});
6467

6568
test('ignores .babelrc when transforming the output by default', async (t) => {
66-
const { code } = await generate('fixtures/basic/main.js');
69+
const code = await generate('fixtures/basic/main.js');
6770
t.true(code.includes('const'));
6871
});
6972

7073
test("allows transform-runtime to be used with `useESModules: false` (the default) and `format: 'cjs'`", async (t) => {
71-
const { code } = await generate(
74+
const code = await generate(
7275
'fixtures/runtime-helpers/main.js',
7376
{
7477
presets: ['@babel/env'],
@@ -92,7 +95,7 @@ module.exports = Foo;
9295
});
9396

9497
test("allows transform-runtime to be used with `useESModules: true` and `format: 'esm'`", async (t) => {
95-
const { code } = await generate(
98+
const code = await generate(
9699
'fixtures/runtime-helpers/main.js',
97100
{
98101
presets: ['@babel/env'],
@@ -114,7 +117,16 @@ export default Foo;
114117
});
115118

116119
test('generates sourcemap by default', async (t) => {
117-
const { code, map } = await generate('fixtures/class/main.js', {}, { sourcemap: true });
120+
const bundle = await rollup({ input: 'fixtures/class/main.js' });
121+
122+
const {
123+
output: [{ code, map }]
124+
} = await bundle.generate({
125+
format: 'cjs',
126+
plugins: [babelPlugin.generated()],
127+
sourcemap: true
128+
});
129+
118130
const target = 'log';
119131
const smc = new SourceMapConsumer(map);
120132
const loc = getLocation(code, code.indexOf(target));
@@ -130,7 +142,7 @@ test('generates sourcemap by default', async (t) => {
130142

131143
test('allows using external-helpers plugin even if the externalHelpers flag is not passed', async (t) => {
132144
const warnings = [];
133-
const { code } = await generate(
145+
const code = await generate(
134146
'fixtures/external-helpers/main.js',
135147
{
136148
presets: ['@babel/env'],
@@ -185,15 +197,20 @@ test('warns when using the "include" option', async (t) => {
185197

186198
test('transforms all chunks in a code-splitting setup', async (t) => {
187199
const bundle = await rollup({ input: 'fixtures/chunks/main.js' });
188-
const { output } = await bundle.generate({
189-
format: 'esm',
190-
plugins: [
191-
babelPlugin.generated({
192-
plugins: ['@babel/syntax-dynamic-import'],
193-
presets: ['@babel/env']
194-
})
195-
]
196-
});
200+
const output = await getCode(
201+
bundle,
202+
{
203+
format: 'esm',
204+
plugins: [
205+
babelPlugin.generated({
206+
plugins: ['@babel/syntax-dynamic-import'],
207+
presets: ['@babel/env']
208+
})
209+
]
210+
},
211+
true
212+
);
213+
197214
t.deepEqual(
198215
output.map(({ code }) => code),
199216
[
@@ -216,14 +233,19 @@ test('transforms all chunks when preserving modules', async (t) => {
216233
input: 'fixtures/preserve-modules/main.js',
217234
preserveModules: true
218235
});
219-
const { output } = await bundle.generate({
220-
format: 'esm',
221-
plugins: [
222-
babelPlugin.generated({
223-
presets: ['@babel/env']
224-
})
225-
]
226-
});
236+
const output = await getCode(
237+
bundle,
238+
{
239+
format: 'esm',
240+
plugins: [
241+
babelPlugin.generated({
242+
presets: ['@babel/env']
243+
})
244+
]
245+
},
246+
true
247+
);
248+
227249
t.deepEqual(
228250
output.map(({ code }) => code),
229251
[
@@ -262,9 +284,8 @@ test('supports customizing the loader', async (t) => {
262284
};
263285
});
264286
const bundle = await rollup({ input: 'fixtures/basic/main.js' });
265-
const {
266-
output: [{ code }]
267-
} = await bundle.generate({ format: 'cjs', plugins: [customBabelPlugin()] });
287+
const code = await getCode(bundle, { format: 'cjs', plugins: [customBabelPlugin()] });
288+
268289
t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment');
269290
t.true(code.includes('console.foobaz'), 'runs the plugin');
270291
});
@@ -282,7 +303,7 @@ test('throws when using a Rollup output format other than esm or cjs', async (t)
282303
});
283304

284305
test('allows using a Rollup output format other than esm or cjs with allowAllFormats', async (t) => {
285-
const { code } = await generate(
306+
const code = await generate(
286307
'fixtures/basic/main.js',
287308
{ presets: ['@babel/env'], allowAllFormats: true },
288309
{ format: 'iife' }
@@ -300,7 +321,7 @@ test('allows using a Rollup output format other than esm or cjs with allowAllFor
300321
});
301322

302323
test('allows using Babel to transform to other formats', async (t) => {
303-
const { code } = await generate(
324+
const code = await generate(
304325
'fixtures/basic/main.js',
305326
{ presets: [['@babel/env', { modules: 'umd' }]] },
306327
{ format: 'esm' }
@@ -330,7 +351,7 @@ test('allows using Babel to transform to other formats', async (t) => {
330351
});
331352

332353
test('loads configuration files when configFile is passed', async (t) => {
333-
const { code } = await generate('fixtures/config-file/main.js', {
354+
const code = await generate('fixtures/config-file/main.js', {
334355
configFile: nodePath.resolve(__dirname, 'fixtures/config-file/config.json')
335356
});
336357
t.is(

0 commit comments

Comments
 (0)