Skip to content

Commit

Permalink
feat: add loader option
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Feb 8, 2024
1 parent ce293b3 commit 4bfd664
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,38 @@ Because this extension builds locally, you need to pre-install dependencies.
"type": "string",
"default": "#e33",
"description": "Danger color for the size text"
},
"bundleSize.loader": {
"type": "object",
"patternProperties": {
".*": {
"type": "string",
"enum": [
"base64",
"binary",
"copy",
"css",
"dataurl",
"default",
"empty",
"file",
"js",
"json",
"jsx",
"local-css",
"text",
"ts",
"tsx"
]
}
},
"default": {},
"markdownDescription": "Override the loader in Bundle Size (eg: `.js: jsx` will force `.js` files to be treated as JSX), see [esbuild#loader](https://esbuild.github.io/api/#loader) for more details"
},
"bundleSize.flowPattern": {
"type": "string",
"default": "\\/node_modules\\/(@react-native|react-native|react-native-linear-gradient)\\/(.*)\\.js$",
"description": "The regexp pattern to match files containing the Flow type"
}
}
```
Expand Down
1 change: 1 addition & 0 deletions measure-bundle-size/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type MeasureOptions = {
stats?: boolean | 'tree' | 'table'
workspaceFolder?: string
flowPattern?: RegExp
loader?: esbuild.BuildOptions['loader']
}

// Lazy async generator API
Expand Down
26 changes: 24 additions & 2 deletions measure-bundle-size/src/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ const bundle = async (
baseDir,
projectPkgFile,
flowPattern,
loader,
stats: statsOpt = false,
cache: cacheOpt = false,
}: {
Expand All @@ -169,6 +170,7 @@ const bundle = async (
stats?: StatsOpt
cache?: boolean
flowPattern?: RegExp
loader?: esbuild.BuildOptions['loader']
}
): Promise<BundleResult> => {
const bundleMark = timeMark<'bundle' | 'zip' | 'analyze'>()
Expand Down Expand Up @@ -251,6 +253,7 @@ const bundle = async (
'.png': 'empty',
'.webp': 'empty',
'.gif': 'empty',
...loader,
},
external,
target: 'esnext',
Expand Down Expand Up @@ -356,6 +359,10 @@ type MeasureOptions = {
* the files pattern to strip flow types
*/
flowPattern?: RegExp
/**
* custom loader for esbuild
*/
loader?: esbuild.BuildOptions['loader']
}

/**
Expand All @@ -364,7 +371,15 @@ type MeasureOptions = {
export async function* measureIterable(
input: string,
fileName?: string | null,
{debug, log, stats, cache, workspaceFolder, flowPattern}: MeasureOptions = {}
{
debug,
log,
stats,
cache,
workspaceFolder,
flowPattern,
loader,
}: MeasureOptions = {}
): AsyncGenerator<MeasureResult> {
if (debug) {
logger.enabled = true
Expand All @@ -391,7 +406,14 @@ export async function* measureIterable(
parseMark.end('parse')
logger(parseMark.print())

const bundleOpts = {baseDir, projectPkgFile, stats, cache, flowPattern}
const bundleOpts = {
baseDir,
projectPkgFile,
stats,
cache,
flowPattern,
loader,
}
for (const importInfo of result.imports) {
const statement = input.substring(importInfo.start, importInfo.end)
// await new Promise((resolve) => setTimeout(resolve, 500))
Expand Down
27 changes: 27 additions & 0 deletions vscode-bundle-size/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,33 @@
"default": "#e33",
"description": "Danger color for the size text"
},
"bundleSize.loader": {
"type": "object",
"patternProperties": {
".*": {
"type": "string",
"enum": [
"base64",
"binary",
"copy",
"css",
"dataurl",
"default",
"empty",
"file",
"js",
"json",
"jsx",
"local-css",
"text",
"ts",
"tsx"
]
}
},
"default": {},
"markdownDescription": "Override the loader in Bundle Size (eg: `.js: jsx` will force `.js` files to be treated as JSX), see [esbuild#loader](https://esbuild.github.io/api/#loader) for more details"
},
"bundleSize.flowPattern": {
"type": "string",
"default": "\\/node_modules\\/(@react-native|react-native|react-native-linear-gradient)\\/(.*)\\.js$",
Expand Down
1 change: 1 addition & 0 deletions vscode-bundle-size/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async function processDocument(document?: vscode.TextDocument) {
log: channelLog,
workspaceFolder: workspaceFolder?.uri.fsPath,
flowPattern: flowPattern ? new RegExp(flowPattern) : undefined,
loader: config.get('loader'),
})) {
if (editor !== vscode.window.activeTextEditor) {
break
Expand Down

0 comments on commit 4bfd664

Please sign in to comment.