Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Remove experimental decorators check from SWC config #57

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const fileExists = (path: string) => {
.catch(() => false);
};

const lookup = (entry: string, target: string): string => {
const dir = dirname(entry);
const targetFile = join(dir, target);
if (fs.existsSync(targetFile)) return targetFile;
return lookup(dir, target);
};

function swc(options: PluginOptions = {}): RollupPlugin {
const filter = createFilter(
options.include || INCLUDE_REGEXP,
Expand All @@ -68,7 +75,6 @@ function swc(options: PluginOptions = {}): RollupPlugin {

return {
name: 'swc',

async resolveId(importee, importer) {
// ignore IDs with null character, these belong to other plugins
if (importee.startsWith('\0')) {
Expand Down Expand Up @@ -107,6 +113,23 @@ function swc(options: PluginOptions = {}): RollupPlugin {
? {}
: getOptions(this, dirname(id), options.tsconfig);

let enaleExperimentalDecorators = false;
nonzzz marked this conversation as resolved.
Show resolved Hide resolved
if (isTypeScript) {
try {
const tsPath = require.resolve('typescript', { paths: [process.cwd()] });
const packageJsonPath = lookup(tsPath, 'package.json');
const { version } = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
nonzzz marked this conversation as resolved.
Show resolved Hide resolved
const [major] = version.split('.');
// typescript 5.x
if (+major >= 5) enaleExperimentalDecorators = true;
} catch {
this.warn({
message: 'Can\'t find typescript, please Check typescript has been installed.',
nonzzz marked this conversation as resolved.
Show resolved Hide resolved
pluginCode: 'SWC_TYPESCRIPT_NOT_EXISTS'
});
}
}

// TODO: SWC is about to add "preserve" jsx
// https://github.com/swc-project/swc/pull/5661
// Respect "preserve" after swc adds the support
Expand All @@ -118,10 +141,12 @@ function swc(options: PluginOptions = {}): RollupPlugin {
parser: {
syntax: isTypeScript ? 'typescript' : 'ecmascript',
[isTypeScript ? 'tsx' : 'jsx']: isTypeScript ? isTsx : isJsx,
decorators: tsconfigOptions.experimentalDecorators
decorators: enaleExperimentalDecorators || tsconfigOptions.experimentalDecorators
},
transform: {
decoratorMetadata: tsconfigOptions.emitDecoratorMetadata,
// @ts-expect-error -- swc types loose this :(
decoratorVersion: enaleExperimentalDecorators ? '2022-03' : '2021-12',
react: {
runtime: useReact17NewTransform
? 'automatic'
Expand Down
Loading