From bec39516febed25fd08317d86fd6c61857b4659b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E9=B8=A1=E6=B2=A1=E5=90=8D?= Date: Mon, 8 Jan 2024 11:01:36 +0800 Subject: [PATCH 1/2] fix: update types --- index.d.ts | 107 +++++++++++++++++++++++++------------------------- index.js | 8 ++-- tsconfig.json | 14 +++++++ 3 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 tsconfig.json diff --git a/index.d.ts b/index.d.ts index 6f4a510..3aa23a3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,64 +1,63 @@ declare module 'vite-plugin-optimizer' { - function optimizer(entries: Entries, options?: OptimizerOptions): import('vite').Plugin; - // https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html#default-exports - export = optimizer; -} - -export interface OptimizerArgs { - /** Generated file cache directory */ - dir: string; -} + export interface OptimizerArgs { + /** Generated file cache directory */ + dir: string; + } -export interface ResultDescription { - /** - * This is consistent with the `alias` behavior. - * - * e.g. - * `import fs from 'fs'` - * or - * `import fs from 'node:fs'` - * - * @example - * { - * // This means that both 'fs' and 'node:fs' are supported. - * find: /^(node:)?fs$/, - * replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js', - * } - */ - alias?: { - find: string | RegExp; + export interface ResultDescription { /** - * If not explicitly specified, will use the path to the generated file as the default. + * This is consistent with the `alias` behavior. + * + * e.g. + * `import fs from 'fs'` + * or + * `import fs from 'node:fs'` + * + * @example + * { + * // This means that both 'fs' and 'node:fs' are supported. + * find: /^(node:)?fs$/, + * replacement: '/project/node_modules/.vite-plugin-optimizer/fs.js', + * } */ - replacement?: string; - }; - code?: string; -} + alias?: { + find: string | RegExp; + /** + * If not explicitly specified, will use the path to the generated file as the default. + */ + replacement?: string; + }; + code?: string; + } -export interface Entries { - [moduleId: string]: - | string - | ResultDescription - | ((args: OptimizerArgs) => string | ResultDescription | Promise | void); -} + export interface Entries { + [moduleId: string]: + | string + | ResultDescription + | ((args: OptimizerArgs) => string | ResultDescription | Promise | void); + } -export interface OptimizerOptions { - /** - * @default ".vite-plugin-optimizer" - */ - dir?: string; - resolveId?: ((id: string) => string | Promise | void); -} + export interface OptimizerOptions { + /** + * @default ".vite-plugin-optimizer" + */ + dir?: string; + resolveId?: ((id: string) => string | Promise | void); + } -// --------- utils --------- + // --------- utils --------- + + export type GenerateRecord = { + alias?: ResultDescription['alias']; + module: string; + // Absolute path of file + filename: string; + }; -export type GenerateRecord = { - alias?: ResultDescription['alias']; - module: string; - // Absolute path of file - filename: string; -}; + export interface GenerateModule { + (entries: Entries, options?: OptimizerOptions): Promise; + } -export interface GenerateModule { - (entries: Entries, options?: OptimizerOptions): Promise; + const optimizer: ((entries: Entries, options?: OptimizerOptions) => import('vite').Plugin); + export default optimizer; } diff --git a/index.js b/index.js index 4df3e45..d850868 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ const DIR = '.vite-plugin-optimizer'; const EXT = '.js'; /** - * @type {import('vite-plugin-optimizer')} + * @type {import('vite-plugin-optimizer')['default']} */ module.exports = function optimizer(entries, options = {}) { if (typeof options.dir === 'undefined') options.dir = DIR; @@ -63,13 +63,13 @@ module.exports = function optimizer(entries, options = {}) { } /** - * @type {import('.').GenerateModule} + * @type {import('vite-plugin-optimizer').GenerateModule} */ async function generateModule(entries, options) { const dir = options.dir; // Here, must be absolute path. /** - * @type {import('.').GenerateRecord[]} + * @type {import('vite-plugin-optimizer').GenerateRecord[]} */ const generateRecords = []; for (const [module, variableType] of Object.entries(entries)) { @@ -85,7 +85,7 @@ async function generateModule(entries, options) { let moduleContent = null; /** - * @type {import('.').GenerateRecord} + * @type {import('vite-plugin-optimizer').GenerateRecord} */ let record = { module, filename }; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..6ecc7b2 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ES2022", + "module": "ESNext", + "esModuleInterop": true, + "moduleResolution": "Node", + "resolveJsonModule": true, + "strict": true, + "allowJs": true, + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "baseUrl": "." + } +} From 3a099d29b6d162053fdbc239e5e34d7fc3378ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E9=B8=A1=E6=B2=A1=E5=90=8D?= Date: Mon, 8 Jan 2024 11:03:50 +0800 Subject: [PATCH 2/2] v1.4.3 --- CHANGELOG.md | 10 +++++++++- package.json | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e9d0c3..5246f12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ -## [2022-10-15] v1.4.1 +## [2024-01-08] v1.4.3 + +- bec3951 fix: update types +- 372e72a Merge pull request #8 from CharleeWa/main +- 7d008ef chore: Specify TypeScript declaration file +- ef68f59 Merge pull request #6 from learnsomesome/patch-1 +- 4294887 Update README.md + +## [2022-10-15] v1.4.2 - bc1935f v1.4.2 - babc0e1 chroe: comments diff --git a/package.json b/package.json index 106ae4c..269f8a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-optimizer", - "version": "1.4.2", + "version": "1.4.3", "description": "Manually Pre-Bundling of Vite", "main": "index.js", "types": "index.d.ts",