diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap new file mode 100644 index 000000000..e7b7f55a2 --- /dev/null +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -0,0 +1,109 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1`] = ` +[ + { + "output": { + "distPath": { + "js": "./", + }, + "filenameHash": false, + "minify": false, + }, + "source": { + "alias": { + "bar": "bar", + "foo": "foo/esm", + }, + "preEntry": "./b.js", + }, + "tools": { + "htmlPlugin": false, + "rspack": { + "experiments": { + "outputModule": true, + }, + "optimization": { + "concatenateModules": true, + }, + "output": { + "library": { + "type": "module", + }, + "module": true, + }, + }, + }, + }, + { + "output": { + "distPath": { + "js": "./", + }, + "filenameHash": false, + "minify": false, + }, + "source": { + "alias": { + "bar": "bar/cjs", + "foo": "foo", + }, + "preEntry": [ + "./a.js", + "./c.js", + "./d.js", + ], + }, + "tools": { + "htmlPlugin": false, + "rspack": { + "experiments": { + "outputModule": true, + }, + "optimization": { + "concatenateModules": true, + }, + "output": { + "library": { + "type": "module", + }, + "module": true, + }, + }, + }, + }, + { + "output": { + "distPath": { + "js": "./", + }, + "filenameHash": false, + "minify": false, + }, + "source": { + "alias": { + "bar": "bar", + "foo": "foo", + }, + "preEntry": "./a.js", + }, + "tools": { + "htmlPlugin": false, + "rspack": { + "experiments": { + "outputModule": true, + }, + "optimization": { + "concatenateModules": true, + }, + "output": { + "library": { + "type": "module", + }, + "module": true, + }, + }, + }, + }, +] +`; diff --git a/packages/core/tests/config.test.ts b/packages/core/tests/config.test.ts new file mode 100644 index 000000000..3a94feebb --- /dev/null +++ b/packages/core/tests/config.test.ts @@ -0,0 +1,185 @@ +import { join } from 'node:path'; +import { describe, expect, test } from 'vitest'; +import { composeCreateRsbuildConfig, loadConfig } from '../src/config'; +import type { RslibConfig } from '../src/types/config'; + +describe('Should load config file correctly', () => { + test('Load config.js in cjs project', async () => { + const fixtureDir = join(__dirname, 'config/cjs'); + const configDir = join(fixtureDir, 'rslib.config.js'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.mjs in cjs project', async () => { + const fixtureDir = join(__dirname, 'config/cjs'); + const configDir = join(fixtureDir, 'rslib.config.mjs'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.ts in cjs project', async () => { + const fixtureDir = join(__dirname, 'config/cjs'); + const configDir = join(fixtureDir, 'rslib.config.ts'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.ts', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.cjs with defineConfig in cjs project', async () => { + const fixtureDir = join(__dirname, 'config/cjs'); + const configDir = join(fixtureDir, 'rslib.config.cjs'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.js in esm project', async () => { + const fixtureDir = join(__dirname, 'config/esm'); + const configDir = join(fixtureDir, 'rslib.config.js'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.cjs in esm project', async () => { + const fixtureDir = join(__dirname, 'config/esm'); + const configDir = join(fixtureDir, 'rslib.config.cjs'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.ts in esm project', async () => { + const fixtureDir = join(__dirname, 'config/esm'); + const configDir = join(fixtureDir, 'rslib.config.ts'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.ts', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); + + test('Load config.mjs with defineConfig in esm project', async () => { + const fixtureDir = join(__dirname, 'config/esm'); + const configDir = join(fixtureDir, 'rslib.config.mjs'); + const config = await loadConfig(configDir); + expect(config).toEqual({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, + _privateMeta: { + configFilePath: configDir, + }, + }); + }); +}); + +describe('Should compose create Rsbuild config correctly', () => { + test('Merge Rsbuild config', async () => { + const rslibConfig: RslibConfig = { + lib: [ + { + format: 'esm', + source: { + alias: { + foo: 'foo/esm', + }, + preEntry: './b.js', + }, + }, + { + format: 'cjs', + source: { + alias: { + bar: 'bar/cjs', + }, + preEntry: ['./c.js', './d.js'], + }, + }, + { + format: 'umd', + }, + ], + source: { + alias: { + foo: 'foo', + bar: 'bar', + }, + preEntry: './a.js', + }, + output: { + filenameHash: false, + minify: true, + }, + }; + const composedRsbuildConfig = await composeCreateRsbuildConfig(rslibConfig); + expect(composedRsbuildConfig).toMatchSnapshot(); + }); +}); diff --git a/packages/core/tests/config/cjs/package.json b/packages/core/tests/config/cjs/package.json new file mode 100644 index 000000000..5bbefffba --- /dev/null +++ b/packages/core/tests/config/cjs/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/packages/core/tests/config/cjs/rslib.config.cjs b/packages/core/tests/config/cjs/rslib.config.cjs new file mode 100644 index 000000000..aa2b913a6 --- /dev/null +++ b/packages/core/tests/config/cjs/rslib.config.cjs @@ -0,0 +1,10 @@ +const { defineConfig } = require('../../../../core/src/config'); + +module.exports = defineConfig((args) => ({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, +})); diff --git a/packages/core/tests/config/cjs/rslib.config.js b/packages/core/tests/config/cjs/rslib.config.js new file mode 100644 index 000000000..2138d4cdd --- /dev/null +++ b/packages/core/tests/config/cjs/rslib.config.js @@ -0,0 +1,8 @@ +module.exports = { + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, +}; diff --git a/packages/core/tests/config/cjs/rslib.config.mjs b/packages/core/tests/config/cjs/rslib.config.mjs new file mode 100644 index 000000000..3d824b6e9 --- /dev/null +++ b/packages/core/tests/config/cjs/rslib.config.mjs @@ -0,0 +1,8 @@ +export default { + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, +}; diff --git a/packages/core/tests/config/cjs/rslib.config.ts b/packages/core/tests/config/cjs/rslib.config.ts new file mode 100644 index 000000000..e23ab3976 --- /dev/null +++ b/packages/core/tests/config/cjs/rslib.config.ts @@ -0,0 +1,8 @@ +export default { + lib: [], + source: { + entry: { + main: './foo/index.ts', + }, + }, +}; diff --git a/packages/core/tests/config/esm/package.json b/packages/core/tests/config/esm/package.json new file mode 100644 index 000000000..3dbc1ca59 --- /dev/null +++ b/packages/core/tests/config/esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/packages/core/tests/config/esm/rslib.config.cjs b/packages/core/tests/config/esm/rslib.config.cjs new file mode 100644 index 000000000..2138d4cdd --- /dev/null +++ b/packages/core/tests/config/esm/rslib.config.cjs @@ -0,0 +1,8 @@ +module.exports = { + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, +}; diff --git a/packages/core/tests/config/esm/rslib.config.js b/packages/core/tests/config/esm/rslib.config.js new file mode 100644 index 000000000..3d824b6e9 --- /dev/null +++ b/packages/core/tests/config/esm/rslib.config.js @@ -0,0 +1,8 @@ +export default { + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, +}; diff --git a/packages/core/tests/config/esm/rslib.config.mjs b/packages/core/tests/config/esm/rslib.config.mjs new file mode 100644 index 000000000..facaa7ee5 --- /dev/null +++ b/packages/core/tests/config/esm/rslib.config.mjs @@ -0,0 +1,10 @@ +import { defineConfig } from '../../../../core/src/config'; + +export default defineConfig((args) => ({ + lib: [], + source: { + entry: { + main: './foo/index.js', + }, + }, +})); diff --git a/packages/core/tests/config/esm/rslib.config.ts b/packages/core/tests/config/esm/rslib.config.ts new file mode 100644 index 000000000..e23ab3976 --- /dev/null +++ b/packages/core/tests/config/esm/rslib.config.ts @@ -0,0 +1,8 @@ +export default { + lib: [], + source: { + entry: { + main: './foo/index.ts', + }, + }, +};