Skip to content

Commit

Permalink
Add compress function option
Browse files Browse the repository at this point in the history
  • Loading branch information
UstymUkhman committed Nov 9, 2022
1 parent ba8e7ad commit d95d99d
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 171 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ void main (void) {

- Starting from `v0.5.0` this plugin supports shaders hot reloading when `watch` option is set to `true`.

- Starting from `v0.5.4` this plugin supports custom `compress` callback function optimize to output shader length after all shader chunks have been included.

### Note: ###

When used with [three.js](https://github.com/mrdoob/three.js) r0.99 and higher, it's possible to include shader chunks as specified in the [documentation](https://threejs.org/docs/index.html?q=Shader#api/en/materials/ShaderMaterial), those imports will be ignored by `vite-plugin-glsl` since they are handled internally by the library itself:
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"types": "src/index.d.ts",
"module": "src/index.js",
"main": "src/index.js",
"version": "0.5.3",
"version": "0.5.4",
"private": false,
"license": "MIT",
"type": "module",
Expand Down Expand Up @@ -48,10 +48,10 @@
"vite": "^3.0.0"
},
"dependencies": {
"@rollup/pluginutils": "^5.0.1"
"@rollup/pluginutils": "^5.0.2"
},
"devDependencies": {
"vite": "^3.1.8"
"vite": "^3.2.3"
},
"engines": {
"node": ">= 16.15.1",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module vite-plugin-glsl
* @author Ustym Ukhman <[email protected]>
* @description Import, inline (and compress) GLSL shader files
* @version 0.5.3
* @version 0.5.4
* @license MIT
*/

Expand Down
6 changes: 3 additions & 3 deletions src/loadShader.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import type { LoadingOptions } from './types.d';
/**
* @function
* @name loadShader
* @description Iterates through all external chunks
* and includes them into the shader's source code
* @description Iterates through all external chunks, includes them
* into the shader's source code and optionally compresses the output
*
* @param {string} source Shader's source code
* @param {string} shader Shader's absolute path
* @param {LoadingOptions} options Configuration object to define:
*
* - warn if the same chunk was imported multiple times
* - default shader extension when no extension is specified
* - whether to compress output shader code
* - whether (and how) to compress output shader code
* - directory for chunk imports from root
*
* @returns {string} Shader file with included chunks
Expand Down
18 changes: 9 additions & 9 deletions src/loadShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,17 @@ function loadChunks (source, path, extension, warn, root) {
/**
* @function
* @name loadShader
* @description Iterates through all external chunks
* and includes them into the shader's source code
* @description Iterates through all external chunks,
* includes them into the shader's source code
* and optionally compresses the output
*
* @param {string} source Shader's source code
* @param {string} shader Shader's absolute path
* @param {LoadingOptions} options Configuration object to define:
*
* - warn if the same chunk was imported multiple times
* - default shader extension when no extension is specified
* - whether to compress output shader code
* - whether (and how) to compress output shader code
* - directory for chunk imports from root
*
* @returns {string} Shader file with included chunks
Expand All @@ -308,13 +309,12 @@ export default function (source, shader, options) {

resetSavedChunks();

return compress ? comressShader(
loadChunks(
source, shader, defaultExtension,
warnDuplicatedImports, root
)
) : loadChunks(
const output = loadChunks(
source, shader, defaultExtension,
warnDuplicatedImports, root
);

return !compress ? output
: typeof compress === 'function'
? compress(output) : comressShader(output);
}
27 changes: 21 additions & 6 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,36 @@
*/
export type GlobPattern = string | string[];

/**
* @const
* @readonly
* @default false
* @typedef {boolean | ((shader: string) => string)}
*
* @description Boolean value or custom callback
* function to optimize output shader length
*
* @param {string} shader Shader code with included chunks
*
* @returns {string} Compressed shader's source code
*/
type Compress = boolean | ((shader: string) => string);

/**
* @typedef {Object}
* @name LoadingOptions
* @description Shader loading config object
*
* @property {boolean} warnDuplicatedImports Warn if the same chunk was imported multiple times
* @property {string} defaultExtension Shader suffix when no extension is specified
* @property {boolean} compress Compress output shader code
* @property {boolean} watch Recompile shader on change
* @property {string} root Directory for root imports
* @property {boolean} warnDuplicatedImports Warn if the same chunk was imported multiple times
* @property {string} defaultExtension Shader suffix when no extension is specified
* @property {Compress} compress Compress output shader code
* @property {boolean} watch Recompile shader on change
* @property {string} root Directory for root imports
*/
export type LoadingOptions = {
warnDuplicatedImports: boolean;
defaultExtension: string;
compress: boolean;
compress: Compress;
watch: boolean;
root: string;
};
Expand Down
Loading

0 comments on commit d95d99d

Please sign in to comment.