Skip to content

Commit 997faf9

Browse files
committed
release: release version
1 parent d4df7ff commit 997faf9

2 files changed

Lines changed: 320 additions & 1 deletion

File tree

dist/index.js

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2020 Mickael Jeanroy
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
"use strict";
26+
27+
function _interopDefault(ex) {
28+
return ex && typeof ex === "object" && "default" in ex ? ex["default"] : ex;
29+
}
30+
31+
var rollup = require("rollup");
32+
var hasIn = _interopDefault(require("lodash.hasin"));
33+
var isNil = _interopDefault(require("lodash.isnil"));
34+
var isEmpty = _interopDefault(require("lodash.isempty"));
35+
var omitBy = _interopDefault(require("lodash.omitby"));
36+
var MagicString = _interopDefault(require("magic-string"));
37+
var diff = require("diff");
38+
var prettier = _interopDefault(require("prettier"));
39+
40+
function _classCallCheck(instance, Constructor) {
41+
if (!(instance instanceof Constructor)) {
42+
throw new TypeError("Cannot call a class as a function");
43+
}
44+
}
45+
46+
function _defineProperties(target, props) {
47+
for (var i = 0; i < props.length; i++) {
48+
var descriptor = props[i];
49+
descriptor.enumerable = descriptor.enumerable || false;
50+
descriptor.configurable = true;
51+
if ("value" in descriptor) descriptor.writable = true;
52+
Object.defineProperty(target, descriptor.key, descriptor);
53+
}
54+
}
55+
56+
function _createClass(Constructor, protoProps, staticProps) {
57+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
58+
if (staticProps) _defineProperties(Constructor, staticProps);
59+
return Constructor;
60+
}
61+
62+
/**
63+
* The plugin options that are currently supported.
64+
* @type {Set<string>}
65+
*/
66+
67+
var OPTIONS = new Set(["sourcemap", "sourceMap", "cwd"]);
68+
/**
69+
* The plugin.
70+
*
71+
* @class
72+
*/
73+
74+
var RollupPluginPrettier = /*#__PURE__*/ (function() {
75+
/**
76+
* Initialize plugin & prettier.
77+
*
78+
* @param {Object} options Initalization option.
79+
*/
80+
function RollupPluginPrettier() {
81+
var options =
82+
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
83+
84+
_classCallCheck(this, RollupPluginPrettier);
85+
86+
// Initialize plugin name.
87+
this.name = "rollup-plugin-prettier"; // Initialize main options.
88+
89+
this._options = omitBy(options, function(value, key) {
90+
return OPTIONS.has(key);
91+
}); // Try to resolve config file it it exists
92+
// Be careful, `resolveConfig` function does not exist on old version of prettier.
93+
94+
if (prettier.resolveConfig) {
95+
var cwd = hasIn(options, "cwd") ? options.cwd : process.cwd();
96+
var configOptions = prettier.resolveConfig.sync(cwd);
97+
98+
if (configOptions != null) {
99+
this._options = Object.assign(configOptions, this._options || {});
100+
}
101+
} // Reset empty options.
102+
103+
if (isEmpty(this._options)) {
104+
this._options = undefined;
105+
} // Check if sourcemap is enabled by default.
106+
107+
if (hasIn(options, "sourcemap")) {
108+
this._sourcemap = options.sourcemap;
109+
} else if (hasIn(options, "sourceMap")) {
110+
console.warn(
111+
"[".concat(
112+
this.name,
113+
"] The sourceMap option is deprecated, please use sourcemap instead."
114+
)
115+
);
116+
this._sourcemap = options.sourceMap;
117+
} else {
118+
this._sourcemap = null;
119+
}
120+
}
121+
/**
122+
* Get the `sourcemap` value.
123+
*
124+
* @return {boolean} The `sourcemap` flag value.
125+
*/
126+
127+
_createClass(RollupPluginPrettier, [
128+
{
129+
key: "getSourcemap",
130+
value: function getSourcemap() {
131+
return this._sourcemap;
132+
}
133+
/**
134+
* Disable sourcemap.
135+
*
136+
* @return {void}
137+
*/
138+
},
139+
{
140+
key: "enableSourcemap",
141+
value: function enableSourcemap() {
142+
this._sourcemap = true;
143+
}
144+
/**
145+
* Reformat source code using prettier.
146+
*
147+
* @param {string} source The source code to reformat.
148+
* @param {boolean} sourcemap If sourcemap should be generated or not.
149+
* @return {Object} The transformation result.
150+
*/
151+
},
152+
{
153+
key: "reformat",
154+
value: function reformat(source, sourcemap) {
155+
var output = prettier.format(source, this._options); // Should we generate sourcemap?
156+
// The sourcemap option may be a boolean or any truthy value (such as a `string`).
157+
// Note that this option should be false by default as it may take a (very) long time.
158+
159+
var defaultSourcemap = isNil(this._sourcemap) ? false : this._sourcemap;
160+
var outputSourcemap = isNil(sourcemap) ? defaultSourcemap : sourcemap;
161+
162+
if (!outputSourcemap) {
163+
return {
164+
code: output
165+
};
166+
}
167+
168+
console.warn(
169+
"[".concat(
170+
this.name,
171+
"] Sourcemap is enabled, computing diff is required"
172+
)
173+
);
174+
console.warn(
175+
"[".concat(
176+
this.name,
177+
"] This may take a moment (depends on the size of your bundle)"
178+
)
179+
);
180+
var magicString = new MagicString(source);
181+
var changes = diff.diffChars(source, output);
182+
183+
if (changes && changes.length > 0) {
184+
var idx = 0;
185+
changes.forEach(function(part) {
186+
if (part.added) {
187+
magicString.prependLeft(idx, part.value);
188+
idx -= part.count;
189+
} else if (part.removed) {
190+
magicString.remove(idx, idx + part.count);
191+
}
192+
193+
idx += part.count;
194+
});
195+
}
196+
197+
return {
198+
code: magicString.toString(),
199+
map: magicString.generateMap({
200+
hires: true
201+
})
202+
};
203+
}
204+
}
205+
]);
206+
207+
return RollupPluginPrettier;
208+
})();
209+
210+
/**
211+
* Create rollup plugin compatible with rollup < 1.0.0
212+
*
213+
* @param {Object} options Plugin options.
214+
* @return {Object} Plugin instance.
215+
*/
216+
217+
function rollupPluginPrettierLegacy(options) {
218+
var plugin = new RollupPluginPrettier(options);
219+
return {
220+
/**
221+
* Plugin name (used by rollup for error messages and warnings).
222+
* @type {string}
223+
*/
224+
name: plugin.name,
225+
226+
/**
227+
* Function called by `rollup` that is used to read the `sourceMap` setting.
228+
*
229+
* @param {Object} opts Rollup options.
230+
* @return {void}
231+
*/
232+
options: function options() {
233+
var opts =
234+
arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
235+
236+
if (isNil(plugin.getSourcemap())) {
237+
// Get the global `sourcemap` option on given object.
238+
// Should support:
239+
// - `sourcemap` (lowercase) option which is the name with rollup >= 0.48.0,
240+
// - `sourceMap` (camelcase) option which is the (deprecated) name with rollup < 0.48.0.
241+
var globalSourcemap = isSourceMapEnabled(opts); // Since rollup 0.48, sourcemap option can be set on the `output` object.
242+
243+
var output = opts.output || {};
244+
var outputSourceMap = Array.isArray(output)
245+
? output.some(isSourceMapEnabled)
246+
: isSourceMapEnabled(output); // Enable or disable `sourcemap` generation.
247+
248+
var sourcemap = globalSourcemap || outputSourceMap;
249+
250+
if (sourcemap) {
251+
plugin.enableSourcemap();
252+
}
253+
}
254+
},
255+
256+
/**
257+
* Function called by `rollup` before generating final bundle.
258+
*
259+
* @param {string} source Souce code of the final bundle.
260+
* @param {Object} outputOptions Output option.
261+
* @return {Object} The result containing a `code` property and, if a enabled, a `map` property.
262+
*/
263+
transformBundle: function transformBundle(source, outputOptions) {
264+
var sourcemap = hasIn(outputOptions, "sourcemap")
265+
? outputOptions.sourcemap
266+
: outputOptions.sourceMap;
267+
return plugin.reformat(source, sourcemap);
268+
}
269+
};
270+
}
271+
/**
272+
* Check if `sourcemap` option is enable or not.
273+
*
274+
* @param {Object} opts Options.
275+
* @return {boolean} `true` if sourcemap is enabled, `false` otherwise.
276+
*/
277+
278+
function isSourceMapEnabled(opts) {
279+
return !!(opts.sourcemap || opts.sourceMap);
280+
}
281+
282+
/**
283+
* Create rollup plugin compatible with rollup >= 1.0.0
284+
*
285+
* @param {Object} options Plugin options.
286+
* @return {Object} Plugin instance.
287+
*/
288+
289+
function rollupPluginPrettierStable(options) {
290+
var plugin = new RollupPluginPrettier(options);
291+
return {
292+
/**
293+
* Plugin name (used by rollup for error messages and warnings).
294+
* @type {string}
295+
*/
296+
name: plugin.name,
297+
298+
/**
299+
* Function called by `rollup` before generating final bundle.
300+
*
301+
* @param {string} source Souce code of the final bundle.
302+
* @param {Object} chunkInfo Chunk info.
303+
* @param {Object} outputOptions Output option.
304+
* @return {Object} The result containing a `code` property and, if a enabled, a `map` property.
305+
*/
306+
renderChunk: function renderChunk(source, chunkInfo, outputOptions) {
307+
return plugin.reformat(source, outputOptions.sourcemap);
308+
}
309+
};
310+
}
311+
312+
var VERSION = rollup.VERSION || "0";
313+
var MAJOR_VERSION = Number(VERSION.split(".")[0]) || 0;
314+
var IS_ROLLUP_LEGACY = MAJOR_VERSION === 0;
315+
var plugin = IS_ROLLUP_LEGACY
316+
? rollupPluginPrettierLegacy
317+
: rollupPluginPrettierStable;
318+
319+
module.exports = plugin;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-plugin-prettier",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "Run prettier formatter with rollup",
55
"main": "dist/index.js",
66
"author": "Mickael Jeanroy <mickael.jeanroy@gmail.com>",

0 commit comments

Comments
 (0)