From c62c7195305c9911224f620856843707bf12b095 Mon Sep 17 00:00:00 2001 From: c4605 Date: Wed, 3 Apr 2019 10:44:04 +0800 Subject: [PATCH 1/6] poi: add poi types --- types/poi/index.d.ts | 223 ++++++++++++++++++++++++++++++++++++++++ types/poi/package.json | 6 ++ types/poi/poi-tests.ts | 3 + types/poi/tsconfig.json | 23 +++++ types/poi/tslint.json | 1 + 5 files changed, 256 insertions(+) create mode 100644 types/poi/index.d.ts create mode 100644 types/poi/package.json create mode 100644 types/poi/poi-tests.ts create mode 100644 types/poi/tsconfig.json create mode 100644 types/poi/tslint.json diff --git a/types/poi/index.d.ts b/types/poi/index.d.ts new file mode 100644 index 0000000000..15ac85edcc --- /dev/null +++ b/types/poi/index.d.ts @@ -0,0 +1,223 @@ +// Type definitions for poi 12.5 +// Project: https://github.com/egoist/poi/ +// Definitions by: c4605 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { ICompiler, Configuration as WebpackConfig } from "webpack"; +import { Configuration as WebpackDevServerConfig } from "webpack-dev-server"; +import { Options as HtmlWebpackPluginOptions } from "html-webpack-plugin"; +import * as WebpackChainConfig from "webpack-chain"; +import CAC from "cac/types/CAC"; + +export type Mode = "production" | "development" | "test"; + +export interface Opts { + type: string; + mode: Mode; + [k: string]: any; +} + +/** + * https://poi.js.org/config.html + */ +export interface Config { + entry?: Config.Entry; + output?: Config.Output; + pages?: Config.Pages; + babel?: Config.Babel; + css?: Config.Css; + assets?: Config.Assets; + envs?: Config.Envs; + constants?: Config.Constants; + chainWebpack?: Config.ChainWebpack; + configureWebpack?: Config.ConfigureWebpack; + publicFolder?: Config.PublicFolder; + devServer?: Config.DevServer; + plugins?: Config.Plugins; +} + +export namespace Config { + type Entry = WebpackConfig["entry"]; + + interface Output { + dir?: string; + clean?: boolean; + format?: "iife" | "cjs" | "umd"; + moduleName?: string; + sourceMap?: boolean; + minimize?: boolean; + publicUrl?: string; + fileNames?: Output.FileNames; + target?: + | "web" + | "electron-renderer" + | "electron-main" + | "node" + | "node-webkit" + | "async-node" + | "webworker"; + html?: Output.Html; + } + namespace Output { + interface FileNames { + js?: string; + css?: string; + font?: string; + image?: string; + } + + interface Html { + title?: string; + filename?: string; + template?: string; + inject?: boolean; + } + } + + interface Pages { + [pageName: string]: + | string + | Partial & { + entry: string; + chunks?: string[]; + }; + } + + interface Babel { + jsx?: string; + transpileModules?: string | string[]; + } + + interface Css { + extract?: boolean; + sourceMap?: boolean; + loaderOptions?: Css.LoaderOptions; + } + namespace Css { + interface LoaderOptions { + css?: any; + sass?: any; + postcss?: any; + less?: any; + stylus?: any; + } + } + + interface Assets { + inlineImageMaxSize?: number; + } + + interface Envs { + [envName: string]: string; + } + + interface Constants { + [constantName: string]: string; + } + + interface ChainWebpack { + (config: WebpackChainConfig, opts: Opts): void; + } + + type ConfigureWebpack = + | WebpackConfig + | ((config: WebpackConfig, opts: Opts) => void | WebpackConfig); + + type PublicFolder = string | boolean; + + interface DevServer { + host?: string; + port?: string | number; + hot?: boolean; + hotOnly?: string; + hotEntries?: string[]; + historyApiFallback?: WebpackDevServerConfig["historyApiFallback"]; + open?: boolean; + proxy?: WebpackDevServerConfig["proxy"]; + https?: WebpackDevServerConfig["https"]; + before?: WebpackDevServerConfig["before"]; + after?: WebpackDevServerConfig["after"]; + headers?: WebpackDevServerConfig["headers"]; + } + + interface PluginOption { + resolve: string; + options?: any; + } + type Plugins = Array; +} + +export interface ConfigLoader { + resolve( + files?: ReadonlyArray, + cwd?: string, + stopDir?: string + ): string | null; + resolve(options?: ConfigLoader.Options): string | null; + + load(files?: ReadonlyArray, cwd?: string, stopDir?: string): any; + load(options?: ConfigLoader.Options): any; +} +export namespace ConfigLoader { + interface Options { + files?: string[]; + cwd?: string; + stopDir?: string; + packageKey?: string; + parseJSON?: (str: string) => any; + } +} + +/** + * https://poi.js.org/api.html#constructor-argv + */ +export class Poi { + mode: Mode; + + cli: CAC; + + command: CAC["command"]; + + config: Config; + + isProd: boolean; + + configLoader: ConfigLoader; + + constructor(argv?: string[]); + + hasDependency(name: string): boolean; + + hasPlugin(name: string): boolean; + + hook(hookName: string, handler: Poi.HookHandler): void; + + resolveCwd(...args: string[]): string; + + resolveOutDir(...args: string[]): string; + + run(): Promise; + + getCacheConfig( + dir: string, + keys: { [k: string]: string }, + files: ReadonlyArray + ): { + cacheDirectory: string; + cacheIdentifier: string; + }; + + createWebpackChain(opts?: { [k: string]: any }): WebpackChainConfig; + + createWebpackCompiler(config: WebpackConfig): ICompiler; + + localResolve(pkg: string, cwd?: string): string | null; + + localRequire(pkg: string, cwd?: string): any; +} + +export namespace Poi { + interface HookHandler { + (config: WebpackChainConfig, opts: Opts): void; + } +} diff --git a/types/poi/package.json b/types/poi/package.json new file mode 100644 index 0000000000..a8453114d7 --- /dev/null +++ b/types/poi/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "cac": "^6.4.2" + } +} diff --git a/types/poi/poi-tests.ts b/types/poi/poi-tests.ts new file mode 100644 index 0000000000..f7d68fb7b4 --- /dev/null +++ b/types/poi/poi-tests.ts @@ -0,0 +1,3 @@ +import { Poi } from "poi"; + +const poi = new Poi(); diff --git a/types/poi/tsconfig.json b/types/poi/tsconfig.json new file mode 100644 index 0000000000..b73cda1e27 --- /dev/null +++ b/types/poi/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "poi-tests.ts" + ] +} diff --git a/types/poi/tslint.json b/types/poi/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/poi/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From fc04d7629eb3c07db4318bd902fda7bf38927947 Mon Sep 17 00:00:00 2001 From: c4605 Date: Thu, 4 Apr 2019 00:14:29 +0800 Subject: [PATCH 2/6] poi: update types --- types/poi/index.d.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/types/poi/index.d.ts b/types/poi/index.d.ts index 15ac85edcc..6fc0e3b536 100644 --- a/types/poi/index.d.ts +++ b/types/poi/index.d.ts @@ -50,6 +50,7 @@ export namespace Config { fileNames?: Output.FileNames; target?: | "web" + | "electron" | "electron-renderer" | "electron-main" | "node" @@ -66,12 +67,14 @@ export namespace Config { image?: string; } - interface Html { + interface HtmlOptions { title?: string; filename?: string; template?: string; inject?: boolean; } + + type Html = boolean | HtmlOptions; } interface Pages { @@ -86,6 +89,14 @@ export namespace Config { interface Babel { jsx?: string; transpileModules?: string | string[]; + namedImports?: string | Babel.NamedImportsOptions; + } + namespace Babel { + interface NamedImportsOptions { + [fileExt: string]: { + [ComponentName: string]: string; + }; + } } interface Css { @@ -129,11 +140,11 @@ export namespace Config { host?: string; port?: string | number; hot?: boolean; - hotOnly?: string; + hotOnly?: boolean; hotEntries?: string[]; historyApiFallback?: WebpackDevServerConfig["historyApiFallback"]; open?: boolean; - proxy?: WebpackDevServerConfig["proxy"]; + proxy?: string | WebpackDevServerConfig["proxy"]; https?: WebpackDevServerConfig["https"]; before?: WebpackDevServerConfig["before"]; after?: WebpackDevServerConfig["after"]; From a6bb5baa87e9278f482b6889ea3baea661303865 Mon Sep 17 00:00:00 2001 From: c4605 Date: Thu, 4 Apr 2019 00:16:04 +0800 Subject: [PATCH 3/6] poi: add some comment --- types/poi/index.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/poi/index.d.ts b/types/poi/index.d.ts index 6fc0e3b536..7fa8c71e23 100644 --- a/types/poi/index.d.ts +++ b/types/poi/index.d.ts @@ -187,6 +187,7 @@ export class Poi { cli: CAC; + /** The current running command */ command: CAC["command"]; config: Config; @@ -197,6 +198,7 @@ export class Poi { constructor(argv?: string[]); + /** Check if a package is included in the `dependencies` or `devDependencies` field your `package.json` */ hasDependency(name: string): boolean; hasPlugin(name: string): boolean; From 50f157a0afe05bd275d0a6d5351f0fa2ec58d074 Mon Sep 17 00:00:00 2001 From: c4605 Date: Sun, 7 Apr 2019 11:04:59 +0800 Subject: [PATCH 4/6] poi: add dependency webpack-chain --- types/poi/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/poi/package.json b/types/poi/package.json index a8453114d7..b348ad1171 100644 --- a/types/poi/package.json +++ b/types/poi/package.json @@ -1,6 +1,7 @@ { "private": true, "dependencies": { - "cac": "^6.4.2" + "cac": "^6.4.2", + "webpack-chain": "^5.2.4" } } From 2967135c088b70108ee18f4093ff8a707a795d5b Mon Sep 17 00:00:00 2001 From: c4605 Date: Tue, 9 Apr 2019 07:54:45 +0800 Subject: [PATCH 5/6] poi: specify typescript version --- types/poi/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/poi/index.d.ts b/types/poi/index.d.ts index 7fa8c71e23..f5087075cc 100644 --- a/types/poi/index.d.ts +++ b/types/poi/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/egoist/poi/ // Definitions by: c4605 // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 import { ICompiler, Configuration as WebpackConfig } from "webpack"; import { Configuration as WebpackDevServerConfig } from "webpack-dev-server"; From e28755ef513eb8a365d8cd35057f06f726b84abc Mon Sep 17 00:00:00 2001 From: c4605 Date: Sun, 14 Apr 2019 23:09:53 +0800 Subject: [PATCH 6/6] poi: write all the export in one namespace --- types/poi/index.d.ts | 358 +++++++++++++++++++++-------------------- types/poi/package.json | 2 +- types/poi/poi-tests.ts | 4 +- 3 files changed, 185 insertions(+), 179 deletions(-) diff --git a/types/poi/index.d.ts b/types/poi/index.d.ts index f5087075cc..f8e346ed9f 100644 --- a/types/poi/index.d.ts +++ b/types/poi/index.d.ts @@ -10,192 +10,22 @@ import { Options as HtmlWebpackPluginOptions } from "html-webpack-plugin"; import * as WebpackChainConfig from "webpack-chain"; import CAC from "cac/types/CAC"; -export type Mode = "production" | "development" | "test"; - -export interface Opts { - type: string; - mode: Mode; - [k: string]: any; -} - -/** - * https://poi.js.org/config.html - */ -export interface Config { - entry?: Config.Entry; - output?: Config.Output; - pages?: Config.Pages; - babel?: Config.Babel; - css?: Config.Css; - assets?: Config.Assets; - envs?: Config.Envs; - constants?: Config.Constants; - chainWebpack?: Config.ChainWebpack; - configureWebpack?: Config.ConfigureWebpack; - publicFolder?: Config.PublicFolder; - devServer?: Config.DevServer; - plugins?: Config.Plugins; -} - -export namespace Config { - type Entry = WebpackConfig["entry"]; - - interface Output { - dir?: string; - clean?: boolean; - format?: "iife" | "cjs" | "umd"; - moduleName?: string; - sourceMap?: boolean; - minimize?: boolean; - publicUrl?: string; - fileNames?: Output.FileNames; - target?: - | "web" - | "electron" - | "electron-renderer" - | "electron-main" - | "node" - | "node-webkit" - | "async-node" - | "webworker"; - html?: Output.Html; - } - namespace Output { - interface FileNames { - js?: string; - css?: string; - font?: string; - image?: string; - } - - interface HtmlOptions { - title?: string; - filename?: string; - template?: string; - inject?: boolean; - } - - type Html = boolean | HtmlOptions; - } - - interface Pages { - [pageName: string]: - | string - | Partial & { - entry: string; - chunks?: string[]; - }; - } - - interface Babel { - jsx?: string; - transpileModules?: string | string[]; - namedImports?: string | Babel.NamedImportsOptions; - } - namespace Babel { - interface NamedImportsOptions { - [fileExt: string]: { - [ComponentName: string]: string; - }; - } - } - - interface Css { - extract?: boolean; - sourceMap?: boolean; - loaderOptions?: Css.LoaderOptions; - } - namespace Css { - interface LoaderOptions { - css?: any; - sass?: any; - postcss?: any; - less?: any; - stylus?: any; - } - } - - interface Assets { - inlineImageMaxSize?: number; - } - - interface Envs { - [envName: string]: string; - } - - interface Constants { - [constantName: string]: string; - } - - interface ChainWebpack { - (config: WebpackChainConfig, opts: Opts): void; - } - - type ConfigureWebpack = - | WebpackConfig - | ((config: WebpackConfig, opts: Opts) => void | WebpackConfig); - - type PublicFolder = string | boolean; - - interface DevServer { - host?: string; - port?: string | number; - hot?: boolean; - hotOnly?: boolean; - hotEntries?: string[]; - historyApiFallback?: WebpackDevServerConfig["historyApiFallback"]; - open?: boolean; - proxy?: string | WebpackDevServerConfig["proxy"]; - https?: WebpackDevServerConfig["https"]; - before?: WebpackDevServerConfig["before"]; - after?: WebpackDevServerConfig["after"]; - headers?: WebpackDevServerConfig["headers"]; - } - - interface PluginOption { - resolve: string; - options?: any; - } - type Plugins = Array; -} - -export interface ConfigLoader { - resolve( - files?: ReadonlyArray, - cwd?: string, - stopDir?: string - ): string | null; - resolve(options?: ConfigLoader.Options): string | null; - - load(files?: ReadonlyArray, cwd?: string, stopDir?: string): any; - load(options?: ConfigLoader.Options): any; -} -export namespace ConfigLoader { - interface Options { - files?: string[]; - cwd?: string; - stopDir?: string; - packageKey?: string; - parseJSON?: (str: string) => any; - } -} - /** * https://poi.js.org/api.html#constructor-argv */ -export class Poi { - mode: Mode; +declare class PoiCore { + mode: PoiCore.Mode; cli: CAC; /** The current running command */ command: CAC["command"]; - config: Config; + config: PoiCore.Config; isProd: boolean; - configLoader: ConfigLoader; + configLoader: PoiCore.ConfigLoader; constructor(argv?: string[]); @@ -204,7 +34,7 @@ export class Poi { hasPlugin(name: string): boolean; - hook(hookName: string, handler: Poi.HookHandler): void; + hook(hookName: string, handler: PoiCore.HookHandler): void; resolveCwd(...args: string[]): string; @@ -230,8 +60,184 @@ export class Poi { localRequire(pkg: string, cwd?: string): any; } -export namespace Poi { +declare namespace PoiCore { + type Mode = "production" | "development" | "test"; + + interface Opts { + type: string; + mode: Mode; + [k: string]: any; + } + + /** + * https://poi.js.org/config.html + */ + interface Config { + entry?: Config.Entry; + output?: Config.Output; + pages?: Config.Pages; + babel?: Config.Babel; + css?: Config.Css; + assets?: Config.Assets; + envs?: Config.Envs; + constants?: Config.Constants; + chainWebpack?: Config.ChainWebpack; + configureWebpack?: Config.ConfigureWebpack; + publicFolder?: Config.PublicFolder; + devServer?: Config.DevServer; + plugins?: Config.Plugins; + } + + namespace Config { + type Entry = WebpackConfig["entry"]; + + interface Output { + dir?: string; + clean?: boolean; + format?: "iife" | "cjs" | "umd"; + moduleName?: string; + sourceMap?: boolean; + minimize?: boolean; + publicUrl?: string; + fileNames?: Output.FileNames; + target?: + | "web" + | "electron" + | "electron-renderer" + | "electron-main" + | "node" + | "node-webkit" + | "async-node" + | "webworker"; + html?: Output.Html; + } + namespace Output { + interface FileNames { + js?: string; + css?: string; + font?: string; + image?: string; + } + + interface HtmlOptions { + title?: string; + filename?: string; + template?: string; + inject?: boolean; + } + + type Html = boolean | HtmlOptions; + } + + interface Pages { + [pageName: string]: + | string + | Partial & { + entry: string; + chunks?: string[]; + }; + } + + interface Babel { + jsx?: string; + transpileModules?: string | string[]; + namedImports?: string | Babel.NamedImportsOptions; + } + namespace Babel { + interface NamedImportsOptions { + [fileExt: string]: { + [ComponentName: string]: string; + }; + } + } + + interface Css { + extract?: boolean; + sourceMap?: boolean; + loaderOptions?: Css.LoaderOptions; + } + namespace Css { + interface LoaderOptions { + css?: any; + sass?: any; + postcss?: any; + less?: any; + stylus?: any; + } + } + + interface Assets { + inlineImageMaxSize?: number; + } + + interface Envs { + [envName: string]: string; + } + + interface Constants { + [constantName: string]: string; + } + + interface ChainWebpack { + (config: WebpackChainConfig, opts: Opts): void; + } + + type ConfigureWebpack = + | WebpackConfig + | ((config: WebpackConfig, opts: Opts) => void | WebpackConfig); + + type PublicFolder = string | boolean; + + interface DevServer { + host?: string; + port?: string | number; + hot?: boolean; + hotOnly?: boolean; + hotEntries?: string[]; + historyApiFallback?: WebpackDevServerConfig["historyApiFallback"]; + open?: boolean; + proxy?: string | WebpackDevServerConfig["proxy"]; + https?: WebpackDevServerConfig["https"]; + before?: WebpackDevServerConfig["before"]; + after?: WebpackDevServerConfig["after"]; + headers?: WebpackDevServerConfig["headers"]; + } + + interface PluginOption { + resolve: string; + options?: any; + } + type Plugins = Array; + } + + interface ConfigLoader { + resolve( + files?: ReadonlyArray, + cwd?: string, + stopDir?: string + ): string | null; + resolve(options?: ConfigLoader.Options): string | null; + + load( + files?: ReadonlyArray, + cwd?: string, + stopDir?: string + ): any; + load(options?: ConfigLoader.Options): any; + } + namespace ConfigLoader { + interface Options { + files?: string[]; + cwd?: string; + stopDir?: string; + packageKey?: string; + parseJSON?: (str: string) => any; + } + } + interface HookHandler { (config: WebpackChainConfig, opts: Opts): void; } } + +export = PoiCore; diff --git a/types/poi/package.json b/types/poi/package.json index b348ad1171..54a71aeaf1 100644 --- a/types/poi/package.json +++ b/types/poi/package.json @@ -1,7 +1,7 @@ { "private": true, "dependencies": { - "cac": "^6.4.2", + "cac": "~6.4.2", "webpack-chain": "^5.2.4" } } diff --git a/types/poi/poi-tests.ts b/types/poi/poi-tests.ts index f7d68fb7b4..82be4a2d49 100644 --- a/types/poi/poi-tests.ts +++ b/types/poi/poi-tests.ts @@ -1,3 +1,3 @@ -import { Poi } from "poi"; +import * as PoiCore from "poi"; -const poi = new Poi(); +const poi = new PoiCore();