Skip to content

Commit

Permalink
test: add dts external test case
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Aug 12, 2024
1 parent c365180 commit c692412
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 15 deletions.
1 change: 1 addition & 0 deletions e2e/cases/auto-external/default/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "auto-external-default-test",
"dependencies": {
"ora": "8.0.1",
"react": "^18.3.1"
}
}
17 changes: 15 additions & 2 deletions e2e/cases/auto-external/default/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig(__dirname), generateBundleCjsConfig(__dirname)],
lib: [
{
...generateBundleEsmConfig(__dirname),
dts: {
bundle: true,
},
},
{
...generateBundleCjsConfig(__dirname),
dts: {
bundle: true,
},
},
],
source: {
entry: {
main: '../__fixtures__/src/index.ts',
main: './src/index.ts',
},
},
});
7 changes: 7 additions & 0 deletions e2e/cases/auto-external/default/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { oraPromise } from 'ora';
import React from 'react';

export type { oraPromise };
export const foo = () => {
return React.version;
};
8 changes: 8 additions & 0 deletions e2e/cases/auto-external/default/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
1 change: 1 addition & 0 deletions e2e/cases/auto-external/false/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "auto-external-false-test",
"dependencies": {
"ora": "8.0.1",
"react": "^18.3.1"
}
}
8 changes: 7 additions & 1 deletion e2e/cases/auto-external/false/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ export default defineConfig({
{
...generateBundleEsmConfig(__dirname),
autoExternal: false,
dts: {
bundle: true,
},
},
{
...generateBundleCjsConfig(__dirname),
autoExternal: false,
dts: {
bundle: true,
},
},
],
source: {
entry: {
main: '../__fixtures__/src/index.ts',
main: './src/index.ts',
},
},
});
7 changes: 7 additions & 0 deletions e2e/cases/auto-external/false/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { oraPromise } from 'ora';
import React from 'react';

export type { oraPromise };
export const foo = () => {
return React.version;
};
8 changes: 8 additions & 0 deletions e2e/cases/auto-external/false/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"strict": true,
"esModuleInterop": true
},
"include": ["src/**/*"]
}
23 changes: 16 additions & 7 deletions e2e/cases/auto-external/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { join } from 'node:path';
import { buildAndGetResults } from '@e2e/helper';
import { buildAndGetAllResults, buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';

test('auto external default should works', async () => {
const fixturePath = join(__dirname, 'default');
const { entries } = await buildAndGetResults(fixturePath);
const { js, dts } = await buildAndGetAllResults(fixturePath);

expect(entries.esm).toContain(
expect(js.entries.esm).toContain(
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
);

expect(entries.cjs).toContain(
expect(js!.entries.cjs).toContain(
'var external_react_namespaceObject = require("react");',
);

// dts should externalized
expect(dts.entries.esm).toContain("import type { oraPromise } from 'ora';");
expect(dts.entries.cjs).toContain("import type { oraPromise } from 'ora';");
});

test('auto external sub path should works', async () => {
Expand All @@ -36,15 +40,20 @@ test('auto external sub path should works', async () => {

test('auto external false should works', async () => {
const fixturePath = join(__dirname, 'false');
const { entries } = await buildAndGetResults(fixturePath);
const { js, dts } = await buildAndGetAllResults(fixturePath);

expect(entries.esm).not.toContain(
expect(js.entries.esm).not.toContain(
'import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react"',
);

expect(entries.cjs).not.toContain(
expect(js.entries.cjs).not.toContain(
'var external_react_namespaceObject = require("react");',
);

// dts should bundled
expect(dts.entries.esm).toContain('export declare function oraPromise');

expect(dts.entries.cjs).toContain('export declare function oraPromise');
});

test('externals should overrides auto external', async () => {
Expand Down
49 changes: 44 additions & 5 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,20 @@ export async function getResults(
};
}

export const buildAndGetResults = async (
fixturePath: string,
type: 'js' | 'dts' = 'js',
): Promise<{
type BuildResult = {
contents: Record<string, Record<string, string>>;
files: Record<string, string[]>;
entries: Record<string, string>;
entryFiles: Record<string, string>;
rspackConfig: InspectConfigResult['origin']['bundlerConfigs'];
rsbuildConfig: InspectConfigResult['origin']['rsbuildConfig'];
isSuccess: boolean;
}> => {
};

export const buildAndGetResults = async (
fixturePath: string,
type: 'js' | 'dts' = 'js',
): Promise<BuildResult> => {
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
process.chdir(fixturePath);
const rsbuildInstance = await build(rslibConfig);
Expand All @@ -132,3 +134,40 @@ export const buildAndGetResults = async (
isSuccess: Boolean(rsbuildInstance),
};
};

export const buildAndGetAllResults = async (
fixturePath: string,
): Promise<{
js: BuildResult;
dts: BuildResult;
}> => {
const rslibConfig = await loadConfig(join(fixturePath, 'rslib.config.ts'));
process.chdir(fixturePath);
const rsbuildInstance = await build(rslibConfig);
const {
origin: { bundlerConfigs, rsbuildConfig },
} = await rsbuildInstance.inspectConfig({ verbose: true });

const jsResults = await getResults(rslibConfig, fixturePath, 'js');
const dtsResults = await getResults(rslibConfig, fixturePath, 'dts');
return {
js: {
contents: jsResults.contents,
files: jsResults.files,
entries: jsResults.entries,
entryFiles: jsResults.entryFiles,
rspackConfig: bundlerConfigs,
rsbuildConfig: rsbuildConfig,
isSuccess: Boolean(rsbuildInstance),
},
dts: {
contents: dtsResults.contents,
files: dtsResults.files,
entries: dtsResults.entries,
entryFiles: dtsResults.entryFiles,
rspackConfig: bundlerConfigs,
rsbuildConfig: rsbuildConfig,
isSuccess: Boolean(rsbuildInstance),
},
};
};
Loading

0 comments on commit c692412

Please sign in to comment.