vite-plugin-glsl
Advanced tools
Comparing version
@@ -5,10 +5,15 @@ { | ||
"homepage": "https://github.com/UstymUkhman/vite-plugin-glsl#readme", | ||
"packageManager": "^npm@9.8.0", | ||
"packageManager": "^npm@10.8.3", | ||
"types": "./src/index.d.ts", | ||
"module": "./src/index.js", | ||
"main": "./src/index.js", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"private": false, | ||
"license": "MIT", | ||
"type": "module", | ||
"author": { | ||
"name": "Ustym Ukhman", | ||
"email": "ustym.ukhman@gmail.com", | ||
"url": "https://github.com/UstymUkhman/" | ||
}, | ||
"repository": { | ||
@@ -22,6 +27,6 @@ "type": "git", | ||
}, | ||
"author": { | ||
"name": "Ustym Ukhman", | ||
"email": "ustym.ukhman@gmail.com", | ||
"url": "https://github.com/UstymUkhman/" | ||
"publishConfig": { | ||
"save-dev": true, | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
@@ -37,3 +42,3 @@ "exports": { | ||
"README.md", | ||
"yarn.lock", | ||
"bun.lock", | ||
"ext.d.ts", | ||
@@ -64,11 +69,11 @@ "LICENSE", | ||
"dependencies": { | ||
"@rollup/pluginutils": "^5.1.0" | ||
"@rollup/pluginutils": "^5.1.4" | ||
}, | ||
"devDependencies": { | ||
"vite": "^6.0.1" | ||
"vite": "^6.1.0" | ||
}, | ||
"engines": { | ||
"node": ">= 16.15.1", | ||
"npm": ">= 8.11.0" | ||
"node": ">= 20.17.0", | ||
"npm": ">= 10.8.3" | ||
} | ||
} |
@@ -59,3 +59,3 @@ # Vite Plugin GLSL # | ||
glsl({ | ||
include: [ // Glob pattern, or array of glob patterns to import | ||
include: [ // Glob pattern, or array of glob patterns to import | ||
'**/*.glsl', '**/*.wgsl', | ||
@@ -65,8 +65,9 @@ '**/*.vert', '**/*.frag', | ||
], | ||
exclude: undefined, // Glob pattern, or array of glob patterns to ignore | ||
warnDuplicatedImports: true, // Warn if the same chunk was imported multiple times | ||
defaultExtension: 'glsl', // Shader suffix when no extension is specified | ||
compress: false, // Compress output shader code | ||
watch: true, // Recompile shader on change | ||
root: '/' // Directory for root imports | ||
exclude: undefined, // Glob pattern, or array of glob patterns to ignore | ||
warnDuplicatedImports: true, // Warn if the same chunk was imported multiple times | ||
removeDuplicatedImports: false, // Automatically remove an already imported chunk | ||
defaultExtension: 'glsl', // Shader suffix when no extension is specified | ||
compress: false, // Compress output shader code | ||
watch: true, // Recompile shader on change | ||
root: '/' // Directory for root imports | ||
}) | ||
@@ -192,2 +193,6 @@ ``` | ||
- Starting from `v1.3.2` this plugin allows to automatically remove already imported chunks with the `removeDuplicatedImports` option set to `true`. | ||
- Starting from `v1.3.1` this plugin is fully compatible with `vite^6.0.0`. | ||
- Starting from `v1.3.0` this plugin will not remove comments starting with `///`, unless `compress` option is set to `true`. | ||
@@ -194,0 +199,0 @@ |
@@ -18,5 +18,5 @@ import type { PluginOptions } from './types.d'; | ||
export default function ({ | ||
include, | ||
exclude, | ||
include, exclude, | ||
warnDuplicatedImports, | ||
removeDuplicatedImports, | ||
defaultExtension, | ||
@@ -23,0 +23,0 @@ compress, |
@@ -5,3 +5,3 @@ /** | ||
* @description Import, inline (and compress) GLSL shader files | ||
* @version 1.3.1 | ||
* @version 1.3.2 | ||
* @license MIT | ||
@@ -51,2 +51,3 @@ */ | ||
warnDuplicatedImports = true, | ||
removeDuplicatedImports = false, | ||
defaultExtension = DEFAULT_EXTENSION, | ||
@@ -74,2 +75,3 @@ compress = false, | ||
const { dependentChunks, outputShader } = loadShader(source, shader, { | ||
removeDuplicatedImports, | ||
warnDuplicatedImports, | ||
@@ -81,6 +83,4 @@ defaultExtension, | ||
if (watch && !prod) { | ||
const chunks = Array.from(dependentChunks.values()).flat(); | ||
chunks.forEach(chunk => this.addWatchFile(chunk)); | ||
} | ||
watch && !prod && Array.from(dependentChunks.values()) | ||
.flat().forEach(chunk => this.addWatchFile(chunk)); | ||
@@ -87,0 +87,0 @@ return await transformWithEsbuild(outputShader, shader, { |
@@ -14,2 +14,3 @@ import type { LoadingOptions, LoadingOutput } from './types.d'; | ||
* - Warn if the same chunk was imported multiple times | ||
* - Automatically remove an already imported chunk | ||
* - Shader suffix when no extension is specified | ||
@@ -23,5 +24,4 @@ * - Compress output shader code | ||
export default function ( | ||
source: string, | ||
shader: string, | ||
source: string, shader: string, | ||
options: LoadingOptions | ||
): LoadingOutput; |
@@ -98,3 +98,2 @@ import { dirname, resolve, extname, posix, sep } from 'path'; | ||
function checkDuplicatedImports (path) { | ||
if (!allChunks.has(path)) return; | ||
const caller = getRecursionCaller(); | ||
@@ -151,10 +150,17 @@ | ||
* have caused a recursion error or warning | ||
* ignoring duplicate chunks if required | ||
* | ||
* @param {string} path Shader's absolute path | ||
* @param {boolean} warn Check already included chunks | ||
* @param {string} path Shader's absolute path | ||
* @param {boolean} warn Check already included chunks | ||
* @param {boolean} ignore Ignore already included chunks | ||
* | ||
* @returns {boolean} Import recursion has occurred | ||
* @returns {boolean | null} Import recursion has occurred | ||
* or chunk was ignored because of `ignore` argument | ||
*/ | ||
function checkRecursiveImports (path, warn) { | ||
warn && checkDuplicatedImports(path); | ||
function checkRecursiveImports (path, warn, ignore) { | ||
if (allChunks.has(path)) { | ||
if (ignore) return null; | ||
warn && checkDuplicatedImports(path); | ||
} | ||
return checkIncludedDependencies(path, path); | ||
@@ -230,19 +236,22 @@ } | ||
* | ||
* @param {string} source Shader's source code | ||
* @param {string} path Shader's absolute path | ||
* @param {string} extension Default shader extension | ||
* @param {boolean} warn Check already included chunks | ||
* @param {string} root Shader's root directory | ||
* @param {string} source Shader's source code | ||
* @param {string} path Shader's absolute path | ||
* @param {Options} options Shader loading config object | ||
* | ||
* @throws {Error} If shader chunks started a recursion loop | ||
* @throws {Error} If shader chunks started a recursion loop | ||
* | ||
* @returns {string} Shader's source code without external chunks | ||
*/ | ||
function loadChunks (source, path, extension, warn, root) { | ||
function loadChunks (source, path, options) { | ||
const { warnDuplicatedImports, removeDuplicatedImports } = options; | ||
const unixPath = path.split(sep).join(posix.sep); | ||
if (checkRecursiveImports(unixPath, warn)) { | ||
return recursiveChunk; | ||
} | ||
const recursion = checkRecursiveImports( | ||
unixPath, warnDuplicatedImports, | ||
removeDuplicatedImports | ||
); | ||
if (recursion) return recursiveChunk; | ||
else if (recursion === null) return; | ||
source = removeSourceComments(source); | ||
@@ -255,2 +264,3 @@ let directory = dirname(unixPath); | ||
const currentDirectory = directory; | ||
const ext = options.defaultExtension; | ||
@@ -262,3 +272,3 @@ source = source.replace(include, (_, chunkPath) => { | ||
const base = cwd().split(sep).join(posix.sep); | ||
chunkPath = base + root + chunkPath; | ||
chunkPath = base + options.root + chunkPath; | ||
} | ||
@@ -275,5 +285,4 @@ | ||
let shader = resolve(directory, chunkPath); | ||
if (!extname(shader)) shader = `${shader}.${ext}`; | ||
if (!extname(shader)) shader = `${shader}.${extension}`; | ||
const shaderPath = shader.split(sep).join(posix.sep); | ||
@@ -284,4 +293,3 @@ dependentChunks.get(unixPath)?.push(shaderPath); | ||
readFileSync(shader, 'utf8'), | ||
shader, extension, | ||
warn, root | ||
shader, options | ||
); | ||
@@ -306,11 +314,12 @@ }); | ||
* @name loadShader | ||
* @description Iterates through all external chunks, | ||
* includes them into the shader's source code | ||
* and optionally compresses the output | ||
* @typedef {import('./types').LoadingOptions} Options | ||
* @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: | ||
* @param {string} source Shader's source code | ||
* @param {string} shader Shader's absolute path | ||
* @param {Options} options Configuration object to define: | ||
* | ||
* - Warn if the same chunk was imported multiple times | ||
* - Automatically remove an already imported chunk | ||
* - Shader suffix when no extension is specified | ||
@@ -320,15 +329,11 @@ * - Compress output shader code | ||
* | ||
* @returns {LoadingOutput} Loaded, parsed (and compress) | ||
* @returns {LoadingOutput} Loaded, parsed (and compressed) | ||
* shader output and Map of shaders that import other chunks | ||
*/ | ||
export default function (source, shader, options) { | ||
const { | ||
warnDuplicatedImports, | ||
defaultExtension, | ||
compress, root | ||
} = options; | ||
const { compress, ...config } = options; | ||
resetSavedChunks(); | ||
let output = loadChunks(source, shader, defaultExtension, warnDuplicatedImports, root); | ||
let output = loadChunks(source, shader, config); | ||
output = compress ? removeSourceComments(output, true) : output; | ||
@@ -335,0 +340,0 @@ |
@@ -1,13 +0,7 @@ | ||
/** | ||
* @const | ||
* @readonly | ||
* @typedef {string | string[]} | ||
*/ | ||
/** @typedef {string | string[]} GlobPattern */ | ||
export type GlobPattern = string | string[]; | ||
/** | ||
* @const | ||
* @readonly | ||
* @default false | ||
* @typedef {boolean | ((shader: string) => string)} | ||
* @typedef {boolean | ((shader: string) => string)} Compress | ||
* | ||
@@ -24,13 +18,14 @@ * @description Boolean value or custom callback | ||
/** | ||
* @typedef {Object} | ||
* @name LoadingOptions | ||
* @typedef {Object} 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 {Compress} compress Compress output shader code | ||
* @property {string} root Directory for root imports | ||
* @property {boolean} warnDuplicatedImports Warn if the same chunk was imported multiple times | ||
* @property {boolean} removeDuplicatedImports Automatically remove an already imported chunk | ||
* @property {string} defaultExtension Shader suffix when no extension is specified | ||
* @property {Compress} compress Compress output shader code | ||
* @property {string} root Directory for root imports | ||
*/ | ||
export type LoadingOptions = { | ||
warnDuplicatedImports: boolean; | ||
removeDuplicatedImports: boolean; | ||
defaultExtension: string; | ||
@@ -43,5 +38,4 @@ compress: Compress; | ||
* @since 0.2.0 | ||
* @typedef {Object} | ||
* @name PluginOptions | ||
* @extends LoadingOptions | ||
* @typedef {Object} PluginOptions | ||
* @description Plugin config object | ||
@@ -57,2 +51,3 @@ * | ||
* warnDuplicatedImports: true, | ||
* removeDuplicatedImports: false, | ||
* defaultExtension: DEFAULT_EXTENSION, | ||
@@ -72,7 +67,7 @@ * compress: false, | ||
* @since 1.1.2 | ||
* @typedef {Object} | ||
* @name LoadingOutput | ||
* @description Loaded, parsed (and compress) shader | ||
* output and Map of shaders that import other chunks | ||
* @typedef {Object} LoadingOutput | ||
* | ||
* @returns {LoadingOutput} Loaded, parsed (and compressed) | ||
* shader output and Map of shaders that import other chunks | ||
* | ||
* @property {Map<string, string[]>} dependentChunks Map of shaders that import other chunks | ||
@@ -79,0 +74,0 @@ * @property {string} outputShader Shader file with included chunks |
40967
58.92%10
11.11%238
2.15%552
-0.18%Updated