gulp-better-rollup
Advanced tools
Comparing version 3.1.0 to 3.2.0
95
index.js
@@ -24,2 +24,9 @@ 'use strict' | ||
function assignCertainProperties(toObject, fromObject, properties = []) { | ||
for (var key of properties) { | ||
if (toObject[key] === undefined && fromObject[key] !== undefined) | ||
toObject[key] = fromObject[key] | ||
} | ||
} | ||
// transformer class | ||
@@ -37,8 +44,7 @@ class GulpRollup extends Transform { | ||
var rollupOptions | ||
if (this.arg2) { | ||
rollupOptions = Object.assign({}, this.arg1) | ||
var inputOptions = Object.assign({}, this.arg1) | ||
var bundleList = parseBundles(this.arg2) | ||
} else { | ||
rollupOptions = {} | ||
var inputOptions = {} | ||
var bundleList = parseBundles(this.arg1) | ||
@@ -48,13 +54,13 @@ } | ||
// user should not specify the input file path, but let him if he insists for some reason | ||
if (rollupOptions.input === undefined) | ||
if (inputOptions.input === undefined) | ||
// determine input from file filename | ||
rollupOptions.input = path.relative(file.cwd, file.path) | ||
inputOptions.input = path.relative(file.cwd, file.path) | ||
else | ||
// rename file if input is given | ||
file.path = path.join(file.cwd, rollupOptions.input) | ||
file.path = path.join(file.cwd, inputOptions.input) | ||
// caching is enabled by default because of the nature of gulp and the watching/recompilatin | ||
// but can be disabled by setting 'cache' to false | ||
if (rollupOptions.cache !== false) | ||
rollupOptions.cache = rollupCache.get(rollupOptions.input) | ||
if (inputOptions.cache !== false) | ||
inputOptions.cache = rollupCache.get(inputOptions.input) | ||
@@ -68,22 +74,25 @@ // enable sourcemap is gulp-sourcemaps plugin is enabled | ||
function generateAndApplyBundle(bundle, generateOptions, targetFile) { | ||
// Sugaring the API by copying convinience objects and properties from rollupOptions | ||
// to generateOptions (if not defined) | ||
if (generateOptions.file === undefined) | ||
generateOptions.file = rollupOptions.file | ||
if (generateOptions['exports'] === undefined) | ||
generateOptions['exports'] = rollupOptions['exports'] | ||
if (generateOptions.format === undefined) | ||
generateOptions.format = rollupOptions.format | ||
if (generateOptions.globals === undefined) | ||
generateOptions.globals = rollupOptions.globals | ||
function generateAndApplyBundle(bundle, outputOptions, targetFile) { | ||
// Sugaring the API by copying convinience objects and properties from inputOptions | ||
// to outputOptions (if not defined) | ||
// Directly copied from https://rollupjs.org/guide/en#outputoptions | ||
var propsToCopy = [ | ||
// core options | ||
'format', 'file', 'dir', /*'name'*/, 'globals', | ||
// advanced options | ||
'paths', 'banner', 'footer', 'intro', 'outro', 'sourcemap', 'sourcemapFile', 'interop', 'extend', | ||
// danger zone | ||
'exports', /*'amd',*/ 'indent', 'strict', 'freeze', 'legacy', 'namespaceToStringTag', | ||
] | ||
assignCertainProperties(outputOptions, inputOptions, propsToCopy) | ||
// Rollup won't bundle iife and umd modules without module name. | ||
// But it won't say anything either, leaving a space for confusion | ||
if (generateOptions.name === undefined) | ||
generateOptions.name = rollupOptions.name || moduleName | ||
if (generateOptions.amd === undefined || generateOptions.amd.id === undefined) | ||
generateOptions.amd = Object.assign({}, generateOptions.amd, {id: generateOptions.name}) | ||
generateOptions.sourcemap = createSourceMap | ||
if (outputOptions.name === undefined) | ||
outputOptions.name = inputOptions.name || moduleName | ||
if (outputOptions.amd === undefined || outputOptions.amd.id === undefined) | ||
outputOptions.amd = Object.assign({}, outputOptions.amd, {id: outputOptions.name}) | ||
outputOptions.sourcemap = createSourceMap | ||
// generate bundle according to given or autocompleted options | ||
return bundle.generate(generateOptions).then(result => { | ||
return bundle.generate(outputOptions).then(result => { | ||
if (result === undefined) return | ||
// Pass sourcemap content and metadata to gulp-sourcemaps plugin to handle | ||
@@ -96,3 +105,3 @@ // destination (and custom name) was given, possibly multiple output bundles. | ||
// return bundled file as buffer | ||
targetFile.contents = new Buffer(result.code) | ||
targetFile.contents = Buffer.from(result.code) | ||
// apply sourcemap to output file | ||
@@ -103,7 +112,7 @@ if (createSourceMap) | ||
} | ||
var createBundle = (bundle, generateOptions, injectNewFile) => { | ||
var createBundle = (bundle, outputOptions, injectNewFile) => { | ||
// custom output name might be set | ||
if (generateOptions.file) { | ||
// setup filename name from generateOptions.file | ||
var newFileName = path.basename(generateOptions.file) | ||
if (outputOptions.file) { | ||
// setup filename name from outputOptions.file | ||
var newFileName = path.basename(outputOptions.file) | ||
var newFilePath = path.join(file.base, newFileName) | ||
@@ -126,3 +135,3 @@ if (injectNewFile) { | ||
}) | ||
return generateAndApplyBundle(bundle, generateOptions, newFile).then(result => { | ||
return generateAndApplyBundle(bundle, outputOptions, newFile).then(result => { | ||
this.push(newFile) | ||
@@ -134,3 +143,3 @@ return result | ||
file.path = newFilePath | ||
return generateAndApplyBundle(bundle, generateOptions, file) | ||
return generateAndApplyBundle(bundle, outputOptions, file) | ||
} | ||
@@ -140,3 +149,3 @@ } else { | ||
// apply data and sourcemaps to the original file | ||
return generateAndApplyBundle(bundle, generateOptions, file) | ||
return generateAndApplyBundle(bundle, outputOptions, file) | ||
} | ||
@@ -147,14 +156,14 @@ } | ||
// custom rollup can be provided inside the config object | ||
rollup = rollupOptions.rollup || rollup | ||
delete rollupOptions.rollup | ||
rollup = inputOptions.rollup || rollup | ||
delete inputOptions.rollup | ||
rollup | ||
// pass basic options to rollup | ||
.rollup(rollupOptions) | ||
.rollup(inputOptions) | ||
// after the magic is done, configure the output format | ||
.then(bundle => { | ||
// cache rollup object if caching is enabled | ||
if (rollupOptions.cache !== false) | ||
rollupCache.set(rollupOptions.input, bundle) | ||
// generate ouput according to (each of) given generateOptions | ||
return Promise.all(bundleList.map((generateOptions, i) => createBundle(bundle, generateOptions, i))) | ||
if (inputOptions.cache !== false) | ||
rollupCache.set(inputOptions.input, bundle) | ||
// generate ouput according to (each of) given outputOptions | ||
return Promise.all(bundleList.map((outputOptions, i) => createBundle(bundle, outputOptions, i))) | ||
}) | ||
@@ -164,4 +173,4 @@ // pass file to gulp and end stream | ||
.catch(err => { | ||
if (rollupOptions.cache !== false) | ||
rollupCache.delete(rollupOptions.input) | ||
if (inputOptions.cache !== false) | ||
rollupCache.delete(inputOptions.input) | ||
process.nextTick(() => { | ||
@@ -177,3 +186,3 @@ this.emit('error', new PluginError(PLUGIN_NAME, err)) | ||
// first argument (rollupOptions) is optional | ||
// first argument (inputOptions) is optional | ||
module.exports = function factory(arg1, arg2) { | ||
@@ -180,0 +189,0 @@ // instantiate the stream class |
{ | ||
"name": "gulp-better-rollup", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Better Gulp plugin for Rollup ES6 module bundler", | ||
@@ -27,4 +27,4 @@ "author": "Mike Kovarik", | ||
"lodash.camelcase": "^4.3.0", | ||
"plugin-error": "^0.1.2", | ||
"rollup": ">=0.48 <0.57", | ||
"plugin-error": "^1.0.1", | ||
"rollup": "^0.60.2", | ||
"vinyl": "^2.1.0", | ||
@@ -35,5 +35,5 @@ "vinyl-sourcemaps-apply": "^0.2.1" | ||
"gulp-sourcemaps": "^2.1.1", | ||
"mocha": "^3.1.2", | ||
"should": "^11.1.1" | ||
"mocha": "^5.1.1", | ||
"should": "^13.2.1" | ||
} | ||
} |
@@ -5,4 +5,17 @@ # gulp-better-rollup | ||
A [Gulp](https://www.npmjs.com/package/gulp) plugin for [Rollup](https://www.npmjs.com/package/rollup) ES6 Bundler. This plugin unlike [gulp-rollup](https://www.npmjs.com/package/gulp-rollup) integrates Rollup deeper into Gulps pipeline chain by taking some of the Rollup API out of your hands in exchange of giving you the full power over the pipeline (to use any gulp plugins). | ||
A [Gulp](https://www.npmjs.com/package/gulp) plugin for [Rollup](https://www.npmjs.com/package/rollup) ES6 Bundler. In comparison with [gulp-rollup](https://www.npmjs.com/package/gulp-rollup), this plugin integrates Rollup deeper into Gulps pipeline chain. It takes some of the Rollup API out of your hands, in exchange for giving you full power over the pipeline (to use any gulp plugins). | ||
## How it works | ||
Rollup is designed to handle reading files, building a dependency tree, transforming content and then writing the transformed files. This doesn't play well with gulp, since gulp is also designed to handle files with `gulp.src()` and `gulp.dest()`. Gulp plugins, by design _should_ just handle in-memory transformations. Not actual files. | ||
To tackle this problem gulp-better-rollup passes the file paths loaded in `gulp.src()` to rollup, rather than the gulp buffer. | ||
This comes with some caveats: | ||
* If you use other gulp plugin before gulp-better-rollup, their transformations will be lost. If the plugin doesn't do source transformations (like for example [gulp-sourcemaps](https://www.npmjs.com/package/gulp-sourcemaps)) this is fine. | ||
* The Rollup "input" argument is unsupported. | ||
* Since the output location is determined by `gulp.dest()`, the output "file" argument passed to Rollup can at most be used to set the file name for a bundle. If you pass a full directory path, only the file name part will be used. In addition, if you pass a file path to `gulp.dest()`, the Rollup "file" argument will be ignored entirely. | ||
* The `gulp-sourcemaps` plugin doesn't (yet) support the `.mjs` extension, that you may want to use to support the ES module format in Node.js. It can inline the sourcemap into the bundle file (using `sourcemaps.write()`), and create an external sourcemap file with `sourcemaps.write(PATH_TO_SOURCEMAP_FOLDER)`. It won't however insert the `//# sourceMappingURL=` linking comment at the end of your `.mjs` file, which effectively renders the sourcemaps useless. | ||
## Installation | ||
@@ -19,2 +32,3 @@ | ||
var rename = require('gulp-rename') | ||
var sourcemaps = require('gulp-sourcemaps') | ||
var rollup = require('gulp-better-rollup') | ||
@@ -27,6 +41,6 @@ var babel = require('rollup-plugin-babel') | ||
.pipe(rollup({ | ||
// notice there is no `input` option as rollup integrates into gulp pipeline | ||
// There is no `input` option as rollup integrates into the gulp pipeline | ||
plugins: [babel()] | ||
}, { | ||
// also rollups `sourcemap` option is replaced by gulp-sourcemaps plugin | ||
// Rollups `sourcemap` option is unsupported. Use `gulp-sourcemaps` plugin instead | ||
format: 'cjs', | ||
@@ -40,3 +54,3 @@ })) | ||
or simply | ||
Or simply: | ||
@@ -47,3 +61,3 @@ ``` js | ||
.pipe(sourcemaps.init()) | ||
// note that UMD and IIFE format requires `name` but it was guessed based on source file `mylibrary.js` | ||
// note that UMD and IIFE format requires `name` but it will be inferred from the source file name `mylibrary.js` | ||
.pipe(rollup({plugins: [babel()]}, 'umd')) | ||
@@ -58,10 +72,10 @@ // save sourcemap as separate file (in the same folder) | ||
### `rollup([rollupOptions,] generateOptions)` | ||
### `rollup([inputOptions,] outputOptions)` | ||
This plugin is based on [the standard Rollup options](https://github.com/rollup/rollup/wiki/JavaScript-API), with following caveats | ||
This plugin is based on [the standard Rollup options](https://rollupjs.org/guide/en#rollup-rollup), with the following exceptions: | ||
#### `rollupOptions` | ||
First argument is object of options found you would specify as [`rollup.rollup(options)` in Rollup API](https://github.com/rollup/rollup/wiki/JavaScript-API#rolluprollup-options-) | ||
#### `inputOptions` | ||
See [`rollup.rollup(inputOptions)` in the Rollup API](https://rollupjs.org/guide/en#inputoptions) | ||
`input` should not be used as the entry file is provided by gulp. It also works with gulp-watch | ||
`input` is unsupported, as the entry file is provided by gulp, which also works with [gulp-watch](https://www.npmjs.com/package/gulp-watch). | ||
@@ -75,3 +89,3 @@ ``` js | ||
But if you really need it for some bizzare reason then you can specify custom entry like so | ||
If you still need it for some reason, then you can specify a custom entry: | ||
@@ -87,24 +101,38 @@ ``` js | ||
`cache` is enabled by default and taken care of by the plugin because usage in cojunction with watchers like [gulp-watch](https://www.npmjs.com/package/gulp-watch) is expected. It can be however disabled by settings `cache` to `false` | ||
`cache` is enabled by default and taken care of by the plugin, because usage in conjunction with watchers like [gulp-watch](https://www.npmjs.com/package/gulp-watch) is expected. It can however be disabled by settings `cache` to `false`. | ||
#### `generateOptions` | ||
`cache` allows injection of custom rollup version. | ||
Second argument is object of options describing output format of the bundle. It's the same thing you would specify as [`bundle.generate(options)` in Rollup API](https://github.com/rollup/rollup/wiki/JavaScript-API#bundlegenerate-options-) or as a single item of `targets` in `rollup.config.js` | ||
```js | ||
const betterRollup = require('gulp-better-rollup'); | ||
`name` and `amd.id` are by default assigned by filename but can be explicitly specified | ||
gulp.task('lib-build', () => { | ||
gulp.src('lib/index.js') | ||
.pipe(betterRollup({ | ||
rollup: require('rollup') | ||
plugins: [babel()], | ||
format: 'cjs', | ||
... | ||
})) | ||
}) | ||
``` | ||
**Caveat:** Exporting to UMD or IIFE format requires to specify `name`. This plugin takes care of autoassigning it based on filename. But if your main file is named `index.js` or `main.js` then your module will be also named `index` or `main`. | ||
#### `outputOptions` | ||
**Caveat:** If you don't want `amd.id` to be automatically assigned for your AMD modules, set `amd.id` to empty string `.pipe(rollup({amd:{id:''}}))` | ||
Options describing the output format of the bundle. See [`bundle.generate(outputOptions)` in the Rollup API](https://rollupjs.org/guide/en#outputoptions). | ||
`intro/outro` are discouraged to use in favor of gulps standard plugins like [gulp-header](https://www.npmjs.com/package/gulp-header) and [gulp-footer](https://www.npmjs.com/package/gulp-footer) | ||
`name` and `amd.id` are inferred from the module file name by default, but can be explicitly specified to override this. For example, if your main file is named `index.js` or `main.js`, then your module would also be named `index` or `main`, which you may not want. | ||
`sourcemap` option is omitted. Use the standard [gulp-sourcemaps](https://www.npmjs.com/package/gulp-sourcemaps) plugin instead. | ||
To use [unnamed modules](http://requirejs.org/docs/api.html#modulename) for amd, set `amd.id` to an empty string, ex: `.pipe(rollup({amd:{id:''}}))`. | ||
`sourcemapFile` is unvailable as well. | ||
`intro` and `outro` are supported, but we encouraged you to use gulps standard plugins like [gulp-header](https://www.npmjs.com/package/gulp-header) and [gulp-footer](https://www.npmjs.com/package/gulp-footer). | ||
`sourcemap` and `sourcemapFile` are unsupported. Use the standard [gulp-sourcemaps](https://www.npmjs.com/package/gulp-sourcemaps) plugin instead. | ||
#### shortcuts | ||
If you don't need to define `plugins` like babel, use `external` modules, explicitly specify `input` file, or any other options of `rollupOptions` object, you can just skip this first argument alltogether. Also `generateOptions` can be replaced by string of module format if you only export in a single format. | ||
You can skip this first argument if you don't need to specify `inputOptions`. | ||
`outputOptions` accepts a string with the module format, in case you only want to support a single format. | ||
``` js | ||
@@ -118,3 +146,3 @@ gulp.task('dev', function() { | ||
**Both `rollupOptions` and `generateOptions` can be also specified as a single object** if you preffer simplicity over semantically relying on the Rollup JS API. This could also come in handy as setting defaults for `generateOptions` when you export multiple formats and you don't want to copy-paste the same `exports` and `blobal` options. | ||
**`inputOptions` and `outputOptions` can also be specified as a shared object** if you prefer simplicity over adherence to the Rollup JS API semantics. | ||
@@ -136,3 +164,3 @@ ``` js | ||
Can be simplified into | ||
Can be simplified into: | ||
@@ -155,3 +183,3 @@ ``` js | ||
Array of `generateOptions` can be provided to export into multiple formats. | ||
`outputOptions` can be an array, in order to export to multiple formats. | ||
@@ -163,3 +191,3 @@ ```js | ||
.pipe(sourcemaps.init()) | ||
.pipe(rollup(rollupOptions, [{ | ||
.pipe(rollup(inputOptions, [{ | ||
file: pkg['jsnext:main'], | ||
@@ -174,7 +202,2 @@ format: 'es', | ||
}) | ||
``` | ||
**Caveat 1:** `file` can take path instead of just a file name, but the file won't be saved there. Exporting files from gulp always relies on the `.pipe(gulp.dest(...))` and not the plugin itself. | ||
**Caveat 2:** `gulp-sourcemaps` plugin doesn't (yet) support the `.mjs` extension you might want to use to export ES format into. Specifically it can inline the sourcemap into the bundle file (using `sourcemaps.write()`), and it can also create external sourcemap file with `sourcemaps.write(PATH_TO_SOURCEMAP_FOLDER)`, it just won't insert the `//# sourceMappingURL=` linking comment at the end of your `.mjs` file, effectivelly rendering the sourcemap useless. | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15875
165
190
+ Added@types/estree@0.0.39(transitive)
+ Added@types/node@22.10.2(transitive)
+ Addedansi-colors@1.1.0(transitive)
+ Addedarr-diff@4.0.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedextend-shallow@3.0.2(transitive)
+ Addedis-extendable@1.0.1(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedplugin-error@1.0.1(transitive)
+ Addedrollup@0.60.7(transitive)
+ Addedundici-types@6.20.0(transitive)
- Removedansi-cyan@0.1.1(transitive)
- Removedansi-red@0.1.1(transitive)
- Removedarr-diff@1.1.0(transitive)
- Removedarr-flatten@1.1.0(transitive)
- Removedarr-union@2.1.0(transitive)
- Removedarray-slice@0.2.3(transitive)
- Removedextend-shallow@1.1.4(transitive)
- Removedkind-of@1.1.0(transitive)
- Removedplugin-error@0.1.2(transitive)
- Removedrollup@0.56.5(transitive)
Updatedplugin-error@^1.0.1
Updatedrollup@^0.60.2