Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/twenty-oranges-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-striper': patch
---

feat: remove some packages for the browser build
41 changes: 31 additions & 10 deletions packages/vite-plugin-striper/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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.
*
Expand Down Expand Up @@ -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[] = []

Expand All @@ -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++) {
Expand All @@ -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)
}
Expand All @@ -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` +
Expand All @@ -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
Expand All @@ -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' })
Expand All @@ -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()
Expand Down
53 changes: 53 additions & 0 deletions packages/vite-plugin-striper/src/lib/transformPackage.spec.ts
Original file line number Diff line number Diff line change
@@ -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'",
],
}
`)
})
})
34 changes: 34 additions & 0 deletions packages/vite-plugin-striper/src/lib/transformPackage.ts
Original file line number Diff line number Diff line change
@@ -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: [] }
}
}