Skip to content

Commit e34868e

Browse files
committed
glslify: error checking, refactor
1 parent a0f9c58 commit e34868e

File tree

7 files changed

+76
-48
lines changed

7 files changed

+76
-48
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ Available in [CHANGES.md](CHANGES.md).
123123
## License
124124

125125
Released under the [MIT license](LICENSE).\
126-
*Thanks to Vincent Wochnik ([rollup-plugin-glsl](https://github.com/vwochnik/rollup-plugin-glsl)) for the whitespace minification code.*
126+
*Strip whitespace function adapted from code by Vincent Wochnik ([rollup-plugin-glsl](https://github.com/vwochnik/rollup-plugin-glsl)).*
127127

128128
Khronos tool binaries (built by the upstream projects) are distributed and installed with this plugin under the terms of the Apache License Version 2.0. See the corresponding LICENSE files in the ``bin`` folder.
129129

package-lock.json

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rollup-plugin-glsl-optimize",
3-
"version": "0.0.14",
3+
"version": "0.0.15",
44
"description": "Import GLSL source files as strings. Pre-processed, validated and optimized with Khronos Group SPIRV-Tools. Supports glslify.",
55
"homepage": "https://github.com/docd27/rollup-plugin-glsl-optimize#readme",
66
"keywords": [
@@ -81,4 +81,4 @@
8181
"rollup-plugin-dts": "^3.0.1",
8282
"typescript": "^4.2.3"
8383
}
84-
}
84+
}

rollup.config.bundletypes.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ function earlyDel(targets = [], deleteOptions = {}) {
2929
};
3030
}
3131

32-
function appendDts (filePath) {
32+
function appendDts(filePath) {
3333
const fileContent = fsSync.readFileSync(filePath, {encoding: 'utf8'});
3434
return {
3535
name: 'append-dts',
36-
renderChunk (code) {
36+
renderChunk(code) {
3737
return `${code}\n${fileContent}`;
38-
}
39-
}
38+
},
39+
};
4040
}
4141

4242
export default [{
@@ -51,5 +51,5 @@ export default [{
5151
// appendDts('src/resources.d.ts'),
5252
del({hook: 'buildEnd', targets: [IN_DIR]}),
5353
],
54-
external: [ 'child_process' ],
54+
external: ['child_process'],
5555
}];

src/index.js

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createFilter} from '@rollup/pluginutils';
22
import {glslProcessSource} from './lib/glslProcess.js';
3-
import {dirname} from 'path';
3+
import {glslifyInit, glslifyProcessSource} from './lib/glslify.js';
44
import * as fsSync from 'fs';
55

66
/**
@@ -44,14 +44,9 @@ function generateCode(source) {
4444
* File extensions within rollup to exclude.
4545
* @property {boolean} glslify
4646
* Process sources using glslify prior to all preprocessing, validation and optimization.
47-
* @property {Partial<GlslifyOptions>} glslifyOptions
47+
* @property {Partial<import('./lib/glslify.js').GlslifyOptions>} glslifyOptions
4848
* When glslify enabled, pass these additional options to glslify.compile()
49-
* @typedef {GLSLPluginGlobalOptions & Partial<import('./lib/glslProcess').GLSLToolOptions>} GLSLPluginOptions
50-
*/
51-
/**
52-
* @typedef {Object} GlslifyBaseOptions
53-
* @property {string} basedir
54-
* @typedef {{[key: string]: any} & GlslifyBaseOptions} GlslifyOptions
49+
* @typedef {GLSLPluginGlobalOptions & Partial<import('./lib/glslProcess.js').GLSLToolOptions>} GLSLPluginOptions
5550
*/
5651
/**
5752
* @param {Partial<GLSLPluginOptions>} userOptions
@@ -69,35 +64,23 @@ export default function glslOptimize(userOptions = {}) {
6964

7065
const filter = createFilter(pluginOptions.include, pluginOptions.exclude);
7166

72-
/** @type {{(src:string, opts:GlslifyOptions):string}} */
73-
let glslifyCompile;
74-
7567
return {
7668
name: 'glsl-optimize',
7769

7870
async options(options) {
7971
if (pluginOptions.glslify) { // Try to dynamically load glslify if installed
80-
try {
81-
// @ts-ignore
82-
const glslify = await import('glslify');
83-
if (glslify && glslify.compile && typeof glslify.compile === 'function') {
84-
glslifyCompile = glslify.compile;
85-
}
86-
} catch {
87-
// do nothing
88-
}
72+
await glslifyInit();
8973
}
9074
return options;
9175
},
9276

77+
/*
78+
We use a load hook instead of transform because we want sourcemaps
79+
to reflect the optimized shader source.
80+
*/
9381
async load(id) {
94-
if (!id || !filter(id)) return;
82+
if (!id || !filter(id) || !fsSync.existsSync(id)) return;
9583

96-
/*
97-
We use a load hook instead of transform because we want sourcemaps
98-
to reflect the optimized shader source.
99-
*/
100-
if (!fsSync.existsSync(id)) return;
10184
let source;
10285
try {
10386
source = fsSync.readFileSync(id, {encoding: 'utf8'});
@@ -113,16 +96,9 @@ export default function glslOptimize(userOptions = {}) {
11396
}
11497

11598
if (pluginOptions.glslify) {
116-
if (!glslifyCompile) {
117-
this.error({message: `glslify could not be found. Install it with npm i -D glslify`});
118-
}
119-
/** @type {GlslifyOptions} */
120-
const glslifyOptions = {
121-
basedir: dirname(id),
122-
...pluginOptions.glslifyOptions,
123-
};
12499
try {
125-
source = glslifyCompile(source, glslifyOptions);
100+
source = await glslifyProcessSource(id, source, pluginOptions.glslifyOptions,
101+
(message) => this.error({message}));
126102
} catch (err) {
127103
this.error({message: `Error processing GLSL source with glslify:\n${err.message}`});
128104
}

src/lib/glslProcess.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ function getBuildDir(id) {
141141
* @param {string} source Source code
142142
* @param {GLSLStageName} stageName
143143
* @param {Partial<GLSLToolOptions>} [glslOptions]
144-
* @param {(message: string) => void} [errorLog]
144+
* @param {(message: string) => void} [warnLog]
145145
* @return {Promise<import('rollup').SourceDescription>}
146146
*/
147-
export async function glslProcessSource(id, source, stageName, glslOptions = {}, errorLog = console.error) {
147+
export async function glslProcessSource(id, source, stageName, glslOptions = {}, warnLog = console.error) {
148148

149149
/** @type {GLSLToolOptions} */
150150
const options = {
@@ -180,7 +180,7 @@ export async function glslProcessSource(id, source, stageName, glslOptions = {},
180180
let outputFile = targetID;
181181

182182
if (!fsSync.existsSync(targetDir)) {
183-
errorLog(`Error resolving path: '${id}' : Khronos glslangValidator may fail to find includes`);
183+
warnLog(`Error resolving path: '${id}' : Khronos glslangValidator may fail to find includes`);
184184
targetDir = process.cwd();
185185
targetID = id;
186186
outputFile = `temp`;

src/lib/glslify.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as path from 'path';
2+
import * as fsSync from 'fs';
3+
4+
/**
5+
* @typedef {Object} GlslifyBaseOptions
6+
* @property {string} basedir
7+
* @typedef {{[key: string]: any} & GlslifyBaseOptions} GlslifyOptions
8+
*/
9+
10+
/** @type {{(src:string, opts:GlslifyOptions):string}} glslify.compile */
11+
let glslifyCompile;
12+
13+
/**
14+
* Try to dynamically load glslify if installed
15+
*/
16+
export async function glslifyInit() {
17+
if (glslifyCompile) return;
18+
try {
19+
// @ts-ignore
20+
const glslify = await import('glslify');
21+
if (glslify && glslify.compile && typeof glslify.compile === 'function') {
22+
glslifyCompile = glslify.compile;
23+
}
24+
} catch {
25+
// do nothing
26+
}
27+
}
28+
29+
/**
30+
* Process source with glslify
31+
* @param {string} id File path
32+
* @param {string} source Source code
33+
* @param {Partial<GlslifyOptions>} options
34+
* @param {(message: string) => never} failError
35+
* @param {(message: string) => void} [warnLog]
36+
*/
37+
export async function glslifyProcessSource(id, source, options, failError, warnLog = console.error) {
38+
if (!glslifyCompile) {
39+
failError(`glslify could not be found. Install it with npm i -D glslify`);
40+
}
41+
42+
let basedir = path.dirname(id);
43+
if (!fsSync.existsSync(basedir)) {
44+
warnLog(`Error resolving path: '${id}' : glslify may fail to find includes`);
45+
basedir = process.cwd();
46+
}
47+
48+
return glslifyCompile(source, /** @type {GlslifyOptions} */({basedir, ...options}));
49+
}

0 commit comments

Comments
 (0)