Skip to content

Commit 3fd8681

Browse files
committed
feat: initate build from kenx kernel
1 parent fbbc886 commit 3fd8681

File tree

13 files changed

+358
-149
lines changed

13 files changed

+358
-149
lines changed

examples/react-ssr-ts/.config/servers.yml

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,37 @@ servers:
2525
# HOST: 0.0.0.0
2626
# PORT: '[env]:VITE_PORT'
2727
bindTo: 'http:default'
28-
builder: true
28+
build: true
2929
options:
30-
root: './src/views'
31-
# Whether your application is a Single Page Application (SPA), a Multi
32-
# Page Application (MPA), or Custom Application (SSR and frameworks with
33-
# custom HTML handling):
34-
#
35-
# 'spa': Include HTML middlewares and use SPA fallback.
36-
# Configure `sirv` with single: true in preview
37-
# 'mpa': Include HTML middlewares
38-
# 'custom': Disable Vite's own HTML serving logic so parent
39-
# server can take control. This requires creating
40-
# Vite server in middleware mode by setting
41-
# `server: { middlewareMode: true }`
42-
appType: 'custom' # spa, mpa, custom
43-
test:
44-
globals: true
45-
# 'happy-dom' or 'jsdom' for a DOM-like environment
46-
environment: 'jsdom'
47-
setupFiles:
48-
- ../test/ui.test.ts
49-
plugins:
50-
- vite.plugins
30+
# Custom Vite server configuration in any
31+
# environment mode.
32+
default:
33+
# Whether your application is a Single Page Application (SPA), a Multi
34+
# Page Application (MPA), or Custom Application (SSR and frameworks with
35+
# custom HTML handling):
36+
#
37+
# 'spa': Include HTML middlewares and use SPA fallback.
38+
# Configure `sirv` with single: true in preview
39+
# 'mpa': Include HTML middlewares
40+
# 'custom': Disable Vite's own HTML serving logic so parent
41+
# server can take control. This requires creating
42+
# Vite server in middleware mode by setting
43+
# `server: { middlewareMode: true }`
44+
appType: 'custom' # spa, mpa, custom
45+
plugins:
46+
- vite.plugins
47+
# Custom Vite server configuration in development
48+
# mode only.
49+
development:
50+
root: './src/views'
51+
test:
52+
globals: true
53+
# 'happy-dom' or 'jsdom' for a DOM-like environment
54+
environment: 'jsdom'
55+
setupFiles:
56+
- ../test/ui.test.ts
57+
# Custom Vite server configuration in production
58+
# mode only.
59+
production:
60+
root: './dist/views'
61+

examples/react-ssr-ts/build

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const Kenx = require('../../packages/node/dist/index.js').default
2+
3+
if( !Kenx ) {
4+
console.error('Kenx-node is not available')
5+
process.exit(1)
6+
}
7+
8+
( async ({ build }) => {
9+
/**
10+
* Build for production
11+
*/
12+
await build()
13+
14+
process.exit(0)
15+
} )( new Kenx() )

examples/react-ssr-ts/src/views/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
<meta charset="UTF-8" />
44
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
<title>Simple React SSR Vite Express</title>
6+
<script type="module" crossorigin src="/static/assets/index-FqhOBsn9.js"></script>
67
</head>
78
<body>
89
<div id="app"><!--outlet--></div>
9-
<script type="module" src="./client.tsx"></script>
1010
</body>
1111
</html>

packages/cli/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"no-multi-str": 1,
4040
"no-multi-spaces": 1,
4141
"no-unneeded-ternary": 1,
42-
"no-unused-expressions": 1,
42+
"no-unused-expressions": 0,
4343
"no-useless-catch": 1,
4444
"no-useless-return": 1,
4545
"prefer-arrow-callback": 2,

packages/cli/src/build.ts

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,19 @@
11

22
import type { BuildAppOptions } from './types'
3-
import { replaceTscAliasPaths } from 'tsc-alias'
4-
import * as tsc from 'tsc-prog'
5-
import Fs from 'fs-extra'
3+
import clc from 'colors'
64

