Skip to content

Commit cee81b3

Browse files
Merge pull request #52 from UmamiAppearance/v0.5.0
V0.5.0
2 parents c0ae543 + 03230bb commit cee81b3

File tree

6 files changed

+89
-18
lines changed

6 files changed

+89
-18
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
[![License](https://img.shields.io/github/license/UmamiAppearance/rollup-plugin-your-function?color=009911&style=for-the-badge)](./LICENSE)
33
[![npm](https://img.shields.io/npm/v/rollup-plugin-your-function?color=009911&style=for-the-badge)](https://www.npmjs.com/package/rollup-plugin-your-function)
44

5-
A very simple rollup-plugin, which gives you the opportunity to manipulate your build files as you like. Dead simple. The last plugin you need (for simple tasks).
5+
A very simple rollup-plugin, which gives you the opportunity to manipulate your build files as you like. Dead simple. The last plugin you need (for small tasks).
66

77

88
## Idea
9-
There are many plugins for rollup available. And many times the only thing you would like to perform is a little change. Now the big research begins: _which plugin fits the best?_
9+
There are many plugins for rollup available. And many times the only thing you would like to perform is a little change, just some regex magic, only a small string replacement, etc. Now the big research begins: _which plugin fits the best?_
1010

1111
Eventually you will find what you need, but now you'll have to figure out the particularities of the plugin. And you find yourself thinking: _"Why was that so complicated? I could have done this to a regular file in seconds!"_.
1212

13-
This is where **rollup-plugin-your-function** comes into play. The _only!_ thing it does, is to take a function, that you create by yourself. Your function needs to take one argument, that is the source file, it must return the output code as the first parameter, optionally a sourcemap as a second. And that's it.
13+
This is where **rollup-plugin-your-function** comes into play. The _only!_ thing it does, is to take a function, that you create by yourself. Your function needs to take one argument, that is the source file, it must return the output code as the first parameter, optionally a sourcemap as a second (otherwise it gets generated automatically and includes the performed changes). And that's it.
1414

1515

1616
## Install

cjs/your-function.cjs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
var pluginutils = require('@rollup/pluginutils');
4-
var MagicString = require('magic-string');
54
var diff = require('diff');
5+
var MagicString = require('magic-string');
66
var colorette = require('colorette');
77

88
/**
@@ -129,7 +129,7 @@ const showDiff = (filename, source, code, diffOption, pluginName) => {
129129
/**
130130
* [rollup-plugin-your-function]{@link https://github.com/UmamiAppearance/rollup-plugin-your-function}
131131
*
132-
* @version 0.4.12
132+
* @version 0.5.0
133133
* @author UmamiAppearance [[email protected]]
134134
* @license MIT
135135
*/
@@ -166,7 +166,26 @@ const yourFunction = (settings={}) => {
166166

167167
if (settings.sourceMap !== false && settings.sourcemap !== false) {
168168
if (!map) {
169-
const ms = new MagicString(code);
169+
170+
// If no Source Map was provided generate one, by
171+
// comparing character by character with the help
172+
// of diff and using the output to apply the changes
173+
// to the source with the help of magic string.
174+
175+
const ms = new MagicString(source);
176+
let i = 0;
177+
178+
for (const diff$1 of diff.diffChars(source, code)) {
179+
180+
if (diff$1.added) {
181+
ms.appendRight(i, diff$1.value);
182+
} else if (diff$1.removed) {
183+
ms.remove(i, i+=diff$1.count);
184+
} else {
185+
i += diff$1.count;
186+
}
187+
}
188+
170189
map = ms.generateMap({ hires: true });
171190
}
172191
} else {

cjs/your-function.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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-your-function",
3-
"version": "0.4.12",
3+
"version": "0.5.0",
44
"description": "Provide a custom function as a rollup plugin.",
55
"main": "./cjs/your-function.cjs",
66
"module": "./src/your-function.js",

src/your-function.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/**
22
* [rollup-plugin-your-function]{@link https://github.com/UmamiAppearance/rollup-plugin-your-function}
33
*
4-
* @version 0.4.12
4+
* @version 0.5.0
55
* @author UmamiAppearance [[email protected]]
66
* @license MIT
77
*/
88

99
import { createFilter } from "@rollup/pluginutils";
10+
import { diffChars } from "diff";
1011
import MagicString from "magic-string";
1112
import showDiff from "./diff.js";
1213

13-
1414
const yourFunction = (settings={}) => {
1515

1616
if (!settings.fn) {
@@ -25,7 +25,7 @@ const yourFunction = (settings={}) => {
2525
: "your-function"
2626
};
2727

28-
const fnWrap = async (source, options) => {
28+
const fnWrap = async (source, options, context) => {
2929

3030
if (!filter(options.id)) return null;
3131

@@ -42,7 +42,56 @@ const yourFunction = (settings={}) => {
4242

4343
if (settings.sourceMap !== false && settings.sourcemap !== false) {
4444
if (!map) {
45-
const ms = new MagicString(code);
45+
46+
// If no Source Map was provided generate one, by
47+
// comparing character by character with the help
48+
// of diff and using the output to apply the changes
49+
// to the source with the help of magic string.
50+
51+
let error = false;
52+
const errorCase = (e) => {
53+
error = true;
54+
let msg = "Automatic source map generation failed, or produced an inaccurate result. If you need a proper source map, you should provide it manually, otherwise you can ignore this message.";
55+
if (e) {
56+
msg = `${msg}\n___\n${e}`;
57+
}
58+
context.warn(msg);
59+
};
60+
61+
const ms = new MagicString(source);
62+
let i = 0;
63+
64+
for (const diff of diffChars(source, code)) {
65+
66+
if (diff.added) {
67+
try {
68+
ms.appendRight(i, diff.value);
69+
} catch(e) {
70+
errorCase(e);
71+
break;
72+
}
73+
}
74+
75+
else if (diff.removed) {
76+
try {
77+
ms.remove(i, i+=diff.count);
78+
} catch(e) {
79+
errorCase(e);
80+
break;
81+
}
82+
}
83+
84+
else {
85+
i += diff.count;
86+
}
87+
}
88+
89+
// Test if the code output of the magic string instance
90+
// matches the output provided by the user
91+
if (!error && ms.toString() !== code) {
92+
errorCase();
93+
}
94+
4695
map = ms.generateMap({ hires: true });
4796
}
4897
} else {
@@ -54,29 +103,32 @@ const yourFunction = (settings={}) => {
54103

55104

56105
if (settings.output) {
57-
plugin.renderChunk = async (source, chunk, outputOptions, meta) => {
106+
plugin.renderChunk = async function(source, chunk, outputOptions, meta) {
58107
return await fnWrap(
59108
source,
60109
{
61110
id: chunk.fileName,
62111
chunk,
63112
outputOptions,
64113
meta
65-
}
114+
},
115+
this
66116
);
67117
};
68118
}
69119

70120
else {
71-
plugin.transform = async (source, id) => {
121+
plugin.transform = async function(source, id) {
72122
return await fnWrap(
73123
source,
74-
{ id }
124+
{ id },
125+
this
75126
);
76127
};
77128
}
78129

79130
return plugin;
80131
};
81132

133+
82134
export { yourFunction };

0 commit comments

Comments
 (0)