Configuring swc
swc
can be configured with .swcrc
file.
#
preset-envSee preset-env for using swc with browserslist
.
#
jscThis is optional and defaults to
#
jsc.externalHelpersYou can use helpers from an external module named @swc/helpers
.
While bundling, this option will greatly reduce your file size.
#
jsc.parsertypescript
:
ecmascript
:
#
jsc.targetStarting from @swc/core
v1.0.27, you can specify the target environment by using the field.
#
jsc.looseStarting from @swc/core
v1.1.4, you can enable loose mode by
In loose mode, swc generates more efficient code.
#
jsc.transformExample
#
jsc.transform.legacyDecoratorYou can use legacy decorators with swc
. To enable legacy decorator, set jsc.transform.legacyDecorator
and jsc.parser.decorators
to true.
e.g.
#
jsc.transform.decoratorMetadataIf you are using typescript and decorators with emitDecoratorMetadata
enabled, you can use swc
for faster iteration like
e.g.
Note: This feature requires v1.2.13+
.
#
jsc.transform.reactruntime
Possible values: automatic
, classic
.
Use automatic
to use new jsx runtimes.
importSource
Only for runtime: automatic
. react
by default.
This option can be overrided with @jsxImportSource foo
.
Determines the runtime library to import.
pragma
Replace the function used when compiling JSX expressions.
This option can be overrided with @jsx foo
.
Defaults to React.createElement
.
pragmaFrag
Replace the component used when compiling JSX fragments.
This option can be overrided with @jsxFrag foo
.
Defaults to React.Fragment
throwIfNamespace
Toggles whether or not to throw an error if an XML namespaced tag name is used. For example:<f:image />
Though the JSX spec allows this, it is disabled by default since React's JSX does not currently have support for it.
development
Toggles plugins that aid in development, such asjsx-self
andjsx-source
.useBuiltins
UseObject.assign()
instead of_extends
. Defaults to false.refresh
#
jsc.transform.constModules.swcrc
:
becomes
#
jsc.transform.optimizerNote: Optimizer of the swc assumes
- It's a module or wrapped in an iife.
- Accessing (get) global variables does not have a side-effect.
It is the same assumption as the google closure compiler.
- You don't add fields to literals like a numeric literal, regular expression or a string literal.
- Files are served as gzipped.
It means that swc will not focus on reducing the size of non-gzipped file size.
Setting this to undefined
skips optimizer pass
#
jsc.transform.optimizer.globalsvars
Variables to inline.
e.g.
.swcrc
:
npx swc '__DEBUG__' --filename input.js
:
#
jsc.transform.optimizer.jsonifyNote: This feature requires v1.1.1
+
minCost
If cost of parsing a pure object literal is larger than this value, the object literal is converted toJSON.parse('{"foo": "bar"}')
. Defaults to 1024.
e.g.
.swcrc
:
This will change all pure object literals to JSON.parse("")
.
#
moduleswc can transpile es6 modules to common js module, umd module or amd module.
#
shared optionsThese options are shared by common js / umd / amd.
.swcrc
:
#
strictBy default, when using exports with swc a non-enumerable __esModule
property is exported. In some cases, this property is used to determine if the import is the default export or if it contains the default export.
To prevent the __esModule
property from being exported, you can set the strict option to true.
Defaults to false
.
#
strictModeIf true, swc emits 'use strict' directive.
Defaults to true
.
#
lazyChanges Babel's compiled import statements to be lazily evaluated when their imported bindings are used for the first time. This can improve the initial load time of your module because evaluating dependencies upfront is sometimes entirely unnecessary. This is especially the case when implementing a library module.
The value of lazy
has a few possible effects:
false
- No lazy initialization of any imported module.true
- Do not lazy-initialize local./foo
imports, but lazy-initfoo
dependencies. Local paths are much more likely to have circular dependencies, which may break if loaded lazily, so they are not lazy by default, whereas dependencies between independent modules are rarely cyclical.Array<string>
- Lazy-initialize all imports with source matching one of the given strings.
The two cases where imports can never be lazy are:
import "foo";
Side-effect imports are automatically non-lazy since their very existence means that there is no binding to later kick-off initialization.export from "foo"
Re-exporting all names requires up-front execution because otherwise there is no way to know what names need to be exported.
Defaults to false
.
#
noInteropBy default, when using exports with swc a non-enumerable __esModule property is exported. This property is then used to determine if the import is the default export or if it contains the default export.
In cases where the auto-unwrapping of default is not needed, you can set the noInterop option to true to avoid the usage of the interopRequireDefault helper (shown in inline form above).
Defaults to false
.
#
common jsTo emit a common js module, you can do so by
.swcrc
:
#
amdTo emit amd module, you can do so by
.swcrc
:
#
moduleIdDefaults to undefined
. If specified, swc emits named amd module.
#
umdTo emit umd
module, you can do so by
.swcrc
:
#
globalsTODO
#
minifyTo get minified output, you can configure swc by
.swcrc
:
#
Multiple entriesStarting with v1.0.47
, you can specify multiple entries. For example,
this make swc compile javascript files as common js module (uses require('foo')
) and compile typescript files as amd modules.
Note that test
option can be used to transcompile only typescript files, like
#
testType: Regex / Regex[]
Usage:
.swcrc
:
#
excludeType: Regex / Regex[]
.swcrc
:
#
sourceMapsStarting from v1.2.50
, you can enable source map by adding sourceMaps: true
or sourceMaps: 'inline'
to the .swcrc
.
Example: