Skip to content
This repository was archived by the owner on Oct 9, 2024. It is now read-only.

Commit 8ec18b4

Browse files
author
Nejc
committed
feat: added parse options
1 parent c0bc9d0 commit 8ec18b4

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@nejcm/rollup-plugin-size-snapshot-vite",
33
"private": false,
4-
"version": "0.0.7",
4+
"version": "0.0.8",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"files": [

src/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Options as AcornOptions } from 'acorn';
12
import bytes from 'bytes';
23
import chalk from 'chalk';
34
import gzipSize from 'gzip-size';
@@ -7,12 +8,11 @@ import * as snapshot from './snapshot';
78
import { treeshakeWithRollup } from './treeshakeWithRollup';
89

910
type Options = {
10-
minify?: boolean;
11-
snapshot?: string;
1211
snapshotPath?: string;
1312
matchSnapshot?: boolean;
1413
threshold?: number;
1514
printInfo?: boolean;
15+
parseOptions?: AcornOptions;
1616
};
1717

1818
type OutputOptions = {
@@ -42,6 +42,7 @@ const validateOptions = (options: any) => {
4242
'matchSnapshot',
4343
'threshold',
4444
'printInfo',
45+
'parseOptions',
4546
];
4647

4748
const invalidKeys = Object.keys(options).filter(
@@ -86,7 +87,7 @@ export const sizeSnapshot = (options: Options = {}): Plugin => {
8687

8788
const minified = (await minify(source)).code || '';
8889
const treeshakeSize = (code: string) =>
89-
Promise.all([treeshakeWithRollup(code)]);
90+
Promise.all([treeshakeWithRollup(code, options.parseOptions)]);
9091

9192
return Promise.all([
9293
gzipSize(minified),

src/treeshakeWithRollup.ts

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import replace from '@rollup/plugin-replace';
2-
import { parse } from 'acorn';
2+
import { Options, parse } from 'acorn';
33
import { rollup } from 'rollup';
44
import { minify } from 'terser';
55

@@ -32,7 +32,10 @@ const resolvePlugin = ({ code }) => ({
3232
},
3333
});
3434

35-
export const treeshakeWithRollup = (code: string): Promise<Output> => {
35+
export const treeshakeWithRollup = (
36+
code: string,
37+
parseOptions?: Options,
38+
): Promise<Output> => {
3639
const config = {
3740
name: 'rollup-plugin-size-snapshot-vite',
3841
input: `/${inputName}`,
@@ -54,21 +57,23 @@ export const treeshakeWithRollup = (code: string): Promise<Output> => {
5457
},
5558
),
5659
)
57-
.then(
58-
(result): Output => {
59-
const { code = '' } = result;
60-
const ast = parse(code, { sourceType: 'module' }) as any;
61-
const import_statements = ast.body
62-
// collect all toplevel import statements
63-
.filter((node) => node.type === 'ImportDeclaration')
64-
// endpos is the next character after node -> substract 1
65-
.map((node) => node.end - node.start)
66-
.reduce((acc, size) => acc + size, 0);
60+
.then((result): Output => {
61+
const { code = '' } = result;
62+
const ast = parse(code, {
63+
sourceType: 'module',
64+
ecmaVersion: 11,
65+
...parseOptions,
66+
}) as any;
67+
const import_statements = ast.body
68+
// collect all toplevel import statements
69+
.filter((node) => node.type === 'ImportDeclaration')
70+
// endpos is the next character after node -> substract 1
71+
.map((node) => node.end - node.start)
72+
.reduce((acc, size) => acc + size, 0);
6773

68-
return {
69-
code: code.length,
70-
import_statements,
71-
};
72-
},
73-
);
74+
return {
75+
code: code.length,
76+
import_statements,
77+
};
78+
});
7479
};

0 commit comments

Comments
 (0)