vxrn 0.1.49 → 0.1.51

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.
Files changed (61) hide show
  1. package/dist/cjs/exports/build.js +7 -91
  2. package/dist/cjs/exports/build.js.map +2 -2
  3. package/dist/cjs/exports/build.native.js +49 -354
  4. package/dist/cjs/exports/build.native.js.map +2 -2
  5. package/dist/cjs/exports/serve.js +1 -1
  6. package/dist/cjs/exports/serve.js.map +1 -1
  7. package/dist/cjs/exports/serve.native.js +1 -1
  8. package/dist/cjs/exports/serve.native.js.map +1 -1
  9. package/dist/cjs/index.js +4 -0
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/index.native.js +8 -0
  12. package/dist/cjs/index.native.js.map +1 -1
  13. package/dist/cjs/types.native.js.map +1 -1
  14. package/dist/cjs/utils/getOptionsFilled.js.map +1 -1
  15. package/dist/cjs/utils/getOptionsFilled.native.js.map +1 -1
  16. package/dist/cjs/utils/getVitePath.js +2 -2
  17. package/dist/cjs/utils/getVitePath.js.map +2 -2
  18. package/dist/cjs/utils/getVitePath.native.js +8 -8
  19. package/dist/cjs/utils/getVitePath.native.js.map +2 -2
  20. package/dist/cjs/utils/readVXRNConfig.js +15 -18
  21. package/dist/cjs/utils/readVXRNConfig.js.map +2 -2
  22. package/dist/cjs/utils/readVXRNConfig.native.js +26 -28
  23. package/dist/cjs/utils/readVXRNConfig.native.js.map +2 -2
  24. package/dist/esm/exports/build.js +8 -96
  25. package/dist/esm/exports/build.js.map +1 -1
  26. package/dist/esm/exports/build.mjs +14 -130
  27. package/dist/esm/exports/build.native.js +50 -360
  28. package/dist/esm/exports/build.native.js.map +2 -2
  29. package/dist/esm/exports/serve.js +1 -1
  30. package/dist/esm/exports/serve.js.map +1 -1
  31. package/dist/esm/exports/serve.mjs +1 -1
  32. package/dist/esm/exports/serve.native.js +1 -1
  33. package/dist/esm/exports/serve.native.js.map +1 -1
  34. package/dist/esm/index.js +4 -0
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/esm/index.mjs +4 -0
  37. package/dist/esm/index.native.js +4 -0
  38. package/dist/esm/index.native.js.map +1 -1
  39. package/dist/esm/utils/getOptionsFilled.js.map +1 -1
  40. package/dist/esm/utils/getOptionsFilled.native.js.map +1 -1
  41. package/dist/esm/utils/getVitePath.js +2 -2
  42. package/dist/esm/utils/getVitePath.js.map +1 -1
  43. package/dist/esm/utils/getVitePath.mjs +2 -2
  44. package/dist/esm/utils/getVitePath.native.js +8 -8
  45. package/dist/esm/utils/getVitePath.native.js.map +2 -2
  46. package/dist/esm/utils/readVXRNConfig.js +16 -19
  47. package/dist/esm/utils/readVXRNConfig.js.map +1 -1
  48. package/dist/esm/utils/readVXRNConfig.mjs +17 -21
  49. package/dist/esm/utils/readVXRNConfig.native.js +27 -29
  50. package/dist/esm/utils/readVXRNConfig.native.js.map +2 -2
  51. package/package.json +8 -8
  52. package/src/exports/build.ts +20 -177
  53. package/src/exports/serve.ts +4 -0
  54. package/src/index.ts +5 -0
  55. package/src/types.ts +9 -0
  56. package/src/utils/getOptionsFilled.ts +0 -1
  57. package/src/utils/getVitePath.ts +3 -3
  58. package/src/utils/readVXRNConfig.ts +18 -25
  59. package/types/index.d.ts +4 -0
  60. package/types/types.d.ts +4 -0
  61. package/types/utils/getOptionsFilled.d.ts +2 -0
