Skip to content

Commit

Permalink
feat: webpack config for plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mediremi authored and KaiVandivier committed Aug 23, 2022
1 parent 8e4dbff commit 3e4275c
Show file tree
Hide file tree
Showing 10 changed files with 680 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const i18n = require('../lib/i18n')
const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const makePlugin = require('../lib/plugin')
const { injectPrecacheManifest } = require('../lib/pwa')
const makeShell = require('../lib/shell')
const { validatePackage } = require('../lib/validatePackage')
Expand Down Expand Up @@ -67,6 +68,7 @@ const handler = async ({

const config = parseConfig(paths)
const shell = makeShell({ config, paths })
const plugin = makePlugin({ config, paths })

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

if (config.entryPoints.plugin) {
await plugin.build()
}

if (config.pwa.enabled) {
reporter.info('Injecting precache manifest...')
await injectPrecacheManifest(paths, config)
Expand Down
19 changes: 18 additions & 1 deletion cli/src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const i18n = require('../lib/i18n')
const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const makePlugin = require('../lib/plugin')
const createProxyServer = require('../lib/proxy')
const { compileServiceWorker } = require('../lib/pwa')
const makeShell = require('../lib/shell')
Expand All @@ -29,6 +30,7 @@ const handler = async ({

const config = parseConfig(paths)
const shell = makeShell({ config, paths })
const plugin = makePlugin({ config, paths })

if (config.type !== 'app') {
reporter.error(
Expand Down Expand Up @@ -128,7 +130,22 @@ const handler = async ({
)
reporter.print('')

await shell.start({ port: newPort })
const shellStartPromise = shell.start({ port: newPort })

if (config.entryPoints.plugin) {
const pluginPort = await detectPort(newPort)
reporter.print(
`The plugin is now available on port ${pluginPort}`
)
reporter.print('')

await Promise.all([
shellStartPromise,
plugin.start({ port: pluginPort }),
])
} else {
await shellStartPromise
}
},
{
name: 'start',
Expand Down
9 changes: 9 additions & 0 deletions cli/src/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,24 @@ module.exports = (cwd = process.cwd()) => {
d2: path.join(base, './.d2/'),
appOutputFilename: 'App.js',
shell: path.join(base, './.d2/shell'),
shellSrc: path.join(base, './.d2/shell/src'),
shellAppEntrypoint: path.join(base, './.d2/shell/src/App.js'),
shellAppDirname,
shellApp: path.join(base, `./.d2/shell/${shellAppDirname}`),
shellPluginBundleEntrypoint: path.join(
base,
'./.d2/shell/src/plugin.js'
),
shellPluginEntrypoint: path.join(base, './.d2/shell/src/Plugin.js'),
shellSrcServiceWorker: path.join(
base,
'./.d2/shell/src/service-worker.js'
),
shellPublic: path.join(base, './.d2/shell/public'),
shellPublicPluginHtml: path.join(
base,
'./.d2/shell/public/plugin.html'
),
shellPublicServiceWorker: path.join(
base,
'./.d2/shell/public/service-worker.js'
Expand Down
38 changes: 38 additions & 0 deletions cli/src/lib/plugin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Based on CRA build script

// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'production'
process.env.NODE_ENV = 'production'

const webpack = require('webpack')
const webpackConfigFactory = require('./webpack.config')

module.exports = async ({ paths }) => {
console.log('Building plugin...')

const webpackConfig = webpackConfigFactory({ env: 'production', paths })
const compiler = webpack(webpackConfig)
return new Promise((resolve, reject) => {
compiler.run(err => {
if (err) {
if (!err.message) {
reject(err)
return
}

let errMessage = err.message
// Add additional information for postcss errors
if (Object.prototype.hasOwnProperty.call(err, 'postcssNode')) {
errMessage +=
'\nCompileError: Begins at CSS selector ' +
err['postcssNode'].selector
}

reject(new Error(errMessage))
return
}

resolve()
})
})
}
17 changes: 17 additions & 0 deletions cli/src/lib/plugin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const build = require('./build')
const start = require('./start')

module.exports = ({ config, paths }) => ({
// build: () => build({ config, paths }),
build: async () => {
try {
await build({ config, paths })
} catch (error) {
console.log('\n\nerror building plugin:')
console.error(error)
console.log('\n')
throw error
}
},
start: ({ port }) => start({ port, config, paths }),
})
3 changes: 3 additions & 0 deletions cli/src/lib/plugin/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO
// module.exports = ({ port, config, paths }) => {}
module.exports = () => {}
Loading

0 comments on commit 3e4275c

Please sign in to comment.