Skip to content

Commit

Permalink
feat: ts, build, docs and lint improvements (#732)
Browse files Browse the repository at this point in the history
* fix: improve ts cmd config

- get defaults from user config
- fix noEmit for js repos
- add more types

* feat: improve build cmd

- remove old bundlesize config, add new options for max bundlesize `bundlesizeMax`
- add more types
- add `bundlesizeMax` default value

BREAKING CHANGE: Config property `bundlesize.maxSize` is deprecated, use `build.bundlesizeMax`

* fix: improve docs cmd

- add more types
- clean code and improve Listr setup

* feat: big utils clean up

- remove unnecessary methods
- add some types
- improve `paths` export, with full paths to dist, test, src and package.json folder/file

* feat: clean lint cmd

- remove old npm deps semver version checks
- update to the new eslint api
- use a new output formatter that actually gives us links we can click and go to the errors

BREAKING CHANGE: old npm deps semver was removed

* fix: add types for cmd options
  • Loading branch information
hugomrdias authored Jan 29, 2021
1 parent 1212314 commit 846bb25
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 331 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const args = cli.fail((msg, err, yargs) => {
if (args.debug) {
console.error('\n', err)
} else {
console.error(chalk.red(err.message))
console.error(err.message)
}
}

Expand Down
6 changes: 6 additions & 0 deletions cmds/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ module.exports = {
describe: 'Analyse bundle size. Default threshold is 100kB, you can override that in `.aegir.js` with the property `bundlesize.maxSize`.',
default: userConfig.build.bundlesize
},
bundlesizeMax: {
alias: 'b',
type: 'boolean',
describe: 'Max threshold for the bundle size.',
default: userConfig.build.bundlesizeMax
},
types: {
type: 'boolean',
describe: 'Build the Typescripts type declarations.',
Expand Down
6 changes: 4 additions & 2 deletions cmds/ts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const { userConfig } = require('../src/config/user')

const EPILOG = `
Presets:
Expand All @@ -23,12 +24,13 @@ module.exports = {
type: 'string',
choices: ['config', 'check', 'types'],
describe: 'Preset to run',
alias: 'p'
alias: 'p',
default: userConfig.ts.preset
},
include: {
type: 'array',
describe: 'Values are merged into the local TS config include property.',
default: []
default: userConfig.ts.include
}
})
},
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@
},
"devDependencies": {
"@types/bytes": "^3.1.0",
"@types/eslint": "^7.2.6",
"@types/fs-extra": "^9.0.6",
"@types/gh-pages": "^3.0.0",
"@types/listr": "^0.14.2",
"@types/polka": "^0.5.2",
"@types/semver": "^7.3.4",
Expand Down
25 changes: 19 additions & 6 deletions src/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ const { fromAegir, gzipSize, pkg, hasTsconfig } = require('./../utils')
const tsCmd = require('../ts')
const { userConfig } = require('../config/user')

/**
* @typedef {import("../types").GlobalOptions} GlobalOptions
* @typedef {import("../types").BuildOptions} BuildOptions
*/

/**
* Build command
*
* @param {any} argv
* @param {GlobalOptions & BuildOptions} argv
*/
module.exports = async (argv) => {
const input = argv._.slice(1)
Expand All @@ -37,19 +42,23 @@ module.exports = async (argv) => {
], {
env: {
NODE_ENV: process.env.NODE_ENV || 'production',
AEGIR_BUILD_ANALYZE: argv.bundlesize,
AEGIR_NODE: argv.node,
AEGIR_TS: argv.tsRepo
AEGIR_BUILD_ANALYZE: argv.bundlesize ? 'true' : 'false',
AEGIR_NODE: argv.node ? 'true' : 'false',
AEGIR_TS: argv.tsRepo ? 'true' : 'false'
},
localDir: path.join(__dirname, '../..'),
preferLocal: true,
stdio: 'inherit'
})

if (argv.bundlesize) {
// @ts-ignore
if (userConfig.bundlesize?.maxSize) {
throw new Error('Config property `bundlesize.maxSize` is deprecated, use `build.bundlesizeMax`!')
}
const stats = readJsonSync(path.join(process.cwd(), 'dist/stats.json'))
const gzip = await gzipSize(path.join(stats.outputPath, stats.assets[0].name))
const maxsize = bytes(/** @type {string} */(userConfig.bundlesize.maxSize))
const maxsize = bytes(userConfig.build.bundlesizeMax)
const diff = gzip - maxsize

console.log('Use http://webpack.github.io/analyse/ to load "./dist/stats.json".')
Expand All @@ -64,6 +73,10 @@ module.exports = async (argv) => {
}

if (argv.types && hasTsconfig) {
await tsCmd({ ...argv, preset: 'types' })
await tsCmd({
...argv,
preset: 'types',
include: userConfig.ts.include
})
}
}
25 changes: 21 additions & 4 deletions src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ const { lilconfigSync } = require('lilconfig')
const merge = require('merge-options')
const utils = require('../utils')

/**
* @typedef {import("./../types").Options} Options
*/

/**
*
* @param {*} hooks
*/
function normalizeHooks (hooks = {}) {
const result = {
browser: {
Expand All @@ -29,6 +37,12 @@ function normalizeHooks (hooks = {}) {
return merge(result, hooks)
}

/**
* Search for local user config
*
* @param {string | undefined} [searchFrom]
* @returns {Options}
*/
const config = (searchFrom) => {
let userConfig
try {
Expand All @@ -49,6 +63,8 @@ const config = (searchFrom) => {
console.error(err)
throw new Error('Error finding your config file.')
}

/** @type {Options} */
const conf = merge(
{
// global options
Expand All @@ -60,14 +76,11 @@ const config = (searchFrom) => {
karma: {},
hooks: {},
entry: utils.fromRoot('src', 'index.js'),
bundlesize: {
path: './dist/index.min.js',
maxSize: '100kB'
},
// build cmd options
build: {
bundle: true,
bundlesize: false,
bundlesizeMax: '100kB',
types: true
},
// linter cmd options
Expand All @@ -91,6 +104,10 @@ const config = (searchFrom) => {
docs: {
publish: false,
entryPoint: 'src/index.js'
},
ts: {
preset: undefined,
include: []
}
},
userConfig,
Expand Down
9 changes: 5 additions & 4 deletions src/config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const path = require('path')
const pascalcase = require('pascalcase')
const webpack = require('webpack')
const merge = require('webpack-merge')
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const TerserPlugin = require('terser-webpack-plugin')
const { fromRoot, pkg, paths, getLibraryName } = require('../utils')
const { fromRoot, pkg, paths } = require('../utils')
const { userConfig } = require('./user')
const isProduction = process.env.NODE_ENV === 'production'
const isTSEnable = process.env.AEGIR_TS === 'true'
Expand All @@ -24,10 +25,10 @@ const base = (env, argv) => {
mode: isProduction ? 'production' : 'development',
entry: [isTSEnable ? fromRoot('src', 'index.ts') : fromRoot('src', 'index.js')],
output: {
path: fromRoot(paths.dist),
path: paths.dist,
filename: filename,
sourceMapFilename: filename + '.map',
library: getLibraryName(pkg.name),
library: pascalcase(pkg.name),
libraryTarget: 'umd',
globalObject: 'self', // Use `self` as `window` doesn't not exist within a Service/Web Worker context
devtoolModuleFilenameTemplate: info => 'file:' + encodeURI(info.absoluteResourcePath)
Expand All @@ -38,7 +39,7 @@ const base = (env, argv) => {
oneOf: [
{
test: /\.(js|ts)$/,
include: fromRoot(paths.src),
include: paths.src,
use: {
loader: require.resolve('babel-loader'),
options: {
Expand Down
39 changes: 31 additions & 8 deletions src/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ const fs = require('fs-extra')
const path = require('path')
const { premove: del } = require('premove')
const {
getListrConfig,
publishDocs,
hasTsconfig,
fromAegir,
fromRoot
} = require('../utils')
const ghPages = require('gh-pages')
const { promisify } = require('util')

const publishPages = promisify(ghPages.publish)

/**
* @typedef {import("../types").GlobalOptions} GlobalOptions
* @typedef {import("../types").DocsOptions} DocsOptions
* @typedef {import("listr").ListrTaskWrapper} Task
*
* @typedef {Object} Options
* @property {string} entryPoint - Entry point for typedoc (defaults: 'src/index.js')
* @property {string[]} forwardOptions - Extra options to forward to the backend
Expand All @@ -23,9 +29,10 @@ const {
/**
* Docs command
*
* @param {any} ctx
* @param {GlobalOptions & DocsOptions} ctx
* @param {Task} task
*/
const docs = async (ctx) => {
const docs = async (ctx, task) => {
/** @type {Options} */
const opts = {
forwardOptions: ctx['--'] ? ctx['--'] : [],
Expand All @@ -37,7 +44,7 @@ const docs = async (ctx) => {
return
}
// run typedoc
await execa(
const proc = execa(
'typedoc',
[
fromRoot(opts.entryPoint),
Expand All @@ -50,15 +57,29 @@ const docs = async (ctx) => {
],
{
localDir: path.join(__dirname, '..'),
preferLocal: true,
stdio: 'inherit'
preferLocal: true
}
)
proc.stdout?.on('data', chunk => {
task.output = chunk.toString()
})
await proc

// write .nojekyll file
fs.writeFileSync('docs/.nojekyll', '')
}

const publishDocs = () => {
return publishPages(
'docs',
// @ts-ignore - promisify returns wrong type
{
dotfiles: true,
message: 'chore: update documentation'
}
)
}

const TASKS = new Listr(
[
{
Expand All @@ -75,7 +96,9 @@ const TASKS = new Listr(
enabled: (ctx) => ctx.publish && hasTsconfig
}
],
getListrConfig()
{
renderer: 'default'
}
)

module.exports = TASKS
Loading

0 comments on commit 846bb25

Please sign in to comment.