Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
BetimBeja committed Feb 15, 2023
1 parent 9f7ec40 commit f12e692
Show file tree
Hide file tree
Showing 8 changed files with 1,586 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 19 additions & 1 deletion README.md
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
```



47 changes: 47 additions & 0 deletions babelify.js
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,
};
75 changes: 75 additions & 0 deletions build.js
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 });
Loading

0 comments on commit f12e692

Please sign in to comment.