Skip to content

test: add transformImport tests #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2024
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
6 changes: 3 additions & 3 deletions e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
| asset | ⚪️ | |
| autoExtension | 🟢 | |
| autoExternal | 🟢 | |
| banner-footer | ⚪️ | |
| banner-footer | 🟢 | |
| buildType | 🟢 | |
| copy | 🟢 | |
| decorator | ⚪️ | |
Expand All @@ -38,8 +38,8 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
| splitting | ⚪️ | |
| style | ⚪️ | |
| target | 🟢 | |
| transformImport | ⚪️ | |
| transformLodash | ⚪️ | |
| transformImport | 🟢 | |
| transformLodash | 🟢 | |
| tsconfig | ⚪️ | |
| tsconfigExtends | ⚪️ | |
| umdGlobals | ⚪️ | |
Expand Down
9 changes: 9 additions & 0 deletions e2e/cases/transform-import/arco-design/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "transform-import-arco-design-test",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"@arco-design/web-react": "^2.64.0"
}
}
49 changes: 49 additions & 0 deletions e2e/cases/transform-import/arco-design/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundle',
},
},
}),
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundleless',
},
},
bundle: false,
}),
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/cjs/bundle',
},
},
}),
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/cjs/bundleless',
},
},
bundle: false,
}),
],
source: {
entry: {
index: './src/index.ts',
},
transformImport: [
{
libraryName: '@arco-design/web-react',
style: true,
libraryDirectory: 'es',
},
],
},
});
3 changes: 3 additions & 0 deletions e2e/cases/transform-import/arco-design/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Button } from '@arco-design/web-react';

console.log(Button);
7 changes: 7 additions & 0 deletions e2e/cases/transform-import/arco-design/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
41 changes: 41 additions & 0 deletions e2e/cases/transform-import/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { join } from 'node:path';
import { type FormatType, buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';

test('transformImport with arco-design', async () => {
const fixturePath = join(__dirname, 'arco-design');
const { contents } = await buildAndGetResults(fixturePath);
const formats: FormatType[] = ['esm0', 'esm1', 'cjs0', 'cjs1'];

for (const format of formats) {
expect(Object.values(contents[format]!)[0]).toContain(
format.startsWith('esm')
? 'import * as __WEBPACK_EXTERNAL_MODULE__arco_design_web_react_es_button__ from "@arco-design/web-react/es/button"'
: 'var button_namespaceObject = require("@arco-design/web-react/es/button")',
);
expect(Object.values(contents[format]!)[0]).toContain(
format.startsWith('esm')
? 'import "@arco-design/web-react/es/button/style"'
: 'require("@arco-design/web-react/es/button/style")',
);
}
});

test('transformImport with lodash', async () => {
const fixturePath = join(__dirname, 'lodash');
const { contents } = await buildAndGetResults(fixturePath);
const formats: FormatType[] = ['esm0', 'esm1', 'cjs0', 'cjs1'];

for (const format of formats) {
expect(Object.values(contents[format]!)[0]).toContain(
format.startsWith('esm')
? 'import * as __WEBPACK_EXTERNAL_MODULE_lodash_get__ from "lodash/get"'
: 'var get_namespaceObject = require("lodash/get")',
);
expect(Object.values(contents[format]!)[0]).toContain(
format.startsWith('esm')
? 'import * as __WEBPACK_EXTERNAL_MODULE_lodash_fp_add__ from "lodash/fp/add"'
: 'var add_namespaceObject = require("lodash/fp/add")',
);
}
});
12 changes: 12 additions & 0 deletions e2e/cases/transform-import/lodash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "transform-import-lodash-test",
"version": "1.0.0",
"private": true,
"type": "module",
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/lodash": "^4.17.7"
}
}
53 changes: 53 additions & 0 deletions e2e/cases/transform-import/lodash/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { generateBundleCjsConfig, generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundle',
},
},
}),
generateBundleEsmConfig({
output: {
distPath: {
root: './dist/esm/bundleless',
},
},
bundle: false,
}),
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/cjs/bundle',
},
},
}),
generateBundleCjsConfig({
output: {
distPath: {
root: './dist/cjs/bundleless',
},
},
bundle: false,
}),
],
source: {
entry: {
index: './src/index.ts',
},
transformImport: [
{
libraryName: 'lodash',
customName: 'lodash/{{ member }}',
},
// Rsbuild customName only support string type for performance reasons
{
libraryName: 'lodash/fp',
customName: 'lodash/fp/{{ member }}',
},
],
},
});
7 changes: 7 additions & 0 deletions e2e/cases/transform-import/lodash/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { get } from 'lodash';
import { add } from 'lodash/fp';

var object = { a: [{ b: { c: 'foo' } }] };

export const addOne = add(1);
export const foo = get(object, 'a[0].b.c');
7 changes: 7 additions & 0 deletions e2e/cases/transform-import/lodash/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function generateBundleCjsConfig(config: LibConfig = {}): LibConfig {
return mergeConfig(cjsBasicConfig, config)!;
}

type FormatType = Format | `${Format}${number}`;
export type FormatType = Format | `${Format}${number}`;
type FilePath = string;

type BuildResult = {
Expand Down
Loading
Loading