Skip to content

Commit ce3836b

Browse files
committed
test: Add vite package test
1 parent 8c7f8c2 commit ce3836b

File tree

7 files changed

+116
-1
lines changed

7 files changed

+116
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
"build": "tsc && rollup -c rollup.config.mjs",
5151
"lint": "exit 0",
5252
"test:unit": "vitest test/unit",
53-
"test:package": "cd test/package && vitest",
53+
"test:package": "npm run test:package:rollup && npm run test:package:vite",
54+
"test:package:rollup": "cd test/package/rollup && vitest",
55+
"test:package:vite": "cd test/package/vite && vitest",
5456
"bump": "./scripts/bump.sh",
5557
"release": "./scripts/release.sh"
5658
},

test/package/vite/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package-lock.json
2+
dist*

test/package/vite/case-options.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { basename } from 'path';
2+
import { defineConfig } from 'vite';
3+
import webpackStats from 'rollup-plugin-webpack-stats';
4+
5+
const baseConfig = {
6+
build: {
7+
outDir: 'dist',
8+
rollupOptions: {
9+
output: {
10+
assetFileNames: 'assets/[name][extname]',
11+
chunkFileNames: 'assets/[name].js',
12+
entryFileNames: 'assets/[name].js',
13+
},
14+
},
15+
},
16+
};
17+
18+
export default defineConfig([
19+
{
20+
...baseConfig,
21+
plugins: [webpackStats()],
22+
},
23+
{
24+
...baseConfig,
25+
build: {
26+
...baseConfig.build,
27+
},
28+
plugins: [
29+
// A contrived demo to show that plugin options can access vite outputOptions
30+
webpackStats((outputOptions) => {
31+
return {
32+
fileName: `stats-${basename(outputOptions.dir)}.json`,
33+
};
34+
}),
35+
],
36+
},
37+
]);

test/package/vite/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>rollup-plugin-stats</title>
6+
</head>
7+
<body>
8+
<script type="module" src="./src/index.js"></script>
9+
</body>
10+
</html>

test/package/vite/options.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
import { beforeEach, describe, test, expect } from 'vitest';
4+
import { build as vite } from 'vite';
5+
import { vol } from 'memfs';
6+
7+
import viteConfigs from './case-options.mjs';
8+
9+
describe('vite options', () => {
10+
beforeEach(() => {
11+
vol.reset();
12+
});
13+
14+
test('should output bundle stats JSON file when options is an object', async () => {
15+
const config = viteConfigs[0];
16+
await vite(config);
17+
18+
const actual = await fs.readFile(path.join(config.output.dir, 'webpack-stats.json'), 'utf8');
19+
const stats = JSON.parse(actual);
20+
expect(stats).toMatchObject({
21+
assets: [
22+
{
23+
name: 'assets/index.js',
24+
size: 739,
25+
},
26+
],
27+
})
28+
});
29+
30+
test('should output bundle stats JSON file when options is a builder function', async () => {
31+
const config = viteConfigs[1];
32+
await vite(config);
33+
34+
const actual = await fs.readFile(path.join(config.build.outDir, 'stats-dist2.json'), 'utf8');
35+
const stats = JSON.parse(actual);
36+
expect(stats).toMatchObject({
37+
assets: [
38+
{
39+
name: 'assets/index.js',
40+
size: 739,
41+
},
42+
],
43+
})
44+
});
45+
});

test/package/vite/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "rollup-plugin-webpack-stats-test-vite",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "vitest"
8+
},
9+
"keywords": [],
10+
"license": "ISC",
11+
"devDependencies": {
12+
"rollup-plugin-webpack-stats": "*",
13+
"vite": "^7.0.0"
14+
}
15+
}

test/package/vite/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(() => {
2+
const message = 'hello world!';
3+
console.log(message);
4+
})();

0 commit comments

Comments
 (0)