Skip to content

Commit

Permalink
📦 Chore(build): migrate to esbuild and change export assignment to es…
Browse files Browse the repository at this point in the history
…m export (#102)

This is a breaking change.
  • Loading branch information
upupming authored Oct 26, 2021
1 parent f133d57 commit 2a6cd18
Show file tree
Hide file tree
Showing 34 changed files with 2,634 additions and 2,485 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ yarn-error.log
temp.js
package-lock.json
tsconfig.json
tslint.json
.vscode/
src/
.travis.yml
11 changes: 8 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
"skipFiles": [
"<node_internals>/**"
],
"preLaunchTask": "npm: debug", // the label in tasks.json
"program": "${workspaceFolder}\\temp.js"
"program": "${workspaceFolder}/bin/picgo",
"args": [
"upload"
],
"outFiles": [
"${workspaceFolder}/**/*.js"
]
}
]
}
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
"eslint.alwaysShowStatus": true,
"editor.tabSize": 2,
"cSpell.words": [
"Molunerfinn",
"aliyun",
"applescript",
"commitlint",
"copyfiles",
"imgur",
"Molunerfinn",
"picgo",
"qiniu",
"smms",
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,21 @@ Thanks to [vs-picgo](https://github.com/Spades-S/vs-picgo) && [Spades-S](https:/

### Use in node project

#### Common JS

```js
const { PicGo } = require('picgo')
```

#### ES Module

```js
import { PicGo } from 'picgo'
```

#### API usage example

```js
const PicGo = require('picgo')
const picgo = new PicGo()

// upload a picture from path
Expand Down
2 changes: 1 addition & 1 deletion bin/picgo
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (configPath !== true && configPath !== '') {
} else {
configPath = ''
}
const PicGo = require('../dist/src/index')
const { PicGo } = require('..')
const picgo = new PicGo(configPath)
picgo.registerCommands()

Expand Down
44 changes: 24 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"name": "picgo",
"version": "1.4.26",
"description": "A tool for picture uploading",
"main": "dist/src/index.js",
"typings": "dist/src/index.d.ts",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"typings": "dist/index.d.ts",
"bin": {
"picgo": "./bin/picgo"
},
Expand All @@ -14,15 +15,13 @@
"start": "node ./bin/picgo",
"lint": "eslint src/**/*.ts && npm run build",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc -p . && npm run copy",
"copy": "copyfiles -f src/utils/clipboard/* dist/src/utils/clipboard && copyfiles -f src/types/* dist/src/types",
"dev": "tsc -w -p .",
"build": "rm -rf ./dist && rollup -c rollup.config.js && ls -lh dist",
"dev": "rollup -c rollup.config.js -w",
"patch": "npm version patch && git push origin master && git push origin --tags",
"minor": "npm version minor && git push origin master && git push origin --tags",
"major": "npm version major && git push origin master && git push origin --tags",
"cz": "git-cz",
"release": "bump-version",
"debug": "tsc -p . --sourcemap && npm run copy"
"release": "bump-version"
},
"keywords": [
"picture",
Expand Down Expand Up @@ -51,14 +50,17 @@
"author": "Molunerfinn",
"license": "MIT",
"devDependencies": {
"@commitlint/cli": "^7.5.2",
"@picgo/bump-version": "^1.0.1",
"@picgo/bump-version": "^1.1.2",
"@rollup/plugin-commonjs": "^21.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.5",
"@rollup/plugin-replace": "^3.0.0",
"@types/cross-spawn": "^6.0.0",
"@types/ejs": "^3.0.5",
"@types/fs-extra": "^5.0.4",
"@types/globby": "^9.1.0",
"@types/image-size": "^0.0.29",
"@types/inquirer": "^0.0.42",
"@types/lodash": "^4.14.175",
"@types/lowdb": "^1.0.4",
"@types/md5": "^2.1.32",
"@types/mime-types": "^2.1.0",
Expand All @@ -70,7 +72,7 @@
"@typescript-eslint/eslint-plugin": "3",
"@typescript-eslint/parser": "^3.2.0",
"babel-eslint": "^10.1.0",
"commitizen": "^3.0.7",
"builtins": "^4.0.0",
"conventional-changelog": "^3.0.6",
"copyfiles": "^2.1.0",
"cz-customizable": "^5.10.0",
Expand All @@ -80,29 +82,35 @@
"eslint-plugin-node": "11",
"eslint-plugin-promise": "4",
"eslint-plugin-standard": "4",
"execa": "^5.1.1",
"husky": "^1.3.1",
"pre-commit": "^1.2.2",
"tslint": "^5.10.0",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.9.5"
"rollup": "^2.58.0",
"rollup-plugin-string": "^3.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.30.0",
"typescript": "^4.4.3"
},
"dependencies": {
"chalk": "^2.4.1",
"commander": "^8.1.0",
"comment-json": "^2.3.1",
"cross-spawn": "^6.0.5",
"dayjs": "^1.7.4",
"download-git-repo": "^1.1.0",
"download-git-repo": "^3.0.2",
"ejs": "^2.6.1",
"fs-extra": "^6.0.1",
"globby": "^8.0.2",
"globby": "^11.0.4",
"image-size": "^0.8.3",
"inquirer": "^6.0.0",
"is-wsl": "^2.2.0",
"lodash": "^4.17.21",
"lodash-id": "^0.14.0",
"lowdb": "^1.0.0",
"md5": "^2.2.1",
"mime-types": "2.1.33",
"minimatch": "^3.0.4",
"minimist": "^1.2.5",
"qiniu": "^7.2.1",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
Expand All @@ -117,10 +125,6 @@
"current": "smms"
},
"plugins": {},
"resolutions": {
"**/**/lodash": "^4.17.12",
"**/**/minimist": ">0.2.1"
},
"engines": {
"node": ">= 12.0.0"
}
Expand Down
82 changes: 82 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { terser } from 'rollup-plugin-terser'
import pkg from './package.json'
import typescript from 'rollup-plugin-typescript2'
import commonjs from '@rollup/plugin-commonjs'
import { string } from 'rollup-plugin-string'
import json from '@rollup/plugin-json'
import builtins from 'builtins'
import replace from '@rollup/plugin-replace'

const version = process.env.VERSION || pkg.version
const sourcemap = 'inline'
const banner = `/*
* picgo@${version}, https://github.com/PicGo/PicGo-Core
* (c) 2018-${new Date().getFullYear()} PicGo Group
* Released under the MIT License.
*/`
const input = './src/index.ts'

const commonOptions = {
// Creating regex of the packages to make sure sub-paths of the
// packages such as `lowdb/adapters/FileSync` are also treated as external
// See https://github.com/rollup/rollup/issues/3684#issuecomment-926558056
external: [
...Object.keys(pkg.dependencies),
...builtins()
].map(packageName => new RegExp(`^${packageName}(/.*)?`)),
plugins: [
typescript({
tsconfigOverride: {
compilerOptions: {
target: 'ES2017',
module: 'ES2015'
}
}
}),
terser(),
commonjs(),
string({
// Required to be specified
include: [
'**/*.applescript',
'**/*.ps1',
'**/*.sh'
]
}),
json(),
replace({
'process.env.PICGO_VERSION': JSON.stringify(pkg.version),
preventAssignment: true
})
],
input
}

/** @type import('rollup').RollupOptions */
const nodeCjs = {
output: [{
file: 'dist/index.cjs.js',
format: 'cjs',
banner,
sourcemap
}],
...commonOptions
}

const nodeEsm = {
output: [{
file: 'dist/index.esm.js',
format: 'esm',
banner,
sourcemap
}],
...commonOptions
}

const bundles = []
const env = process.env.BUNDLES || ''
if (env.includes('cjs')) bundles.push(nodeCjs)
if (env.includes('esm')) bundles.push(nodeEsm)
if (bundles.length === 0) bundles.push(nodeCjs, nodeEsm)

export default bundles
4 changes: 2 additions & 2 deletions src/core/Lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { handleUrlEncode } from '../utils/common'
import { IBuildInEvent } from '../utils/enum'
import { createContext } from '../utils/createContext'

class Lifecycle extends EventEmitter {
export class Lifecycle extends EventEmitter {
private readonly ctx: IPicGo

constructor (ctx: IPicGo) {
Expand All @@ -30,7 +30,7 @@ class Lifecycle extends EventEmitter {
await this.doUpload(ctx)
await this.afterUpload(ctx)
return ctx
} catch (e) {
} catch (e: any) {
ctx.log.warn(IBuildInEvent.FAILED)
ctx.emit(IBuildInEvent.UPLOAD_PROGRESS, -1)
ctx.emit(IBuildInEvent.FAILED, e)
Expand Down
11 changes: 4 additions & 7 deletions src/core/PicGo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path'
import { EventEmitter } from 'events'
import { homedir } from 'os'
import Commander from '../lib/Commander'
import Logger from '../lib/Logger'
import { Logger } from '../lib/Logger'
import Lifecycle from './Lifecycle'
import LifecyclePlugins, { setCurrentPluginName } from '../lib/LifecyclePlugins'
import uploaders from '../plugins/uploader'
Expand All @@ -16,12 +16,11 @@ import Request from '../lib/Request'
import DB from '../utils/db'
import PluginHandler from '../lib/PluginHandler'
import { IBuildInEvent, IBusEvent } from '../utils/enum'
import { version } from '../../package.json'
import { eventBus } from '../utils/eventBus'
import { RequestPromiseAPI } from 'request-promise-native'
import { isConfigKeyInBlackList, isInputConfigValid } from '../utils/common'

class PicGo extends EventEmitter implements IPicGo {
export class PicGo extends EventEmitter implements IPicGo {
private _config!: IConfig
private lifecycle!: Lifecycle
private db!: DB
Expand All @@ -40,7 +39,7 @@ class PicGo extends EventEmitter implements IPicGo {
* use request instead
*/
Request!: Request
VERSION: string = version
VERSION: string = process.env.PICGO_VERSION
GUI_VERSION?: string

get pluginLoader (): IPluginLoader {
Expand Down Expand Up @@ -99,7 +98,7 @@ class PicGo extends EventEmitter implements IPicGo {
// load third-party plugins
this._pluginLoader.load()
this.lifecycle = new Lifecycle(this)
} catch (e) {
} catch (e: any) {
this.emit(IBuildInEvent.UPLOAD_PROGRESS, -1)
this.log.error(e)
throw e
Expand Down Expand Up @@ -211,5 +210,3 @@ class PicGo extends EventEmitter implements IPicGo {
}
}
}

export default PicGo
19 changes: 19 additions & 0 deletions src/custom-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* eslint-disable @typescript-eslint/naming-convention */
declare module '*.sh' {
const src: string
export default src
}
declare module '*.applescript' {
const src: string
export default src
}
declare module '*.ps1' {
const src: string
export default src
}

declare namespace NodeJS {
interface ProcessEnv {
readonly PICGO_VERSION: string
}
}
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import PicGo from './core/PicGo'
export { PicGo } from './core/PicGo'
export { Lifecycle } from './core/Lifecycle'

export = PicGo
export { Logger } from './lib/Logger'
export { PluginHandler } from './lib/PluginHandler'
export { LifecyclePlugins } from './lib/LifecyclePlugins'
export { Commander } from './lib/Commander'
export { PluginLoader } from './lib/PluginLoader'
export { Request } from './lib/Request'

export * from './types'
5 changes: 2 additions & 3 deletions src/lib/Commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { Command } from 'commander'
import inquirer, { Inquirer } from 'inquirer'
import { IPlugin, ICommander, IPicGo } from '../types'
import commanders from '../plugins/commander'
import { version } from '../../package.json'

class Commander implements ICommander {
export class Commander implements ICommander {
private list: {
[propName: string]: IPlugin
}
Expand All @@ -22,7 +21,7 @@ class Commander implements ICommander {

init (): void {
this.program
.version(version, '-v, --version')
.version(process.env.PICGO_VERSION, '-v, --version')
.option('-d, --debug', 'debug mode', () => {
this.ctx.setConfig({
debug: true
Expand Down
2 changes: 1 addition & 1 deletion src/lib/LifecyclePlugins.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IPlugin, ILifecyclePlugins } from '../types'

class LifecyclePlugins implements ILifecyclePlugins {
export class LifecyclePlugins implements ILifecyclePlugins {
static currentPlugin: string | null
private readonly list: Map<string, IPlugin>
private readonly pluginIdMap: Map<string, string[]>
Expand Down
Loading

0 comments on commit 2a6cd18

Please sign in to comment.