Skip to content

Commit

Permalink
build cjs and esm bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuel Hein committed Sep 7, 2022
1 parent 2bbe5b5 commit 5c37f1b
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ jobs:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm run bundle
- run: npm run coverage
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
.nyc_output
coverage
.DS_Store
tmp
type-injector-*.tgz
92 changes: 92 additions & 0 deletions build-bundles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// @ts-check
const shelljs = require('shelljs');
const uglifyjs = require('uglify-js');
const fs = require('fs');
const path = require('path');
const { rollup } = require('rollup');

const bundlesDir = path.join(__dirname, 'dist/bundles');

/** @type {Partial<import('rollup').RollupOptions>} */
const defaultOptions = {
input: 'tmp/es/index.js',
output: {
name: 'typeInjector',
exports: 'named',
}
};

buildAll();

async function buildAll() {
shelljs.mkdir('-p', bundlesDir);
build(`tsconfig.build.json`);
build(`tsconfig.es.json`);
await Promise.all([
buildCommonJsBundle(),
buildEsmBundle(),
buildIifeBundle(),
]);
shelljs.exec('npm pack', { fatal: true });
}

async function buildCommonJsBundle() {
const outFile = 'type-injector.cjs';
/** @type {import('rollup').OutputOptions} */
const outputOptions = {
...defaultOptions.output,
format: 'commonjs',
file: path.join(bundlesDir, outFile),
};
await rollup(defaultOptions).then((build) => build.write(outputOptions));
minifyBundle(outFile);
}

async function buildEsmBundle() {
const outFile = 'type-injector.mjs';
/** @type {import('rollup').OutputOptions} */
const outputOptions = {
...defaultOptions.output,
format: 'esm',
file: path.join(bundlesDir, outFile),
};
await rollup(defaultOptions).then((build) => build.write(outputOptions));
minifyBundle(outFile);
}

async function buildIifeBundle() {
const outFile = 'type-injector.js';
/** @type {import('rollup').OutputOptions} */
const outputOptions = {
...defaultOptions.output,
format: 'iife',
file: path.join(bundlesDir, outFile),
};
await rollup(defaultOptions).then((build) => build.write(outputOptions));
minifyBundle(outFile);
}

function build(tsconfig) {
let cmd = 'node ./node_modules/.bin/tsc';
if (tsconfig) cmd += ' -p ' + tsconfig;
shelljs.exec(cmd, { fatal: true });
}

function minifyBundle(filename) {
const code = fs.readFileSync(path.join(bundlesDir, filename), 'utf-8');
const result = uglifyjs.minify(code, {
compress: {
arguments: true,
assignments: true,
hoist_props: true,
passes: 3,
}
});
if (result.error) {
console.error(result.error);
process.exit(1);
}
const ext = path.extname(filename);
fs.writeFileSync(path.join(bundlesDir, `${path.basename(filename, ext)}.min${ext}`), result.code);
}

99 changes: 94 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "type-injector",
"version": "1.0.0-alpha",
"description": "inject typescript classes recoursively",
"main": "index.js",
"scripts": {
"test": "mocha -r ts-node/register ./src/**/*.spec.ts",
"test:watch": "watch-npm test",
"lint": "eslint .eslintrc.cjs .",
"build": "tsc -P tsconfig.build.json",
"bundle": "node build-bundles.js",
"coverage": "nyc --reporter=html --reporter=text-summary --exclude '**/*.spec.ts' mocha -r ts-node/register ./src/**/*.spec.ts",
"doc": "typedoc --plugin typedoc-plugin-markdown --out ./typedoc --excludePrivate --excludeInternal --excludeProtected src/index.ts --includeVersion --readme none"
},
Expand Down Expand Up @@ -49,10 +49,32 @@
"eslint-plugin-tsdoc": "^0.2.16",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"rollup": "^2.79.0",
"shelljs": "^0.8.5",
"ts-node": "^10.9.1",
"typedoc": "^0.23.13",
"typedoc-plugin-markdown": "^3.13.5",
"typescript": "^4.8.2",
"uglify-js": "^3.17.0",
"watch-npm": "^1.0.1"
}
},
"files": [
"dist"
],
"main": "./dist/bundles/type-injector.cjs",
"types": "./dist/index.d.ts",
"module": "./dist/index.js",
"es2020": "./dist/bundles/type-injector.mjs",
"exports": {
"./package.json": {
"default": "./package.json"
},
".": {
"default": "./dist/bundles/type-injector.mjs",
"types": "./dist/index.d.ts",
"es2020": "./dist/bundles/type-injector.mjs",
"node": "./dist/bundles/type-injector.cjs"
}
},
"sideEffects": false
}
6 changes: 5 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
"module": "ES2022",
"types": [],
"lib": ["es2019", "WebWorker"],
"outDir": "./dist"
"outDir": "./dist",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"inlineSources": true,
},
"files": [
"src/index.ts"
Expand Down
17 changes: 17 additions & 0 deletions tsconfig.es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": [ "dom" ], /* Specify library files to be included in the compilation. */
"declaration": false, /* Generates corresponding '.d.ts' file. */
"outDir": "./tmp/es", /* Redirect output structure to the directory. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"files": [
"src/index.ts"
],
"exclude": [
"src/**/*.spec.ts"
]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["es2019", "dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
Expand Down

0 comments on commit 5c37f1b

Please sign in to comment.