|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2016-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 omitBy = _interopDefault(require("lodash.omitby")); |
| 32 | +var isEmpty = _interopDefault(require("lodash.isempty")); |
| 33 | +var hasIn = _interopDefault(require("lodash.hasin")); |
| 34 | +var isNil = _interopDefault(require("lodash.isnil")); |
| 35 | +var MagicString = _interopDefault(require("magic-string")); |
| 36 | +var diff = require("diff"); |
| 37 | +var esformatter = _interopDefault(require("esformatter")); |
| 38 | + |
| 39 | +/** |
| 40 | + * The internal plugin options. |
| 41 | + */ |
| 42 | + |
| 43 | +const OPTIONS = new Set(["sourcemap"]); |
| 44 | +/** |
| 45 | + * The rollup plugin for ESFormatter. |
| 46 | + */ |
| 47 | + |
| 48 | +class RollupPluginEsFormatter { |
| 49 | + /** |
| 50 | + * Initialize plugin. |
| 51 | + * |
| 52 | + * @param {Object} options Initialization option. |
| 53 | + * @constructor |
| 54 | + */ |
| 55 | + constructor(options = {}) { |
| 56 | + this.name = "rollup-plugin-esformatter"; // Initialize main options. |
| 57 | + |
| 58 | + this._options = omitBy(options || {}, (value, key) => OPTIONS.has(key)); // Reset to undefined if esformatter options are empty. |
| 59 | + |
| 60 | + if (isEmpty(this._options)) { |
| 61 | + this._options = undefined; |
| 62 | + } // Check if sourcemap is enabled by default. |
| 63 | + |
| 64 | + this._sourcemap = hasIn(options, "sourcemap") ? options.sourcemap : null; |
| 65 | + } |
| 66 | + /** |
| 67 | + * Get the `sourcemap` value. |
| 68 | + * |
| 69 | + * @return {boolean} The `sourcemap` flag value. |
| 70 | + */ |
| 71 | + |
| 72 | + getSourcemap() { |
| 73 | + return this._sourcemap; |
| 74 | + } |
| 75 | + /** |
| 76 | + * Disable sourcemap. |
| 77 | + * |
| 78 | + * @return {void} |
| 79 | + */ |
| 80 | + |
| 81 | + enableSourcemap() { |
| 82 | + this._sourcemap = true; |
| 83 | + } |
| 84 | + /** |
| 85 | + * Function called by `rollup` before generating final bundle. |
| 86 | + * |
| 87 | + * @param {string} source Souce code of the final bundle. |
| 88 | + * @param {boolean} sourcemap Force sourcemap for this output. |
| 89 | + * @return {Object} The result containing a `code` property and, if source map is enabled, a `map` property. |
| 90 | + */ |
| 91 | + |
| 92 | + reformat(source, sourcemap) { |
| 93 | + const output = esformatter.format(source, this._options); // No need to do more. |
| 94 | + |
| 95 | + const defaultSourcemap = isNil(this._sourcemap) ? false : this._sourcemap; |
| 96 | + const outputSourcemap = isNil(sourcemap) ? defaultSourcemap : sourcemap; |
| 97 | + |
| 98 | + if (!outputSourcemap) { |
| 99 | + return { |
| 100 | + code: output |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + console.warn( |
| 105 | + `[${this.name}] Sourcemap is enabled, computing diff is required` |
| 106 | + ); |
| 107 | + console.warn( |
| 108 | + `[${this.name}] This may take a moment (depends on the size of your bundle)` |
| 109 | + ); |
| 110 | + const magicString = new MagicString(source); |
| 111 | + const changes = diff.diffChars(source, output); |
| 112 | + let idx = 0; |
| 113 | + |
| 114 | + if (changes && changes.length > 0) { |
| 115 | + changes.forEach(part => { |
| 116 | + if (part.added) { |
| 117 | + magicString.prependLeft(idx, part.value); |
| 118 | + idx -= part.count; |
| 119 | + } else if (part.removed) { |
| 120 | + magicString.remove(idx, idx + part.count); |
| 121 | + } |
| 122 | + |
| 123 | + idx += part.count; |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + return { |
| 128 | + code: magicString.toString(), |
| 129 | + map: magicString.generateMap({ |
| 130 | + hires: true |
| 131 | + }) |
| 132 | + }; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Create plugin instance. |
| 138 | + * |
| 139 | + * @param {*} options Plugin options. |
| 140 | + * @return {Objects} The plugin instance. |
| 141 | + */ |
| 142 | + |
| 143 | +function rollupPlugin(options) { |
| 144 | + const plugin = new RollupPluginEsFormatter(options); |
| 145 | + return { |
| 146 | + /** |
| 147 | + * The plugin name. |
| 148 | + * @type {string} |
| 149 | + */ |
| 150 | + name: plugin.name, |
| 151 | + |
| 152 | + /** |
| 153 | + * Function called by `rollup` before generating final bundle. |
| 154 | + * |
| 155 | + * @param {string} source Souce code of the final bundle. |
| 156 | + * @param {Object} chunk The current chunk. |
| 157 | + * @param {Object} outputOptions Output option. |
| 158 | + * @return {Object} The result containing a `code` property and, if source map is enabled, a `map` property. |
| 159 | + */ |
| 160 | + renderChunk(source, chunk, outputOptions = {}) { |
| 161 | + return plugin.reformat(source, outputOptions.sourcemap); |
| 162 | + } |
| 163 | + }; |
| 164 | +} |
| 165 | + |
| 166 | +module.exports = rollupPlugin; |
0 commit comments