-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
152 lines (150 loc) · 4.4 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* 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) {
function getManifestFiles() {
const manifestPath = path.resolve(
sails.config.appPath,
'.tmp',
'public',
'manifest.json'
)
const data = require(manifestPath)
const files = data.allFiles
return files
}
return {
generateScripts: function generateScripts() {
const manifestFiles = getManifestFiles()
let scripts = []
manifestFiles.forEach((file) => {
if (file.endsWith('.js')) {
scripts.push(`<script type="text/javascript" src="${file}"></script>`)
}
})
return scripts.join('\n')
},
generateStyles: function generateStyles() {
const manifestFiles = getManifestFiles()
let styles = []
manifestFiles.forEach((file) => {
if (file.endsWith('.css')) {
styles.push(`<link rel="stylesheet" href="${file}">`)
}
})
return styles.join('\n')
},
defaults: {
shipwright: {
build: {}
}
},
/**
* Runs when this Sails app loads/lifts.
*/
initialize: async function () {
const hook = this
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: {
manifest: true,
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: 'split-by-experience'
}
},
server: {
port: sails.config.port,
strictPort: true,
printUrls: false
},
dev: {
writeToDisk: (file) => file.includes('manifest.json') // Write manifest file
}
})
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') {
await rsbuild.build()
} else {
const rsbuildDevServer = await rsbuild.createDevServer()
sails.after('hook:http:loaded', async () => {
sails.hooks.http.app.use(rsbuildDevServer.middlewares)
rsbuildDevServer.connectWebSocket({
server: sails.hooks.http.server
})
})
sails.on('lifted', async () => {
await rsbuildDevServer.afterListen()
})
sails.on('lower', async () => {
await rsbuildDevServer.close()
})
sails.after('lifted', () => {})
}
sails.config.views.locals = {
shipwright: {
scripts: hook.generateScripts,
styles: hook.generateStyles
}
}
} catch (error) {
sails.log.error(error)
}
}
}
}