793 releases (245 breaking)

new 0.245.12 Feb 1, 2023
0.244.4 Jan 24, 2023
0.238.1 Dec 27, 2022
0.233.1 Nov 30, 2022
0.13.0 Mar 31, 2021

#791 in Web programming

Download history 6830/week @ 2022-10-13 7981/week @ 2022-10-20 6412/week @ 2022-10-27 3883/week @ 2022-11-03 3230/week @ 2022-11-10 4326/week @ 2022-11-17 2077/week @ 2022-11-24 3204/week @ 2022-12-01 3998/week @ 2022-12-08 3289/week @ 2022-12-15 2352/week @ 2022-12-22 1745/week @ 2022-12-29 4361/week @ 2023-01-05 5251/week @ 2023-01-12 4325/week @ 2023-01-19 4474/week @ 2023-01-26

19,086 downloads per month
Used in 19 crates (13 directly)

Apache-2.0

4.5MB
115K SLoC

Testing

tests/exec

All files named exec.js or exec.ts will be executed, and the test system will ensure that original source code and transpiled source code print the same output to the console.

You can use ./scripts/check-issues.sh to automatically unignore fixed tests.


lib.rs:

The main crate of the swc project.

Customizing

This is documentation for building custom build tools on top of swc.

Dependency version management

swc has swc_css, which re-exports required modules.

Testing

See [testing] and swc_ecma_transform_testing.

Custom javascript transforms

What is JsWord?

It's basically an interned string. See [swc_atoms].

Choosing between JsWord vs String

You should prefer JsWord over [String] if it's going to be stored in an AST node.

See [swc_atoms] for detailed description.

Fold vs VisitMut vs Visit

See [swc_visit] for detailed description.

Variable management (Scoping)

See [swc_ecma_transforms_base::resolver::resolver_with_mark].

How identifiers work

See the doc on [swc_ecma_ast::Ident] or on [swc_ecma_transforms_base::resolver::resolver_with_mark].

Comparing two identifiers

See [swc_ecma_utils::Id]. You can use [swc_ecma_utils::IdentLike::to_id] to extract important parts of an [swc_ecma_ast::Ident].

Creating a unique identifier

See [swc_ecma_utils::private_ident].

Prepending statements

If you want to prepend statements to the beginning of a file, you can use [swc_ecma_utils::prepend_stmts] or [swc_ecma_utils::prepend] if len == 1.

These methods are aware of the fact that "use strict" directive should be first in a file, and insert statements after directives.

Improving readability

Each stuffs are documented at itself.

  • If you are creating or binding an [swc_ecma_ast::Expr] with operator, you can use [swc_ecma_ast::op].

  • If you want to create [swc_ecma_ast::CallExpr], you can use [swc_ecma_utils::ExprFactory::as_callee] to create callee.

  • If you want to create [swc_ecma_ast::CallExpr] or [swc_ecma_ast::NewExpr], you can use [swc_ecma_utils::ExprFactory::as_arg] to create arguments.

  • If you want to create [swc_ecma_ast::MemberExpr] where all identifiers are static (e.g. Object.prototype.hasOwnProperty), you can use [swc_ecma_utils::member_expr].

  • If you want to create [swc_ecma_ast::MemberExpr], you can use [swc_ecma_utils::ExprFactory::as_obj] to create object field.

Reducing binary size

The visitor expands to a lot of code. You can reduce it by using macros like

Note that this will make typescript-related nodes not processed, but it's typically fine as typescript::strip is invoked at the start and it removes typescript-specific nodes.

Porting expr.evaluate() of babel

See [swc_ecma_minifier::eval::Evaluator].

Dependencies

~16–26MB
~522K SLoC