diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 00000000..eccaf8ce --- /dev/null +++ b/dist/index.js @@ -0,0 +1,100 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017 Mickael Jeanroy + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +'use strict'; + +var _ = require('lodash'); +var MagicString = require('magic-string'); +var diff = require('diff'); +var prettier = require('prettier'); + +var NAME = 'rollup-plugin-prettier'; + +module.exports = function () { + var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + + var sourceMap = void 0; + + return { + /** + * Plugin name (used by rollup for error messages and warnings). + * @type {string} + */ + name: NAME, + + /** + * Function called by `rollup` that is used to read the `sourceMap` setting. + * + * @param {Object} opts Rollup options. + * @return {void} + */ + options: function options() { + var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; + + sourceMap = !!opts.sourceMap; + }, + + + /** + * Function called by `rollup` before generating final bundle. + * + * @param {string} source Souce code of the final bundle. + * @return {Object} The result containing a `code` property and, if a enabled, a `map` property. + */ + transformBundle: function transformBundle(source) { + var output = prettier.format(source, options); + + // No need to do more. + if (!sourceMap) { + return { code: output }; + } + + console.log('[' + NAME + '] Source-map is enabled, computing diff is required'); + console.log('[' + NAME + '] This may take a moment (depends on the size of your bundle)'); + + var magicString = new MagicString(source); + var changes = diff.diffChars(source, output); + + 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); + } + + idx += part.count; + }); + + return { + code: magicString.toString(), + map: magicString.generateMap({ + hires: true + }) + }; + } + }; +}; \ No newline at end of file diff --git a/package.json b/package.json index d44f7e95..274bebac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-prettier", - "version": "0.1.0", + "version": "0.2.0", "description": "Run prettier formatter with rollup", "main": "dist/index.js", "author": "Mickael Jeanroy ",