5htp 0.6.2-99 → 0.6.3-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,6 +53,13 @@ function Plugin(babel, { app, side, debug }: TOptions) {
53
53
 
54
54
  const t = babel.types as typeof types;
55
55
 
56
+ type TPluginState = {
57
+ filename: string,
58
+ file: TFileInfos,
59
+ apiInjectedRootFunctions: WeakSet<types.Node>,
60
+ needsUseContextImport: boolean
61
+ }
62
+
56
63
  /*
57
64
  - Wrap route.get(...) with (app: Application) => { }
58
65
  - Inject chunk ID into client route options
@@ -70,14 +77,14 @@ function Plugin(babel, { app, side, debug }: TOptions) {
70
77
  const stats = page.data.stats;
71
78
  */
72
79
 
73
- const plugin: PluginObj<{
74
- filename: string,
75
- file: TFileInfos
76
- }> = {
80
+ const plugin: PluginObj<TPluginState> = {
77
81
  pre(state) {
78
82
  this.filename = state.opts.filename as string;
79
83
 
80
84
  this.file = getFileInfos(this.filename);
85
+
86
+ this.apiInjectedRootFunctions = new WeakSet();
87
+ this.needsUseContextImport = false;
81
88
  },
82
89
  visitor: {
83
90
  // Find @app imports
@@ -203,6 +210,8 @@ function Plugin(babel, { app, side, debug }: TOptions) {
203
210
  */
204
211
  if (side === 'client' && !clientServices.includes(serviceName)) {
205
212
 
213
+ ensureApiExposedInRootFunction(path, this);
214
+
206
215
  // Get complete call path
207
216
  const apiPath = '/api/' + completePath.join('/');
208
217
 
@@ -255,6 +264,8 @@ function Plugin(babel, { app, side, debug }: TOptions) {
255
264
 
256
265
  if (!this.file.process)
257
266
  return;
267
+
268
+ ensureUseContextImport(path, this);
258
269
 
259
270
  const wrappedrouteDefs = wrapRouteDefs( this.file );
260
271
  if (wrappedrouteDefs)
@@ -264,6 +275,187 @@ function Plugin(babel, { app, side, debug }: TOptions) {
264
275
  }
265
276
  }
266
277
 
278
+ function ensureApiExposedInRootFunction(
279
+ path: NodePath<types.CallExpression>,
280
+ pluginState: TPluginState
281
+ ) {
282
+ if (path.scope.hasBinding('api'))
283
+ return;
284
+
285
+ const rootFunctionPath = getRootFunctionPath(path);
286
+ if (!rootFunctionPath)
287
+ return;
288
+
289
+ // Root function should be at the program body level (not nested in another function / expression)
290
+ if (rootFunctionPath.getFunctionParent())
291
+ return;
292
+ if (!isProgramBodyLevelFunction(rootFunctionPath))
293
+ return;
294
+
295
+ if (pluginState.apiInjectedRootFunctions.has(rootFunctionPath.node))
296
+ return;
297
+
298
+ const exposeApiDeclaration = t.variableDeclaration('const', [
299
+ t.variableDeclarator(
300
+ t.objectPattern([
301
+ t.objectProperty(t.identifier('api'), t.identifier('api'), false, true),
302
+ ]),
303
+ t.callExpression(t.identifier('useContext'), [])
304
+ )
305
+ ]);
306
+
307
+ const body = rootFunctionPath.node.body;
308
+ if (body.type === 'BlockStatement') {
309
+ body.body.unshift(exposeApiDeclaration);
310
+ } else {
311
+ rootFunctionPath.node.body = t.blockStatement([
312
+ exposeApiDeclaration,
313
+ t.returnStatement(body)
314
+ ]);
315
+ }
316
+
317
+ pluginState.apiInjectedRootFunctions.add(rootFunctionPath.node);
318
+ pluginState.needsUseContextImport = true;
319
+ }
320
+
321
+ function getRootFunctionPath(path: NodePath): NodePath<types.Function | types.ArrowFunctionExpression> | undefined {
322
+
323
+ let functionPath = path.getFunctionParent();
324
+ if (!functionPath)
325
+ return;
326
+
327
+ // Only support plain functions / arrow functions (no class/object methods)
328
+ if (!(
329
+ functionPath.isFunctionDeclaration()
330
+ || functionPath.isFunctionExpression()
331
+ || functionPath.isArrowFunctionExpression()
332
+ ))
333
+ return;
334
+
335
+ let parentFunction = functionPath.getFunctionParent();
336
+ while (parentFunction) {
337
+
338
+ if (!(
339
+ parentFunction.isFunctionDeclaration()
340
+ || parentFunction.isFunctionExpression()
341
+ || parentFunction.isArrowFunctionExpression()
342
+ ))
343
+ break;
344
+
345
+ functionPath = parentFunction;
346
+ parentFunction = functionPath.getFunctionParent();
347
+ }
348
+
349
+ return functionPath;
350
+ }
351
+
352
+ function isProgramBodyLevelFunction(path: NodePath): boolean {
353
+
354
+ const parent = path.parentPath;
355
+ if (!parent)
356
+ return false;
357
+
358
+ // function Foo() {}
359
+ if (parent.isProgram())
360
+ return true;
361
+
362
+ // export default function Foo() {} / export default () => {}
363
+ if (
364
+ parent.isExportDefaultDeclaration()
365
+ &&
366
+ parent.parentPath?.isProgram()
367
+ )
368
+ return true;
369
+
370
+ // export const Foo = () => {}
371
+ if (
372
+ parent.isExportNamedDeclaration()
373
+ &&
374
+ parent.parentPath?.isProgram()
375
+ )
376
+ return true;
377
+
378
+ // const Foo = () => {} (top-level) / export const Foo = () => {}
379
+ if (parent.isVariableDeclarator()) {
380
+
381
+ const declaration = parent.parentPath;
382
+ if (!declaration?.isVariableDeclaration())
383
+ return false;
384
+
385
+ const declarationParent = declaration.parentPath;
386
+ if (!declarationParent)
387
+ return false;
388
+
389
+ if (declarationParent.isProgram())
390
+ return true;
391
+
392
+ if (
393
+ declarationParent.isExportNamedDeclaration()
394
+ &&
395
+ declarationParent.parentPath?.isProgram()
396
+ )
397
+ return true;
398
+ }
399
+
400
+ return false;
401
+ }
402
+
403
+ function ensureUseContextImport(path: NodePath<types.Program>, pluginState: TPluginState) {
404
+
405
+ if (!pluginState.needsUseContextImport)
406
+ return;
407
+
408
+ const body = path.node.body;
409
+
410
+ // Already imported as a value import
411
+ for (const stmt of body) {
412
+ if (
413
+ stmt.type === 'ImportDeclaration'
414
+ &&
415
+ stmt.source.value === '@/client/context'
416
+ &&
417
+ stmt.importKind !== 'type'
418
+ &&
419
+ stmt.specifiers.some(s =>
420
+ s.type === 'ImportDefaultSpecifier' && s.local.name === 'useContext'
421
+ )
422
+ )
423
+ return;
424
+ }
425
+
426
+ // Try to reuse an existing value import from the same module
427
+ for (const stmt of body) {
428
+ if (
429
+ stmt.type !== 'ImportDeclaration'
430
+ ||
431
+ stmt.source.value !== '@/client/context'
432
+ ||
433
+ stmt.importKind === 'type'
434
+ )
435
+ continue;
436
+
437
+ const hasDefaultImport = stmt.specifiers.some(s => s.type === 'ImportDefaultSpecifier');
438
+ if (!hasDefaultImport) {
439
+ stmt.specifiers.unshift(
440
+ t.importDefaultSpecifier(t.identifier('useContext'))
441
+ );
442
+ return;
443
+ }
444
+ }
445
+
446
+ // Otherwise, add a new import (placed after existing imports)
447
+ const importDeclaration = t.importDeclaration(
448
+ [t.importDefaultSpecifier(t.identifier('useContext'))],
449
+ t.stringLiteral('@/client/context')
450
+ );
451
+
452
+ let insertIndex = 0;
453
+ while (insertIndex < body.length && body[insertIndex].type === 'ImportDeclaration')
454
+ insertIndex++;
455
+
456
+ body.splice(insertIndex, 0, importDeclaration);
457
+ }
458
+
267
459
  function getFileInfos( filename: string ): TFileInfos {
268
460
 
269
461
  const file: TFileInfos = {
@@ -302,7 +494,7 @@ function Plugin(babel, { app, side, debug }: TOptions) {
302
494
 
303
495
  function transformDataFetchers(
304
496
  path: NodePath<types.CallExpression>,
305
- routerDefContext: PluginObj,
497
+ routerDefContext: TPluginState,
306
498
  routeDef: TRouteDefinition
307
499
  ) {
308
500
  path.traverse({
@@ -638,4 +830,4 @@ function Plugin(babel, { app, side, debug }: TOptions) {
638
830
  }
639
831
 
640
832
  return plugin;
641
- }
833
+ }
@@ -24,7 +24,7 @@ module.exports = (app: App, dev: boolean, client: boolean) => ([
24
24
  // Texte brut
25
25
  {
26
26
  type: 'asset/source',
27
- test: /\.(md|hbs|sql|txt|csv)$/,
27
+ test: /\.(md|hbs|sql|txt|csv|html)$/,
28
28
  },
29
29
 
30
30
  // Polices dans un fichier distinc dans le dossier dédié
package/compiler/index.ts CHANGED
@@ -17,7 +17,6 @@ import cli from '..';
17
17
  import createServerConfig from './server';
18
18
  import createClientConfig from './client';
19
19
  import { TCompileMode } from './common';
20
- import { routerServices } from './common/babel/plugins/services';
21
20
 
22
21
  type TCompilerCallback = (compiler: webpack.Compiler) => void
23
22
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "5htp",
3
3
  "description": "Convenient TypeScript framework designed for Performance and Productivity.",
4
- "version": "0.6.2-99",
4
+ "version": "0.6.3-1",
5
5
  "author": "Gaetan Le Gac (https://github.com/gaetanlegac)",
6
6
  "repository": "git://github.com/gaetanlegac/5htp.git",
7
7
  "license": "MIT",