Skip to content

Commit

Permalink
feat(cli): create plugin entrypoint wrapper during compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
mediremi authored and KaiVandivier committed Aug 23, 2022
1 parent 04ece0a commit 8e4dbff
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
11 changes: 9 additions & 2 deletions cli/src/lib/compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const fs = require('fs-extra')
const makeBabelConfig = require('../../../config/makeBabelConfig.js')
const {
verifyEntrypoints,
overwriteAppEntrypoint,
createAppEntrypointWrapper,
createPluginEntrypointWrapper,
} = require('./entrypoints.js')
const {
extensionPattern,
Expand Down Expand Up @@ -70,10 +71,16 @@ const compile = async ({

verifyEntrypoints({ config, paths })
if (isApp) {
await overwriteAppEntrypoint({
await createAppEntrypointWrapper({
entrypoint: config.entryPoints.app,
paths,
})
if (config.entryPoints.plugin) {
await createPluginEntrypointWrapper({
entrypoint: config.entryPoints.plugin,
paths,
})
}
}

const outDir = isApp
Expand Down
20 changes: 16 additions & 4 deletions cli/src/lib/compiler/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,26 @@ exports.verifyEntrypoints = ({
verifyLibraryEntrypoint(config.entryPoints.lib)
}

exports.overwriteAppEntrypoint = async ({ entrypoint, paths }) => {
const getEntrypointWrapper = async ({ entrypoint, paths }) => {
const relativeEntrypoint = entrypoint.replace(/^(\.\/)?src\//, '')
const outRelativeEntrypoint = normalizeExtension(relativeEntrypoint)
const shellAppSource = await fs.readFile(paths.shellSourceEntrypoint)

return shellAppSource
.toString()
.replace(/'.\/D2App\/app'/g, `'./D2App/${outRelativeEntrypoint}'`)
}

exports.createAppEntrypointWrapper = async ({ entrypoint, paths }) => {
await fs.writeFile(
paths.shellAppEntrypoint,
shellAppSource
.toString()
.replace(/'.\/D2App\/app'/g, `'./D2App/${outRelativeEntrypoint}'`)
await getEntrypointWrapper({ entrypoint, paths })
)
}

exports.createPluginEntrypointWrapper = async ({ entrypoint, paths }) => {
await fs.writeFile(
paths.shellPluginEntrypoint,
await getEntrypointWrapper({ entrypoint, paths })
)
}
1 change: 1 addition & 0 deletions cli/src/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module.exports = (cwd = process.cwd()) => {
shellAppEntrypoint: path.join(base, './.d2/shell/src/App.js'),
shellAppDirname,
shellApp: path.join(base, `./.d2/shell/${shellAppDirname}`),
shellPluginEntrypoint: path.join(base, './.d2/shell/src/Plugin.js'),
shellSrcServiceWorker: path.join(
base,
'./.d2/shell/src/service-worker.js'
Expand Down
1 change: 1 addition & 0 deletions cli/src/lib/shell/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = ({ config, paths }) => ({
pipe: false,
})
},
// TODO: remove? Test command does not seem to call this method
test: async () => {
await exec({
cmd: 'yarn',
Expand Down
2 changes: 1 addition & 1 deletion shell/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#ffffff" />
<!--
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
Expand Down
26 changes: 26 additions & 0 deletions shell/public/plugin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<title>%REACT_APP_DHIS2_APP_NAME% plugin | DHIS2</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this plugin.</noscript>
<div id="dhis2-app-root"></div>
<div id="dhis2-portal-root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

0 comments on commit 8e4dbff

Please sign in to comment.