-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,586 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,20 @@ | ||
# build-npm-package | ||
# @shko.online/build-npm-package | ||
This repository contains our npm package that simplifies building npm packages with embedded typings. | ||
|
||
This package uses `babel` and `typescript` to generate different builds of your package and make it compatible with different module types. | ||
The following repository has been used as "inspiration" https://github.com/storybookjs/storybook/tree/main/scripts and a lot of credit goes to the `StorybookJs` contributors. | ||
|
||
To use this package install it as a development dependency from NPM. | ||
|
||
`npm` | ||
```cmd | ||
npm install -D @shko.online/build-npm-package | ||
``` | ||
|
||
`yarn` | ||
```cmd | ||
yarn add -D @shko.online/build-npm-package | ||
``` | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const chalk = require('chalk'); | ||
const fs = require('fs-extra'); | ||
const path = require('path'); | ||
const { run } = require('./run'); | ||
|
||
function getCommand(watch, dir) { | ||
const args = [ | ||
'./src', | ||
`--out-dir=${dir}`, | ||
`--config-file=${path.resolve('.babelrc.js').replace(' ', '\\ ')}`, | ||
'--extensions=.js,.jsx,.ts,.tsx' | ||
]; | ||
|
||
if (watch) { | ||
args.push('-w'); | ||
} | ||
console.log(`npx babel ${args.join(' ')}`); | ||
return `npx babel ${args.join(' ')}`; | ||
} | ||
|
||
async function babelify(options = {}) { | ||
const { watch = false, silent = true, errorCallback } = options; | ||
|
||
if (!(await fs.pathExists('src'))) { | ||
if (!silent) { | ||
console.warn(`No '${chalk.yellow('src')}' dir`); | ||
} | ||
return; | ||
} | ||
|
||
const runners = watch | ||
? | ||
['./lib/cjs', './lib/esm'].map((dir) => { | ||
const command = getCommand(watch, dir); | ||
return run({ watch, dir, silent, command, errorCallback }); | ||
}) | ||
: ['./lib/cjs', './lib/esm', './lib/modern'].map((dir) => { | ||
const command = getCommand(watch, dir); | ||
return run({ dir, silent, command, errorCallback }); | ||
}); | ||
|
||
await Promise.all(runners); | ||
} | ||
|
||
module.exports = { | ||
babelify, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/env node | ||
|
||
const { babelify } = require('./babelify'); | ||
const { tscfy } = require('./tscfy'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs-extra'); | ||
const log = require('npmlog'); | ||
const path = require('path'); | ||
const readPkgUp = require('read-pkg-up'); | ||
const shell = require('shelljs'); | ||
|
||
const distFolder = 'lib'; | ||
|
||
async function removeDist() { | ||
await fs.remove(distFolder); | ||
} | ||
|
||
async function cleanup() { | ||
// remove files after babel --copy-files output | ||
if (await fs.pathExists(path.join(process.cwd(), distFolder))) { | ||
|
||
const filesToRemove = shell.find(distFolder).filter((filePath) => { | ||
|
||
|
||
// Remove all copied TS files (but not the .d.ts) | ||
if (/\.tsx?$/.test(filePath) && !/\.d\.ts$/.test(filePath)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}); | ||
if (filesToRemove.length) { | ||
shell.rm('-f', ...filesToRemove); | ||
} | ||
} | ||
} | ||
|
||
function logError(type, packageJson, errorLogs) { | ||
log.error(`FAILED (${type}) : ${errorLogs}`); | ||
log.error( | ||
`FAILED to compile ${type}: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}` | ||
); | ||
} | ||
|
||
|
||
async function build({ cwd, flags }) { | ||
const modules = true; | ||
const { packageJson } = await readPkgUp(cwd); | ||
const message = chalk.gray(`Built: ${chalk.bold(`${packageJson.name}@${packageJson.version}`)}`); | ||
console.time(message); | ||
|
||
if (flags.includes('--reset')) { | ||
await removeDist(); | ||
} | ||
|
||
await Promise.all([ | ||
babelify({ | ||
modules, | ||
watch: flags.includes('--watch'), | ||
errorCallback: (errorLogs) => logError('js', packageJson, errorLogs), | ||
}), | ||
tscfy({ | ||
watch: flags.includes('--watch'), | ||
errorCallback: (errorLogs) => logError('ts', packageJson, errorLogs), | ||
}), | ||
]); | ||
|
||
await cleanup(); | ||
console.timeEnd(message); | ||
} | ||
|
||
const flags = process.argv.slice(2); | ||
const cwd = process.cwd(); | ||
|
||
build({ cwd, flags }); |
Oops, something went wrong.