From c09ad21e9c4cb2451176260ab8764314bf72a96e Mon Sep 17 00:00:00 2001 From: Mickael Jeanroy Date: Wed, 26 Oct 2022 22:55:06 +0200 Subject: [PATCH] release: release version --- dist/index.d.ts | 53 +++++++++++++ dist/index.js | 206 ++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 dist/index.d.ts create mode 100644 dist/index.js diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 00000000..750c64bb --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,53 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2021 @pastelmind + * + * 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. + */ + +/** + * @file Type definition for rollup-plugin-prettier + */ + +import type {Options as PrettierOptions} from 'prettier'; +import type {Plugin} from 'rollup'; + +declare namespace prettier { + interface Options extends PrettierOptions { + /** + * Directory to look for a Prettier config file. + * + * If omitted, defaults to `process.cwd()`. + */ + cwd?: string; + + /** + * Whether to generate a sourcemap. + * + * Note: This may take some time because rollup-plugin-prettier diffs the + * output to manually generate a sourcemap. + */ + sourcemap?: boolean | 'silent'; + } +} + +declare function prettier(options?: prettier.Options): Plugin; + +export = prettier; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 00000000..a77310fd --- /dev/null +++ b/dist/index.js @@ -0,0 +1,206 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2022 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 hasIn = require("lodash.hasin"); +var isEmpty = require("lodash.isempty"); +var isNil = require("lodash.isnil"); +var omitBy = require("lodash.omitby"); +var MagicString = require("magic-string"); +var diff = require("diff"); +var prettier = require("prettier"); + +function _interopNamespaceDefault(e) { + var n = Object.create(null); + if (e) { + Object.keys(e).forEach(function (k) { + if (k !== "default") { + var d = Object.getOwnPropertyDescriptor(e, k); + Object.defineProperty( + n, + k, + d.get + ? d + : { + enumerable: true, + get: function () { + return e[k]; + }, + } + ); + } + }); + } + n.default = e; + return Object.freeze(n); +} + +var diff__namespace = /*#__PURE__*/ _interopNamespaceDefault(diff); + +/** + * The plugin options that are currently supported. + * @type {Set} + */ +const OPTIONS = new Set(["sourcemap", "cwd"]); + +/** + * The plugin. + * + * @class + */ +class RollupPluginPrettier { + /** + * Initialize plugin & prettier. + * + * @param {Object} options Initalization option. + */ + constructor(options = {}) { + // Initialize plugin name. + this.name = "rollup-plugin-prettier"; + + // Initialize main options. + this._options = omitBy(options, (value, key) => OPTIONS.has(key)); + + // Try to resolve config file it it exists + // Be careful, `resolveConfig` function does not exist on old version of prettier. + if (prettier.resolveConfig) { + const cwd = hasIn(options, "cwd") ? options.cwd : process.cwd(); + const configOptions = prettier.resolveConfig.sync(cwd); + if (configOptions != null) { + this._options = Object.assign(configOptions, this._options || {}); + } + } + + // Reset empty options. + if (isEmpty(this._options)) { + this._options = undefined; + } + + // Check if sourcemap is enabled by default. + if (hasIn(options, "sourcemap")) { + this._sourcemap = options.sourcemap; + } else { + this._sourcemap = null; + } + } + + /** + * Get the `sourcemap` value. + * + * @return {boolean} The `sourcemap` flag value. + */ + getSourcemap() { + return this._sourcemap; + } + + /** + * Disable sourcemap. + * + * @return {void} + */ + enableSourcemap() { + this._sourcemap = true; + } + + /** + * Reformat source code using prettier. + * + * @param {string} source The source code to reformat. + * @param {boolean} sourcemap If sourcemap should be generated or not. + * @return {Object} The transformation result. + */ + reformat(source, sourcemap) { + const output = prettier.format(source, this._options); + + // 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. + const defaultSourcemap = isNil(this._sourcemap) ? false : this._sourcemap; + const outputSourcemap = isNil(sourcemap) ? defaultSourcemap : sourcemap; + if (!outputSourcemap) { + return { + code: output, + }; + } + if (defaultSourcemap !== "silent") { + console.warn( + `[${this.name}] Sourcemap is enabled, computing diff is required` + ); + console.warn( + `[${this.name}] This may take a moment (depends on the size of your bundle)` + ); + } + const magicString = new MagicString(source); + const changes = diff__namespace.diffChars(source, output); + if (changes && changes.length > 0) { + let idx = 0; + changes.forEach((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, + }), + }; + } +} + +/** + * Create rollup plugin compatible with rollup >= 1.0.0 + * + * @param {Object} options Plugin options. + * @return {Object} Plugin instance. + */ +function rollupPluginPrettier(options) { + const plugin = new RollupPluginPrettier(options); + return { + /** + * Plugin name (used by rollup for error messages and warnings). + * @type {string} + */ + name: plugin.name, + /** + * Function called by `rollup` before generating final bundle. + * + * @param {string} source Souce code of the final bundle. + * @param {Object} chunkInfo Chunk info. + * @param {Object} outputOptions Output option. + * @return {Object} The result containing a `code` property and, if a enabled, a `map` property. + */ + renderChunk(source, chunkInfo, outputOptions) { + return plugin.reformat(source, outputOptions.sourcemap); + }, + }; +} + +module.exports = rollupPluginPrettier; diff --git a/package.json b/package.json index 9f9896b9..8703eaba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rollup-plugin-prettier", - "version": "2.3.0", + "version": "3.0.0", "description": "Run prettier formatter with rollup", "main": "dist/index.js", "author": "Mickael Jeanroy ",