Skip to content

Commit d0e74cb

Browse files
committed
release: release version
1 parent 292fc60 commit d0e74cb

File tree

3 files changed

+305
-1
lines changed

3 files changed

+305
-1
lines changed

dist/index.d.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2023 @pastelmind <https://github.com/pastelmind>
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+
/**
26+
* @file Type definition for rollup-plugin-prettier
27+
*/
28+
29+
import type {Options as PrettierOptions} from 'prettier';
30+
import type {Plugin} from 'rollup';
31+
32+
declare namespace prettier {
33+
interface Options extends PrettierOptions {
34+
/**
35+
* Directory to look for a Prettier config file.
36+
*
37+
* If omitted, defaults to `process.cwd()`.
38+
*/
39+
cwd?: string;
40+
41+
/**
42+
* Whether to generate a sourcemap.
43+
*
44+
* Note: This may take some time because rollup-plugin-prettier diffs the
45+
* output to manually generate a sourcemap.
46+
*/
47+
sourcemap?: boolean | 'silent';
48+
}
49+
}
50+
51+
declare function prettier(options?: prettier.Options): Plugin;
52+
53+
export = prettier;

dist/index.js

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
/**
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017-2023 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+
var hasIn = require("lodash.hasin");
28+
var isEmpty = require("lodash.isempty");
29+
var isNil = require("lodash.isnil");
30+
var omitBy = require("lodash.omitby");
31+
var MagicString = require("magic-string");
32+
var diff = require("diff");
33+
var prettier = require("prettier");
34+
35+
function _interopNamespaceDefault(e) {
36+
var n = Object.create(null);
37+
if (e) {
38+
Object.keys(e).forEach(function (k) {
39+
if (k !== "default") {
40+
var d = Object.getOwnPropertyDescriptor(e, k);
41+
Object.defineProperty(
42+
n,
43+
k,
44+
d.get
45+
? d
46+
: {
47+
enumerable: true,
48+
get: function () {
49+
return e[k];
50+
},
51+
}
52+
);
53+
}
54+
});
55+
}
56+
n.default = e;
57+
return Object.freeze(n);
58+
}
59+
60+
var diff__namespace = /*#__PURE__*/ _interopNamespaceDefault(diff);
61+
62+
/**
63+
* The plugin options that are currently supported.
64+
* @type {Set<string>}
65+
*/
66+
const OPTIONS = new Set(["sourcemap", "cwd"]);
67+
68+
/**
69+
* Resolve prettier config, using `resolveConfig` by default, switch to `resolveConfigSync` otherwise.
70+
* If none of these methods are available, returns `null` as a fallback.
71+
*
72+
* @param {string} cwd The current working directory.
73+
* @returns {Promise<object | null>} A promise resolved with prettier configuration, or null.
74+
*/
75+
function resolvePrettierConfig(cwd) {
76+
// Be careful, `resolveConfig` function does not exist on old version of prettier.
77+
if (prettier.resolveConfig) {
78+
return prettier.resolveConfig(cwd);
79+
}
80+
if (prettier.resolveConfigSync) {
81+
return Promise.resolve(prettier.resolveConfigSync(cwd));
82+
}
83+
return Promise.resolve(null);
84+
}
85+
86+
/**
87+
* The plugin.
88+
*
89+
* @class
90+
*/
91+
class RollupPluginPrettier {
92+
/**
93+
* Initialize plugin & prettier.
94+
*
95+
* @param {Object} options Initalization option.
96+
*/
97+
constructor(options = {}) {
98+
// Initialize plugin name.
99+
this.name = "rollup-plugin-prettier";
100+
101+
// Initialize main options.
102+
this._options = Promise.resolve(
103+
omitBy(options, (value, key) => OPTIONS.has(key))
104+
);
105+
106+
// Try to resolve config file if it exists
107+
const cwd = hasIn(options, "cwd") ? options.cwd : process.cwd();
108+
this._options = Promise.all([
109+
resolvePrettierConfig(cwd),
110+
this._options,
111+
]).then((results) =>
112+
Object.assign({}, ...results.map((result) => result || {}))
113+
);
114+
115+
// Reset empty options.
116+
this._options = this._options.then((options) =>
117+
isEmpty(options) ? undefined : options
118+
);
119+
120+
// Check if sourcemap is enabled by default.
121+
this._sourcemap = hasIn(options, "sourcemap") ? options.sourcemap : null;
122+
}
123+
124+
/**
125+
* Get the `sourcemap` value.
126+
*
127+
* @return {boolean} The `sourcemap` flag value.
128+
*/
129+
getSourcemap() {
130+
return this._sourcemap;
131+
}
132+
133+
/**
134+
* Disable sourcemap.
135+
*
136+
* @return {void}
137+
*/
138+
enableSourcemap() {
139+
this._sourcemap = true;
140+
}
141+
142+
/**
143+
* Reformat source code using prettier.
144+
*
145+
* @param {string} source The source code to reformat.
146+
* @param {boolean} sourcemap If sourcemap should be generated or not.
147+
* @return {Promise<{code: string, map: object}|{code: string}>} The transformation result.
148+
*/
149+
reformat(source, sourcemap) {
150+
return this._options.then((options) =>
151+
this._reformat(source, sourcemap, options)
152+
);
153+
}
154+
155+
/**
156+
* Reformat source code using prettier.
157+
*
158+
* @param {string} source The source code to reformat.
159+
* @param {boolean} sourcemap If sourcemap should be generated or not.
160+
* @param {object} options Prettier options.
161+
* @returns {Promise<{code: string, map: object}|{code: string}>} The reformat response.
162+
* @private
163+
*/
164+
_reformat(source, sourcemap, options) {
165+
return Promise.resolve(prettier.format(source, options)).then((output) =>
166+
this._processOutput(source, sourcemap, output)
167+
);
168+
}
169+
170+
/**
171+
* Process output generated by prettier:
172+
* - Generate sourcemap if need be.
173+
* - Otherwise returns prettier output as chunk output.
174+
*
175+
* @param {string} source The source code to reformat.
176+
* @param {boolean} sourcemap If sourcemap should be generated or not.
177+
* @param {string} output Prettier output.
178+
* @returns {{code: string, map: object}|{code: string}} The reformat response.
179+
* @private
180+
*/
181+
_processOutput(source, sourcemap, output) {
182+
// Should we generate sourcemap?
183+
// The sourcemap option may be a boolean or any truthy value (such as a `string`).
184+
// Note that this option should be false by default as it may take a (very) long time.
185+
const defaultSourcemap = isNil(this._sourcemap) ? false : this._sourcemap;
186+
const outputSourcemap = isNil(sourcemap) ? defaultSourcemap : sourcemap;
187+
if (!outputSourcemap) {
188+
return {
189+
code: output,
190+
};
191+
}
192+
if (defaultSourcemap !== "silent") {
193+
console.warn(
194+
`[${this.name}] Sourcemap is enabled, computing diff is required`
195+
);
196+
console.warn(
197+
`[${this.name}] This may take a moment (depends on the size of your bundle)`
198+
);
199+
}
200+
const magicString = new MagicString(source);
201+
const changes = diff__namespace.diffChars(source, output);
202+
if (changes && changes.length > 0) {
203+
let idx = 0;
204+
changes.forEach((part) => {
205+
if (part.added) {
206+
magicString.prependLeft(idx, part.value);
207+
idx -= part.count;
208+
} else if (part.removed) {
209+
magicString.remove(idx, idx + part.count);
210+
}
211+
idx += part.count;
212+
});
213+
}
214+
return {
215+
code: magicString.toString(),
216+
map: magicString.generateMap({
217+
hires: true,
218+
}),
219+
};
220+
}
221+
}
222+
223+
/**
224+
* Create rollup plugin compatible with rollup >= 1.0.0
225+
*
226+
* @param {Object} options Plugin options.
227+
* @return {Object} Plugin instance.
228+
*/
229+
function rollupPluginPrettier(options) {
230+
const plugin = new RollupPluginPrettier(options);
231+
return {
232+
/**
233+
* Plugin name (used by rollup for error messages and warnings).
234+
* @type {string}
235+
*/
236+
name: plugin.name,
237+
/**
238+
* Function called by `rollup` before generating final bundle.
239+
*
240+
* @param {string} source Souce code of the final bundle.
241+
* @param {Object} chunkInfo Chunk info.
242+
* @param {Object} outputOptions Output option.
243+
* @return {Promise<Object>} The result containing a `code` property and, if a enabled, a `map` property.
244+
*/
245+
renderChunk(source, chunkInfo, outputOptions) {
246+
return plugin.reformat(source, outputOptions.sourcemap);
247+
},
248+
};
249+
}
250+
251+
module.exports = rollupPluginPrettier;

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": "3.0.0",
3+
"version": "3.1.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)