From 3256aeb4ab9200eae2f29fef2b3a95cd62b54775 Mon Sep 17 00:00:00 2001 From: jycouet Date: Thu, 14 Dec 2023 23:44:09 +0100 Subject: [PATCH 1/2] :zap: IMPROVE: strategy to remove package --- .changeset/good-dolphins-heal.md | 5 ++ .../vite-plugin-striper/src/lib/plugin.ts | 7 ++- .../src/lib/transformPackage.spec.ts | 57 ++++++++++++++++++- .../src/lib/transformPackage.ts | 30 ++++++++-- 4 files changed, 89 insertions(+), 10 deletions(-) create mode 100644 .changeset/good-dolphins-heal.md diff --git a/.changeset/good-dolphins-heal.md b/.changeset/good-dolphins-heal.md new file mode 100644 index 000000000..a77d447e1 --- /dev/null +++ b/.changeset/good-dolphins-heal.md @@ -0,0 +1,5 @@ +--- +'vite-plugin-striper': patch +--- + +remove browser package, update strategy to replace to const diff --git a/packages/vite-plugin-striper/src/lib/plugin.ts b/packages/vite-plugin-striper/src/lib/plugin.ts index 1c2b92b7a..2193931d2 100644 --- a/packages/vite-plugin-striper/src/lib/plugin.ts +++ b/packages/vite-plugin-striper/src/lib/plugin.ts @@ -104,6 +104,9 @@ 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( `` + @@ -111,7 +114,7 @@ export function striper(options?: ViteStriperOptions): Plugin[] { `${green('-----')}\n` + `${rest.code}` + `\n${green(':::::')}\n` + - `${info}` + + `${info.join('\n')}` + `\n${green('-----')}` + ``, ) @@ -128,7 +131,7 @@ export function striper(options?: ViteStriperOptions): Plugin[] { `${green('-----')}\n` + `${rest.code}` + `\n${green(':::::')}\n` + - `${info}` + + `${info.join('\n')}` + `\n${green('-----')}` + ``, ) diff --git a/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts b/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts index 9f057eeba..599413b62 100644 --- a/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts +++ b/packages/vite-plugin-striper/src/lib/transformPackage.spec.ts @@ -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 @@ -27,7 +27,9 @@ describe('package', () => { expect(transformed).toMatchInlineSnapshot(` { - "code": "@Entity(\\"tasks\\", { + "code": "const ObjectId = null; + + @Entity(\\"tasks\\", { allowApiCrud: true }) export class Task { @@ -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'", ], } `) diff --git a/packages/vite-plugin-striper/src/lib/transformPackage.ts b/packages/vite-plugin-striper/src/lib/transformPackage.ts index d30d0b44b..1b8ae643a 100644 --- a/packages/vite-plugin-striper/src/lib/transformPackage.ts +++ b/packages/vite-plugin-striper/src/lib/transformPackage.ts @@ -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 { @@ -12,13 +15,30 @@ 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 }, @@ -26,7 +46,7 @@ export const removePackages = async (code: string, packages_to_strip: string[]) 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: [] } From a09317810a816635ad57c9ae0db09e0053ac2d62 Mon Sep 17 00:00:00 2001 From: jycouet Date: Thu, 14 Dec 2023 23:47:37 +0100 Subject: [PATCH 2/2] . --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 76942d6d4..6dc6ce624 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -58,7 +58,7 @@ jobs: # nodeVersion: 18 - name: 🚧 Run nx build - run: pnpm nx:build + run: pnpm build - name: ➕ Create template run: