Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
require.extensions['.jsx'] = (_module, filename) => {
const source = readFileSync(filename, 'utf8');
const hash = checksum(`/* sucrase-jsx (${sucrase.getVersion()}) | ${filename} */ ${source}`);
const cached = join(cacheDir, `${hash}.js`);
try {
_module._compile(readFileSync(cached, 'utf8'), filename);
} catch (_) {
const res = sucrase.transform(source, {
transforms: [ 'jsx' ],
filePath: filename
}).code;
_module._compile(res, filename);
writeFile(cached, res, (err) => {
if (err) {
console.error('%c[Powercord:JSX]', 'color: #7289da', 'Failed to write to cache');
console.error(err);
}
});
}
};
export function process(code) {
code = `${IMPORTS}${code}`;
if (!code.match(/[\s\b;,]export[{ ]/)) {
code = code.replace(
/([\s\b;,])((async )?(function|class)[\s{])/g,
'$1export default $2'
);
}
let out = {};
try {
out = transform(code, {
filePath: 'repl.js',
sourceMapOptions: {
compiledFilename: 'repl.js'
},
transforms: ['jsx', 'typescript', 'imports'],
// omit _jsxSource junk
production: true,
// .default fixing since we're using shim modules
enableLegacyTypeScriptModuleInterop: true,
enableLegacyBabel5ModuleInterop: true,
jsxPragma: 'h',
jsxFragmentPragma: 'Fragment'
});
} catch (err) {
if (err.name !== 'SyntaxError' && /unexpected\stoken/i.test(err.message)) {
let old = err;
file: any,
enc: string,
// tslint:disable-next-line no-any
cb: (err?: any, data?: any) => void,
): void {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new PluginError(PLUGIN_NAME, "Streaming is not supported."));
return;
}
try {
const resultCode = transform(file.contents.toString(), {filePath: file.path, ...options})
.code;
file.contents = Buffer.from(resultCode);
file.path = replaceExt(file.path, ".js");
this.push(file);
} catch (e) {
e.message = `Error when processing file ${file.path}: ${e.message}`;
this.emit("error", new PluginError(PLUGIN_NAME, e));
}
cb();
});
}
exports.process = (src, filename) => {
const transforms = getTransforms(filename);
if (transforms !== null) {
const result = transform(src, {
filePath: filename,
jsxFragmentPragma: 'Fragment', // Preact style JSX
jsxPragma: 'h',
production: true, // Don't add debug attributes to snapshots
transforms,
});
return {
code: result.code,
map: result.sourceMap,
};
}
return { code: src, map: '' };
};
if (transpiled) {
// Ignore async
if (transpiled instanceof Promise) {
warn(WARN_SERVER_TRANSPILE);
} else {
code = transpiled;
}
}
}
if (!isModule(code)) {
return code;
}
return sucrase.transform(code, {
transforms: ['imports'],
filePath
}).code;
},
{
function loader(code: string): string {
const webpackRemainingChain = getRemainingRequest(this).split("!");
const filePath = webpackRemainingChain[webpackRemainingChain.length - 1];
const options: Options = getOptions(this) as Options;
return transform(code, {filePath, ...options}).code;
}
const handleJSX = (code) => {
const transformed = transform(code, options);
return transformed.code;
};
export function process(src: string, filename: string): string {
const transforms = getTransforms(filename);
if (transforms !== null) {
return transform(src, {transforms, filePath: filename}).code;
} else {
return src;
}
}
() =>
Sucrase.transform(config.code, {transforms: getSelectedTransforms(), filePath: getFilePath()})
.code,
);