75
export const execute = async ( options: BuildAppOptions ): Promise<void> => {
8-
try {
9-
let hasTsConfig = false
10-
try { hasTsConfig = (await Fs.readJSON('tsconfig.json')) !== null }
11-
catch( error ) { console.warn('No custom `tsconfig.json`: Using default configuration') }
6+
/**
7+
* Build in for production: Enforce environment mode
8+
*/
9+
// process.env.NODE_ENV = 'production'
1210

13-
/**
14-
* Build TypeScript activated projects programmatically.
15-
*
16-
* IMPORTANT: Less suited for development builds so look out
17-
* for an alternative in the next versions.
18-
*/
19-
tsc.build({
20-
basePath: process.cwd(),
21-
configFilePath: hasTsConfig ? 'tsconfig.json' : undefined, // Inherited config (optional)
22-
clean: {
23-
outDir: true,
24-
declarationDir: true
25-
},
26-
compilerOptions: {
27-
rootDir: 'src',
28-
outDir: 'dist',
29-
declaration: true,
30-
skipLibCheck: true
31-
},
32-
include: ['src/**/*'],
33-
exclude: ['**/*.test.ts', '**/*.spec.ts']
34-
})
35-
36-
/**
37-
* Replace alias paths with relative paths after
38-
* typescript compilation.
39-
*
40-
* Note:
41-
* Usefull when you add aliases that reference
42-
* other projects outside your tsconfig.json project
43-
* by providing a relative path to the baseUrl.
44-
*/
45-
hasTsConfig && await replaceTscAliasPaths({ configFile: 'tsconfig.json' })
46-
}
47-
catch( error: any ) {
48-
console.error( error )
11+
/**
12+
* Run Kenx framework
13+
*/
14+
try { await import(`${process.cwd()}/build`) }
15+
catch( error ) {
16+
console.log( clc.red.bold('No `build` file at your project root'), error )
4917
process.exit(1)
5018
}
5119
}

packages/node/.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
build
2+
build
3+
dist

packages/node/.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"no-multi-str": 1,
4141
"no-multi-spaces": 1,
4242
"no-unneeded-ternary": 1,
43-
"no-unused-expressions": 1,
43+
"no-unused-expressions": 0,
4444
"no-useless-catch": 1,
4545
"no-useless-return": 1,
4646
"prefer-arrow-callback": 2,

packages/node/src/index.ts

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ async function createAuxiliaryServer( config: AuxiliaryServerConfig ){
101101
}
102102
}
103103

104+
async function createBuild( config: AuxiliaryServerConfig ){
105+
try {
106+
if( !config.plugin )
107+
throw new Error(`Undefined <${config.type}> server plugin`)
108+
109+
const
110+
{ plugin, options } = config,
111+
Builder = await Setup.importPlugin( plugin ),
112+
instance: ServerPlugin<unknown> = new Builder( Setup, options )
113+
114+
typeof instance.build == 'function' && await instance.build()
115+
}
116+
catch( error ) {
117+
Setup.context.error('Auxiliary server:', error )
118+
process.exit(1)
119+
}
120+
}
121+
104122
async function createResource( config: ResourceConfig ){
105123
try {
106124
const Resource = await Setup.importPlugin( config.plugin )
@@ -200,7 +218,7 @@ async function toSingleton( takeover?: string[] ){
200218
*/
201219
const Args = Object.values( getResource( takeover ) )
202220

203-
Setup.context.log('Takeover ...')
221+
Setup.context.debug('Takeover ...')
204222
entrypoint.default( ...Args )
205223
}
206224
catch( error ) {
@@ -259,7 +277,7 @@ async function toMVC(){
259277
if( !models ) Setup.context.warn('No models available for <controllers>')
260278
if( !views ) Setup.context.warn('No views available for <controllers>')
261279

262-
Setup.context.log('Takeover ...')
280+
Setup.context.debug('Takeover ...')
263281
cFactory.default( ...Object.values( cFactoryDeps ), models, views )
264282
}
265283
catch( error ) {
@@ -299,7 +317,7 @@ export default class Core {
299317
* Load setup configuration
300318
*
301319
*/
302-
await Setup.initialize()
320+
await Setup.dev()
303321
const { servers, databases } = Setup.getConfig()
304322

305323
/**
@@ -363,7 +381,7 @@ export default class Core {
363381
async dispatch( takeover?: string [] ){
364382
// Assumed `autoload` method has resolved
365383
Setup ?
366-
Setup.context.log('Ready')
384+
Setup.context.debug('Ready')
367385
: process.exit(1)
368386

369387
const { directory } = Setup.getConfig()
@@ -386,4 +404,36 @@ export default class Core {
386404
default: toSingleton( takeover )
387405
}
388406
}
407+
408+
async build(){
409+
/**
410+
* Load Environment Variables
411+
*/
412+
// dotenv.config() // Load default .env variables
413+
414+
/**
415+
* Build application
416+
*/
417+
await Setup.build()
418+
419+
const
420+
{ servers } = Setup.getConfig(),
421+
builders = servers.filter( ({ build }: any ) => (build === true) )
422+
423+
/**
424+
* Run builders: Server that must build
425+
* their own assets. Eg. UI bundlers, ...
426+
*/
427+
if( Array.isArray( builders ) )
428+
for await ( const config of builders ) {
429+
const { type } = config
430+
431+
switch( type ) {
432+
case 'http': break
433+
default: await createBuild( config as AuxiliaryServerConfig ); break
434+
}
435+
436+
console.log(`<${type.toUpperCase()}> - build completed`)
437+
}
438+
}
389439
}

packages/node/src/lib/context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ export default class Context {
66
this.namespace = namespace
77
}
88

9-
log( ...args: any[] ){
9+
debug( ...args: unknown[] ){
1010
process.env.NODE_ENV !== 'production'
11-
&& console.log(`[${this.namespace.toUpperCase()}] -`, ...args )
11+
&& console.debug(`[${this.namespace.toUpperCase()}] -`, ...args )
1212
}
13-
error( ...args: any[] ){
13+
error( ...args: unknown[] ){
1414
process.env.NODE_ENV !== 'production'
1515
&& console.error(`[${this.namespace.toUpperCase()}] -`, ...args )
1616
}
17-
warn( ...args: any[] ){
17+
warn( ...args: unknown[] ){
1818
process.env.NODE_ENV !== 'production'
1919
&& console.warn(`[${this.namespace.toUpperCase()}] -`, ...args )
2020
}

0 commit comments

Comments
 (0)