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

Support automatic JSX runtime and configuration via tsconfig.json #6501

Merged
merged 8 commits into from
Jun 26, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function first() {
output("first(): factory evaluated");
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
output("first(): called");
};
}

function second() {
output("second(): factory evaluated");
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
output("second(): called");
};
}

class ExampleClass {
@first()
@second()
method() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"experimentalDecorators": true
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = <div />;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"preact": "^10.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = <div />;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"preact": "^17"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"jsxImportSource": "preact"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = <div />;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"react": "^17"
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = <><div /></>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"preact": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"jsxFactory": "JSX",
"jsxFragmentFactory": "JSXFragment"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"private": true,
"dependencies": {
"preact": "*"
"preact": "^10.4"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = <div />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"jsx": "react"
}
}
Empty file.
69 changes: 69 additions & 0 deletions packages/core/integration-tests/test/transpilation.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,75 @@ describe('transpilation', function() {
assert(!file.includes('@swc/helpers'));
});

it('should support the automatic JSX runtime with React >= 17', async function() {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic/index.js'),
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('react/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});

it('should support the automatic JSX runtime with preact >= 10.5', async function() {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic-preact/index.js'),
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('preact/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});

it('should support the automatic JSX runtime with explicit tsconfig.json', async function() {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic-tsconfig/index.js'),
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('preact/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});

it('should support explicit JSX pragma in tsconfig.json', async function() {
let b = await bundle(
path.join(__dirname, '/integration/jsx-pragma-tsconfig/index.js'),
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('JSX(JSXFragment'));
assert(file.includes('JSX("div"'));
});

it('should support explicitly enabling JSX in tsconfig.json', async function() {
let b = await bundle(
path.join(__dirname, '/integration/jsx-tsconfig/index.js'),
);

let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('React.createElement("div"'));
});

it('should support enabling decorators in tsconfig.json', async function() {
let b = await bundle(
path.join(__dirname, '/integration/decorators/index.ts'),
);

let output = [];
await run(b, {
output(o) {
output.push(o);
},
});

assert.deepEqual(output, [
'first(): factory evaluated',
'second(): factory evaluated',
'second(): called',
'first(): called',
]);
});

it('should support transpiling optional chaining', async function() {
let b = await bundle(
path.join(__dirname, '/integration/babel-optional-chaining/index.js'),
Expand Down
25 changes: 24 additions & 1 deletion packages/transformers/js/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use swc_ecmascript::transforms::resolver::resolver_with_mark;
use swc_ecmascript::transforms::{
compat::reserved_words::reserved_words, fixer, helpers, hygiene,
optimization::simplify::dead_branch_remover, optimization::simplify::expr_simplifier,
pass::Optional, react, typescript,
pass::Optional, proposals::decorators, react, typescript,
};
use swc_ecmascript::visit::FoldWith;

Expand Down Expand Up @@ -64,6 +64,9 @@ pub struct Config {
is_jsx: bool,
jsx_pragma: Option<String>,
jsx_pragma_frag: Option<String>,
automatic_jsx_runtime: bool,
jsx_import_source: Option<String>,
decorators: bool,
is_development: bool,
react_refresh: bool,
targets: Option<HashMap<String, String>>,
Expand Down Expand Up @@ -219,6 +222,15 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
} else {
None
};

react_options.runtime = if config.automatic_jsx_runtime {
if let Some(import_source) = config.jsx_import_source {
react_options.import_source = import_source;
}
Some(react::Runtime::Automatic)
} else {
Some(react::Runtime::Classic)
};
}

module = {
Expand All @@ -227,6 +239,15 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
react::react(source_map.clone(), Some(&comments), react_options),
config.is_jsx
),
// Decorators can use type information, so must run before the TypeScript pass.
Optional::new(
decorators::decorators(decorators::Config {
legacy: true,
// Always disabled for now, SWC's implementation doesn't match TSC.
emit_metadata: false
}),
config.decorators
),
Optional::new(typescript::strip(), config.is_type_script)
);

Expand Down Expand Up @@ -376,6 +397,7 @@ fn parse(
let mut tsconfig = TsConfig::default();
tsconfig.tsx = config.is_jsx;
tsconfig.dynamic_import = true;
tsconfig.decorators = config.decorators;
Syntax::Typescript(tsconfig)
} else {
let mut esconfig = EsConfig::default();
Expand All @@ -384,6 +406,7 @@ fn parse(
esconfig.export_default_from = true;
esconfig.export_namespace_from = true;
esconfig.import_meta = true;
esconfig.decorators = config.decorators;
Syntax::Es(esconfig)
};

Expand Down
Loading