Skip to content

Commit

Permalink
feat: build pluginified app
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiVandivier committed Feb 13, 2024
1 parent 700628e commit 684db12
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
31 changes: 23 additions & 8 deletions cli/config/plugin.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const babelWebpackConfig = {
const cssRegex = /\.css$/
const cssModuleRegex = /\.module\.css$/

module.exports = ({ env: webpackEnv, config, paths }) => {
module.exports = ({ env: webpackEnv, config, paths, pluginifiedApp }) => {
const isProduction = webpackEnv === 'production'
const isDevelopment = !isProduction

Expand Down Expand Up @@ -94,18 +94,33 @@ module.exports = ({ env: webpackEnv, config, paths }) => {
].filter(Boolean)
}

// .d2/shell/src/index.js or .../plugin.index.js
const entry = pluginifiedApp
? paths.shellAppBundleEntrypoint
: paths.shellPluginBundleEntrypoint
// plugin.html or app.html
const htmlFilename = pluginifiedApp
? paths.pluginifiedAppLaunchPath
: paths.pluginLaunchPath
// .d2/shell/public/app.html or .../plugin.html
const htmlTemplate = pluginifiedApp
? paths.shellPublicPluginifiedAppHtml
: paths.shellPublicPluginHtml

const outputFilenamePrefix = pluginifiedApp ? 'app' : 'plugin'

return {
mode: webpackEnv,
bail: isProduction,
entry: paths.shellPluginBundleEntrypoint,
entry: entry,
output: {
path: paths.shellBuildOutput,
filename: isProduction
? 'static/js/plugin-[name].[contenthash:8].js'
: 'static/js/plugin.bundle.js',
? `static/js/${outputFilenamePrefix}-[name].[contenthash:8].js`
: `static/js/${outputFilenamePrefix}.bundle.js`,
chunkFilename: isProduction
? 'static/js/plugin-[name].[contenthash:8].chunk.js'
: 'static/js/plugin-[name].chunk.js',
? `static/js/${outputFilenamePrefix}-[name].[contenthash:8].chunk.js`
: `static/js/${outputFilenamePrefix}-[name].chunk.js`,
// TODO: investigate dev source maps here (devtoolModuleFilenameTemplate)
},
optimization: {
Expand Down Expand Up @@ -143,8 +158,8 @@ module.exports = ({ env: webpackEnv, config, paths }) => {
Object.assign(
{
inject: true,
filename: paths.pluginLaunchPath,
template: paths.shellPublicPluginHtml,
filename: htmlFilename,
template: htmlTemplate,
},
isProduction
? {
Expand Down
4 changes: 4 additions & 0 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const handler = async ({
const config = parseConfig(paths)
const shell = makeShell({ config, paths })
const plugin = makePlugin({ config, paths })
const pluginifiedApp = makePlugin({ config, paths, pluginifiedApp: true })

if (config.type === 'app') {
setAppParameters(standalone, config)
Expand Down Expand Up @@ -132,6 +133,9 @@ const handler = async ({
reporter.info('Building appShell...')
await shell.build()

reporter.info('Building pluginified app...')
await pluginifiedApp.build()

if (config.entryPoints.plugin) {
reporter.info('Building plugin...')
await plugin.build()
Expand Down
6 changes: 6 additions & 0 deletions cli/src/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = (cwd = process.cwd()) => {
shell: path.join(base, './.d2/shell'),
shellSrc: path.join(base, './.d2/shell/src'),
shellAppEntrypoint: path.join(base, './.d2/shell/src/App.js'),
shellAppBundleEntrypoint: path.join(base, './.d2/shell/src/index.js'),
shellAppDirname,
shellApp: path.join(base, `./.d2/shell/${shellAppDirname}`),
shellPluginBundleEntrypoint: path.join(
Expand All @@ -75,6 +76,10 @@ module.exports = (cwd = process.cwd()) => {
base,
'./.d2/shell/public/plugin.html'
),
shellPublicPluginifiedAppHtml: path.join(
base,
'./.d2/shell/public/app.html'
),
shellPublicServiceWorker: path.join(
base,
'./.d2/shell/public/service-worker.js'
Expand Down Expand Up @@ -114,6 +119,7 @@ module.exports = (cwd = process.cwd()) => {

launchPath: 'index.html',
pluginLaunchPath: 'plugin.html',
pluginifiedAppLaunchPath: 'app.html',
}

reporter.debug('PATHS', paths)
Expand Down
3 changes: 2 additions & 1 deletion cli/src/lib/plugin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ const { reporter } = require('@dhis2/cli-helpers-engine')
const webpack = require('webpack')
const webpackConfigFactory = require('../../../config/plugin.webpack.config')

module.exports = async ({ config, paths }) => {
module.exports = async ({ config, paths, pluginifiedApp }) => {
reporter.debug('Building plugin...')

const webpackConfig = webpackConfigFactory({
env: 'production',
config,
paths,
pluginifiedApp,
})
const compiler = webpack(webpackConfig)
return new Promise((resolve, reject) => {
Expand Down
6 changes: 3 additions & 3 deletions cli/src/lib/plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const build = require('./build')
const start = require('./start')

module.exports = ({ config, paths }) => ({
build: () => build({ config, paths }),
start: ({ port }) => start({ port, config, paths }),
module.exports = ({ config, paths, pluginifiedApp = false }) => ({
build: () => build({ config, paths, pluginifiedApp }),
start: ({ port }) => start({ port, config, paths, pluginifiedApp }),
})
3 changes: 2 additions & 1 deletion cli/src/lib/plugin/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const webpackConfigFactory = require('../../../config/plugin.webpack.config')

module.exports = async ({ port, config, paths }) => {
module.exports = async ({ port, config, paths, pluginifiedApp }) => {
const webpackConfig = webpackConfigFactory({
// todo: change to development, but this creates a compilation error
// can read more here: https://github.com/dhis2/app-platform/pull/740/files/69411d9b61845cbd0d053f2313bdbd4e80fdf2ac#r1031576956
env: 'production',
config,
paths,
pluginifiedApp,
})
const compiler = webpack(webpackConfig)

Expand Down
26 changes: 26 additions & 0 deletions shell/public/app.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>Plugin</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 684db12

Please sign in to comment.