Skip to content

Commit 69f663c

Browse files
v0.5.0
1 parent cee81b3 commit 69f663c

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

cjs/your-function.cjs

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ const showDiff = (filename, source, code, diffOption, pluginName) => {
134134
* @license MIT
135135
*/
136136

137-
138137
const yourFunction = (settings={}) => {
139138

140139
if (!settings.fn) {
@@ -149,7 +148,7 @@ const yourFunction = (settings={}) => {
149148
: "your-function"
150149
};
151150

152-
const fnWrap = async (source, options) => {
151+
const fnWrap = async (source, options, context) => {
153152

154153
if (!filter(options.id)) return null;
155154

@@ -172,20 +171,50 @@ const yourFunction = (settings={}) => {
172171
// of diff and using the output to apply the changes
173172
// to the source with the help of magic string.
174173

174+
let error = false;
175+
const errorCase = (e) => {
176+
error = true;
177+
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.";
178+
if (e) {
179+
msg = `${msg}\n___\n${e}`;
180+
}
181+
context.warn(msg);
182+
};
183+
175184
const ms = new MagicString(source);
176185
let i = 0;
177186

178187
for (const diff$1 of diff.diffChars(source, code)) {
179188

180189
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 {
190+
try {
191+
ms.appendRight(i, diff$1.value);
192+
} catch(e) {
193+
errorCase(e);
194+
break;
195+
}
196+
}
197+
198+
else if (diff$1.removed) {
199+
try {
200+
ms.remove(i, i+=diff$1.count);
201+
} catch(e) {
202+
errorCase(e);
203+
break;
204+
}
205+
}
206+
207+
else {
185208
i += diff$1.count;
186209
}
187210
}
188211

212+
// Test if the code output of the magic string instance
213+
// matches the output provided by the user
214+
if (!error && ms.toString() !== code) {
215+
errorCase();
216+
}
217+
189218
map = ms.generateMap({ hires: true });
190219
}
191220
} else {
@@ -197,24 +226,26 @@ const yourFunction = (settings={}) => {
197226

198227

199228
if (settings.output) {
200-
plugin.renderChunk = async (source, chunk, outputOptions, meta) => {
229+
plugin.renderChunk = async function(source, chunk, outputOptions, meta) {
201230
return await fnWrap(
202231
source,
203232
{
204233
id: chunk.fileName,
205234
chunk,
206235
outputOptions,
207236
meta
208-
}
237+
},
238+
this
209239
);
210240
};
211241
}
212242

213243
else {
214-
plugin.transform = async (source, id) => {
244+
plugin.transform = async function(source, id) {
215245
return await fnWrap(
216246
source,
217-
{ id }
247+
{ id },
248+
this
218249
);
219250
};
220251
}

0 commit comments

Comments
 (0)