Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1.4.3 #9

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
107 changes: 53 additions & 54 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<string | ResultDescription | void> | void);
}
export interface Entries {
[moduleId: string]:
| string
| ResultDescription
| ((args: OptimizerArgs) => string | ResultDescription | Promise<string | ResultDescription | void> | void);
}

export interface OptimizerOptions {
/**
* @default ".vite-plugin-optimizer"
*/
dir?: string;
resolveId?: ((id: string) => string | Promise<string | void> | void);
}
export interface OptimizerOptions {
/**
* @default ".vite-plugin-optimizer"
*/
dir?: string;
resolveId?: ((id: string) => string | Promise<string | void> | 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<GenerateRecord[]>;
}

export interface GenerateModule {
(entries: Entries, options?: OptimizerOptions): Promise<GenerateRecord[]>;
const optimizer: ((entries: Entries, options?: OptimizerOptions) => import('vite').Plugin);
export default optimizer;
}
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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 };

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"esModuleInterop": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"strict": true,
"allowJs": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"baseUrl": "."
}
}