Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
DominusKelvin committed Jun 6, 2024
2 parents 3d3544b + a11d465 commit f85c0bf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sails-hook-shipwright",
"version": "0.1.2",
"version": "0.2.0",
"description": "The modern asset pipeline for Sails",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit f85c0bf

Please sign in to comment.