Skip to content

Commit

Permalink
release: release version
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed May 10, 2017
1 parent a260f86 commit c7db0bd
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
100 changes: 100 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -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
})
};
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down

0 comments on commit c7db0bd

Please sign in to comment.