Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use creact-react-app/react-scripts instead of .d2 folder #711

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"node-http-proxy-json": "^0.1.9",
"parse-author": "^2.0.0",
"parse-gitignore": "^1.0.1",
"react-scripts": "^5.0.0",
"styled-jsx": "^4.0.1",
"webpack": "^5.41.1",
"workbox-build": "^6.1.5"
Expand All @@ -69,6 +70,7 @@
"testEnvironment": "node"
},
"devDependencies": {
"cross-spawn": "^7.0.3",
"outdent": "^0.8.0"
}
}
176 changes: 56 additions & 120 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,135 +4,94 @@ const fs = require('fs-extra')
const { compile } = require('../lib/compiler')
const exitOnCatch = require('../lib/exitOnCatch')
const generateManifests = require('../lib/generateManifests')
const i18n = require('../lib/i18n')
const i18n = require('../lib/i18n/index.js')
const {
buildModes,
determineBuildMode,
exitWhenPackageInvalid,
printBuildParam,
setAppParameters,
} = require('../lib/index.js')
const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const { injectPrecacheManifest } = require('../lib/pwa')
const makeShell = require('../lib/shell')
const { validatePackage } = require('../lib/validatePackage')
const { craBuild } = require('../lib/shell')
const { handler: pack } = require('./pack.js')

const buildModes = ['development', 'production']

const getNodeEnv = () => {
let nodeEnv = process.env['NODE_ENV']
if (nodeEnv) {
nodeEnv = nodeEnv.toLowerCase()
if (buildModes.includes(nodeEnv)) {
return nodeEnv
}
}
return null
}

const printBuildParam = (key, value) => {
reporter.print(chalk.green(` - ${key} :`), chalk.yellow(value))
}
const setAppParameters = (standalone, config) => {
process.env.PUBLIC_URL = process.env.PUBLIC_URL || '.'
printBuildParam('PUBLIC_URL', process.env.PUBLIC_URL)

if (
standalone === false ||
(typeof standalone === 'undefined' && !config.standalone)
) {
const defaultBase = config.coreApp ? `..` : `../../..`
process.env.DHIS2_BASE_URL = process.env.DHIS2_BASE_URL || defaultBase

printBuildParam('DHIS2_BASE_URL', process.env.DHIS2_BASE_URL)
} else {
printBuildParam('DHIS2_BASE_URL', '<standalone>')
}
}

const handler = async ({
const handler = ({
cwd = process.cwd(),
mode,
mode: modeArg,
dev,
watch,
standalone,
shell: shellSource,
verify,
force,
pack: packAppOutput,
}) => {
const paths = makePaths(cwd)

mode = mode || (dev && 'development') || getNodeEnv() || 'production'
loadEnvFiles(paths, mode)

reporter.print(chalk.green.bold('Build parameters:'))
printBuildParam('Mode', mode)
verbose,
}) =>
exitOnCatch(
async () => {
const paths = makePaths(cwd)
const mode = determineBuildMode(modeArg, dev)

const config = parseConfig(paths)
const shell = makeShell({ config, paths })
loadEnvFiles(paths, mode)

if (config.type === 'app') {
setAppParameters(standalone, config)
}
reporter.print(chalk.green.bold('Build parameters:'))
printBuildParam('Mode', mode)

await fs.remove(paths.buildOutput)
const config = parseConfig(paths)

await exitOnCatch(
async () => {
if (
!(await validatePackage({
config,
paths,
offerFix: !process.env.CI,
noVerify: !verify,
}))
) {
reporter.error(
'Failed to validate package, use --no-verify to skip these checks'
)
process.exit(1)
if (config.type === 'app') {
setAppParameters(standalone, config)
}

reporter.info('Generating internationalization strings...')
await i18n.extract({
input: paths.src,
output: paths.i18nStrings,
paths,
})
await i18n.generate({
input: paths.i18nStrings,
output: paths.i18nLocales,
namespace: 'default',
paths,
})
await fs.remove(paths.buildOutput)
await exitWhenPackageInvalid(config, paths, verify)

if (config.type === 'app') {
reporter.info('Bootstrapping local appShell...')
await shell.bootstrap({ shell: shellSource, force })
}
reporter.info('Generating internationalization strings...')
await i18n.extractAndGenerate(paths)

reporter.info(
`Building ${config.type} ${chalk.bold(config.name)}...`
)

if (config.type === 'app') {
await compile({
config,
paths,
mode,
watch,
})

// Manifest generation moved here so these static assets can be
// precached by Workbox during the shell build step
reporter.info('Generating manifests...')
await generateManifests(paths, config, process.env.PUBLIC_URL)

// @TODO: Figure out how to do this
// const { injectPrecacheManifest } = require('../lib/pwa')
// if (config.pwa.enabled) {
// reporter.info('Injecting precache manifest...')
// await injectPrecacheManifest(paths, config)
// }

reporter.info('Ensuring that a build folder exists')
fs.ensureDirSync(paths.buildOutput)

// CRA Manages service worker compilation here
reporter.info('Building appShell...')
await shell.build()
reporter.info('Creating a production build...')
await craBuild({ config, verbose, cwd })

if (!fs.pathExistsSync(paths.buildAppOutput)) {
reporter.error('No build output found')
process.exit(1)
}

if (packAppOutput) {
const bundle = path.parse(paths.buildAppBundle)

if (config.pwa.enabled) {
reporter.info('Injecting precache manifest...')
await injectPrecacheManifest(paths, config)
await fs.remove(paths.buildAppBundleOutput)

// update bundle archive
await pack({
destination: path.resolve(cwd, bundle.dir),
filename: bundle.base,
})
}

reporter.print(chalk.green('\n**** DONE! ****'))
} else {
await Promise.all([
compile({
Expand All @@ -158,29 +117,6 @@ const handler = async ({
}
)

if (config.type === 'app') {
if (!fs.pathExistsSync(paths.shellBuildOutput)) {
reporter.error('No build output found')
process.exit(1)
}

await fs.copy(paths.shellBuildOutput, paths.buildAppOutput)

if (packAppOutput) {
const bundle = path.parse(paths.buildAppBundle)

await fs.remove(paths.buildAppBundleOutput)
// update bundle archive
await pack({
destination: path.resolve(cwd, bundle.dir),
filename: bundle.base,
})
}

reporter.print(chalk.green('\n**** DONE! ****'))
}
}

const command = {
aliases: 'b',
desc: 'Build a production app bundle for use with the DHIS2 app-shell',
Expand Down
Loading