Skip to content

Commit 4e60c6a

Browse files
committed
up
1 parent 9b2a12a commit 4e60c6a

File tree

8 files changed

+137
-31
lines changed

8 files changed

+137
-31
lines changed

pnpm-lock.yaml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 82 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,100 @@
1-
import path, { join } from 'node:path';
2-
import { buildAndGetResults } from 'test-helper';
1+
import fs from 'node:fs';
2+
import { join } from 'node:path';
3+
import { buildAndGetResults, queryContent } from 'test-helper';
34
import { describe, expect, test } from 'vitest';
45

56
describe('shebang', async () => {
6-
const fixturePath = join(__dirname, 'shebang/bundle');
7-
const { entries } = await buildAndGetResults({ fixturePath });
7+
const fixturePath = join(__dirname, 'shebang');
8+
const { entries, contents, entryFiles } = await buildAndGetResults({
9+
fixturePath,
10+
});
11+
12+
describe('bundle', async () => {
13+
test('shebang at the beginning', async () => {
14+
expect(entries.esm0!.startsWith('#!/usr/bin/env node')).toBe(true);
15+
});
16+
17+
test('shebang at the beginning even if minified', async () => {
18+
expect(entries.esm1!.startsWith('#!/usr/bin/env node')).toBe(true);
19+
});
20+
});
21+
22+
describe('bundle-false', async () => {
23+
test('shebang at the beginning', async () => {
24+
const index = queryContent(contents.esm2!, 'index.js', {
25+
basename: true,
26+
});
27+
expect(index!.startsWith('#!/usr/bin/env node')).toBe(true);
28+
29+
const bar = queryContent(contents.esm2!, 'bar.js', { basename: true });
30+
expect(bar!.startsWith('#!/usr/bin/env node')).toBe(true);
31+
32+
const foo = queryContent(contents.esm2!, 'foo.js', { basename: true });
33+
expect(foo!.includes('#!')).toBe(false);
34+
});
35+
36+
test('shebang at the beginning even if minified', async () => {
37+
const index = queryContent(contents.esm3!, 'index.js', {
38+
basename: true,
39+
});
40+
expect(index!.startsWith('#!/usr/bin/env node')).toBe(true);
841

9-
test('shebang at the beginning', async () => {
10-
expect(entries.esm0!.startsWith('#!/usr/bin/env node')).toBe(true);
42+
const bar = queryContent(contents.esm3!, 'bar.js', {
43+
basename: true,
44+
});
45+
expect(bar!.startsWith('#!/usr/bin/env node')).toBe(true);
46+
47+
const foo = queryContent(contents.esm2!, 'foo.js', { basename: true });
48+
expect(foo!.includes('#!')).toBe(false);
49+
});
50+
51+
test.todo('shebang commented by JS parser should be striped', async () => {
52+
const index = queryContent(contents.esm3!, 'index.js', {
53+
basename: true,
54+
});
55+
expect(index!.includes('//#!')).toBe(false);
56+
57+
const bar = queryContent(contents.esm3!, 'bar.js', {
58+
basename: true,
59+
});
60+
expect(bar!.includes('//#!')).toBe(false);
61+
});
1162
});
1263

13-
test('shebang at the beginning even if minified', async () => {
14-
expect(entries.esm1!.startsWith('#!/usr/bin/env node')).toBe(true);
64+
describe('chmod', async () => {
65+
test('shebang at the beginning', async () => {
66+
const filePath = entryFiles.esm0!;
67+
const fileStats = fs.statSync(filePath);
68+
expect(fileStats.mode).toBe(0o100755);
69+
});
70+
71+
test('shebang at the beginning even if minified', async () => {
72+
const filePath = entryFiles.esm1!;
73+
const fileStats = fs.statSync(filePath);
74+
expect(fileStats.mode).toBe(0o100755);
75+
});
1576
});
1677
});
1778

1879
describe('react', async () => {
1980
const fixturePath = join(__dirname, 'react/bundleless');
2081
const { contents } = await buildAndGetResults({ fixturePath });
21-
test('React directive at the beginning', async () => {
22-
const bar = Object.entries(contents.esm0!).find(([key]) => {
23-
return path.basename(key) === 'bar.js';
24-
})?.[1];
25-
26-
expect(bar!.startsWith(`'use server';`)).toBe(true);
27-
28-
const foo = Object.entries(contents.esm0!).find(([key]) => {
29-
return path.basename(key) === 'foo.js';
30-
})?.[1];
31-
32-
expect(foo!.startsWith(`'use client';`)).toBe(true);
33-
});
3482

35-
test('React directive at the beginning even if minified', async () => {
36-
const bar = Object.entries(contents.esm1!).find(([key]) => {
37-
return path.basename(key) === 'bar.js';
38-
})?.[1];
83+
describe('bundle-false', async () => {
84+
test('React directive at the beginning', async () => {
85+
const foo = queryContent(contents.esm0!, 'foo.js', { basename: true });
86+
expect(foo!.startsWith(`'use client';`)).toBe(true);
3987

40-
expect(bar!.startsWith(`'use server';`)).toBe(true);
88+
const bar = queryContent(contents.esm0!, 'bar.js', { basename: true });
89+
expect(bar!.startsWith(`'use server';`)).toBe(true);
90+
});
4191

42-
const foo = Object.entries(contents.esm1!).find(([key]) => {
43-
return path.basename(key) === 'foo.js';
44-
})?.[1];
92+
test('React directive at the beginning even if minified', async () => {
93+
const foo = queryContent(contents.esm1!, 'foo.js', { basename: true });
94+
expect(foo!.startsWith(`'use client';`)).toBe(true);
4595

46-
expect(foo!.startsWith(`'use client';`)).toBe(true);
96+
const bar = queryContent(contents.esm1!, 'bar.js', { basename: true });
97+
expect(bar!.startsWith(`'use server';`)).toBe(true);
98+
});
4799
});
48100
});

tests/integration/directive/shebang/bundle/rslib.config.ts renamed to tests/integration/directive/shebang/rslib.config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ const esmShared = {
99
},
1010
};
1111

12+
const esmSharedBundleFalse = {
13+
bundle: false,
14+
source: {
15+
entry: {
16+
index: './src/*',
17+
},
18+
},
19+
};
20+
1221
export default defineConfig({
1322
lib: [
1423
generateBundleEsmConfig({
@@ -30,6 +39,25 @@ export default defineConfig({
3039
},
3140
},
3241
}),
42+
generateBundleEsmConfig({
43+
...esmSharedBundleFalse,
44+
shims: { esm: { __dirname: true, __filename: true } },
45+
output: {
46+
distPath: {
47+
root: './dist/bundle-false/esm0',
48+
},
49+
},
50+
}),
51+
generateBundleEsmConfig({
52+
...esmSharedBundleFalse,
53+
shims: { esm: { __dirname: true, __filename: true } },
54+
output: {
55+
minify: true,
56+
distPath: {
57+
root: './dist/bundle-false/esm1',
58+
},
59+
},
60+
}),
3361
],
3462
output: {
3563
target: 'node',

tests/scripts/shared.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import assert from 'node:assert';
22
import fs from 'node:fs';
3-
import { dirname, join, normalize } from 'node:path';
3+
import path, { dirname, join, normalize } from 'node:path';
44
import { fileURLToPath } from 'node:url';
55
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
66
import {
@@ -319,3 +319,25 @@ export function getFileBySuffix(
319319
assert(content);
320320
return content;
321321
}
322+
323+
export function queryContent(
324+
contents: Record<string, string>,
325+
query: string | RegExp,
326+
options: {
327+
basename?: boolean;
328+
} = {},
329+
): string | null {
330+
const basename = options?.basename ?? false;
331+
const matched = Object.entries(contents).find(([key]) => {
332+
const toQueried = basename ? path.basename(key) : key;
333+
return typeof query === 'string'
334+
? toQueried === query
335+
: query.test(toQueried);
336+
});
337+
338+
if (!matched) {
339+
return null;
340+
}
341+
342+
return matched[1];
343+
}

0 commit comments

Comments
 (0)