Skip to content

Commit

Permalink
chore: Build multiple modules instead of one index.js
Browse files Browse the repository at this point in the history
- refactor: Move build command to build.sh
  note: this will allow multiple module build entry files

- feat: Add indexify.js script, this will generate moduleName.js files
  note: this is because tsdx.config.js override do not generate js files

- feat: Add tsdx.config.js to generate multiple esm, cjs modules

- refactor: Move index file out from module folders
  refer jaredpalmer/tsdx#175 (comment)

Important note:
Base on the comment tsdx do not support multiple entry
  • Loading branch information
Wilson Lim committed Feb 7, 2021
1 parent 65e46f0 commit b995027
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 18,551 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
tsconfigRootDir: __dirname,
project: ['./tsconfig.lint.json'],
},
ignorePatterns: ['.eslintrc.js', 'scripts/*'],
ignorePatterns: ['.eslintrc.js', 'scripts/*', 'tsdx.config.js'],
rules: {
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
'import/no-mutable-exports': 0,
Expand Down
8 changes: 8 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
echo "build multiple modules"
tsdx build \
--entry=src/index.ts \
--entry=src/promise.ts \
--entry=src/validation.ts \
--entry=src/timeconvert.ts \
&& node ./scripts/flowgen.js \
&& node ./scripts/indexify.js \
18,541 changes: 10 additions & 18,531 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build && node ./scripts/flowgen.js",
"build": "bash ./build.sh",
"test": "tsdx test",
"lint": "eslint ./src ./test --ext .js,.ts",
"lint-fix": "eslint ./src ./test --ext .js,.ts --fix",
"prepare": "tsdx build && node ./scripts/flowgen.js",
"prepare": "bash ./build.sh",
"size": "size-limit",
"analyze": "size-limit --why"
},
Expand All @@ -33,14 +33,14 @@
},
"name": "utilzed",
"author": "Wilson Lim",
"module": "dist/utilzed.esm.js",
"module": "dist/index.esm.js",
"size-limit": [
{
"path": "dist/utilzed.cjs.production.min.js",
"path": "dist/index.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/utilzed.esm.js",
"path": "dist/index.esm.js",
"limit": "10 KB"
}
],
Expand Down
2 changes: 2 additions & 0 deletions scripts/flowgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const distFolder = './dist';
const fs = require('fs');
const { beautify, compiler } = require('flowgen');

console.log('generating flow files');

function getAllFilePaths(dirPath, filePaths = []) {
const files = fs.readdirSync(dirPath);

Expand Down
8 changes: 8 additions & 0 deletions scripts/index.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

'use strict'

if (process.env.NODE_ENV === 'production') {
module.exports = require('./__MODULE__.cjs.production.min.js')
} else {
module.exports = require('./__MODULE__.cjs.development.js')
}
30 changes: 30 additions & 0 deletions scripts/indexify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var fs = require('fs');
var path = require('path');
var dirPath = path.resolve('./dist');
var filesList;

console.log('generate module.js files')

fs.readdir(dirPath, function(err, files) {
filesList = files.filter(function(e) {
return e.includes('esm.js.map');
});
console.log('built modules to convert:');
console.log(filesList);
});

fs.readFile('./scripts/index.tpl', 'utf8', function(err, data) {
if (err) {
return console.log(err);
}

filesList.forEach(element => {
const moduleName = element.split('.')[0];
var result = data.replace(/__MODULE__/g, moduleName);
console.log(`output to ./dist/${moduleName}.js`);
fs.writeFile(`./dist/${moduleName}.js`, result, 'utf8', function(err) {
if (err) return console.log(err);
});
});
});

7 changes: 7 additions & 0 deletions src/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import waitForTrue from './promise/waitForTrue';
import sleep from './promise/sleep';

export default {
waitForTrue,
sleep,
};
7 changes: 0 additions & 7 deletions src/promise/index.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import isEmptyOrZero from './validation/isEmptyOrZero';
import versionCheck from './validation/versionCheck';

export default {
isEmptyOrZero,
versionCheck,
};
7 changes: 0 additions & 7 deletions src/validation/index.ts

This file was deleted.

19 changes: 19 additions & 0 deletions tsdx.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = {
rollup(config, options) {
const outputDir = process.cwd() + '/dist/';
const extension =
'.' +
config.output.file
.split('.')
.slice(1)
.join('.');
let filename = config.input.split('src/')[1]; // remove src/
filename = filename.split('.')[0]; // remove extension, if any

config.output.file = outputDir + filename + extension;
console.log(config.output.file);
// replace / with __ for UMD names
config.output.name = filename.replace('/', '__');
return config;
},
};

0 comments on commit b995027

Please sign in to comment.