-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (104 loc) · 3.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* shipwright hook
*
* @description :: A hook definition. Extends Sails by adding shadow routes, implicit actions, and/or initialization logic.
* @docs :: https://sailsjs.com/docs/concepts/extending-sails/hooks
*/
const path = require('path')
const { defineConfig, mergeRsbuildConfig } = require('@rsbuild/core')
module.exports = function defineShipwrightHook(sails) {
return {
/**
* Runs when this Sails app loads/lifts.
*/
initialize: async function () {
const appPath = sails.config.appPath
const defaultConfigs = defineConfig({
source: {
entry: {
app: path.resolve(appPath, 'assets', 'js', 'app.js')
},
alias: {
'@': path.resolve(appPath, 'assets', 'js'),
'~': path.resolve(appPath, 'assets')
}
},
output: {
filenameHash: false,
distPath: {
root: '.tmp/public',
css: 'css',
js: 'js',
font: 'fonts',
image: 'images',
html: '/'
},
copy: [
{
from: path.resolve(appPath, 'assets', 'images'),
to: path.resolve(appPath, '.tmp', 'public', 'images'),
noErrorOnMissing: true
},
{
from: path.resolve(appPath, 'assets', 'fonts'),
to: path.resolve(appPath, '.tmp', 'public', 'fonts'),
noErrorOnMissing: true
},
{
from: path.resolve(appPath, 'assets', 'dependencies'),
to: path.resolve(appPath, '.tmp', 'public', 'dependencies'),
noErrorOnMissing: true
},
{
context: path.resolve(appPath, 'assets'),
from: '**/*.html',
to: path.resolve(appPath, '.tmp', 'public'),
noErrorOnMissing: true
}
]
},
tools: {
htmlPlugin: false
},
performance: {
chunkSplit: {
strategy: 'all-in-one'
}
},
server: {
port: sails.config.port,
strictPort: true,
printUrls: false
}
})
const config = mergeRsbuildConfig(
defaultConfigs,
sails.config.shipwright.build
)
const { createRsbuild } = require('@rsbuild/core')
try {
const rsbuild = await createRsbuild({ rsbuildConfig: config })
if (process.env.NODE_ENV == 'production') {
rsbuild.build()
} else {
const rsbuildDevServer = await rsbuild.createDevServer()
sails.after('hook:http:loaded', async () => {
sails.hooks.http.app.use(rsbuildDevServer.middlewares)
sails.hooks.http.server.on(
'upgrade',
rsbuildDevServer.onHTTPUpgrade
)
})
sails.on('lifted', async () => {
await rsbuildDevServer.afterListen()
})
sails.on('lower', async () => {
await rsbuildDevServer.close()
})
}
} catch (error) {
sails.error(error)
}
}
}
}