Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 40 additions & 3 deletions e2e/cases/output/charset/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import { build } from '@e2e/helper';
import { build, dev, rspackOnlyTest } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should allow to set output.charset to ascii', async ({ page }) => {
rspackOnlyTest(
'should allow to set output.charset to ascii in development mode',
async ({ page }) => {
const rsbuild = await dev({
cwd: __dirname,
page,
rsbuildConfig: {
dev: {
writeToDisk: true,
},
output: {
charset: 'ascii',
},
},
});

expect(await page.evaluate('window.a')).toBe('你好 world!');

const files = await rsbuild.getDistFiles();

const [, content] = Object.entries(files).find(
([name]) => name.endsWith('.js') && name.includes('static/js/index'),
)!;

// in Rspack is: \\u4f60\\u597D world!
expect(
content.toLocaleLowerCase().includes('\\u4f60\\u597d world!'),
).toBeTruthy();

await rsbuild.close();
},
);

test('should allow to set output.charset to ascii in production mode', async ({
page,
}) => {
const rsbuild = await build({
cwd: __dirname,
page,
Expand All @@ -28,7 +63,9 @@ test('should allow to set output.charset to ascii', async ({ page }) => {
await rsbuild.close();
});

test('should allow to set output.charset to utf8', async ({ page }) => {
test('should allow to set output.charset to utf8 in production mode', async ({
page,
}) => {
const rsbuild = await build({
cwd: __dirname,
rsbuildConfig: {
Expand Down
22 changes: 17 additions & 5 deletions packages/core/src/plugins/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ function applyScriptCondition({
}
}

function getDefaultSwcConfig(
browserslist: string[],
cacheRoot: string,
): SwcLoaderOptions {
function getDefaultSwcConfig({
browserslist,
cacheRoot,
config,
}: {
browserslist: string[];
cacheRoot: string;
config: NormalizedEnvironmentConfig;
}): SwcLoaderOptions {
return {
jsc: {
externalHelpers: true,
Expand All @@ -81,6 +86,9 @@ function getDefaultSwcConfig(
*/
keepImportAttributes: true,
},
output: {
charset: config.output.charset,
},
},
isModule: 'unknown',
env: {
Expand Down Expand Up @@ -137,7 +145,11 @@ export const pluginSwc = (): RsbuildPlugin => ({
return;
}

const swcConfig = getDefaultSwcConfig(browserslist, cacheRoot);
const swcConfig = getDefaultSwcConfig({
browserslist,
cacheRoot,
config,
});

applyTransformImport(swcConfig, config.source.transformImport);
applySwcDecoratorConfig(swcConfig, config);
Expand Down
6 changes: 6 additions & 0 deletions packages/core/tests/__snapshots__/builder.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -196,6 +199,9 @@ exports[`should use Rspack as the default bundler > apply Rspack correctly 1`] =
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down
24 changes: 24 additions & 0 deletions packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -196,6 +199,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -625,6 +631,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -674,6 +683,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1103,6 +1115,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1148,6 +1163,9 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1539,6 +1557,9 @@ exports[`tools.rspack > should match snapshot 1`] = `
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1588,6 +1609,9 @@ exports[`tools.rspack > should match snapshot 1`] = `
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,9 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1491,6 +1494,9 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1832,6 +1838,9 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down Expand Up @@ -1877,6 +1886,9 @@ exports[`environment config > tools.rspack / bundlerChain can be configured in e
"keepImportAttributes": true,
},
"externalHelpers": true,
"output": {
"charset": "utf8",
},
"parser": {
"decorators": true,
"syntax": "typescript",
Expand Down
Loading
Loading