rollup-plugin-prettier
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -27,3 +27,2 @@ /** | ||
var _ = require('lodash'); | ||
var MagicString = require('magic-string'); | ||
@@ -35,7 +34,17 @@ var diff = require('diff'); | ||
module.exports = function () { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
module.exports = function (options) { | ||
var sourcemap = null; | ||
var sourceMap = void 0; | ||
if (options && hasSourceMap(options)) { | ||
sourcemap = isSourceMapEnabled(options); | ||
// Delete custom option. | ||
deleteSourceMap(options); | ||
// Do not send an empty option object. | ||
if (Object.keys(options).length === 0) { | ||
options = undefined; | ||
} | ||
} | ||
return { | ||
@@ -57,3 +66,16 @@ /** | ||
sourceMap = !!opts.sourceMap; | ||
if (sourcemap == null) { | ||
// Get the global `sourcemap` option on given object. | ||
// Should support: | ||
// - `sourcemap` (lowercase) option which is the name with rollup >= 0.48.0, | ||
// - `sourceMap` (camelcase) option which is the (deprecated) name with rollup < 0.48.0. | ||
var globalSourcemap = isSourceMapEnabled(opts); | ||
// Since rollup 0.48, sourcemap option can be set on the `output` object. | ||
var output = opts.output || {}; | ||
var outputSourceMap = Array.isArray(output) ? output.some(isSourceMapEnabled) : isSourceMapEnabled(output); | ||
// Enable or disable `sourcemap` generation. | ||
sourcemap = globalSourcemap || outputSourceMap; | ||
} | ||
}, | ||
@@ -66,13 +88,16 @@ | ||
* @param {string} source Souce code of the final bundle. | ||
* @param {Object} oo Output option. | ||
* @return {Object} The result containing a `code` property and, if a enabled, a `map` property. | ||
*/ | ||
transformBundle: function transformBundle(source) { | ||
transformBundle: function transformBundle(source, oo) { | ||
var output = prettier.format(source, options); | ||
// No need to do more. | ||
if (!sourceMap) { | ||
// Should we generate sourcemap? | ||
// The sourcemap option may be a boolean or any truthy value (such as a `string`). | ||
// Note that this option should be false by default as it may take a (very) long time. | ||
if (!sourcemap) { | ||
return { code: output }; | ||
} | ||
console.log('[' + NAME + '] Source-map is enabled, computing diff is required'); | ||
console.log('[' + NAME + '] Sourcemap is enabled, computing diff is required'); | ||
console.log('[' + NAME + '] This may take a moment (depends on the size of your bundle)'); | ||
@@ -83,14 +108,16 @@ | ||
var idx = 0; | ||
if (changes && changes.length > 0) { | ||
var idx = 0; | ||
_.forEach(changes, function (part) { | ||
if (part.added) { | ||
magicString.prependLeft(idx, part.value); | ||
idx -= part.count; | ||
} else if (part.removed) { | ||
magicString.remove(idx, idx + part.count); | ||
} | ||
changes.forEach(function (part) { | ||
if (part.added) { | ||
magicString.prependLeft(idx, part.value); | ||
idx -= part.count; | ||
} else if (part.removed) { | ||
magicString.remove(idx, idx + part.count); | ||
} | ||
idx += part.count; | ||
}); | ||
idx += part.count; | ||
}); | ||
} | ||
@@ -105,2 +132,51 @@ return { | ||
}; | ||
}; | ||
}; | ||
var SOURCE_MAPS_OPTS = ['sourcemap', // Name of the property with rollup >= 0.48. | ||
'sourceMap']; | ||
/** | ||
* Check if property exist on an object. | ||
* | ||
* @param {Object} o The object. | ||
* @param {string} prop The property name. | ||
* @return {boolean} `true` if property is defined on object, `false` otherwise. | ||
*/ | ||
function has(o, prop) { | ||
return prop in o; | ||
} | ||
/** | ||
* Check if `sourcemap` option is defined on option object. | ||
* | ||
* @param {Object} opts Options. | ||
* @return {boolean} `true` if sourcemap is defined, `false` otherwise. | ||
*/ | ||
function hasSourceMap(opts) { | ||
return SOURCE_MAPS_OPTS.some(function (p) { | ||
return has(opts, p); | ||
}); | ||
} | ||
/** | ||
* Check if `sourcemap` option is enable or not. | ||
* | ||
* @param {Object} opts Options. | ||
* @return {boolean} `true` if sourcemap is enabled, `false` otherwise. | ||
*/ | ||
function isSourceMapEnabled(opts) { | ||
return !!SOURCE_MAPS_OPTS.find(function (p) { | ||
return opts[p]; | ||
}); | ||
} | ||
/** | ||
* Delete sourcemap option on object. | ||
* | ||
* @param {Object} opts The object. | ||
*/ | ||
function deleteSourceMap(opts) { | ||
SOURCE_MAPS_OPTS.forEach(function (p) { | ||
return delete opts[p]; | ||
}); | ||
} |
{ | ||
"name": "rollup-plugin-prettier", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Run prettier formatter with rollup", | ||
@@ -28,23 +28,25 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"diff": "3.2.0", | ||
"lodash": "4.17.4", | ||
"magic-string": "0.19.1", | ||
"diff": "3.3.0", | ||
"magic-string": "0.22.4", | ||
"prettier": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "6.24.1", | ||
"babel-core": "6.26.0", | ||
"babel-preset-es2015": "6.24.1", | ||
"del": "2.2.2", | ||
"eslint": "3.19.0", | ||
"eslint-config-google": "0.7.1", | ||
"del": "3.0.0", | ||
"eslint": "4.5.0", | ||
"eslint-config-google": "0.9.1", | ||
"gulp": "3.9.1", | ||
"gulp-babel": "6.1.2", | ||
"gulp-babel": "7.0.0", | ||
"gulp-bump": "2.7.0", | ||
"gulp-eslint": "3.0.1", | ||
"gulp-git": "2.2.0", | ||
"gulp-eslint": "4.0.0", | ||
"gulp-git": "2.4.2", | ||
"gulp-jasmine": "2.4.2", | ||
"gulp-util": "3.0.8", | ||
"jasmine-core": "2.6.1", | ||
"run-sequence": "1.2.2" | ||
"jasmine-core": "2.8.0", | ||
"q": "1.5.0", | ||
"rollup": "0.48.2", | ||
"run-sequence": "2.1.0", | ||
"tmp": "0.0.33" | ||
} | ||
} |
@@ -36,5 +36,6 @@ # rollup-plugin-prettier | ||
If source map is enabled in the global rollup options, then a source map will be generated on the formatted bundle. | ||
Note that this may take some time since `prettier` package is not able to generate a sourcemap : this plugin must compute the diff between the original bundle and the formatted result and generate the corresponding source map. | ||
If source map is enabled in the global rollup options, then a source map will be generated on the formatted bundle (except if sourcemap are explicitely disabled in the prettier options). | ||
Note that this may take some time since `prettier` package is not able to generate a sourcemap and his plugin must compute the diff between the original bundle and the formatted result and generate the corresponding sourcemap: for this reason, sourcemap are disabled by default. | ||
Here is an example: | ||
@@ -47,7 +48,13 @@ | ||
module.exports = { | ||
entry: path.join(__dirname, 'src', 'index.js'), | ||
dest: path.join(__dirname, 'dist', 'bundle.js'), | ||
sourceMap: true, | ||
input: path.join(__dirname, 'src', 'index.js'), | ||
output: { | ||
file: path.join(__dirname, 'dist', 'bundle.js'), | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
prettier(), | ||
prettier({ | ||
sourceMap: true, // Can also be disabled/enabled here. | ||
}), | ||
], | ||
@@ -59,2 +66,5 @@ }; | ||
- 0.3.0 | ||
- Support new `sourcemap` (lowercase) option of rollup. | ||
- Sourcemap can now be activated/disabled in the plugin options. | ||
- 0.2.0 | ||
@@ -61,0 +71,0 @@ - Dependency update (`magic-string`) |
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
10137
3
149
78
17
+ Addeddiff@3.3.0(transitive)
+ Addedmagic-string@0.22.4(transitive)
- Removedlodash@4.17.4
- Removeddiff@3.2.0(transitive)
- Removedlodash@4.17.4(transitive)
- Removedmagic-string@0.19.1(transitive)
Updateddiff@3.3.0
Updatedmagic-string@0.22.4