@@ -1,17 +1,14 @@
1
- import { build as esbuild } from 'esbuild'
1
+ import FSExtra from 'fs-extra'
2
2
  import { resolve as importMetaResolve } from 'import-meta-resolve'
3
- import fs from 'node:fs'
4
- import { tmpdir } from 'node:os'
5
- import Path from 'node:path'
3
+ import { rm } from 'node:fs/promises'
4
+ import type { RollupOutput } from 'rollup'
6
5
  import { mergeConfig, build as viteBuild, type UserConfig } from 'vite'
7
- import FSExtra from 'fs-extra'
8
- import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
9
6
  import type { VXRNConfig } from '../types'
10
7
  import { getBaseViteConfig } from '../utils/getBaseViteConfig'
11
- import { getHtml } from '../utils/getHtml'
12
8
  import { getOptimizeDeps } from '../utils/getOptimizeDeps'
13
- import { getOptionsFilled, type VXRNConfigFilled } from '../utils/getOptionsFilled'
14
- import { assertIsError } from '../utils/assert'
9
+ import { getOptionsFilled } from '../utils/getOptionsFilled'
10
+
11
+ const { existsSync } = FSExtra
15
12
 
16
13
  Error.stackTraceLimit = Infinity
17
14
 
@@ -23,20 +20,21 @@ export const resolveFile = (path: string) => {
23
20
  }
24
21
  }
25
22
 
26
- const { ensureDir, existsSync, readFile, pathExists } = FSExtra
27
-
28
23
  type BuildOptions = { step?: string; page?: string }
29
24
 
30
- // web only for now
31
-
32
- // TODO:
33
- // - make this only build native or web bundles
34
- // - move router stuff into router package
35
- // - generateStaticPages becomes a vite 'post' postbuild callback in router plugin
36
-
37
25
  export const build = async (optionsIn: VXRNConfig, buildOptions: BuildOptions = {}) => {
38
26
  const options = await getOptionsFilled(optionsIn)
39
27
 
28
+ // lets always clean dist folder for now to be sure were correct
29
+ if (existsSync('dist')) {
30
+ await rm('dist', { recursive: true, force: true })
31
+ }
32
+
33
+ // lets always clean dist folder for now to be sure were correct
34
+ if (existsSync('node_modules/.vite')) {
35
+ await rm('node_modules/.vite', { recursive: true, force: true })
36
+ }
37
+
40
38
  // TODO?
41
39
  process.env.NODE_ENV = 'production'
42
40
 
@@ -50,15 +48,14 @@ export const build = async (optionsIn: VXRNConfig, buildOptions: BuildOptions =
50
48
  root: options.root,
51
49
  clearScreen: false,
52
50
  optimizeDeps,
53
- }
54
- ) satisfies UserConfig
51
+ } satisfies UserConfig
52
+ )
55
53
 
56
54
  if (options.webConfig) {
57
55
  webBuildConfig = mergeConfig(webBuildConfig, options.webConfig) as any
58
56
  }
59
57
 
