Skip to content

Commit

Permalink
🐛 Fix inability to compile Script assets with Catnip code
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmoMyzrailGorynych committed Nov 3, 2024
1 parent 4ea9b0e commit 8cecdab
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/node_requires/exporter/scripts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ExporterError, highlightProblem} from './ExporterError';
import {coffeeScriptOptions} from './scriptableProcessor';
import {compile as compileCatnip} from '../catnip/compiler';

const compileCoffee = require('coffeescript').CoffeeScript.compile;
const typeScript = require('sucrase').transform;
Expand All @@ -8,22 +9,41 @@ export const stringifyScripts = (scripts: IScript[]): string =>
scripts.reduce((acc, script) => {
let code;
try { // Apply converters to the user's code first
code = script.language === 'coffeescript' ?
compileCoffee(script.code, coffeeScriptOptions) :
typeScript(script.code, {
switch (script.language) {
case 'typescript':
({code} = typeScript(script.code, {
transforms: ['typescript']
}).code;
}));
break;
case 'coffeescript':
code = compileCoffee(script.code, coffeeScriptOptions);
break;
case 'catnip':
code = compileCatnip(script.code as BlockScript, {
resourceId: script.uid,
resourceName: script.name,
resourceType: script.type,
eventKey: 'onRun'
});
break;
default: throw new Error(`Unsupported script language: ${script.language}`);
}
return acc + `'${script.name}': function (options) {${code}},`;
} catch (e) {
const errorMessage = `${e.name || 'An error'} occured while compiling script ${script.name}`;
const exporterError = new ExporterError(errorMessage, {
resourceId: script.uid,
resourceName: script.name,
resourceType: script.type,
problematicCode: highlightProblem(e.code || code, e.location || e.loc),
clue: 'syntax'
}, e);
throw exporterError;
if (e instanceof ExporterError) {
// Passthrough already formatted errors, mainly coming from Catnip
throw e;
} else {
const exporterError = new ExporterError(errorMessage, {
resourceId: script.uid,
resourceName: script.name,
resourceType: script.type,
problematicCode: highlightProblem(e.code || code, e.location || e.loc),
clue: 'syntax'
}, e);
throw exporterError;
}
}
}, '');

Expand Down

0 comments on commit 8cecdab

Please sign in to comment.