Skip to content

Commit

Permalink
[feat] add asset hashing (#6)
Browse files Browse the repository at this point in the history
* feat: generate and inject script and style tags

* feat: change chunk split strategy to split-by-experience
  • Loading branch information
DominusKelvin authored Jun 6, 2024
1 parent 31000ab commit e0f8875
Showing 1 changed file with 49 additions and 6 deletions.
55 changes: 49 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,49 @@
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
}
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')
}

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')
}
return {
defaults: {
shipwright: {
build: {}
}
},
/**
* Runs when this Sails app loads/lifts.
*/
initialize: async function () {
const appPath = sails.config.appPath

const defaultConfigs = defineConfig({
source: {
entry: {
Expand All @@ -26,7 +62,7 @@ module.exports = function defineShipwrightHook(sails) {
}
},
output: {
filenameHash: false,
manifest: true,
distPath: {
root: '.tmp/public',
css: 'css',
Expand Down Expand Up @@ -64,13 +100,16 @@ module.exports = function defineShipwrightHook(sails) {
},
performance: {
chunkSplit: {
strategy: 'all-in-one'
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(
Expand All @@ -80,8 +119,8 @@ module.exports = function defineShipwrightHook(sails) {
const { createRsbuild } = require('@rsbuild/core')
try {
const rsbuild = await createRsbuild({ rsbuildConfig: config })
if (process.env.NODE_ENV == 'production') {
rsbuild.build()
if (process.env.NODE_ENV === 'production') {
await rsbuild.build()
} else {
const rsbuildDevServer = await rsbuild.createDevServer()
sails.after('hook:http:loaded', async () => {
Expand All @@ -97,9 +136,13 @@ module.exports = function defineShipwrightHook(sails) {
sails.on('lower', async () => {
await rsbuildDevServer.close()
})
sails.after('lifted', () => {})
}
sails.config.views.locals = {
shipwright: { scripts: generateScripts, styles: generateStyles }
}
} catch (error) {
sails.error(error)
sails.log.error(error)
}
}
}
Expand Down

0 comments on commit e0f8875

Please sign in to comment.