Skip to content

Commit e2bda71

Browse files
authored
Merge pull request #12 from SuperFlyTV/fix/assets-handling
fix: assets json not being generated
2 parents c59245f + 6b46b0d commit e2bda71

File tree

7 files changed

+108
-30
lines changed

7 files changed

+108
-30
lines changed

lib/main.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
declare module '*.png' // PNG file format is handled by rollup
22
declare module '*.svg' // SVG file format is handled by rollup
3+
declare module '*.gif' // GIF file format is handled by rollup
34

45
declare const __VERSION__: string // Injected by rollup
56
declare const __VERSION_TSR__: string // Injected by rollup

lib/rollup/configFactory.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import replacePlugin from '@rollup/plugin-replace'
88
import { getTranslations } from '../translation/bundle.mjs'
99
import { urlPluginExt } from './urlPlugin.mjs'
1010
import { uploadPlugin } from './uploadPlugin.mjs'
11+
import { tsconfigPathsPlugin } from './tsPathsPlugin.mjs'
1112
import path from 'node:path'
1213
import { execSync } from 'node:child_process'
1314
import { createRequire } from 'node:module'
@@ -73,10 +74,11 @@ export async function RollupConfigFactory(sources, distDir, server, development)
7374

7475
generatedCode: 'es2015',
7576

76-
plugins: [server ? uploadPlugin(server, id, development) : undefined],
77+
plugins: [uploadPlugin(server, id, development)],
7778
},
7879

