@ossy/app 1.11.6 → 1.11.8

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.
package/cli/dev.js DELETED
@@ -1,342 +0,0 @@
1
- import path from 'path';
2
- import url from 'url';
3
- import fs from 'fs';
4
- import {
5
- printBuildOverview,
6
- getBuildOverviewSnapshot,
7
- filePathToRoute,
8
- discoverFilesByPattern,
9
- PAGE_FILE_PATTERN,
10
- writePagesManifest,
11
- resolveApiSource,
12
- resolveTaskSource,
13
- resetOssyBuildDir,
14
- compileOssyNodeArtifacts,
15
- copyOssyAppRuntime,
16
- writeAppRuntimeShims,
17
- createOssyAppBundlePlugins,
18
- createOssyClientRollupPlugins,
19
- writePageHydrateStubs,
20
- buildClientHydrateInput,
21
- clientHydrateIdForPage,
22
- ossyGeneratedDir,
23
- OSSY_GEN_PAGES_BASENAME,
24
- OSSY_PAGES_RUNTIME_BASENAME,
25
- writeResourceTemplatesBarrelIfPresent,
26
- resourceTemplatesDir,
27
- OSSY_RESOURCE_TEMPLATES_OUT,
28
- } from './build.js';
29
- import prerenderReactTask from './prerender-react.task.js'
30
- import { createBuildDashboard } from './build-terminal.js'
31
- import { watch } from 'rollup';
32
- import arg from 'arg'
33
- import { spawn } from 'node:child_process'
34
- // import inject from '@rollup/plugin-inject'
35
-
36
- export const dev = async (cliArgs) => {
37
- const options = arg({
38
- '--config': String,
39
- '-c': '--config',
40
- }, { argv: cliArgs, permissive: true })
41
-
42
-
43
- const scriptDir = path.dirname(url.fileURLToPath(import.meta.url))
44
- const buildPath = path.resolve('build')
45
- const srcDir = path.resolve('src')
46
- const configPath = path.resolve(options['--config'] || 'src/config.js');
47
- let currentPageFiles = discoverFilesByPattern(srcDir, PAGE_FILE_PATTERN)
48
-
49
- resetOssyBuildDir(buildPath)
50
-
51
- const resourceTemplatesResult = writeResourceTemplatesBarrelIfPresent({
52
- cwd: process.cwd(),
53
- log: false,
54
- })
55
- let resourceTemplatesDevLogged = false
56
-
57
- const ossyDir = ossyGeneratedDir(buildPath)
58
- const pagesGeneratedPath = path.join(ossyDir, OSSY_GEN_PAGES_BASENAME)
59
- writePagesManifest({
60
- pageFiles: currentPageFiles,
61
- srcDir,
62
- pagesGeneratedPath,
63
- })
64
- writePageHydrateStubs(currentPageFiles, srcDir, ossyDir)
65
- const clientHydrateInput = buildClientHydrateInput(currentPageFiles, srcDir, ossyDir)
66
-
67
- let apiOverviewFiles = []
68
- let taskOverviewFiles = []
69
- const refreshApiTaskManifests = () => {
70
- apiOverviewFiles = resolveApiSource({ srcDir, buildPath }).apiOverviewFiles
71
- taskOverviewFiles = resolveTaskSource({ srcDir, buildPath }).taskOverviewFiles
72
- }
73
- refreshApiTaskManifests()
74
- let middlewareSourcePath = path.resolve(options['--middleware-source'] || 'src/middleware.js');
75
- const publicDir = path.resolve('public')
76
-
77
- if (currentPageFiles.length === 0) {
78
- console.log('\n \x1b[1m@ossy/app\x1b[0m \x1b[2mdev\x1b[0m')
79
- printBuildOverview({
80
- pagesSourcePath: pagesGeneratedPath,
81
- apiOverviewFiles,
82
- configPath,
83
- pageFiles: currentPageFiles,
84
- })
85
- if (resourceTemplatesResult.wrote && resourceTemplatesResult.path) {
86
- console.log(
87
- `[@ossy/app][resource-templates] merged ${resourceTemplatesResult.count} template(s) → ${path.relative(process.cwd(), resourceTemplatesResult.path)}`
88
- )
89
- }
90
- }
91
-
92
- if (!fs.existsSync(middlewareSourcePath)) {
93
- middlewareSourcePath = path.resolve(scriptDir, 'middleware.js')
94
- }
95
-
96
- const configSourcePath = fs.existsSync(configPath)
97
- ? configPath
98
- : path.resolve(scriptDir, 'default-config.js')
99
-
100
- const pagesEntryPath = path.join(ossyDir, OSSY_PAGES_RUNTIME_BASENAME)
101
-
102
- const runNodeBundles = async () => {
103
- refreshApiTaskManifests()
104
- await compileOssyNodeArtifacts({
105
- pageFiles: currentPageFiles,
106
- srcDir,
107
- ossyDir,
108
- apiFiles: apiOverviewFiles,
109
- taskFiles: taskOverviewFiles,
110
- nodeEnv: 'development',
111
- })
112
- writeAppRuntimeShims({
113
- middlewareSourcePath,
114
- configSourcePath,
115
- ossyDir,
116
- })
117
- copyOssyAppRuntime({ scriptDir, buildPath })
118
- }
119
-
120
- await runNodeBundles()
121
-
122
- const clientPlugins = createOssyClientRollupPlugins({
123
- nodeEnv: 'development',
124
- copyPublicFrom: publicDir,
125
- buildPath,
126
- })
127
-
128
- // `dir` must not be an ancestor of hydrate stubs (`build/.ossy/…`) or Rollup watch errors.
129
- const clientOutput = {
130
- dir: path.join(buildPath, 'public'),
131
- format: 'esm',
132
- entryFileNames ({ name }) {
133
- if (name.startsWith('hydrate__')) {
134
- const pageId = name.slice('hydrate__'.length)
135
- return `static/${pageId}.js`
136
- }
137
- return 'static/[name].js'
138
- },
139
- chunkFileNames: 'static/[name]-[hash].js',
140
- }
141
-
142
- let restartTimer = null
143
- const scheduleRestart = () => {
144
- clearTimeout(restartTimer)
145
- restartTimer = setTimeout(async () => {
146
- await triggerReload()
147
- restartServer()
148
- }, 100)
149
- }
150
-
151
- let serverProcess = null
152
- const startServer = () => {
153
- if (serverProcess) return
154
- serverProcess = spawn(process.execPath, [path.resolve(buildPath, 'server.js'), ...process.argv.slice(3)], {
155
- stdio: 'inherit',
156
- env: {
157
- ...process.env,
158
- OSSY_DEV_RELOAD: '1',
159
- NODE_ENV: 'development',
160
- },
161
- })
162
- serverProcess.on('exit', () => {
163
- serverProcess = null
164
- })
165
- }
166
-
167
- const restartServer = () => {
168
- if (!serverProcess) return startServer()
169
- serverProcess.kill('SIGTERM')
170
- serverProcess = null
171
- startServer()
172
- }
173
-
174
- const triggerReload = async () => {
175
- const port = process.env.PORT || '3000'
176
- try {
177
- await fetch(`http://localhost:${port}/__ossy_reload`, { method: 'POST' })
178
- } catch {
179
- // server might not be up yet
180
- }
181
- }
182
-
183
- const nodeWatchOpts = { watch: { clearScreen: false } }
184
- const watchConfigs = []
185
- if (Object.keys(clientHydrateInput).length > 0) {
186
- watchConfigs.push({
187
- input: clientHydrateInput,
188
- output: clientOutput,
189
- plugins: clientPlugins,
190
- watch: { clearScreen: false },
191
- })
192
- } else {
193
- watchConfigs.push({
194
- input: '\0ossy-dev-noop',
195
- output: {
196
- file: path.join(ossyDir, '.dev-noop-out.mjs'),
197
- format: 'esm',
198
- inlineDynamicImports: true,
199
- },
200
- plugins: [
201
- {
202
- name: 'ossy-dev-noop',
203
- resolveId (id) {
204
- if (id === '\0ossy-dev-noop') return id
205
- return null
206
- },
207
- load (id) {
208
- if (id === '\0ossy-dev-noop') return 'export default 0\n'
209
- return null
210
- },
211
- },
212
- ...createOssyAppBundlePlugins({ nodeEnv: 'development' }),
213
- ],
214
- ...nodeWatchOpts,
215
- })
216
- }
217
- const watcher = watch(watchConfigs)
218
-
219
- watcher.on('event', async (event) => {
220
- if (event.code === 'BUNDLE_START') {
221
- console.log(' \x1b[2m◐\x1b[0m \x1b[1m@ossy/app\x1b[0m \x1b[2mrollup watch …\x1b[0m')
222
- }
223
- if (event.code === 'ERROR') {
224
- console.error(' \x1b[31m✖\x1b[0m \x1b[1m@ossy/app\x1b[0m build error', event.error)
225
- }
226
- if (event.code === 'BUNDLE_END') {
227
- console.log(
228
- ` \x1b[32m✔\x1b[0m \x1b[1m@ossy/app\x1b[0m \x1b[2mbundles\x1b[0m \x1b[2m(${event.duration}ms)\x1b[0m`
229
- )
230
- }
231
- if (event.code === 'END') {
232
- writeAppRuntimeShims({
233
- middlewareSourcePath,
234
- configSourcePath,
235
- ossyDir,
236
- })
237
- copyOssyAppRuntime({ scriptDir, buildPath })
238
- if (currentPageFiles.length > 0) {
239
- const pageIds = [
240
- ...new Set(currentPageFiles.map((f) => clientHydrateIdForPage(f, srcDir))),
241
- ].sort()
242
- const overviewSnap = getBuildOverviewSnapshot({
243
- pagesSourcePath: pagesGeneratedPath,
244
- apiOverviewFiles,
245
- configPath,
246
- pageFiles: currentPageFiles,
247
- })
248
- const idToPath = Object.fromEntries(
249
- currentPageFiles.map((f) => [
250
- clientHydrateIdForPage(f, srcDir),
251
- filePathToRoute(f, srcDir).path,
252
- ])
253
- )
254
- const reporter = pageIds.length
255
- ? createBuildDashboard({
256
- mode: 'prerender-only',
257
- pageIds,
258
- idToPath,
259
- overview: {
260
- title: '@ossy/app dev',
261
- configRel: overviewSnap.configRel,
262
- apiRoutes: overviewSnap.apiRoutes,
263
- },
264
- })
265
- : null
266
- reporter?.start()
267
- if (
268
- !resourceTemplatesDevLogged &&
269
- resourceTemplatesResult.wrote &&
270
- resourceTemplatesResult.path &&
271
- reporter
272
- ) {
273
- reporter.pushLog(
274
- `[resource-templates] merged ${resourceTemplatesResult.count} → ${path.relative(process.cwd(), resourceTemplatesResult.path)}`
275
- )
276
- resourceTemplatesDevLogged = true
277
- }
278
- try {
279
- await prerenderReactTask.handler({
280
- op: 'prerenderPagesParallel',
281
- pagesEntryPath,
282
- configSourcePath,
283
- publicDir: path.join(buildPath, 'public'),
284
- reporter,
285
- })
286
- } catch (err) {
287
- console.error(' \x1b[31m✖\x1b[0m \x1b[1m@ossy/app\x1b[0m prerender failed', err)
288
- } finally {
289
- reporter?.dispose()
290
- }
291
- }
292
- scheduleRestart()
293
- }
294
- })
295
-
296
- const regenApiGenerated = () => {
297
- if (options['--api-source']) return
298
- void runNodeBundles().then(() => scheduleRestart())
299
- }
300
-
301
- const regenTasksGenerated = () => {
302
- void runNodeBundles().then(() => scheduleRestart())
303
- }
304
-
305
- fs.watch(srcDir, { recursive: true }, (eventType, filename) => {
306
- if (!filename) return
307
- if (/\.page\.(jsx?|tsx?)$/.test(filename)) {
308
- const refreshedPageFiles = discoverFilesByPattern(srcDir, PAGE_FILE_PATTERN)
309
- currentPageFiles = refreshedPageFiles
310
- const regenPath = path.join(ossyGeneratedDir(buildPath), OSSY_GEN_PAGES_BASENAME)
311
- writePagesManifest({
312
- pageFiles: refreshedPageFiles,
313
- srcDir,
314
- pagesGeneratedPath: regenPath,
315
- })
316
- writePageHydrateStubs(refreshedPageFiles, srcDir, ossyDir)
317
- void runNodeBundles().then(() => {
318
- if (typeof watcher?.invalidate === 'function') {
319
- for (const f of refreshedPageFiles) {
320
- const hid = clientHydrateIdForPage(f, srcDir)
321
- watcher.invalidate(path.join(ossyDir, `hydrate-${hid}.jsx`))
322
- }
323
- }
324
- scheduleRestart()
325
- })
326
- }
327
- if (/\.api\.(mjs|cjs|js)$/.test(filename)) {
328
- regenApiGenerated()
329
- }
330
- if (/\.task\.(mjs|cjs|js)$/.test(filename)) {
331
- regenTasksGenerated()
332
- }
333
- const norm = filename.replace(/\\/g, '/')
334
- if (/\.resource\.js$/.test(norm) && norm.includes('resource-templates/')) {
335
- writeResourceTemplatesBarrelIfPresent({ cwd: process.cwd(), log: false })
336
- const rtOut = path.join(resourceTemplatesDir(process.cwd()), OSSY_RESOURCE_TEMPLATES_OUT)
337
- if (fs.existsSync(rtOut) && typeof watcher?.invalidate === 'function') {
338
- watcher.invalidate(rtOut)
339
- }
340
- }
341
- })
342
- };