diff --git a/.changeset/twenty-oranges-reply.md b/.changeset/twenty-oranges-reply.md new file mode 100644 index 000000000..2ea9b6975 --- /dev/null +++ b/.changeset/twenty-oranges-reply.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-striper': patch +--- + +feat: remove some packages for the browser build diff --git a/packages/vite-plugin-striper/src/lib/plugin.ts b/packages/vite-plugin-striper/src/lib/plugin.ts index ad5565b64..1c2b92b7a 100644 --- a/packages/vite-plugin-striper/src/lib/plugin.ts +++ b/packages/vite-plugin-striper/src/lib/plugin.ts @@ -5,6 +5,7 @@ import type { Plugin } from 'vite' import { watchAndRun } from 'vite-plugin-watch-and-run' import { transformDecorator } from './transformDecorator.js' +import { removePackages } from './transformPackage.js' import { transformWarningThrow, type WarningThrow } from './transformWarningThrow.js' export type ViteStriperOptions = { @@ -13,6 +14,11 @@ export type ViteStriperOptions = { */ decorators?: string[] + /** + * for example: `['mongodb']` + */ + packages?: string[] + /** * If true, skip warnings if a throw is not a class. * @@ -44,7 +50,7 @@ export type ViteStriperOptions = { * ``` * */ -export function striper(sCptions?: ViteStriperOptions): Plugin[] { +export function striper(options?: ViteStriperOptions): Plugin[] { const log = new Log('striper') let listOrThrow: WarningThrow[] = [] @@ -67,7 +73,7 @@ export function striper(sCptions?: ViteStriperOptions): Plugin[] { enforce: 'pre', config: async () => { - if (sCptions?.log_on_throw_is_not_a_new_class) { + if (options?.log_on_throw_is_not_a_new_class) { const files = getFilesUnder(getProjectPath()) listOrThrow = [] for (let i = 0; i < files.length; i++) { @@ -77,7 +83,7 @@ export function striper(sCptions?: ViteStriperOptions): Plugin[] { absolutePath, getProjectPath(), code, - sCptions?.log_on_throw_is_not_a_new_class, + options?.log_on_throw_is_not_a_new_class, ) listOrThrow.push(...list) } @@ -95,13 +101,13 @@ export function striper(sCptions?: ViteStriperOptions): Plugin[] { return } - if (sCptions && (sCptions?.decorators ?? []).length > 0) { - const { info, ...rest } = await transformDecorator(code, sCptions.decorators ?? []) + if (options && options?.decorators && options.decorators.length > 0) { + const { info, ...rest } = await transformDecorator(code, options.decorators) - if (sCptions?.debug && info.length > 0) { + if (options?.debug && info.length > 0) { log.info( `` + - `${gray('File :')} ${yellow(filepath)}\n` + + `${gray('File:')} ${yellow(filepath)}\n` + `${green('-----')}\n` + `${rest.code}` + `\n${green(':::::')}\n` + @@ -110,8 +116,23 @@ export function striper(sCptions?: ViteStriperOptions): Plugin[] { ``, ) } + } - return rest + if (options && options?.packages && options.packages.length > 0) { + const { info, ...rest } = await removePackages(code, options.packages) + + if (options?.debug && info.length > 0) { + log.info( + `` + + `${gray('File:')} ${yellow(filepath)}\n` + + `${green('-----')}\n` + + `${rest.code}` + + `\n${green(':::::')}\n` + + `${info}` + + `\n${green('-----')}` + + ``, + ) + } } return @@ -125,7 +146,7 @@ export function striper(sCptions?: ViteStriperOptions): Plugin[] { logs: [], watch: ['**'], run: async (server, absolutePath) => { - if (sCptions?.log_on_throw_is_not_a_new_class) { + if (options?.log_on_throw_is_not_a_new_class) { // Only file in our project if (absolutePath && absolutePath.startsWith(getProjectPath())) { const code = readFileSync(absolutePath, { encoding: 'utf8' }) @@ -134,7 +155,7 @@ export function striper(sCptions?: ViteStriperOptions): Plugin[] { absolutePath, getProjectPath(), code, - sCptions?.log_on_throw_is_not_a_new_class, + options?.log_on_throw_is_not_a_new_class, ) listOrThrow.push(...list) display() diff --git a/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts b/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts new file mode 100644 index 000000000..9f057eeba --- /dev/null +++ b/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts @@ -0,0 +1,53 @@ +import { describe, expect, it } from 'vitest' + +import { removePackages } from './transformPackage.js' + +describe('package', () => { + it('rmv lib', async () => { + const code = `import { ObjectId } from 'mongodb' + @Entity('tasks', { + allowApiCrud: true + }) + export class Task { + @Fields.string({ + valueConverter: { + fromDb: (x) => x?.toString(), + toDb: (x) => { + const r = new ObjectId(x) + console.log(r) + return r + } + } + }) + aMongoDbIdField = '' + } + ` + + const transformed = await removePackages(code, ['mongodb']) + + expect(transformed).toMatchInlineSnapshot(` + { + "code": "@Entity(\\"tasks\\", { + allowApiCrud: true + }) + export class Task { + @Fields.string({ + valueConverter: { + fromDb: x => x?.toString(), + + toDb: x => { + const r = new ObjectId(x); + console.log(r); + return r; + } + } + }) + aMongoDbIdField = \\"\\"; + }", + "info": [ + "Striped: 'mongodb'", + ], + } + `) + }) +}) diff --git a/packages/vite-plugin-striper/src/lib/transformPackage.ts b/packages/vite-plugin-striper/src/lib/transformPackage.ts new file mode 100644 index 000000000..d30d0b44b --- /dev/null +++ b/packages/vite-plugin-striper/src/lib/transformPackage.ts @@ -0,0 +1,34 @@ +import { parse } from '@babel/parser' +import * as recast from 'recast' + +const { visit } = recast.types + +export const removePackages = async (code: string, packages_to_strip: string[]) => { + try { + const ast = parse(code, { + plugins: ['typescript', 'decorators-legacy', 'importAssertions'], + sourceType: 'module', + }) + + const packages_striped: string[] = [] + + // Code to remove imports + visit(ast, { + visitImportDeclaration(path) { + const packageName = path.node.source.value + if (packages_to_strip.includes(String(packageName))) { + path.prune() + packages_striped.push(String(packageName)) + } + return false + }, + }) + + return { + code: recast.print(ast).code, + info: packages_striped.map(pkg => `Striped: '${pkg}'`), + } + } catch (error) { + return { code, info: [] } + } +}