mirror of
https://github.com/yugasun/qrcode-decoder.git
synced 2026-08-12 20:27:56 +08:00
feat: refactor with ts
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
import * as path from 'path';
|
||||
import * as rollup from 'rollup';
|
||||
import resolve from 'rollup-plugin-node-resolve';
|
||||
import typescript2 from 'rollup-plugin-typescript2';
|
||||
import { uglify } from 'rollup-plugin-uglify';
|
||||
import globals from 'rollup-plugin-node-globals';
|
||||
import commonjs from 'rollup-plugin-commonjs';
|
||||
import ts from 'typescript';
|
||||
|
||||
import { createLogger } from './logger';
|
||||
|
||||
const logBundle = createLogger('bundle');
|
||||
|
||||
const plugins: rollup.Plugin[] = [
|
||||
resolve({
|
||||
browser: true,
|
||||
jsnext: true,
|
||||
preferBuiltins: true,
|
||||
}),
|
||||
commonjs(),
|
||||
typescript2({
|
||||
tsconfig: path.join(__dirname, '..', 'tsconfig.json'),
|
||||
typescript: ts, // ensure we're using the same typescript (3.x) for rollup as for regular builds etc
|
||||
tsconfigOverride: {
|
||||
module: 'esnext',
|
||||
stripInternal: true,
|
||||
emitDeclarationOnly: false,
|
||||
composite: false,
|
||||
declaration: false,
|
||||
declarationMap: false,
|
||||
sourceMap: false,
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
async function bundleEsm() {
|
||||
const bundle = await rollup.rollup({
|
||||
input: path.join(process.cwd(), 'src/index.ts'),
|
||||
plugins: [...plugins, globals()],
|
||||
external: ['jsqr'],
|
||||
});
|
||||
await bundle.write({
|
||||
file: 'dist/index.esm.js',
|
||||
name: 'QrcodeDecoder',
|
||||
format: 'esm',
|
||||
sourcemap: false,
|
||||
globals: {
|
||||
jsqr: 'jsqr',
|
||||
tslib: 'tslib',
|
||||
},
|
||||
});
|
||||
}
|
||||
async function bundleUmd() {
|
||||
const bundle = await rollup.rollup({
|
||||
input: path.join(process.cwd(), 'src/index.ts'),
|
||||
plugins: [...plugins],
|
||||
external: ['jsqr'],
|
||||
});
|
||||
await bundle.write({
|
||||
file: 'dist/index.js',
|
||||
exports: 'named',
|
||||
name: 'QrcodeDecoder',
|
||||
format: 'umd',
|
||||
sourcemap: false,
|
||||
globals: {
|
||||
jsqr: 'jsqr',
|
||||
tslib: 'tslib',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function bundleAio() {
|
||||
const bundle = await rollup.rollup({
|
||||
input: path.join(process.cwd(), 'src/index.ts'),
|
||||
plugins: [
|
||||
...plugins,
|
||||
uglify({
|
||||
compress: {
|
||||
drop_debugger: true,
|
||||
drop_console: true,
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
await bundle.write({
|
||||
file: 'dist/index.min.js',
|
||||
name: 'QrcodeDecoder',
|
||||
format: 'iife',
|
||||
sourcemap: false,
|
||||
});
|
||||
}
|
||||
|
||||
async function bundle() {
|
||||
try {
|
||||
const outputs = process.argv.slice(2)[0].split(',');
|
||||
logBundle(`Creating bundle`);
|
||||
|
||||
if (outputs.indexOf('esm') === -1) {
|
||||
logBundle(`Skipping esm`);
|
||||
} else {
|
||||
logBundle(`Creating esm`);
|
||||
await bundleEsm();
|
||||
}
|
||||
|
||||
if (outputs.indexOf('umd') === -1) {
|
||||
logBundle(`Skipping umd`);
|
||||
} else {
|
||||
logBundle(`Creating umd`);
|
||||
|
||||
await bundleUmd();
|
||||
}
|
||||
|
||||
if (outputs.indexOf('aio') === -1) {
|
||||
logBundle(`Skipping umd aio`);
|
||||
} else {
|
||||
logBundle(`Creating umd aio`);
|
||||
|
||||
await bundleAio();
|
||||
}
|
||||
} catch (err) {
|
||||
logBundle('Failed to bundle:');
|
||||
logBundle(err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
bundle();
|
||||
@@ -0,0 +1,16 @@
|
||||
import * as l from 'fancy-log';
|
||||
const log = <typeof import('fancy-log')>(<any>(<any>l).default || l);
|
||||
import * as c from 'chalk';
|
||||
const chalk = <import('chalk').Chalk>(c.default || c);
|
||||
|
||||
export function createLogger(name: string): typeof log {
|
||||
const prefix = `> ${chalk.green(name)} `;
|
||||
const logger = <typeof log>log.bind(log, prefix);
|
||||
logger.info = log.info.bind(log, prefix);
|
||||
logger.dir = log.dir.bind(log, prefix);
|
||||
logger.warn = log.warn.bind(log, prefix);
|
||||
logger.error = log.error.bind(log, prefix);
|
||||
return logger;
|
||||
}
|
||||
|
||||
export { chalk as c };
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"esModuleInterop": true,
|
||||
"module": "commonjs",
|
||||
"target": "es2015",
|
||||
"resolveJsonModule": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user