Skip to content

Commit

Permalink
🎉 NEW: should strip now (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycouet authored Jan 8, 2024
1 parent 87432e8 commit ae3353f
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-stingrays-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-plugin-striper': minor
---

striper is now actually spripping things! (it was not updating the transformed code before)
1 change: 1 addition & 0 deletions packages/vite-plugin-striper/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AUTH_SECRET = 'top secret'
1 change: 1 addition & 0 deletions packages/vite-plugin-striper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@sveltejs/package": "2.2.2",
"@sveltejs/vite-plugin-svelte": "^3.0.0",
"publint": "0.2.4",
"remult": "^0.23.0",
"svelte": "4.2.1",
"svelte-check": "3.6.0",
"tslib": "2.6.2",
Expand Down
5 changes: 5 additions & 0 deletions packages/vite-plugin-striper/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { sequence } from '@sveltejs/kit/hooks'

import { handleRemult } from './hooks/handleRemult'

export const handle = sequence(handleRemult)
7 changes: 7 additions & 0 deletions packages/vite-plugin-striper/src/hooks/handleRemult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { remultSveltekit } from 'remult/remult-sveltekit'

import { ActionsController } from '../shared/actionsController.js'

export const handleRemult = remultSveltekit({
controllers: [ActionsController],
})
10 changes: 9 additions & 1 deletion packages/vite-plugin-striper/src/lib/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,16 @@ export function striper(options?: ViteStriperOptions): Plugin[] {
return
}

let infosNumber = 0

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

infosNumber += info.length

if (options?.debug && info.length > 0) {
log.info(
`` +
Expand All @@ -123,7 +127,7 @@ export function striper(options?: ViteStriperOptions): Plugin[] {

if (options && options?.packages && options.packages.length > 0) {
const { info, ...rest } = await removePackages(code, options.packages)

infosNumber += info.length
if (options?.debug && info.length > 0) {
log.info(
`` +
Expand All @@ -138,6 +142,10 @@ export function striper(options?: ViteStriperOptions): Plugin[] {
}
}

if (infosNumber > 0) {
return { code, map: null }
}

return
},
},
Expand Down
44 changes: 41 additions & 3 deletions packages/vite-plugin-striper/src/lib/transformDecorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export class TasksController {
@BackendMethod({
allowed: Allow.authenticated
})
static async setAllCompleted(completed: boolean) {}
static async setAllCompleted(completed) {}
@BackendMethod({
allowed: Allow.authenticated
})
static async Yop(completed: boolean) {}
static async Yop(completed) {}
}",
"info": [
"Striped: 'BackendMethod'",
Expand Down Expand Up @@ -202,7 +202,7 @@ export class TasksController {
@BackendMethod({
allowed: false
})
static async init(hello: string) {}
static async init(hello) {}
}",
"info": [
"Striped: 'BackendMethod'",
Expand Down Expand Up @@ -415,4 +415,42 @@ export class TasksController {
}
`)
})

it('should strip import types', async () => {
const code = `import { AUTH_SECRET } from '$env/static/private'
import { BackendMethod, type Allowed, remult } from 'remult'
export class ActionsController {
@BackendMethod({
// Only unauthenticated users can call this method
allowed: () => remult.user === undefined,
})
static async read(info: Allowed) {
console.log('AUTH_SECRET', AUTH_SECRET)
return AUTH_SECRET + ' ' + info
}
}
`

const transformed = await transformDecorator(code, ['BackendMethod'])

expect(transformed).toMatchInlineSnapshot(`
{
"code": "import { BackendMethod, remult } from "remult";
export class ActionsController {
@BackendMethod({
allowed: () => remult.user === undefined
})
static async read(info) {}
}",
"info": [
"Striped: 'BackendMethod'",
"Removed: 'AUTH_SECRET' from '$env/static/private'",
"Removed: 'Allowed' from 'remult'",
],
}
`)
})
})
16 changes: 15 additions & 1 deletion packages/vite-plugin-striper/src/lib/transformDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,23 @@ export const transformDecorator = async (code: string, decorators_to_strip: stri
return true
})

// If one of decorators was found, empty the function body
// If one of the decorators was found, empty the function body and remove types
if (foundDecorator) {
path.node.body.body = []

// Remove the return type of the function
if (path.node.returnType) {
delete path.node.returnType
}

// Remove the types of all parameters
path.node.params.forEach(param => {
// @ts-ignore
if (param.typeAnnotation) {
// @ts-ignore
delete param.typeAnnotation
}
})
}

this.traverse(path)
Expand Down
1 change: 1 addition & 0 deletions packages/vite-plugin-striper/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Hello

<a href="/demoThrowClass">ThrowClass</a>
<a href="/demoThrowClass">ThrowRandom</a>
<a href="/decorators">decorators</a>
13 changes: 13 additions & 0 deletions packages/vite-plugin-striper/src/routes/decorators/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts">
import { onMount } from 'svelte'
import { ActionsController } from '../../shared/actionsController.js'
let secretContent = '???'
onMount(async () => {
secretContent = await ActionsController.read(true)
})
</script>

<h2>Bye bye decorators</h2>
secretContent: {secretContent}
13 changes: 13 additions & 0 deletions packages/vite-plugin-striper/src/shared/actionsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AUTH_SECRET } from '$env/static/private'
import { BackendMethod, type Allowed, remult } from 'remult'

export class ActionsController {
@BackendMethod({
// Only unauthenticated users can call this method
allowed: () => remult.user === undefined,
})
static async read(info: Allowed) {
console.info('AUTH_SECRET', AUTH_SECRET)
return AUTH_SECRET + ' ' + info
}
}
6 changes: 5 additions & 1 deletion packages/vite-plugin-striper/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { striper } from './src/lib/plugin.js'

export default defineConfig({
plugins: [
striper({ log_on_throw_is_not_a_new_class: true }),
striper({
decorators: ['BackendMethod'],
debug: false,
log_on_throw_is_not_a_new_class: true,
}),
//
sveltekit(),
],
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ae3353f

Please sign in to comment.