Skip to content

Commit

Permalink
fix: can not collect grant when minify (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Jul 21, 2024
1 parent 767d207 commit 23e0713
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
33 changes: 20 additions & 13 deletions packages/vite-plugin-monkey/src/node/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readFileSync } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import type { OutputBundle, PluginContext } from 'rollup';
import type { OutputBundle, PluginContext, OutputChunk } from 'rollup';
import { normalizePath, transformWithEsbuild } from 'vite';
import { logger } from './_logger';
import { FinalMonkeyOption } from './types';
Expand Down Expand Up @@ -308,19 +308,26 @@ export async function* walk(dirPath: string) {

export const collectGrant = (
context: PluginContext,
bundleOrCode: OutputBundle | string | string[],
chunks: OutputChunk[],
injectCssCode: string | undefined,
minify: boolean,
): Set<string> => {
const codes: string[] = [];
if (typeof bundleOrCode == 'string') {
codes.push(bundleOrCode);
} else if (Array.isArray(bundleOrCode)) {
codes.push(...bundleOrCode);
} else {
Object.values(bundleOrCode).forEach((chunk) => {
if (chunk.type == 'chunk') {
codes.push(chunk.code);
}
});
const codes = new Set<string>();
if (injectCssCode) {
codes.add(injectCssCode);
}
for (const chunk of chunks) {
if (minify) {
// https://github.com/lisonge/vite-plugin-monkey/issues/166
const modules = Object.values(chunk.modules);
modules.forEach((m) => {
const code = m.code;
if (code) {
codes.add(code);
}
});
}
codes.add(chunk.code);
}
const unusedMembers = new Set(GM_keywords.filter((s) => s.includes(`.`)));
const unusedIdentifiers = new Set(
Expand Down
10 changes: 8 additions & 2 deletions packages/vite-plugin-monkey/src/node/plugins/finalBundle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OutputChunk, RollupOutput } from 'rollup';
import { Plugin, build } from 'vite';
import { Plugin, ResolvedConfig, build } from 'vite';
import { lazyValue } from '../_lazy';
import {
collectGrant,
Expand All @@ -23,10 +23,14 @@ const polyfillId = '\0vite/legacy-polyfills';
const systemJsImportMapPrefix = `user`;

export const finalBundlePlugin = (finalOption: FinalMonkeyOption): Plugin => {
let viteConfig: ResolvedConfig;
return {
name: 'monkey:finalBundle',
apply: 'build',
enforce: 'post',
async configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
},
async generateBundle(_, rawBundle) {
const entryChunks: OutputChunk[] = [];
const chunks: OutputChunk[] = [];
Expand Down Expand Up @@ -213,7 +217,9 @@ export const finalBundlePlugin = (finalOption: FinalMonkeyOption): Plugin => {
if (finalOption.build.autoGrant) {
collectGrantSet = collectGrant(
this,
chunks.map((s) => s.code).concat(injectCssCode || ``),
chunks,
injectCssCode,
viteConfig.build.minify !== false,
);
} else {
collectGrantSet = new Set<string>();
Expand Down

0 comments on commit 23e0713

Please sign in to comment.