Default option is: { compress: {}, mangle: true }.
If set it to false, then SWC minification will be disabled, if set it to true then will it applies default option. If you pass an object, then this object will be merged with default option.
If this field is not undefined or false, the plugin will import style for the given component path.
For example, let’s say the original code is import { Button } from 'foo'.
If style is set to:
true, code will be extended by: import 'foo/lib/button/style'.
'css, code will be extended by: import 'foo/lib/button/style/css'.
(path) => path + 'less', code will be extended by: import 'foo/lib/button.less'.
styleLibraryDirectory
Type: string
Default: undefined
If this field is set, style will be ignored.
This field decides the path of style to import, for example, set this to 'styles', import { Button } from 'foo' will become:
import Button from "foo/lib/button";
import "foo/styles/button";
camelToDashComponentName
Type: boolean
Default: true
Wether to transform specifier to kebab case when added to import path. Eg: myText to foo/lib/my-text.
transformToDefaultImport
Type: boolean
Default: true
Wether to transform this import expression to default import. Eg: import { Button } from 'foo' will be transformed to import { Button } from 'foo/lib/button';.
type LockCorejsVersion = {
corejs?: string;
swcHelpers?: string;
};
Use this to rewrite core-js and @swc/helpers import path, this is helpful if you are an author of a library, and that library code contains @swc/helpers import, but you don’t want your user to specify @swc/helpers as dependencies, you can achieve that in the following way.
type Emotion = boolean | {
// default is true. It will be disabled when build type is production.
sourceMap?: boolean,
// default is 'dev-only'.
autoLabel?: 'never' | 'dev-only' | 'always',
// default is '[local]'.
// Allowed values: `[local]` `[filename]` and `[dirname]`
// This option only works when autoLabel is set to 'dev-only' or 'always'.
// It allows you to define the format of the resulting label.
// The format is defined via string where variable parts are enclosed in square brackets [].
// For example labelFormat: "my-classname--[local]", where [local] will be replaced with the name of the variable the result is assigned to.
labelFormat?: string,
// default is undefined.
// This option allows you to tell the compiler what imports it should
// look at to determine what it should transform so if you re-export
// Emotion's exports, you can still use transforms.
importMap?: {
[packageName: string]: {
[exportName: string]: {
canonicalImport?: [string, string],
styledBaseImport?: [string, string],
}
}
},
},
Modern.js SWC Plugins
This repo provides an executable with some built-in SWC plugins for Modern.js and other Node.js users.
Quick start
Install
Install the plugin by using:
Contributing
Please read the Contributing Guide.
Usage
Transform
Minify
Config
PluginConfigpresetReact
Ported from
@babel/preset-react. The value you passed will be merged with default option.Default option is:
presetEnv
Ported from
@babel/preset-env. The value you passed will be merged with default option.Default option is:
jsMinify
booleanor compress configuration.Default option is:
{ compress: {}, mangle: true }.If set it to
false, then SWC minification will be disabled, if set it totruethen will it applies default option. If you pass an object, then this object will be merged with default option.extensions
ObjectSome plugins ported from Babel.
extensions.pluginImport
Ported from babel-plugin-import.
libraryName
stringThe package that need to be transformed,eg. in
import { a } from 'foo',**libraryName**should befoo.libraryDirectory
stringlibThe path prefix that to import. For example
Buttonwill be transformed tosome-lib/lib/button.style
'css' | string | boolean | ((input: string) => string | undefined)undefinedIf this field is not
undefinedorfalse, the plugin will import style for the given component path.For example, let’s say the original code is
import { Button } from 'foo'. Ifstyleis set to:true, code will be extended by:import 'foo/lib/button/style'.'css, code will be extended by:import 'foo/lib/button/style/css'.(path) => path + 'less', code will be extended by:import 'foo/lib/button.less'.styleLibraryDirectory
stringundefinedIf this field is set,
stylewill be ignored.This field decides the path of style to import, for example, set this to
'styles',import { Button } from 'foo'will become:camelToDashComponentName
booleantrueWether to transform specifier to kebab case when added to import path. Eg:
myTexttofoo/lib/my-text.transformToDefaultImport
booleantrueWether to transform this import expression to default import. Eg:
import { Button } from 'foo'will be transformed toimport { Button } from 'foo/lib/button';.customName
string | ((name: string) => string | undefined)undefinedYou can use this to customize the transformation.
Assume the original code is:
And set
customNameto:Result:
customStyleName
string | ((name: string) => string | undefined)undefinedThe same with
customName, but just for style import expression.extensions.reactUtils
ObjectSome little help utils for
React.reactUtils.autoImportReact
booleanAutomatically import
Reactas global variable, eg:import React from 'react'. Mostly used for generatedReact.createElement.reactUtils.removeEffect
booleanRemove
useEffectcall.reactUtils.removePropTypes
Remove
Reactruntime type checking. This is ported from @babel/plugin-react-transform-remove-prop-types, All the configurations remain the same.extensions.lodash
{ cwd?: string; ids?: string;}{ cwd: process.cwd(), ids: [] }Ported from @babel/plugin-lodash.
Note there is a small difference that
lodash-compatis currently deprecated so we do not supportlodash-compatpackage.extensions.modularizeImports
More detail on Next.js modularize-imports
extensions.lockCorejsVersion
Use this to rewrite
core-jsand@swc/helpersimport path, this is helpful if you are an author of a library, and that library code contains@swc/helpersimport, but you don’t want your user to specify@swc/helpersas dependencies, you can achieve that in the following way.By doing so, the following code:
will become something like this:
extensions.styledComponents
More detail at https://nextjs.org/docs/advanced-features/compiler#styled-components
extensions.emotion
More detail at https://nextjs.org/docs/advanced-features/compiler#emotion
extensions.modernjsSsrLoaderId
booleanundefinedEnable some transform only needed by Modern.js