chore: opt ci

This commit is contained in:
liihuu 2023-12-11 05:02:05 +08:00
parent dba0127f28
commit 4b879422a9
15 changed files with 201 additions and 161 deletions

View File

@ -1,8 +1,5 @@
config/
dist/
types/
docs/
types/
scripts/
dist/
docs/
.eslintrc.cjs
index.js

View File

@ -12,10 +12,8 @@
* limitations under the License.
*/
'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./dist/klinecharts.min.js')
module.exports = require('./umd/klinecharts.min.js')
} else {
module.exports = require('./dist/klinecharts.js')
module.exports = require('./umd/klinecharts.js')
}

View File

@ -2,14 +2,14 @@
"name": "klinecharts",
"version": "9.6.0",
"description": "Lightweight k-line chart built with html5 canvas",
"main": "./dist/klinecharts.min.js",
"module": "index.js",
"types": "./types/index.d.ts",
"main": "./dist/main.js",
"module": "./dist/index.js",
"unpkg": "./dist/umd/klinecharts.min.js",
"types": "./dist/index.d.ts",
"type": "module",
"files": [
"LICENSE",
"README.md",
"index.js",
"types/",
"dist/",
"licenses/",
"NOTICE"
@ -35,11 +35,14 @@
"private": false,
"scripts": {
"lint": "eslint src",
"clean": "node scripts/clean.cjs",
"build-core:dev": "cross-env NODE_ENV=development node scripts/build.cjs",
"build-core:prod": "cross-env NODE_ENV=production node scripts/build.cjs",
"build-core": "npm run build-core:dev && npm run build-core:prod",
"build-dts": "dts-bundle-generator --no-banner true --fail-on-class true --umd-module-name klinecharts -o types/index.d.ts src/index.ts",
"clean": "node scripts/clean.js",
"build-esm": "node scripts/build-esm.js",
"build-cjs": "cross-env NODE_ENV=production node scripts/build-cjs.js",
"build-umd:dev": "cross-env NODE_ENV=development node scripts/build-umd.js",
"build-umd:prod": "cross-env NODE_ENV=production node scripts/build-umd.js",
"build-umd": "npm run build-umd:dev && npm run build-umd:prod",
"build-core": "npm run build-esm && npm run build-cjs && npm run build-umd",
"build-dts": "dts-bundle-generator --no-banner true --fail-on-class true --umd-module-name klinecharts -o dist/index.d.ts src/index.ts",
"build": "npm run clean && npm run build-core && npm run build-dts",
"prepare": "husky install",
"docs:pre-work": "npm run pre-work --prefix docs",

10
scripts/build-cjs.js Normal file
View File

@ -0,0 +1,10 @@
import build from './build.js'
import { resolvePath } from './utils.js'
const fileName = 'main.cjs'
const index = resolvePath('index.js')
build({
index,
fileName,
format: 'cjs'
})

11
scripts/build-esm.js Normal file
View File

@ -0,0 +1,11 @@
import build from './build.js'
import { resolvePath } from './utils.js'
const fileName = 'index.js'
const index = resolvePath('index.ts', resolvePath('src'))
build({
index,
fileName,
format: 'esm'
})

15
scripts/build-umd.js Normal file
View File

@ -0,0 +1,15 @@
import build from './build.js'
import { isDev, env } from './config.js'
import { resolvePath } from './utils.js'
const fileName = isDev ? 'klinecharts.js' : 'klinecharts.min.js'
const index = resolvePath('index.ts', resolvePath('src'))
build({
index,
replaceValues: { "process.env.NODE_ENV": JSON.stringify(env) },
fileName,
format: 'umd',
parentDir: 'umd',
name: 'klinecharts'
})

View File

@ -1,34 +0,0 @@
'use strict';
const rollup = require('rollup');
const chalk = require('chalk');
const { inputConfig, outputConfig, version } = require('./config.cjs');
const env = process.env.NODE_ENV;
async function build() {
const fileName = env === 'development' ? 'klinecharts.js' : 'klinecharts.min.js'
const fileNameVersion = `v${version} ${fileName}`
console.log(`Creating an optimized version ${chalk.blue(fileNameVersion)} build...\n`);
const input = inputConfig(env);
try {
const startTime = new Date().getTime()
const bundle = await rollup.rollup(input);
const output = outputConfig(env, fileName);
await bundle.write(output);
console.log(chalk.green(`\n✔️ Compiled ${fileNameVersion} successfully.\n`));
console.log(`Done in ${((new Date().getTime() - startTime) / 1000 / 60).toFixed(2)}s.\n`)
} catch (err) {
console.log(`\n\n${chalk.red(err)}\n`);
console.log(chalk.red(`✖️ Failed to compile ${fileNameVersion}.\n`));
process.exit(1);
}
}
build();

28
scripts/build.js Normal file
View File

@ -0,0 +1,28 @@
import { rollup } from 'rollup'
import chalk from 'chalk'
import { createInputConfig, createOutputConfig, version, env } from './config.js'
async function build({ index, replaceValues, fileName, format, parentDir, name }) {
const text = `version ${version}${env ? ` ${env} `: ' '}${format} file`
console.log(`Start building ${text}...\n`)
try {
const startTime = new Date().getTime()
const bundle = await rollup(createInputConfig({ input: index, replaceValues }))
await bundle.write(createOutputConfig({
fileName, format, name, parentDir
}));
console.log(chalk.green(`\n✔️ Compiled ${text} successfully.\n`));
console.log(`Done in ${((new Date().getTime() - startTime) / 1000 / 60).toFixed(2)}s.\n`)
} catch (err) {
console.log(`\n\n${chalk.red(err)}\n`);
console.log(chalk.red(`✖️ Failed to compile ${text}.\n`));
process.exit(1);
}
}
export default build

View File

@ -1,19 +1,20 @@
'use strict';
const fs = require('fs');
const chalk = require('chalk');
import fs from 'fs'
import chalk from 'chalk'
const { resolvePath, buildDir } = require('./paths.cjs');
import { resolvePath } from './utils.js'
const buildDir = resolvePath('dist')
let totalFileCount = 0
let deletedFileCount = 0
function eachFiles (dir) {
if (fs.existsSync(dir)) {
const files = fs.readdirSync(dir);
const files = fs.readdirSync(dir)
files.forEach(file => {
const path = resolvePath(file, dir);
const path = resolvePath(file, dir)
if (fs.statSync(path).isDirectory()) {
eachFiles(path);
eachFiles(path)
} else {
totalFileCount++
}
@ -23,29 +24,29 @@ function eachFiles (dir) {
function deleteFiles (dir) {
if (fs.existsSync(dir)) {
const files = fs.readdirSync(dir);
const files = fs.readdirSync(dir)
files.forEach(file => {
const path = resolvePath(file, dir);
const path = resolvePath(file, dir)
if (fs.statSync(path).isDirectory()) {
deleteDir(path);
deleteFiles(path)
} else {
fs.unlinkSync(path);
fs.unlinkSync(path)
deletedFileCount++
process.stdout.clearLine(process.stdout);
process.stdout.cursorTo(0);
const percent = `${Math.round(deletedFileCount / totalFileCount * 100)}%`;
process.stdout.write(`${chalk.blue(`${percent}(${deletedFileCount}/${totalFileCount}): ${file}`)}`, 'utf-8');
process.stdout.cursorTo(0)
const percent = `${Math.round(deletedFileCount / totalFileCount * 100)}%`
process.stdout.write(`${chalk.blue(`${percent}(${deletedFileCount}/${totalFileCount}): ${file}`)}`, 'utf-8')
}
})
fs.rmdirSync(dir);
fs.rmdirSync(dir)
}
}
function clean () {
eachFiles(buildDir);
deleteFiles(buildDir);
eachFiles(buildDir)
deleteFiles(buildDir)
console.log(chalk.green('\n\n✔ Clean successfully.\n'));
console.log(chalk.green('\n\n✔ Clean successfully.\n'))
}
clean();
clean()

View File

@ -1,69 +0,0 @@
'use strict';
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const eslint = require('@rollup/plugin-eslint');
const replace = require('@rollup/plugin-replace');
const commonjs = require('@rollup/plugin-commonjs');
const typescript = require('@rollup/plugin-typescript');
const terser = require('@rollup/plugin-terser');
const fileSize = require('rollup-plugin-filesize');
const progress = require('rollup-plugin-progress');
const paths = require('./paths.cjs');
const packageJson = require(paths.packageJson);
const version = packageJson.version;
const plugins = (env) => [
typescript(),
eslint({
throwOnError: true
}),
nodeResolve(),
commonjs(),
progress(),
replace({
preventAssignment: true,
values: {
'__BUILD_ENV__': env,
'__BUILD_VERSION__': version
}
}),
fileSize(),
env === 'production' && terser({
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true
}
})
];
function inputConfig (env) {
return { input: paths.index, plugins: plugins(env) };
}
function outputConfig (env, fileName) {
const isDevelopment = env === 'development';
return {
file: paths.resolvePath(fileName, paths.buildDir),
format: 'umd',
name: 'klinecharts',
sourcemap: isDevelopment,
indent: false,
banner: `
/**
* @license
* KLineChart v${version}
* Copyright (c) 2019 lihu.
* Licensed under Apache License 2.0 https://www.apache.org/licenses/LICENSE-2.0
*/`.trim(),
};
}
module.exports = {
inputConfig,
outputConfig,
version
};

82
scripts/config.js Normal file
View File

@ -0,0 +1,82 @@
import { nodeResolve } from '@rollup/plugin-node-resolve'
import eslint from '@rollup/plugin-eslint'
import replace from '@rollup/plugin-replace'
// import commonjs from '@rollup/plugin-commonjs'
import typescript from '@rollup/plugin-typescript'
import terser from '@rollup/plugin-terser'
import fileSize from 'rollup-plugin-filesize'
import progress from 'rollup-plugin-progress'
import { resolvePath, getVersion } from './utils.js'
const version = getVersion()
const env = process.env.NODE_ENV
const isDev = env === 'development'
const isProd = env === 'production'
const buildDir = resolvePath('dist')
function createInputConfig ({ input, replaceValues }) {
return {
input,
plugins: [
typescript(),
eslint({
throwOnError: true
}),
nodeResolve(),
// commonjs(),
progress(),
replace({
preventAssignment: true,
values: {
'__VERSION__': version,
...replaceValues
}
}),
fileSize(),
isProd && terser()
].filter(p => !!p)
}
}
function createOutputConfig ({
fileName, format, name, parentDir
}) {
let file
if (parentDir) {
file = resolvePath(fileName, resolvePath(parentDir, buildDir))
} else {
file = resolvePath(fileName, buildDir)
}
const config = {
file,
format,
sourcemap: isDev,
indent: false,
banner: `
/**
* @license
* KLineChart v${version}
* Copyright (c) 2019 lihu.
* Licensed under Apache License 2.0 https://www.apache.org/licenses/LICENSE-2.0
*/`.trim(),
}
if (!!name) {
config.name = name
}
return config
}
export {
createInputConfig,
createOutputConfig,
version,
env,
isDev,
isProd
}

View File

@ -1,14 +0,0 @@
'use strict';
const path = require('path');
const fs = require('fs');
const root = fs.realpathSync(process.cwd());
const resolvePath = (relativePath, rootDirectory = root) => path.resolve(rootDirectory, relativePath);
module.exports = {
resolvePath,
buildDir: resolvePath('dist'),
index: resolvePath('src/index.ts'),
packageJson: resolvePath('package.json')
};

13
scripts/utils.js Normal file
View File

@ -0,0 +1,13 @@
import path from 'path'
import fs from 'fs'
const root = fs.realpathSync(process.cwd())
const resolvePath = (relativePath, rootDirectory = root) => path.resolve(rootDirectory, relativePath)
function getVersion () {
const result = fs.readFileSync(resolvePath('package.json'), 'utf-8')
return JSON.parse(result).version
}
export { resolvePath, getVersion }

View File

@ -12,8 +12,7 @@
* limitations under the License.
*/
// @ts-expect-error
const DEV = '__BUILD_ENV__' === 'development'
const DEV = process.env.NODE_ENV === 'development'
function log (templateText: string, tagStyle: string, messageStyle: string, api: string, invalidParam: string, append: string): void {
if (DEV) {
@ -36,15 +35,15 @@ export function logWarn (api: string, invalidParam: string, append?: string): vo
export function logError (api: string, invalidParam: string, append?: string): void {
log(
'%c😟 klinecharts error%c %s%s%s',
'padding:3px 4px;border-radius:2px;color:#ffffff;background-color:#EF5350;',
'color:#EF5350;',
'padding:3px 4px;border-radius:2px;color:#ffffff;background-color:#F92855;',
'color:#F92855;',
api, invalidParam, append ?? ''
)
}
export function logTag (): void {
log(
'%c❤ Welcome to klinecharts. Version is __BUILD_VERSION__',
'%c❤ Welcome to klinecharts. Version is __VERSION__',
'border-radius:4px;border:dashed 1px #1677FF;line-height:70px;padding:0 20px;margin:16px 0;font-size:14px;color:#1677FF;',
'',
'',

View File

@ -69,7 +69,7 @@ let chartBaseId = 1
* @return {string}
*/
function version (): string {
return '__BUILD_VERSION__'
return '__VERSION__'
}
/**