Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #141 from Comandeer/t/137
Browse files Browse the repository at this point in the history
Add ability to pass plugins to Babel
  • Loading branch information
Comandeer committed Nov 22, 2018
2 parents 9a6b50a + 67fbf72 commit 2b694c7
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 13 deletions.
20 changes: 12 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -69,6 +69,7 @@
"rollup": ">=0.58.0"
},
"devDependencies": {
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/register": "^7.0.0",
"@comandeer/eslint-config": "^0.1.0",
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Expand Up @@ -16,7 +16,8 @@ function minify( options = {} ) {
const babelConf = {
presets: [ [ minifyPreset, minifyOptions ] ],
sourceMaps: typeof options.sourceMap !== 'undefined' ? Boolean( options.sourceMap ) : true,
comments: typeof options.comments !== 'undefined' ? Boolean( options.comments ) : true
comments: typeof options.comments !== 'undefined' ? Boolean( options.comments ) : true,
plugins: Array.isArray( options.plugins ) ? options.plugins : []
};
let banner;

Expand All @@ -26,11 +27,11 @@ function minify( options = {} ) {
const bannerContent = getCommentContent( banner );
let isAlreadyInserted = false;

babelConf.plugins = [
babelConf.plugins = babelConf.plugins.concat( [
[ bannerPlugin, {
banner
} ]
];
] );

if ( !babelConf.comments ) {
babelConf.shouldPrintComment = ( comment ) => {
Expand Down
3 changes: 2 additions & 1 deletion src/utils.js
Expand Up @@ -37,7 +37,8 @@ function filterMinifyOptions( options ) {
'banner',
'bannerNewLine',
'sourceMap',
'comments'
'comments',
'plugins'
];
const minifyOptions = {};

Expand Down
20 changes: 20 additions & 0 deletions tests/banner.js
Expand Up @@ -160,4 +160,24 @@ describe( 'banner and comments support', () => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\/[^\n]/ );
} );
} );

// #138
it ( 'works with other plugins', () => {
return createTransformTest( {
skipBabel: true,
fixture: 'dynamicImport',
rollupOptions: {
plugins: [
plugin( {
plugins: [
'@babel/plugin-syntax-dynamic-import'
],
banner: '/* hublabubla */'
} )
]
}
} ).then( ( { bundle } ) => {
expect( bundle.code ).to.match( /^\/\* hublabubla \*\// );
} );
} );
} );
3 changes: 3 additions & 0 deletions tests/fixtures/dynamicImport.js
@@ -0,0 +1,3 @@
import( 'whatever' ).then( () => {

} );
3 changes: 2 additions & 1 deletion tests/helpers/createTransformTest.js
Expand Up @@ -22,13 +22,14 @@ function getFixturePath( fixtureName ) {

function createTransformTest( {
fixture = defaultFixture,
skipBabel = false,
babelOptions = defaultBabelOptions,
rollupOptions = defaultRollupOptions,
bundleOptions = defaultBundleOptions
} = {} ) {
const path = getFixturePath( fixture );
const code = readFileSync( path, 'utf8' );
const babeledCode = transform( code, babelOptions );
const babeledCode = skipBabel !== true ? transform( code, babelOptions ) : null;

rollupOptions.input = path;

Expand Down
44 changes: 44 additions & 0 deletions tests/index.js
@@ -1,5 +1,7 @@
import chai from 'chai';
import dynamicImportPlugin from '@babel/plugin-syntax-dynamic-import';
import createTransformTest from './helpers/createTransformTest.js';
import { defaultBabelOptions } from './helpers/createTransformTest.js';
import plugin from '../src/index.js';

const expect = chai.expect;
Expand Down Expand Up @@ -36,4 +38,46 @@ describe( 'plugin and its configuration', () => {
expect( bundle.code.trim() ).to.equal( transpiled.code );
} );
} );

// #137, #138
it( 'allows passing additional plugins by name', () => {
const pluginsOption = [ '@babel/plugin-syntax-dynamic-import' ];

return createTransformTest( {
fixture: 'dynamicImport',
babelOptions: Object.assign( {}, defaultBabelOptions, {
plugins: pluginsOption
} ),
rollupOptions: {
plugins: [
plugin( {
plugins: pluginsOption
} )
]
}
} ).then( ( { bundle, transpiled } ) => {
expect( bundle.code.trim() ).to.equal( transpiled.code );
} );
} );

// #137, #138
it( 'allows passing additional plugins by instance', () => {
const pluginsOption = [ dynamicImportPlugin ];

return createTransformTest( {
fixture: 'dynamicImport',
babelOptions: Object.assign( {}, defaultBabelOptions, {
plugins: pluginsOption
} ),
rollupOptions: {
plugins: [
plugin( {
plugins: pluginsOption
} )
]
}
} ).then( ( { bundle, transpiled } ) => {
expect( bundle.code.trim() ).to.equal( transpiled.code );
} );
} );
} );

0 comments on commit 2b694c7

Please sign in to comment.