-
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
Emanuel Hein
committed
Sep 7, 2022
1 parent
2bbe5b5
commit 5c37f1b
Showing
8 changed files
with
236 additions
and
10 deletions.
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
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 |
---|---|---|
|
@@ -3,3 +3,5 @@ node_modules | |
.nyc_output | ||
coverage | ||
.DS_Store | ||
tmp | ||
type-injector-*.tgz |
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,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); | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,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" | ||
] | ||
} |
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