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/good-dolphins-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-striper': patch
---

remove browser package, update strategy to replace to const
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
# nodeVersion: 18

- name: 🚧 Run nx build
run: pnpm nx:build
run: pnpm build

- name: ➕ Create template
run:
Expand Down
7 changes: 5 additions & 2 deletions packages/vite-plugin-striper/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,17 @@ export function striper(options?: ViteStriperOptions): Plugin[] {
if (options && options?.decorators && options.decorators.length > 0) {
const { info, ...rest } = await transformDecorator(code, options.decorators)

// Update the code for later transforms
code = rest.code

if (options?.debug && info.length > 0) {
log.info(
`` +
`${gray('File:')} ${yellow(filepath)}\n` +
`${green('-----')}\n` +
`${rest.code}` +
`\n${green(':::::')}\n` +
`${info}` +
`${info.join('\n')}` +
`\n${green('-----')}` +
``,
)
Expand All @@ -128,7 +131,7 @@ export function striper(options?: ViteStriperOptions): Plugin[] {
`${green('-----')}\n` +
`${rest.code}` +
`\n${green(':::::')}\n` +
`${info}` +
`${info.join('\n')}` +
`\n${green('-----')}` +
``,
)
Expand Down
57 changes: 54 additions & 3 deletions packages/vite-plugin-striper/src/lib/transformPackage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'
import { removePackages } from './transformPackage.js'

describe('package', () => {
it('rmv lib', async () => {
it('1 replace', async () => {
const code = `import { ObjectId } from 'mongodb'
@Entity('tasks', {
allowApiCrud: true
Expand All @@ -27,7 +27,9 @@ describe('package', () => {

expect(transformed).toMatchInlineSnapshot(`
{
"code": "@Entity(\\"tasks\\", {
"code": "const ObjectId = null;

@Entity(\\"tasks\\", {
allowApiCrud: true
})
export class Task {
Expand All @@ -45,7 +47,56 @@ describe('package', () => {
aMongoDbIdField = \\"\\";
}",
"info": [
"Striped: 'mongodb'",
"Replaced import from 'mongodb'",
],
}
`)
})

it('2 replaces', async () => {
const code = `import { ObjectId, demo } from 'mongodb'
@Entity('tasks', {
allowApiCrud: true
})
export class Task {
@Fields.string({
valueConverter: {
fromDb: (x) => x?.toString(),
toDb: (x) => {
const r = new ObjectId(x)
const u = demo
}
}
})
aMongoDbIdField = ''
}
`

const transformed = await removePackages(code, ['mongodb'])

expect(transformed).toMatchInlineSnapshot(`
{
"code": "const ObjectId = null;
const demo = null;

@Entity(\\"tasks\\", {
allowApiCrud: true
})
export class Task {
@Fields.string({
valueConverter: {
fromDb: x => x?.toString(),

toDb: x => {
const r = new ObjectId(x);
const u = demo;
}
}
})
aMongoDbIdField = \\"\\";
}",
"info": [
"Replaced import from 'mongodb'",
],
}
`)
Expand Down
30 changes: 25 additions & 5 deletions packages/vite-plugin-striper/src/lib/transformPackage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { parse } from '@babel/parser'
import * as recast from 'recast'

const { visit } = recast.types
const {
visit,
types: { builders },
} = recast

export const removePackages = async (code: string, packages_to_strip: string[]) => {
try {
Expand All @@ -12,21 +15,38 @@ export const removePackages = async (code: string, packages_to_strip: string[])

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))
const specifiers = path.node.specifiers!
const replacementNodes = specifiers
.map(specifier => {
if (specifier.type === 'ImportSpecifier') {
return builders.variableDeclaration('const', [
builders.variableDeclarator(
builders.identifier(String(specifier.imported.name)),
builders.literal(null),
),
])
}
})
.filter(Boolean) // Remove undefined values

if (replacementNodes.length > 0) {
path.replace(...replacementNodes)
packages_striped.push(String(packageName))
} else {
path.prune()
}
}
return false
},
})

return {
code: recast.print(ast).code,
info: packages_striped.map(pkg => `Striped: '${pkg}'`),
info: packages_striped.map(pkg => `Replaced import from '${pkg}'`),
}
} catch (error) {
return { code, info: [] }
Expand Down