Skip to content

Commit

Permalink
use getCode helper where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Dec 25, 2019
1 parent a72c7cd commit 00df737
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 69 deletions.
70 changes: 35 additions & 35 deletions packages/babel/test/as-input-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { rollup } from 'rollup';
import { SourceMapConsumer } from 'source-map';
import jsonPlugin from '@rollup/plugin-json';

import { getCode } from '../../../util/test';

import babelPlugin from '..';

process.chdir(__dirname);
Expand Down Expand Up @@ -51,15 +53,14 @@ async function generate(input, babelOptions = {}, generateOptions = {}, rollupOp
...rollupOptions
});

const {
output: [generated]
} = await bundle.generate(Object.assign({ format: 'cjs' }, generateOptions));

return generated;
return getCode(bundle, {
format: 'cjs',
...generateOptions
});
}

test('runs code through babel', async (t) => {
const { code } = await generate('fixtures/basic/main.js');
const code = await generate('fixtures/basic/main.js');
t.false(code.includes('const'));
t.is(
code,
Expand All @@ -72,17 +73,17 @@ console.log("the answer is ".concat(answer));
});

test('adds helpers', async (t) => {
const { code } = await generate('fixtures/class/main.js');
const code = await generate('fixtures/class/main.js');
t.true(code.includes('function _classCallCheck'));
});

test('adds helpers in loose mode', async (t) => {
const { code } = await generate('fixtures/class-loose/main.js');
const code = await generate('fixtures/class-loose/main.js');
t.true(code.includes('function _inherits'));
});

test('does not babelify excluded code', async (t) => {
const { code } = await generate('fixtures/exclusions/main.js', { exclude: '**/foo.js' });
const code = await generate('fixtures/exclusions/main.js', { exclude: '**/foo.js' });
// eslint-disable-next-line no-template-curly-in-string
t.false(code.includes('${foo()}'));
t.true(code.includes('=> 42'));
Expand All @@ -98,7 +99,15 @@ console.log("the answer is ".concat(foo()));
});

test('generates sourcemap by default', async (t) => {
const { code, map } = await generate('fixtures/class/main.js', {}, { sourcemap: true });
const bundle = await rollup({
input: 'fixtures/class/main.js',
plugins: [babelPlugin({ babelHelpers: 'bundled' })]
});

const {
output: [{ code, map }]
} = await bundle.generate({ format: 'cjs', sourcemap: true });

const target = 'log';
const smc = new SourceMapConsumer(map);
const loc = getLocation(code, code.indexOf(target));
Expand All @@ -121,21 +130,15 @@ test('works with proposal-decorators (#18)', async (t) => {
});

test('checks config per-file', async (t) => {
const bundle = await rollup({
input: 'fixtures/checks/main.js',
plugins: [babelPlugin({ babelHelpers: 'bundled' })]
});
const {
output: [{ code }]
} = await bundle.generate({ output: { format: 'esm' } });
const code = await generate('fixtures/checks/main.js', {}, { format: 'esm' });
t.true(code.includes('class Foo'));
t.true(code.includes('var Bar'));
t.false(code.includes('class Bar'));
});

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

test('allows transform-runtime to inject esm version of helpers', async (t) => {
const warnings = [];
const { code } = await generate(
const code = await generate(
'fixtures/runtime-helpers-esm/main.js',
{ babelHelpers: 'runtime' },
{
Expand Down Expand Up @@ -209,7 +212,7 @@ test('allows transform-runtime to be used instead of bundled helpers, but throws
});

test('allows using external-helpers plugin in combination with @babel/plugin-external-helpers', async (t) => {
const { code } = await generate('fixtures/external-helpers/main.js', {
const code = await generate('fixtures/external-helpers/main.js', {
babelHelpers: 'external'
});
t.false(code.includes('function _classCallCheck'));
Expand All @@ -234,7 +237,7 @@ module.exports = main;
});

test('correctly renames helpers (#22)', async (t) => {
const { code } = await generate('fixtures/named-function-helper/main.js');
const code = await generate('fixtures/named-function-helper/main.js');
t.false(code.includes('babelHelpers_get get'), 'helper was incorrectly renamed');
});

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

test('produces valid code with typeof helper', async (t) => {
const { code } = await generate('fixtures/typeof/main.js');
const code = await generate('fixtures/typeof/main.js');
t.false(code.includes('var typeof'));
});

test('handles babelrc with ignore option used', async (t) => {
const { code } = await generate('fixtures/ignored-file/main.js');
const code = await generate('fixtures/ignored-file/main.js');
t.true(code.includes('class Ignored'));
});

test('transpiles only files with default extensions', async (t) => {
const { code } = await generate(
const code = await generate(
'fixtures/extensions-default/main.js',
{},
{},
Expand All @@ -274,7 +277,7 @@ test('transpiles only files with default extensions', async (t) => {
});

test('transpiles only files with whitelisted extensions', async (t) => {
const { code } = await generate('fixtures/extensions-custom/main.js', {
const code = await generate('fixtures/extensions-custom/main.js', {
extensions: ['.js', '.other']
});
t.true(code.includes('class Es '), 'should not transpile .es');
Expand Down Expand Up @@ -365,9 +368,8 @@ test('supports customizing the loader', async (t) => {
input: 'fixtures/basic/main.js',
plugins: [customBabelPlugin({ babelHelpers: 'bundled' })]
});
const {
output: [{ code }]
} = await bundle.generate({ format: 'cjs' });
const code = await getCode(bundle);

t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment');
t.true(code.includes('console.foobaz'), 'runs the plugin');
});
Expand Down Expand Up @@ -401,9 +403,8 @@ test('supports overriding the plugin options in custom loader', async (t) => {
input: 'fixtures/basic/main.js',
plugins: [customBabelPlugin({ babelHelpers: 'bundled' })]
});
const {
output: [{ code }]
} = await bundle.generate({ format: 'cjs' });
const code = await getCode(bundle);

t.false(
code.includes('// Generated by some custom loader'),
'does not add the comment to ignored file'
Expand All @@ -412,7 +413,7 @@ test('supports overriding the plugin options in custom loader', async (t) => {
});

test('uses babel plugins passed in to the rollup plugin', async (t) => {
const { code } = await generate('fixtures/basic/main.js', {
const code = await generate('fixtures/basic/main.js', {
plugins: [[replaceConsoleLogProperty, { replace: 'foobaz' }]]
});
t.true(code.includes('console.foobaz'));
Expand All @@ -427,8 +428,7 @@ test('can be used as an input plugin while transforming the output', async (t) =
})
]
});
const {
output: [{ code }]
} = await bundle.generate({ format: 'cjs' });
const code = await getCode(bundle);

t.false(code.includes('const'));
});
89 changes: 55 additions & 34 deletions packages/babel/test/as-output-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import test from 'ava';
import { rollup } from 'rollup';
import { SourceMapConsumer } from 'source-map';

import { getCode } from '../../../util/test';

import babelPlugin from '..';

process.chdir(__dirname);
Expand Down Expand Up @@ -44,31 +46,32 @@ function replaceConsoleLogProperty({ types: t }) {
}

async function generate(input, babelOptions = {}, generateOptions = {}, rollupOptions = {}) {
const bundle = await rollup(Object.assign({ input }, rollupOptions));
const {
output: [generated]
} = await bundle.generate({
const bundle = await rollup({
input,
...rollupOptions
});

return getCode(bundle, {
format: 'cjs',
plugins: [babelPlugin.generated(babelOptions)],
...generateOptions
});
return generated;
}

test('allows running the plugin on the output via output options', async (t) => {
const { code } = await generate('fixtures/basic/main.js', {
const code = await generate('fixtures/basic/main.js', {
presets: ['@babel/env']
});
t.false(code.includes('const'));
});

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

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

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

test('generates sourcemap by default', async (t) => {
const { code, map } = await generate('fixtures/class/main.js', {}, { sourcemap: true });
const bundle = await rollup({ input: 'fixtures/class/main.js' });

const {
output: [{ code, map }]
} = await bundle.generate({
format: 'cjs',
plugins: [babelPlugin.generated()],
sourcemap: true
});

const target = 'log';
const smc = new SourceMapConsumer(map);
const loc = getLocation(code, code.indexOf(target));
Expand All @@ -130,7 +142,7 @@ test('generates sourcemap by default', async (t) => {

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

test('transforms all chunks in a code-splitting setup', async (t) => {
const bundle = await rollup({ input: 'fixtures/chunks/main.js' });
const { output } = await bundle.generate({
format: 'esm',
plugins: [
babelPlugin.generated({
plugins: ['@babel/syntax-dynamic-import'],
presets: ['@babel/env']
})
]
});
const output = await getCode(
bundle,
{
format: 'esm',
plugins: [
babelPlugin.generated({
plugins: ['@babel/syntax-dynamic-import'],
presets: ['@babel/env']
})
]
},
true
);

t.deepEqual(
output.map(({ code }) => code),
[
Expand All @@ -216,14 +233,19 @@ test('transforms all chunks when preserving modules', async (t) => {
input: 'fixtures/preserve-modules/main.js',
preserveModules: true
});
const { output } = await bundle.generate({
format: 'esm',
plugins: [
babelPlugin.generated({
presets: ['@babel/env']
})
]
});
const output = await getCode(
bundle,
{
format: 'esm',
plugins: [
babelPlugin.generated({
presets: ['@babel/env']
})
]
},
true
);

t.deepEqual(
output.map(({ code }) => code),
[
Expand Down Expand Up @@ -262,9 +284,8 @@ test('supports customizing the loader', async (t) => {
};
});
const bundle = await rollup({ input: 'fixtures/basic/main.js' });
const {
output: [{ code }]
} = await bundle.generate({ format: 'cjs', plugins: [customBabelPlugin()] });
const code = await getCode(bundle, { format: 'cjs', plugins: [customBabelPlugin()] });

t.true(code.includes('// Generated by some custom loader'), 'adds the custom comment');
t.true(code.includes('console.foobaz'), 'runs the plugin');
});
Expand All @@ -282,7 +303,7 @@ test('throws when using a Rollup output format other than esm or cjs', async (t)
});

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

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

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

0 comments on commit 00df737

Please sign in to comment.