Skip to content

Commit

Permalink
feat: enable autoExtension by default (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Aug 7, 2024
1 parent 5b4384e commit b9ae23e
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 28 deletions.
46 changes: 35 additions & 11 deletions e2e/cases/autoExtension/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
import { extname, join } from 'node:path';
import { buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';
import { describe, expect, test } from 'vitest';

test('autoExtension generate .mjs in build artifacts with esm format when type is commonjs', async () => {
const fixturePath = join(__dirname, 'type-commonjs');
const { entryFiles } = await buildAndGetResults(fixturePath);
expect(extname(entryFiles.esm!)).toEqual('.mjs');
expect(extname(entryFiles.cjs!)).toEqual('.js');
describe('autoExtension: true', () => {
test('generate .mjs in build artifacts with esm format when type is commonjs', async () => {
const fixturePath = join(__dirname, 'type-commonjs');
const { entryFiles } = await buildAndGetResults(fixturePath);
expect(extname(entryFiles.esm!)).toEqual('.mjs');
expect(extname(entryFiles.cjs!)).toEqual('.js');
});

test('generate .cjs in build artifacts with cjs format when type is module', async () => {
const fixturePath = join(__dirname, 'type-module');
const { entryFiles } = await buildAndGetResults(fixturePath);
expect(extname(entryFiles.esm!)).toEqual('.js');
expect(extname(entryFiles.cjs!)).toEqual('.cjs');
});
});

test('autoExtension generate .cjs in build artifacts with cjs format when type is module', async () => {
const fixturePath = join(__dirname, 'type-module');
const { entryFiles } = await buildAndGetResults(fixturePath);
expect(extname(entryFiles.esm!)).toEqual('.js');
expect(extname(entryFiles.cjs!)).toEqual('.cjs');
describe('autoExtension: false', () => {
test('generate .js in both cjs and esm build artifacts when type is commonjs', async () => {
const fixturePath = join(__dirname, 'type-commonjs');
const { entryFiles } = await buildAndGetResults(
fixturePath,
'autoExtension.false.config.ts',
);
expect(extname(entryFiles.esm!)).toEqual('.js');
expect(extname(entryFiles.cjs!)).toEqual('.js');
});

test('generate .js in both cjs and esm build artifacts when type is module', async () => {
const fixturePath = join(__dirname, 'type-module');
const { entryFiles } = await buildAndGetResults(
fixturePath,
'autoExtension.false.config.ts',
);
expect(extname(entryFiles.esm!)).toEqual('.js');
expect(extname(entryFiles.cjs!)).toEqual('.js');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
autoExtension: false,
}),
generateBundleCjsConfig(__dirname, {
autoExtension: false,
}),
],
source: {
entry: {
main: './src/index.ts',
},
},
});
9 changes: 1 addition & 8 deletions e2e/cases/autoExtension/type-commonjs/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
autoExtension: true,
}),
generateBundleCjsConfig(__dirname, {
autoExtension: true,
}),
],
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: './src/index.ts',
Expand Down
18 changes: 18 additions & 0 deletions e2e/cases/autoExtension/type-module/autoExtension.false.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
autoExtension: false,
}),
generateBundleCjsConfig(__dirname, {
autoExtension: false,
}),
],
source: {
entry: {
main: './src/index.ts',
},
},
});
9 changes: 1 addition & 8 deletions e2e/cases/autoExtension/type-module/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
autoExtension: true,
}),
generateBundleCjsConfig(__dirname, {
autoExtension: true,
}),
],
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
source: {
entry: {
main: './src/index.ts',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ async function composeLibRsbuildConfig(
) {
const config = mergeRsbuildConfig<LibConfig>(rsbuildConfig, libConfig);

const { format, autoExtension = false } = config;
const { format, autoExtension = true } = config;
const formatConfig = composeFormatConfig(format!);
const autoExtensionConfig = composeAutoExtensionConfig(
format!,
Expand Down

0 comments on commit b9ae23e

Please sign in to comment.