Skip to content

Commit

Permalink
test: add sourcemap tests (#187)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Sep 9, 2024
1 parent d842231 commit a7bd23d
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 5 deletions.
2 changes: 1 addition & 1 deletion e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Rslib will try to cover the common scenarios in the [integration test cases of M
| shims | 🟡 | Support shims `__filename` and `__dirname` in esm</br> `import.meta.url` in cjs need to be supported |
| sideEffects | ⚪️ | |
| sourceDir | ⚪️ | |
| sourceMap | ⚪️ | |
| sourceMap | 🟢 | |
| splitting | ⚪️ | |
| style | ⚪️ | |
| target | 🟢 | |
Expand Down
1 change: 1 addition & 0 deletions e2e/cases/sourcemap/__fixtures__/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
7 changes: 7 additions & 0 deletions e2e/cases/sourcemap/__fixtures__/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
6 changes: 6 additions & 0 deletions e2e/cases/sourcemap/default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "sourcemap-default-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
11 changes: 11 additions & 0 deletions e2e/cases/sourcemap/default/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig()],
source: {
entry: {
index: '../__fixtures__/src/index.ts',
},
},
});
6 changes: 6 additions & 0 deletions e2e/cases/sourcemap/external/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "sourcemap-external-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
16 changes: 16 additions & 0 deletions e2e/cases/sourcemap/external/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig()],
source: {
entry: {
index: '../__fixtures__/src/index.ts',
},
},
output: {
sourceMap: {
js: 'cheap-module-source-map',
},
},
});
62 changes: 62 additions & 0 deletions e2e/cases/sourcemap/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os from 'node:os';
import { join } from 'node:path';
import { buildAndGetResults } from '@e2e/helper';
import { expect, test } from 'vitest';

test('should not generate js sourcemap by default', async () => {
const fixturePath = join(__dirname, 'default');
const { contents } = await buildAndGetResults(fixturePath, 'js');
const files = Object.keys(contents.esm);

expect(files).toMatchInlineSnapshot(`
[
"<ROOT>/e2e/cases/sourcemap/default/dist/esm/index.js",
]
`);
});

test('should generate js external sourcemap: cheap-module-source-map', async () => {
const fixturePath = join(__dirname, 'external');
const { contents } = await buildAndGetResults(fixturePath, 'js');
const files = Object.keys(contents.esm);

expect(files).toMatchInlineSnapshot(`
[
"<ROOT>/e2e/cases/sourcemap/external/dist/esm/index.js",
"<ROOT>/e2e/cases/sourcemap/external/dist/esm/index.js.map",
]
`);
});

test('should generate js inline sourcemap: inline-cheap-module-source-map', async () => {
const fixturePath = join(__dirname, 'inline');
const { contents } = await buildAndGetResults(fixturePath, 'js');
const files = Object.keys(contents.esm);
const code = Object.values(contents.esm);

expect(files).toMatchInlineSnapshot(`
[
"<ROOT>/e2e/cases/sourcemap/inline/dist/esm/index.js",
]
`);

if (os.platform() === 'win32') {
expect(code).toMatchInlineSnapshot(`
[
"const foo = 'foo';
export { foo };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9zb3VyY2VtYXAtaW5saW5lLXRlc3QvLi4vX19maXh0dXJlc19fL3NyYy9pbmRleC50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgY29uc3QgZm9vID0gJ2Zvbyc7XHJcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9",
]
`);
} else {
expect(code).toMatchInlineSnapshot(`
[
"const foo = 'foo';
export { foo };
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly9zb3VyY2VtYXAtaW5saW5lLXRlc3QvLi4vX19maXh0dXJlc19fL3NyYy9pbmRleC50cyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgY29uc3QgZm9vID0gJ2Zvbyc7XG4iXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEifQ==",
]
`);
}
});
6 changes: 6 additions & 0 deletions e2e/cases/sourcemap/inline/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "sourcemap-inline-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
16 changes: 16 additions & 0 deletions e2e/cases/sourcemap/inline/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { generateBundleEsmConfig } from '@e2e/helper';
import { defineConfig } from '@rslib/core';

export default defineConfig({
lib: [generateBundleEsmConfig()],
source: {
entry: {
index: '../__fixtures__/src/index.ts',
},
},
output: {
sourceMap: {
js: 'inline-cheap-module-source-map',
},
},
});
7 changes: 3 additions & 4 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ export async function getResults(

const regex =
type === 'dts'
? /\.d.(ts|cts|mts)$/
? /\.d.(ts|cts|mts)(\.map)?$/
: type === 'css'
? /\.css$/
: /\.(js|cjs|mjs)$/;
? /\.css(\.map)?$/
: /\.(js|cjs|mjs)(\.map)?$/;

const content: Record<string, string> = await globContentJSON(globFolder, {
absolute: true,
ignore: ['**/*.map'],
});

const fileSet = Object.keys(content)
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7bd23d

Please sign in to comment.