60
58
  if (buildOptions.step !== 'generate') {
61
- console.info(`build client`)
62
59
  await viteBuild(
63
60
  mergeConfig(webBuildConfig, {
64
61
  build: {
@@ -72,12 +69,6 @@ export const build = async (optionsIn: VXRNConfig, buildOptions: BuildOptions =
72
69
  console.info(`build server`)
73
70
  const { output } = (await viteBuild(
74
71
  mergeConfig(webBuildConfig, {
75
- // optimizeDeps: {
76
- // esbuildOptions: {
77
- // format: 'cjs',
78
- // },
79
- // },
80
-
81
72
  ssr: {
82
73
  noExternal: optimizeDeps.include,
83
74
  optimizeDeps,
@@ -95,155 +86,7 @@ export const build = async (optionsIn: VXRNConfig, buildOptions: BuildOptions =
95
86
  } satisfies UserConfig)
96
87
  )) as RollupOutput
97
88
 
98
- console.info(`generating static pages`)
99
- await generateStaticPages(options, output)
100
- }
101
-
102
- async function generateStaticPages(
103
- options: VXRNConfigFilled,
104
- serverOutput: (OutputChunk | OutputAsset)[]
105
- ) {
106
- const toAbsolute = (p) => Path.resolve(options.root, p)
107
-
108
- const staticDir = toAbsolute(`dist/static`)
109
- await ensureDir(staticDir)
110
- const template = fs.readFileSync(toAbsolute('index.html'), 'utf-8')
111
-
112
- const render = (await import(`${options.root}/dist/server/entry-server.js`)).render
113
-
114
- // load routes
115
- // const entry = serverOutput.find(
116
- // (x) => x.type === 'chunk' && x.facadeModuleId?.includes('entry-server')
117
- // )
118
-
119
- const assets: OutputAsset[] = []
120
-
121
- const allRoutes: {
122
- path: string
123
- params: Object
124
- loaderData: any
125
- }[] = []
126
-
127
- for (const output of serverOutput) {
128
- if (output.type === 'asset') {
129
- assets.push(output)
130
- continue
131
- }
132
-
133
- const id = output.facadeModuleId || ''
134
- const file = Path.basename(id)
135
- const name = file.replace(/\.[^/.]+$/, '')
136
-
137
- if (!id || file[0] === '_' || file.includes('entry-server')) {
138
- continue
139
- }
140
- if (id.includes('+api')) {
141
- continue
142
- }
143
-
144
- const endpointPath = Path.join(options.root, 'dist/server', output.fileName)
145
-
146
- let exported
147
- try {
148
- exported = await import(endpointPath)
149
- } catch (err) {
150
- console.error(`Error importing page (original error)`, err)
151
- // err cause not showing in vite or something
152
- throw new Error(`Error importing page: ${endpointPath}`, {
153
- cause: err,
154
- })
155
- }
156
-
157
- const paramsList = ((await exported.generateStaticParams?.()) ?? [{}]) as Object[]
158
-
159
- for (const params of paramsList) {
160
- const path = getUrl(params)
161
- const loaderData = (await exported.loader?.({ path, params })) ?? {}
162
- allRoutes.push({ path, params, loaderData })
163
- }
164
-
165
- function getUrl(_params = {}) {
166
- if (name === 'index') {
167
- return '/'
168
- }
169
- if (name.startsWith('[...')) {
170
- const part = name.replace('[...', '').replace(']', '')
171
- return `/${_params[part]}`
172
- }
173
- return `/${name
174
- .split('/')
175
- .map((part) => {
176
- if (part[0] === '[') {
177
- const found = _params[part.slice(1, part.length - 1)]
178
- if (!found) {
179
- console.warn('not found', { _params, part })
180
- }
181
- return found
182
- }
183
- return part
184
- })
185
- .join('/')}`
186
- }
187
- }
188
-
189
- // can build them in parallel
190
- // const allRoutes = (
191
- // await Promise.all(
192
- // )
193
- // ).flat()
194
-
195
- // for now just inline
196
- const cssStringRaw = assets
197
- .filter((x) => x.name?.endsWith('.css'))
198
- .map((x) => x.source)
199
- .join('\n\n')
200
-
201
- // awkward way to get prefixes:
202
- const tmpCssFile = Path.join(tmpdir(), 'tmp.css')
203
- await FSExtra.writeFile(tmpCssFile, cssStringRaw, 'utf-8')
204
- await esbuild({
205
- entryPoints: [tmpCssFile],
206
- target: 'safari17',
207
- bundle: true,
208
- minifyWhitespace: true,
209
- sourcemap: false,
210
- outfile: tmpCssFile,
211
- loader: { '.css': 'css' },
212
- })
213
- const cssString = await FSExtra.readFile(tmpCssFile, 'utf-8')
214
-
215
- // pre-render each route...
216
- for (const { path, loaderData, params } of allRoutes) {
217
- try {
218
- const loaderProps = { params }
219
- globalThis['__vxrnLoaderProps__'] = loaderProps
220
- console.info(`render`, path)
221
- const { appHtml, headHtml } = await render({ path })
222
- const slashFileName = `${path === '/' ? '/index' : path}.html`
223
- const clientHtmlPath = toAbsolute(`dist/client${slashFileName}`)
224
- const clientHtml = existsSync(clientHtmlPath) ? await readFile(clientHtmlPath, 'utf-8') : null
225
- const html = getHtml({
226
- template: clientHtml || template,
227
- appHtml,
228
- headHtml,
229
- loaderData,
230
- loaderProps,
231
- css: cssString,
232
- })
233
- const filePath = toAbsolute(`dist/static${slashFileName}`)
234
- await ensureDir(Path.dirname(filePath))
235
- fs.writeFileSync(toAbsolute(filePath), html)
236
- } catch (err) {
237
- assertIsError(err)
238
- console.error(`og error because cause not working`, err)
239
- throw new Error(
240
- `Error building static page: ${path} with:
241
- loaderData: ${JSON.stringify(loaderData || null)}
242
- params: ${JSON.stringify(params || null)}`,
243
- {
244
- cause: err,
245
- }
246
- )
247
- }
89
+ if (options.afterBuild) {
90
+ await options.afterBuild(options, output)
248
91
  }
249
92
  }
@@ -13,6 +13,10 @@ export const serve = async (optionsIn: VXRNConfig) => {
13
13
  gzip: true,
14
14
  })
15
15
 
16
+ if (options.serve) {
17
+ options.serve(options, app)
18
+ }
19
+
16
20
  app.use(
17
21
  defineEventHandler(async ({ node: { req, res } }) => {
18
22
  await new Promise<void>((response) => {
package/src/index.ts CHANGED
@@ -1,4 +1,9 @@
1
1
  export { build } from './exports/build'
2
2
  export { dev } from './exports/dev'
3
3
 
4
+ export * from './utils/getHtml'
5
+ export * from './utils/getOptionsFilled'
6
+ export * from './utils/getOptimizeDeps'
7
+ export * from './utils/getBaseViteConfig'
8
+
4
9
  export * from './types'
package/src/types.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { Options as FlowOptions } from '@vxrn/vite-flow'
2
+ import type { App } from 'h3'
3
+ import type { OutputAsset, OutputChunk } from 'rollup'
2
4
  import type { InlineConfig } from 'vite'
3
5
 
4
6
  export type VXRNConfig = {
@@ -19,6 +21,13 @@ export type VXRNConfig = {
19
21
  webConfig?: InlineConfig
20
22
  nativeConfig?: InlineConfig
21
23
  flow?: FlowOptions
24
+
25
+ afterBuild?: (
26
+ options: VXRNConfig,
27
+ output: [OutputChunk, ...(OutputChunk | OutputAsset)[]]
28
+ ) => void | Promise<void>
29
+
30
+ serve?: (options: VXRNConfig, app: App) => void
22
31
  }
23
32
 
24
33
  export type HMRListener = (update: { file: string; contents: string }) => void
@@ -19,7 +19,6 @@ export async function getOptionsFilled(options: VXRNConfig) {
19
19
  })
20
20
 
21
21
  const packageRootDir = join(require.resolve('vxrn'), '../../..')
22
-
23
22
  const cacheDir = join(root, 'node_modules', '.vxrn')
24
23
  const internalPatchesDir = join(packageRootDir, 'patches')
25
24
  const userPatchesDir = join(root, 'patches')
@@ -1,7 +1,6 @@
1
1
  import { join, relative, dirname } from 'node:path'
2
+ import { createRequire } from 'node:module'
2
3
 
3
- // @ts-ignore
4
- import resolve from 'esm-resolve'
5
4
  import FSExtra from 'fs-extra'
6
5
 
7
6
  export async function getVitePath(
@@ -31,7 +30,8 @@ export async function getVitePath(
31
30
  }
32
31
 
33
32
  const sourceFile = join(process.cwd(), 'index.js')
34
- const resolved = resolve(sourceFile)(moduleName)
33
+ const require = createRequire(moduleName)
34
+ const resolved = require.resolve(sourceFile)
35
35
  // figure out symlinks
36
36
  if (!resolved) {
37
37
  throw new Error(
@@ -1,42 +1,35 @@
1
1
  import FSExtra from 'fs-extra'
2
2
  import type { VXRNConfig } from '../types'
3
- import { createServer } from 'vite'
4
- import jiti from 'jiti'
3
+ import { join } from 'node:path'
4
+ import { build } from 'esbuild'
5
5
 
6
6
  export async function readVXRNConfig(): Promise<VXRNConfig> {
7
7
  if (!(await FSExtra.pathExists('vxrn.config.ts'))) {
8
8
  return {}
9
9
  }
10
10
 
11
- if (process.env.VXRN_CJS) {
12
- const requireFile = jiti(process.cwd(), {
13
- esmResolve: true,
14
- })
15
- const userConfig = requireFile('./vxrn.config.ts')
16
- return resolveOptionalAsyncFunction(userConfig?.default ?? {})
17
- }
11
+ const outfile = join(process.cwd(), 'vxrn.config.js')
18
12
 
19
- // try esm load
20
13
  try {
21
- // somewhat hacky creating a server just to read config?
22
- const vite = await createServer({
23
- logLevel: 'silent',
24
- appType: 'custom',
25
- })
26
-
27
- const userConfig = await vite.ssrLoadModule('./vxrn.config.ts', {
28
- fixStacktrace: true,
14
+ // output to same place so paths and relative things work
15
+ await build({
16
+ entryPoints: ['vxrn.config.ts'],
17
+ bundle: false,
18
+ outfile,
19
+ platform: 'node',
20
+ format: 'esm',
21
+ sourcemap: false,
22
+ minify: false,
29
23
  })
30
24
 
31
- await vite.close()
32
- return resolveOptionalAsyncFunction(userConfig?.default ?? {})
33
- } catch (err: any) {
34
- console.info(` [vxrn] Error loading config via ESM:
25
+ const exportedConfig = (await import(outfile)).default
35
26
 
36
- ${err.stack}
37
-
38
- To load as CommonJS, set VXRN_CJS=1`)
27
+ return await resolveOptionalAsyncFunction(exportedConfig)
28
+ } catch (err) {
29
+ console.error(` [vxrn] Error building vxrn.config.ts`)
39
30
  throw err
31
+ } finally {
32
+ FSExtra.removeSync(outfile)
40
33
  }
41
34
  }
42
35
 
package/types/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  export { build } from './exports/build';
2
2
  export { dev } from './exports/dev';
3
+ export * from './utils/getHtml';
4
+ export * from './utils/getOptionsFilled';
5
+ export * from './utils/getOptimizeDeps';
6
+ export * from './utils/getBaseViteConfig';
3
7
  export * from './types';
4
8
  //# sourceMappingURL=index.d.ts.map
package/types/types.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { Options as FlowOptions } from '@vxrn/vite-flow';
2
+ import type { App } from 'h3';
3
+ import type { OutputAsset, OutputChunk } from 'rollup';
2
4
  import type { InlineConfig } from 'vite';
3
5
  export type VXRNConfig = {
4
6
  /**
@@ -18,6 +20,8 @@ export type VXRNConfig = {
18
20
  webConfig?: InlineConfig;
19
21
  nativeConfig?: InlineConfig;
20
22
  flow?: FlowOptions;
23
+ afterBuild?: (options: VXRNConfig, output: [OutputChunk, ...(OutputChunk | OutputAsset)[]]) => void | Promise<void>;
24
+ serve?: (options: VXRNConfig, app: App) => void;
21
25
  };
22
26
  export type HMRListener = (update: {
23
27
  file: string;
@@ -17,6 +17,8 @@ export declare function getOptionsFilled(options: VXRNConfig): Promise<{
17
17
  webConfig?: import("vite").InlineConfig | undefined;
18
18
  nativeConfig?: import("vite").InlineConfig | undefined;
19
19
  flow?: import("@vxrn/vite-flow").Options | undefined;
20
+ afterBuild?: ((options: VXRNConfig, output: [import("rollup").OutputChunk, ...(import("rollup").OutputChunk | import("rollup").OutputAsset)[]]) => void | Promise<void>) | undefined;
21
+ serve?: ((options: VXRNConfig, app: import("h3").App) => void) | undefined;
20
22
  }>;
21
23
  type State = {
22
24
  applyPatches?: boolean;