Skip to content

Commit

Permalink
export depper files
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Jun 1, 2022
1 parent e726216 commit cd62558
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
64 changes: 59 additions & 5 deletions rollup-plugin-glslify.js
Expand Up @@ -40,12 +40,53 @@ module.exports = function glslify(userOptions = {}) {
}, userOptions);

const filter = createFilter(options.include, options.exclude);
const emittedFiles = new Set();

return {
name: 'glslify',

resolveId(id) {
if (!filter(id)) {
return null;
}

if (!emittedFiles.has(id)) {
return null;
}

return id;
},

load(id) {
if (!filter(id)) {
return null;
}

if (!emittedFiles.has(id)) {
return null;
}

return {
code: `// ${id}`,
moduleSideEffects: 'no-treeshake',
map: {
mappings: '',
},
};
},

transform(code, id) {
if (!filter(id)) return;
if (!filter(id)) {
return null;
}

if (!code) {
return null;
}

if (emittedFiles.has(id)) {
return ' ';
}

const fileOptions = Object.assign({
basedir: dirname(id)
Expand All @@ -72,6 +113,10 @@ module.exports = function glslify(userOptions = {}) {
cwd: opts.basedir || basedir,
});

depper.on('file', (fileName) => {
emittedFiles.add(fileName);
});

let transforms = opts.transform || [];
transforms = Array.isArray(transforms) ? transforms : [transforms];
transforms.forEach((transform) => {
Expand Down Expand Up @@ -110,14 +155,23 @@ module.exports = function glslify(userOptions = {}) {
return source;
}

code = compile(code, fileOptions);

let output = compile(code, fileOptions);
if (options.compress !== false) {
code = compressShader(code);
output = compressShader(output);
}

const outputCode = [
`export default ${JSON.stringify(output)}; // eslint-disable-line`,
];

for (const emittedFile of emittedFiles) {
if (filter(emittedFile)) {
outputCode.unshift(`import '${emittedFile}';`);
}
}

return {
code: `export default ${JSON.stringify(code)}; // eslint-disable-line`,
code: outputCode.join('\n'),
map: { mappings: '' }
};
}
Expand Down
8 changes: 2 additions & 6 deletions rollup.config.mjs
Expand Up @@ -39,14 +39,10 @@ export default [
babel.babel({
babelHelpers: 'bundled',
}),
glslify({
include: [
'**/*.vert',
'**/*.frag',
],
}),
glslify(),
license({
sourcemap: true,
debug: true,
cwd: __dirname, // Default is process.cwd()
thirdParty: {
includePrivate: true, // Default is false.
Expand Down

0 comments on commit cd62558

Please sign in to comment.