Skip to content

Commit

Permalink
Merge pull request #12 from ErKeLost/future_generate_bundle
Browse files Browse the repository at this point in the history
Future generate bundle
  • Loading branch information
ErKeLost authored Nov 30, 2022
2 parents c4e2ee9 + 97f3297 commit 59be9ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
18 changes: 9 additions & 9 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ export default defineConfig({
imagemin({
// 捆绑前构建 或者 捆绑后构建
// beforeBundle: true,
beforeBundle: false,
mode: 'sharp',
// beforeBundle: false,
// mode: 'sharp',
// mode: 'squoosh',
compress: {
// jpg: {
// quality: 0,
// },
// jpeg: {
// quality: 100,
// quality: 25,
// },
// png: {
// quality: 100,
// quality: 50,
// },
// webp: {
// quality: 60,
// },
},
conversion: [
// { from: 'png', to: 'avif' },
// { from: 'jpeg', to: 'webp' },
],
cache: false,
// conversion: [
// { from: 'png', to: 'avif' },
// { from: 'jpeg', to: 'webp' },
// ],
// cache: false,
}),
],
});
2 changes: 1 addition & 1 deletion src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
filterExtension,
exists,
generateImageID,
parseURL,
transformFileName,
} from './utils';
import { basename, extname, join, resolve } from 'pathe';
Expand Down Expand Up @@ -129,6 +128,7 @@ export default class Context {
);
return `export default ${devalue(generatePath)}`;
}
return '';
}

// 生成bundle
Expand Down
16 changes: 8 additions & 8 deletions src/core/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { encodeMap, encodeMapBack } from './encodeMap';
import { compressSuccess, logger } from './log';
import chalk from 'chalk';
import { extname } from 'pathe';
import { sharpOptions } from './types';
async function initSharp(config) {
const { files, outputPath, cache, chunks, options, isTurn } = config;
const images = files.map(async (filePath: string) => {
Expand All @@ -31,19 +32,18 @@ async function initSharp(config) {
(item) => item.from === extname(fileRootPath).slice(1),
);
let resultBuffer;
console.log(await sharp(fileRootPath));

const fileExt = extname(fileRootPath).slice(1);
if (currentType !== undefined) {
const merge = {
...sharpOptions[ext],
...options.compress[currentType.to],
};
resultBuffer = await sharp(fileRootPath)
[currentType.to](options.compress[currentType.to])
[currentType.to](merge)
.toBuffer();
} else {
resultBuffer = await sharp(fileRootPath)
[fileExt]({
quality: 50,
})
.toBuffer();
const merge = { ...sharpOptions[ext], ...options.compress[ext] };
resultBuffer = await sharp(fileRootPath)[fileExt](merge).toBuffer();
}
await proFs.writeFile(filepath, resultBuffer);
const data = await proFs.stat(filepath);
Expand Down
14 changes: 11 additions & 3 deletions src/core/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export const defaultOptions: Options = {
effort: 5,
pass: 1,
sns: 50,
uv_mode: 0 /*UVMode.UVModeAuto*/,
csp_type: 0 /*Csp.kYCoCg*/,
uv_mode: 0 /* UVMode.UVModeAuto */,
csp_type: 0 /* Csp.kYCoCg */,
error_diffusion: 0,
use_random_matrix: false,
},
Expand Down Expand Up @@ -135,7 +135,7 @@ export const sharpOptions: any = {
compressionLevel: 6,
adaptiveFiltering: false,
force: true,
palette: false,
palette: true,
quality: 75,
effort: 7,
bitdepth: 8,
Expand Down Expand Up @@ -176,3 +176,11 @@ export const sharpOptions: any = {
resolutionUnit: 'inch',
},
};

export const resolveDefaultOptions = {
mode: 'sharp',
conversion: [],
beforeBundle: false,
cache: false,
compress: {},
};
16 changes: 3 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import { resolveDefaultOptions } from './core/types/index';
import { createUnplugin } from 'unplugin';
import Context from './core/context';
import { parseId } from './core/utils';

export default createUnplugin<any | undefined>((options = {}): any => {
const ctx = new Context();
if (!options.conversion) {
options.conversion = [];
}
if (!options.mode) {
options.mode = 'sharp';
}
const obj = Object.assign(options, resolveDefaultOptions);
return {
name: 'unplugin-imagemin',
apply: 'build',
enforce: 'pre',
async configResolved(config) {
ctx.handleMergeOptionHook({ ...config, options });
ctx.handleMergeOptionHook({ ...config, options: obj });
},
async load(id) {
const imageModuleFlag = ctx.filter(id);
if (imageModuleFlag) {
const { path } = parseId(id);
ctx.setAssetsPath(path);
}
if (options.beforeBundle) {
const res = ctx.loadBundleHook(id);
if (res) {
Expand Down

0 comments on commit 59be9ae

Please sign in to comment.