|
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'; |
3 | 4 | import { describe, expect, test } from 'vitest'; |
4 | 5 |
|
5 | 6 | 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); |
8 | 41 |
|
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 | + }); |
11 | 62 | }); |
12 | 63 |
|
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 | + }); |
15 | 76 | }); |
16 | 77 | }); |
17 | 78 |
|
18 | 79 | describe('react', async () => { |
19 | 80 | const fixturePath = join(__dirname, 'react/bundleless'); |
20 | 81 | 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 | | - }); |
34 | 82 |
|
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); |
39 | 87 |
|
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 | + }); |
41 | 91 |
|
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); |
45 | 95 |
|
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 | + }); |
47 | 99 | }); |
48 | 100 | }); |
0 commit comments