Skip to content

Commit

Permalink
perf: stop auto-generating shiled() in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Oct 10, 2022
1 parent 9ba3fb9 commit 32a3008
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
7 changes: 7 additions & 0 deletions examples/vite/.testRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function testRun(
run(cmd, { serverIsReadyMessage })
}

{
const isDev = cmd !== 'npm run preview'
if (isDev) {
skipShieldGenerationTest = true
}
}

test('example', async () => {
{
const html = await fetchHtml('/')
Expand Down
13 changes: 8 additions & 5 deletions telefunc/node/server/runTelefunc/applyShield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ function applyShield(runContext: {
telefunctionName: string
telefunctionArgs: unknown[]
logInvalidRequests: boolean
isProduction: boolean
}): { isValidRequest: boolean } {
const { telefunction } = runContext

const hasShield = !shieldIsMissing(telefunction)
assertWarning(
hasShield || telefunction.length === 0,
`The telefunction ${runContext.telefunctionName} accepts arguments yet is missing \`shield()\`, see https://telefunc.com/shield`,
{ onlyOnce: true }
)
if (runContext.isProduction) {
assertWarning(
hasShield || telefunction.length === 0,
`The telefunction ${runContext.telefunctionName} accepts arguments yet is missing \`shield()\`, see https://telefunc.com/shield`,
{ onlyOnce: true }
)
}
if (!hasShield) {
return { isValidRequest: true }
}
Expand Down
12 changes: 9 additions & 3 deletions telefunc/node/vite/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ export { transform }

function transform(): Plugin {
let root: string
let isDev: boolean = false
return {
name: 'telefunc:transform',
enforce: 'pre',
configResolved: (config) => {
root = toPosixPath(config.root)
assert(root)
},
configureServer() {
isDev = true
},
async transform(code, id, options) {
if (!id.includes('.telefunc.')) {
return
Expand All @@ -25,9 +29,11 @@ function transform(): Plugin {
if (isClientSide) {
code = await transformTelefuncFileClientSide(code, id, root)
} else {
code = (await transformTelefuncFileServerSide(code, id, root, true))
if (id.endsWith('.ts')) {
code = generateShield(code)
code = await transformTelefuncFileServerSide(code, id, root, true)
if (!isDev) {
if (id.endsWith('.ts')) {
code = generateShield(code)
}
}
}

Expand Down

0 comments on commit 32a3008

Please sign in to comment.