Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions packages/core/src/configChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export const CHAIN_ID = {
},
/** Predefined rule groups */
ONE_OF: {
JS_TRANSFORM: 'js-transform',
JS_RAW: 'js-raw',
SVG: 'svg',
SVG_RAW: 'svg-asset-raw',
SVG_URL: 'svg-asset-url',
Expand Down
23 changes: 12 additions & 11 deletions packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,20 @@ export const pluginSwc = (): RsbuildPlugin => ({
const rule = chain.module
.rule(CHAIN_ID.RULE.JS)
.test(SCRIPT_REGEX)
.type('javascript/auto')
// When using `new URL('./path/to/foo.js', import.meta.url)`,
// the module should be treated as an asset module rather than a JS module.
.dependency({ not: 'url' })
// exclude `import './foo.js?raw'`
.resourceQuery({ not: RAW_QUERY_REGEX });
.dependency({ not: 'url' });

// Support for `import rawJs from "a.js?raw"`
chain.module
.rule(CHAIN_ID.RULE.JS_RAW)
.test(SCRIPT_REGEX)
.type('asset/source')
.resourceQuery(RAW_QUERY_REGEX);
rule
.oneOf(CHAIN_ID.ONE_OF.JS_RAW)
.resourceQuery(RAW_QUERY_REGEX)
.type('asset/source');

// Transform TypeScript/JSX/ESNext code
const transformRule = rule
.oneOf(CHAIN_ID.ONE_OF.JS_TRANSFORM)
.type('javascript/auto');

const dataUriRule = chain.module
.rule(CHAIN_ID.RULE.JS_DATA_URI)
Expand Down Expand Up @@ -171,7 +172,7 @@ export const pluginSwc = (): RsbuildPlugin => ({
api.context.rootPath,
);
if (coreJsDir) {
for (const item of [rule, dataUriRule]) {
for (const item of [transformRule, dataUriRule]) {
Comment thread
chenjiahan marked this conversation as resolved.
item.resolve.alias.set('core-js', coreJsDir);
}
}
Expand All @@ -194,7 +195,7 @@ export const pluginSwc = (): RsbuildPlugin => ({
delete mergedConfig.env;
}

rule
transformRule
.use(CHAIN_ID.USE.SWC)
.loader(builtinSwcLoaderName)
.options(mergedConfig);
Expand Down
86 changes: 43 additions & 43 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -130,55 +130,55 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
/\\\\\\.\\(\\?:ts\\|tsx\\|jsx\\|mts\\|cts\\)\\$/,
/\\[\\\\\\\\/\\]@rsbuild\\[\\\\\\\\/\\]core\\[\\\\\\\\/\\]dist\\[\\\\\\\\/\\]/,
],
"resourceQuery": {
"not": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
},
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"type": "javascript/auto",
"use": [
"oneOf": [
{
"loader": "builtin:swc-loader",
"options": {
"collectTypeScriptInfo": {
"exportedEnum": false,
"typeExports": true,
},
"env": {
"targets": [
"chrome >= 107",
"edge >= 107",
"firefox >= 104",
"safari >= 16",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"type": "asset/source",
},
{
"type": "javascript/auto",
"use": [
{
"loader": "builtin:swc-loader",
"options": {
"collectTypeScriptInfo": {
"exportedEnum": false,
"typeExports": true,
},
"env": {
"targets": [
"chrome >= 107",
"edge >= 107",
"firefox >= 104",
"safari >= 16",
],
},
"isModule": "unknown",
"jsc": {
"experimental": {
"cacheRoot": "<ROOT>/node_modules/.cache/.swc",
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
"tsx": false,
},
"transform": {
"decoratorVersion": "2022-03",
"legacyDecorator": false,
},
},
},
},
},
],
},
],
},
{
"resourceQuery": /\\[\\?&\\]raw\\(\\?:&\\|=\\|\\$\\)/,
"test": /\\\\\\.\\(\\?:js\\|jsx\\|mjs\\|cjs\\|ts\\|tsx\\|mts\\|cts\\)\\$/,
"type": "asset/source",
},
{
"mimetype": {
Expand Down
Loading
Loading