diff --git a/package-lock.json b/package-lock.json index 954ec15..3d1d1e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1980,6 +1980,15 @@ "@babel/helper-plugin-utils": "^7.0.0" } }, + "@babel/plugin-syntax-dynamic-import": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.0.0.tgz", + "integrity": "sha512-Gt9xNyRrCHCiyX/ZxDGOcBnlJl0I3IWicpZRC4CdC0P5a/I07Ya2OAMEBU+J7GmRFVmIetqEYRko6QYRuKOESw==", + "dev": true, + "requires": { + "@babel/helper-plugin-utils": "^7.0.0" + } + }, "@babel/plugin-syntax-json-strings": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/@babel/plugin-syntax-json-strings/-/plugin-syntax-json-strings-7.0.0.tgz", @@ -5340,7 +5349,6 @@ "version": "0.1.4", "bundled": true, "dev": true, - "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -5662,8 +5670,7 @@ "is-buffer": { "version": "1.1.6", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "is-builtin-module": { "version": "1.0.0", @@ -5747,7 +5754,6 @@ "version": "3.2.2", "bundled": true, "dev": true, - "optional": true, "requires": { "is-buffer": "^1.1.5" } @@ -5794,8 +5800,7 @@ "longest": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "lru-cache": { "version": "4.1.3", @@ -6061,8 +6066,7 @@ "repeat-string": { "version": "1.6.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "require-directory": { "version": "2.1.1", diff --git a/package.json b/package.json index 11ca7f5..02f1305 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 1c83674..ae68ca7 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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 ) => { diff --git a/src/utils.js b/src/utils.js index a02de30..5ba47e3 100644 --- a/src/utils.js +++ b/src/utils.js @@ -37,7 +37,8 @@ function filterMinifyOptions( options ) { 'banner', 'bannerNewLine', 'sourceMap', - 'comments' + 'comments', + 'plugins' ]; const minifyOptions = {}; diff --git a/tests/banner.js b/tests/banner.js index 76398c7..52b1410 100644 --- a/tests/banner.js +++ b/tests/banner.js @@ -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 \*\// ); + } ); + } ); } ); diff --git a/tests/fixtures/dynamicImport.js b/tests/fixtures/dynamicImport.js new file mode 100644 index 0000000..76e5d56 --- /dev/null +++ b/tests/fixtures/dynamicImport.js @@ -0,0 +1,3 @@ +import( 'whatever' ).then( () => { + +} ); diff --git a/tests/helpers/createTransformTest.js b/tests/helpers/createTransformTest.js index 4fc4cf8..f91c062 100644 --- a/tests/helpers/createTransformTest.js +++ b/tests/helpers/createTransformTest.js @@ -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; diff --git a/tests/index.js b/tests/index.js index c76db17..1facbf2 100644 --- a/tests/index.js +++ b/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; @@ -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 ); + } ); + } ); } );