Replies: 2 comments
-
Plan 1Place all the AST transform in rules: [
{
test: /\.mdx$/,
use: ["@mdx-js/loader"], // js loader, do string transform
transformer: { presetEnv: 'es5', emotion: true }, // or `parser`/`generator`, do AST transform
},
]
|
Beta Was this translation helpful? Give feedback.
-
Plan 2Introducing rules: [
{
test: /\.mdx$/,
use: [
"@mdx-js/loader", // js loader, but we can intergrate `mdxjs-rs` to implement `builtin:mdx-loader`
{
loader: "builtin:swc-loader",
options: {
plugins: [
["presetEnv", { targets: 'es5' }],
"emotion",
],
},
},
],
},
]
|
Beta Was this translation helpful? Give feedback.
-
Currently, there are JavaScript AST transform options located in the
builtins
options (such asbuiltins.react
,builtins.presetEnv
,builtins.decorator
,builtins.emotion
, andbuiltins.relay
). Typically, these options should be configured by babel-loader plugins in Webpack. However, in Rspack, we do not recommend using babel-loader in Rspack because it would negatively impact performance. We use SWC to parse, generate, and transform JavaScript AST to implement features like JSX and decorator. As our use cases grow, we need to implement presetEnv, emotion, and relay. Since not all projects need these features, we have made them configurable within thebuiltins
options. However, there is a problem with this approach. For example, a project may use both emotion and styled-components. Files undersrc/emotion
may use emotion, while files undersrc/styled
may use styled-components. Similarly, a project may use React, Solid, and Vue simultaneously, which requires different JSX transformation logic depending on the file path. Therefore, the proper location for JavaScript AST transformation should be undermodule.rule[]
.However, there are two remaining questions:
Should we use a loader to perform AST transformations, like using babel-loader in Webpack?
module.rule[].parse
andmodule.rule[].generate
can be used to configure parse and generate options. A specificmodule.rule[].transform
for Rspack might be a good alternative way to perform AST transformations in a loader.Should we keep loader-runner in Rust or move it to JavaScript?
builtin:sass-loader
, and we haven't expose this on our document for some reason). However, the scenarios that require a built-in loader are very few (sass-embedded-host-rust, mdxjs-rs, currently hard to find the thrid one), and there may be other ways to implement them (e.g., usingtype: 'mdx' | 'sass'
,module.rule[].loader: { mdx: true }
).PS:
builtins.react
,builtins.presetEnv
and all other AST transform related options placed inbuiltins
before v1.0.0, we will compatible with them to avoid breaking changes.Beta Was this translation helpful? Give feedback.
All reactions