techor
Version:
Author technology like a top leader
65 lines (62 loc) • 2.35 kB
JavaScript
import { transform } from '@swc/core';
import extend from '@techor/extend';
import { createFilter } from '../node_modules/.pnpm/@rollup_pluginutils@5.1.4_rollup@4.40.2/node_modules/@rollup/pluginutils/dist/es/index.mjs';
function swc({ tsconfigFile, include, exclude, minify, ...options } = {}, tsconfig) {
const filter = createFilter(include, exclude);
let swcOptions = {
jsc: {
parser: {},
transform: {}
}
};
swcOptions = extend(swcOptions, options);
if (tsconfig?.compilerOptions?.experimentalDecorators) {
swcOptions.jsc.parser.decorators = true;
swcOptions.jsc.transform.legacyDecorator = true;
swcOptions.jsc.transform.decoratorMetadata = tsconfig?.compilerOptions.emitDecoratorMetadata;
}
if (tsconfig?.compilerOptions?.jsx) {
swcOptions.jsc.transform.react = {
pragma: tsconfig?.compilerOptions.jsxFactory,
pragmaFrag: tsconfig?.compilerOptions.jsxFragmentFactory,
importSource: tsconfig?.compilerOptions.jsxImportSource
};
}
if (tsconfig?.compilerOptions?.target) {
swcOptions.jsc.target = tsconfig?.compilerOptions.target.toLocaleLowerCase();
}
return {
name: 'techor-swc',
async transform (code, id) {
if (!filter(id)) return null;
const transformed = await transform(code, {
filename: id,
sourceMaps: true,
...extend(swcOptions, {
jsc: {
parser: /\.tsx?$/.test(id) ? {
syntax: 'typescript',
tsx: /\.tsx$/.test(id)
} : {
syntax: 'ecmascript',
jsx: /\.jsx$/.test(id)
}
}
})
});
return {
code: transformed.code,
map: transformed.map && JSON.parse(transformed.map)
};
},
async renderChunk (code, chunk) {
return minify ? await transform(code, {
...swcOptions,
sourceMaps: true,
minify: true,
filename: chunk.fileName
}) : null;
}
};
}
export { swc as default };