Skip to content

Commit

Permalink
Expose shared code as window.shared in the front end app
Browse files Browse the repository at this point in the history
Signed-off-by: Axel Boberg <[email protected]>
  • Loading branch information
axelboberg committed Apr 20, 2024
1 parent 686599c commit 0bb0761
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 124 deletions.
4 changes: 1 addition & 3 deletions app/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { SocketContext } from './socketContext'

import { useWebsocket } from './hooks/useWebsocket'

import { deepApply } from './utils/apply'

import * as shortcuts from './utils/shortcuts'
import * as browser from './utils/browser'
import * as api from './api'
Expand Down Expand Up @@ -181,7 +179,7 @@ export default function App () {
be sure to copy the current state
or React won't treat it as an update
*/
const newLocal = deepApply({ ...localRef.current }, data)
const newLocal = window?.shared?.merge?.deep({ ...localRef.current }, data)
setLocal(newLocal)
}

Expand Down
120 changes: 0 additions & 120 deletions app/utils/apply.js

This file was deleted.

13 changes: 13 additions & 0 deletions shared/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: 2024 Sveriges Television AB
//
// SPDX-License-Identifier: MIT

/*
Expose the shared utils as window.bridge
if we're running in a browser
*/
if (typeof window !== 'undefined') {
window.shared = {
merge: require('./merge')
}
}
1 change: 1 addition & 0 deletions shared/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,5 @@ function mergeDeep (targetObj, sourceObj) {
}
return targetObj
}

exports.deep = mergeDeep
31 changes: 30 additions & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ module.exports = [
]
}
}),

merge({ ...DEFAULT_CONFIG }, {
name: 'shared',
entry: {
shared: './shared'
},
module: {
rules: [
{
test: /\.(js)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
[
'@babel/plugin-transform-runtime',
{ regenerator: true }
]
],
sourceType: 'script'
}
}
}
]
}
}),

merge({ ...DEFAULT_CONFIG }, {
name: 'api',
entry: {
Expand All @@ -78,7 +107,7 @@ module.exports = [
module: {
rules: [
{
test: /\.(js|jsx)$/,
test: /\.(js)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
Expand Down
1 change: 1 addition & 0 deletions webpack.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const prod = {
the server to /app/template.js
*/
assets: [
`${hash}.shared.bundle.js`,
`${hash}.app.bundle.css`,
`${hash}.app.bundle.js`,
`${hash}.api.bundle.js`
Expand Down

0 comments on commit 0bb0761

Please sign in to comment.