@velcro/plugin-sucrase
Version:
A sucrase plugin for Velcro that adds support for Typescript, jsx, ES Modules and more
61 lines (57 loc) • 2.12 kB
JavaScript
import { transform } from 'sucrase';
function sucrasePlugin(options = {}) {
const extensions = ['js'];
if (options.transforms) {
for (const transform of options.transforms) {
switch (transform) {
case 'jsx':
extensions.push('jsx');
break;
case 'typescript':
extensions.push('ts', 'tsx');
break;
}
}
}
const uriTestRx = new RegExp(`\.(?:${extensions.join('|')})$`, 'i');
const jsxPragmaRx = /\/\*\*\s*@jsx\s+(\S+)\s*\*+\//;
const jsxFragmentPragmaRx = /\/\*\*\s*@jsxFragment\s+(\S+)\s*\*+\//;
return {
name: 'sucrasePlugin',
transform(ctx, uri, code) {
if (!uriTestRx.test(uri.path)) {
return;
}
const sucraseOptions = {
transforms: ['imports', ...(options.transforms || [])],
filePath: uri.toString(),
production: ctx.nodeEnv === 'production',
sourceMapOptions: {
compiledFilename: uri.toString(),
},
};
if (!options.jsxPragma) {
// If not specified try to detect the pragma
const jsxPragmaMatches = code.match(jsxPragmaRx);
if (jsxPragmaMatches) {
sucraseOptions.jsxPragma = jsxPragmaMatches[1];
}
}
if (!options.jsxFragmentPragma) {
// If not specified try to detect the pragma
const jsxFragmentPragmaMatches = code.match(jsxFragmentPragmaRx);
if (jsxFragmentPragmaMatches) {
sucraseOptions.jsxFragmentPragma = jsxFragmentPragmaMatches[1];
}
}
const result = transform(code, sucraseOptions);
return {
code: result.code,
sourceMap: result.sourceMap,
};
},
};
}
const version = '0.56.2';
export { sucrasePlugin, version };
//# sourceMappingURL=index.js.map