Skip to content

Commit

Permalink
feat: build unbundled libraries, validate package when building (LIBS…
Browse files Browse the repository at this point in the history
…-125) (#492)


Improves treeshaking of platform library builds

Offers to automatically update package.json for library builds, see [the documentation for more information](https://platform.dhis2.nu/#/troubleshooting?id=upgrading-to-v56-for-libraries-only-correcting-packagejson-export-declarations)
  • Loading branch information
amcgee committed Feb 20, 2021
1 parent 0da2a3c commit 4fb3fae
Show file tree
Hide file tree
Showing 15 changed files with 497 additions and 691 deletions.
12 changes: 9 additions & 3 deletions adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
"publishConfig": {
"access": "public"
},
"main": "build/cjs/lib.js",
"module": "build/es/lib.js",
"main": "./build/cjs/index.js",
"module": "./build/es/index.js",
"exports": {
"import": "./build/es/index.js",
"require": "./build/cjs/index.js"
},
"files": [
"build"
],
Expand All @@ -22,7 +26,9 @@
"devDependencies": {
"@dhis2/cli-app-scripts": "5.5.3",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5"
"enzyme-adapter-react-16": "^1.15.5",
"react": "^16.8",
"react-dom": "^16.8"
},
"scripts": {
"build": "d2-app-scripts build",
Expand Down
43 changes: 0 additions & 43 deletions cli/config/app.babel.config.js

This file was deleted.

32 changes: 0 additions & 32 deletions cli/config/babel.config.js

This file was deleted.

6 changes: 4 additions & 2 deletions cli/config/jest.transform.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const babelConfig = require('./babel.config')
module.exports = require('babel-jest').createTransformer(babelConfig)
const makeBabelConfig = require('./makeBabelConfig.js')
module.exports = require('babel-jest').createTransformer(
makeBabelConfig({ mode: 'test' })
)
74 changes: 74 additions & 0 deletions cli/config/makeBabelConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const browserTargets = require('./.browserlistrc')
const jestTargets = { node: 'current' }

const getBabelModuleType = moduleType => {
switch (moduleType) {
case 'cjs':
case 'commonjs':
return 'commonjs'
case 'systemjs':
case 'umd':
case 'amd':
return moduleType
case 'es':
default:
return false
}
}
const makeBabelConfig = ({ moduleType, mode }) => {
const isTest = mode === 'test'

return {
presets: [
require('@babel/preset-react'),
require('@babel/preset-typescript'),
[
require('@babel/preset-env'),
{
modules: isTest
? 'commonjs'
: getBabelModuleType(moduleType),
targets: isTest ? jestTargets : browserTargets,
},
],
],
plugins: [
// Adds syntax support for import()
require('@babel/plugin-syntax-dynamic-import').default,

/*
* These plugins actually transform code
*/

// Automatically include a React import when JSX is present
require('babel-plugin-react-require'),

// Adds support for arrow-function class properties
// class { handleClick = () => { } }
require('@babel/plugin-proposal-class-properties'),

// Adds syntax support for optional chaining (?.)
require('@babel/plugin-proposal-optional-chaining'),

// Adds support for default value using ?? operator
require('@babel/plugin-proposal-nullish-coalescing-operator'),
],
env: {
production: {
plugins: [
[require('styled-jsx/babel'), { optimizeForSpeed: true }],
],
},
development: {
plugins: [
[require('styled-jsx/babel'), { optimizeForSpeed: true }],
],
},
test: {
plugins: [require('styled-jsx/babel-test')],
},
},
}
}

module.exports = makeBabelConfig
14 changes: 1 addition & 13 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,11 @@
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
"@babel/plugin-syntax-class-properties": "^7.8.3",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.8.3",
"@babel/plugin-syntax-optional-chaining": "^7.8.3",
"@babel/preset-env": "^7.9.0",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.6.0",
"@dhis2/app-shell": "5.5.3",
"@dhis2/cli-helpers-engine": "^1.5.0",
"@dhis2/cli-helpers-engine": "^2.1.1",
"archiver": "^3.1.1",
"axios": "^0.20.0",
"babel-jest": "^24.9.0",
Expand All @@ -53,14 +49,6 @@
"lodash": "^4.17.11",
"parse-author": "^2.0.0",
"parse-gitignore": "^1.0.1",
"rollup": "^1.21.4",
"rollup-plugin-babel": "^4.3.2",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.0.1",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-visualizer": "^3.2.2",
"styled-jsx": "<3.3.3"
},
"bin": {
Expand Down
50 changes: 44 additions & 6 deletions cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const makeShell = require('../lib/shell')
const { validatePackage } = require('../lib/validatePackage')

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

Expand Down Expand Up @@ -51,6 +52,7 @@ const handler = async ({
watch,
standalone,
shell: shellSource,
verify,
force,
}) => {
const paths = makePaths(cwd)
Expand All @@ -72,6 +74,20 @@ const handler = async ({

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)
}

reporter.info('Generating internationalization strings...')
await i18n.extract({ input: paths.src, output: paths.i18nStrings })
await i18n.generate({
Expand All @@ -88,16 +104,33 @@ const handler = async ({
reporter.info(
`Building ${config.type} ${chalk.bold(config.name)}...`
)
await compile({
config,
paths,
mode,
watch,
})

if (config.type === 'app') {
await compile({
config,
paths,
mode,
watch,
})
reporter.info('Building appShell...')
await shell.build()
} else {
await Promise.all([
compile({
config,
paths,
moduleType: 'es',
mode,
watch,
}),
compile({
config,
paths,
moduleType: 'cjs',
mode,
watch,
}),
])
}
},
{
Expand Down Expand Up @@ -146,6 +179,11 @@ const command = {
description: 'Build in development mode',
conflicts: 'mode',
},
verify: {
type: 'boolean',
description: 'Validate package before building',
default: true,
},
watch: {
type: 'boolean',
description: 'Watch source files for changes',
Expand Down
12 changes: 12 additions & 0 deletions cli/src/commands/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const loadEnvFiles = require('../lib/loadEnvFiles')
const parseConfig = require('../lib/parseConfig')
const makePaths = require('../lib/paths')
const makeShell = require('../lib/shell')
const { validatePackage } = require('../lib/validatePackage')

const defaultPort = 3000

Expand Down Expand Up @@ -35,6 +36,17 @@ const handler = async ({

await exitOnCatch(
async () => {
if (!(await validatePackage({ config, paths, offerFix: false }))) {
reporter.print(
'Package validation issues are ignored when running "d2-app-scripts start"'
)
reporter.print(
`${chalk.bold(
'HINT'
)}: Run "d2-app-scripts build" to automatically fix some of these issues`
)
}

reporter.info('Generating internationalization strings...')
await i18n.extract({ input: paths.src, output: paths.i18nStrings })
await i18n.generate({
Expand Down
Loading

0 comments on commit 4fb3fae

Please sign in to comment.