7980
plugins: [
81+
tsconfigPathsPlugin(),
8082
replacePlugin({
8183
preventAssignment: true,
8284
values: {

lib/rollup/tsPathsPlugin.mjs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import alias from '@rollup/plugin-alias'
2+
import ts from 'typescript'
3+
import path from 'node:path'
4+
import fs from 'node:fs'
5+
6+
function escapeRegExp(str) {
7+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
8+
}
9+
10+
/**
11+
* Reimplement a tsconfig-paths plugin for rollup, so that image assets follow the tsconfig paths.
12+
*
13+
*/
14+
export function tsconfigPathsPlugin() {
15+
const tsconfigPath = path.join(process.cwd(), 'tsconfig.json')
16+
17+
if (!fs.existsSync(tsconfigPath)) return null
18+
19+
const read = ts.readConfigFile(tsconfigPath, ts.sys.readFile)
20+
if (read.error) return null
21+
22+
const parsed = ts.parseJsonConfigFileContent(read.config, ts.sys, path.dirname(tsconfigPath))
23+
const baseUrl = parsed.options.baseUrl || '.'
24+
const paths = parsed.options.paths || {}
25+
26+
const absBase = path.resolve(path.dirname(tsconfigPath), baseUrl)
27+
28+
const entries = []
29+
for (const [key, targets] of Object.entries(paths)) {
30+
if (!targets || !targets[0]) continue
31+
const keyIsWildcard = key.endsWith('/*')
32+
const target = targets[0]
33+
const targetIsWildcard = target.endsWith('/*')
34+
const cleanedKey = keyIsWildcard ? key.slice(0, -2) : key
35+
const cleanedTarget = targetIsWildcard ? target.slice(0, -2) : target
36+
const replacementBase = path.join(absBase, cleanedTarget)
37+
38+
if (keyIsWildcard) {
39+
entries.push({
40+
// match imports like '@foo/bar' -> '@foo/(.*)'
41+
find: new RegExp('^' + escapeRegExp(cleanedKey) + '/(.*)$'),
42+
replacement: replacementBase + '/$1',
43+
})
44+
} else {
45+
entries.push({
46+
find: cleanedKey,
47+
replacement: replacementBase,
48+
})
49+
}
50+
}
51+
52+
if (entries.length === 0) return null
53+
54+
return alias({ entries })
55+
}

lib/rollup/uploadPlugin.mjs

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import fs from 'node:fs/promises'
33

44
/**
5+
* Plugin to upload the built blueprint to a server after bundling
6+
* This also writes out a json file of the assets, which is needed for the bundling process
57
* @returns {import('rollup').OutputPlugin}
68
*/
79
export const uploadPlugin = (server, bundleId, development) => {
@@ -20,23 +22,25 @@ export const uploadPlugin = (server, bundleId, development) => {
2022
} else if (item.type === 'chunk') {
2123
if (!item.isEntry) throw new Error(`Expected a single code chunk: ${id}`)
2224

23-
try {
24-
const res = await fetch(
25-
server + '/api/private/blueprints/restore/' + bundleId + (development ? '?developmentMode=1' : ''),
26-
{
27-
method: 'POST',
28-
body: item.code,
29-
headers: {
30-
'Content-Type': 'text/javascript',
31-
},
25+
if (server) {
26+
try {
27+
const res = await fetch(
28+
server + '/api/private/blueprints/restore/' + bundleId + (development ? '?developmentMode=1' : ''),
29+
{
30+
method: 'POST',
31+
body: item.code,
32+
headers: {
33+
'Content-Type': 'text/javascript',
34+
},
35+
}
36+
)
37+
if (!res.ok) {
38+
throw new Error(`HTTP ${res.status}: ${await res.text()}`)
3239
}
33-
)
34-
if (!res.ok) {
35-
throw new Error(`HTTP ${res.status}: ${await res.text()}`)
40+
console.log(`Blueprints '${id}' uploaded`)
41+
} catch (e) {
42+
console.error(`Blueprints '${id}' upload failed:`, e.toString(), e.stack)
3643
}
37-
console.log(`Blueprints '${id}' uploaded`)
38-
} catch (e) {
39-
console.error(`Blueprints '${id}' upload failed:`, e.toString(), e.stack)
4044
}
4145
} else {
4246
// @ts-expect-error
@@ -48,20 +52,22 @@ export const uploadPlugin = (server, bundleId, development) => {
4852
fs.writeFile(`dist/${bundleId}-assets.json`, JSON.stringify(assetsBundle)).catch((e) => {
4953
console.error(`Failed to write assets bundle to disk:`, e.toString(), e.stack)
5054
})
51-
try {
52-
const res = await fetch(server + '/api/private/blueprints/assets', {
53-
method: 'POST',
54-
body: JSON.stringify(assetsBundle),
55-
headers: {
56-
'Content-Type': 'application/json',
57-
},
58-
})
59-
if (!res.ok) {
60-
throw new Error(`HTTP ${res.status}: ${await res.text()}`)
55+
if (server) {
56+
try {
57+
const res = await fetch(server + '/api/private/blueprints/assets', {
58+
method: 'POST',
59+
body: JSON.stringify(assetsBundle),
60+
headers: {
61+
'Content-Type': 'application/json',
62+
},
63+
})
64+
if (!res.ok) {
65+
throw new Error(`HTTP ${res.status}: ${await res.text()}`)
66+
}
67+
console.log(`Blueprints assets uploaded`)
68+
} catch (e) {
69+
console.error(`Blueprints assets upload failed:`, e.toString(), e.stack)
6170
}
62-
console.log(`Blueprints assets uploaded`)
63-
} catch (e) {
64-
console.error(`Blueprints assets upload failed:`, e.toString(), e.stack)
6571
}
6672
} else {
6773
fs.rm(`dist/${bundleId}-assets.json`).catch(() => {

lib/rollup/urlPlugin.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import fs from 'node:fs/promises'
1010
* @returns {import('rollup').Plugin}
1111
*/
1212
export const urlPluginExt = (destDir) => {
13-
const include = ['**/*.svg', '**/*.png']
13+
const include = ['**/*.svg', '**/*.png', '**/*.gif']
1414
const sourceDir = null // TODO?
1515
const fileName = '[hash][extname]'
1616

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"node": ">=20.0.0"
1919
},
2020
"dependencies": {
21+
"@rollup/plugin-alias": "^6.0.0",
2122
"@rollup/plugin-commonjs": "^29.0.0",
2223
"@rollup/plugin-json": "^6.1.0",
2324
"@rollup/plugin-node-resolve": "^16.0.3",

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ __metadata:
354354
languageName: node
355355
linkType: hard
356356

357+
"@rollup/plugin-alias@npm:^6.0.0":
358+
version: 6.0.0
359+
resolution: "@rollup/plugin-alias@npm:6.0.0"
360+
peerDependencies:
361+
rollup: ">=4.0.0"
362+
peerDependenciesMeta:
363+
rollup:
364+
optional: true
365+
checksum: 10c0/d3cd6a236b6d209dd9d3882fab84ad6bd253dcc5bb9fc1308ac1c08d4217ce5cfac805271ebe36daab4816f6bf5096b1f824a9eb1eb4158c67976fde765266be
366+
languageName: node
367+
linkType: hard
368+
357369
"@rollup/plugin-commonjs@npm:^29.0.0":
358370
version: 29.0.0
359371
resolution: "@rollup/plugin-commonjs@npm:29.0.0"
@@ -3292,6 +3304,7 @@ __metadata:
32923304
version: 0.0.0-use.local
32933305
resolution: "sofie-blueprint-tools@workspace:."
32943306
dependencies:
3307+
"@rollup/plugin-alias": "npm:^6.0.0"
32953308
"@rollup/plugin-commonjs": "npm:^29.0.0"
32963309
"@rollup/plugin-json": "npm:^6.1.0"
32973310
"@rollup/plugin-node-resolve": "npm:^16.0.3"

0 commit comments

Comments
 (0)