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

refactor(build): update rollup shaderChunks #5602

Merged
merged 4 commits into from
Aug 30, 2023
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
54 changes: 2 additions & 52 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import terser from '@rollup/plugin-terser';
import dts from 'rollup-plugin-dts';
import jscc from 'rollup-plugin-jscc';
import { visualizer } from 'rollup-plugin-visualizer';
import { shaderChunks } from './utils/rollup-shader-chunks.mjs';

/** @typedef {import('rollup').RollupOptions} RollupOptions */
/** @typedef {import('rollup').Plugin} Plugin */
Expand Down Expand Up @@ -149,57 +150,6 @@ function engineLayerImportValidation(rootFile, enable) {
};
}

/**
* @param {boolean} enable - Enable or disable the plugin.
* @returns {Plugin} The plugin.
*/
function shaderChunks(enable) {
const filter = createFilter([
'**/*.vert.js',
'**/*.frag.js'
], []);

return {
name: 'shaderChunks',
transform(code, id) {
if (!enable || !filter(id)) return undefined;

code = code.replace(/\/\* glsl \*\/\`((.|\r|\n)*)\`/, (match, glsl) => {

// Remove carriage returns
glsl = glsl.replace(/\r/g, '');

// 4 spaces to tabs
glsl = glsl.replace(/ {4}/g, '\t');

// Remove comments
glsl = glsl.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g, '');

// Trim all whitespace from line endings
glsl = glsl.split('\n').map(line => line.trimEnd()).join('\n');

// Restore final new line
glsl += '\n';

// Comment removal can leave an empty line so condense 2 or more to 1
glsl = glsl.replace(/\n{2,}/g, '\n');

// Remove new line character at the start of the string
if (glsl.length > 1 && glsl[0] === '\n') {
glsl = glsl.substr(1);
}

return JSON.stringify(glsl);
});

return {
code: code,
map: null
};
}
};
}

/**
* The ES5 options for babel(...) plugin.
*
Expand Down Expand Up @@ -400,7 +350,7 @@ function buildTarget(buildType, moduleFormat) {
output: outputOptions,
plugins: [
jscc(jsccOptions[buildType] || jsccOptions.release),
shaderChunks(buildType !== 'debug'),
shaderChunks({ enabled: buildType !== 'debug' }),
engineLayerImportValidation(rootFile, buildType === 'debug'),
buildType !== 'debug' ? strip(stripOptions) : undefined,
babel(babelOptions[moduleFormat]),
Expand Down
52 changes: 52 additions & 0 deletions utils/rollup-shader-chunks.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createFilter } from '@rollup/pluginutils';

/** @typedef {import('rollup').Plugin} Plugin */
/** @typedef {string | string[]} GlobPattern */
/**
* @typedef {Object | null} PluginOptions
* @property {GlobPattern?} include - pattern(s array) to import
* @property {GlobPattern?} exclude - pattern(s array) to ignore
* @property {boolean?} enabled - enable the plugin
*/

/**
* @type {readonly string[]}
*/
const DEFAULT_SHADERS = Object.freeze(['**/*.js']);

/**
* @param {PluginOptions} options - Plugin config object
* @returns {Plugin} The plugin that converts shader code.
*/
export function shaderChunks({
include = DEFAULT_SHADERS,
exclude = undefined,
enabled = true
} = {}) {
epreston marked this conversation as resolved.
Show resolved Hide resolved
const filter = createFilter(include, exclude);

return {
name: 'shaderChunks',
transform(source, shader) {
if (!enabled || !filter(shader)) return;

source = source.replace(/\/\* *glsl *\*\/\s*`(.*?)`/gs, function (match, glsl) {
return JSON.stringify(
glsl
.trim() // trim whitespace
.replace(/\r/g, '') // Remove carriage returns
.replace(/ {4}/g, '\t') // 4 spaces to tabs
.replace(/[ \t]*\/\/.*\n/g, '') // remove single line comments
.replace(/[ \t]*\/\*[\s\S]*?\*\//g, '') // remove multi line comments
.concat('\n') // ensure final new line
.replace(/\n{2,}/g, '\n') // condense 2 or more empty lines to 1
);
});

return {
code: source,
map: null
};
}
};
}