From ac9f902522cde011e85d521d7c5d7ea125843947 Mon Sep 17 00:00:00 2001 From: sakshamsaxena Date: Sat, 28 Dec 2019 02:34:09 +0530 Subject: [PATCH 1/2] trying to clone app.js and feeding that to browserify --- tasks/build/combineModules.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/tasks/build/combineModules.js b/tasks/build/combineModules.js index e050b402c9..79e85a97cd 100644 --- a/tasks/build/combineModules.js +++ b/tasks/build/combineModules.js @@ -42,21 +42,25 @@ module.exports = function(grunt) { } // Populate the source file path array with concerned files' path - const srcDirPath = './src'; - const srcFilePath = []; + var srcFilePath = []; + const imp = 'import '; for (var j = 0; j < sources.length; j++) { var source = sources[j]; var base = source.substring(0, source.lastIndexOf('/')); if (base === 'core' || module_src.search(base) !== -1) { - // Push the resolved paths directly - const filePath = - source.search('.js') !== -1 ? source : source + '.js'; - var fullPath = path.resolve(srcDirPath, filePath); + var fullPath; + if (source === 'core/main') { + fullPath = imp + 'p5 from ' + "'./" + source + "';"; + } else { + fullPath = imp + "'./" + source + "';"; + } srcFilePath.push(fullPath); } } - console.log(srcFilePath); + srcFilePath = srcFilePath.join('\n'); + srcFilePath += '\nmodule.exports = p5;'; + fs.writeFileSync('./src/app2.js', srcFilePath, 'utf8'); // Check whether combineModules is minified const isMin = args === 'min'; const filename = isMin ? 'p5Custom.pre-min.js' : 'p5Custom.js'; @@ -65,7 +69,7 @@ module.exports = function(grunt) { const libFilePath = path.resolve('lib/modules/' + filename); // Invoke Browserify programatically to bundle the code - let browseified = browserify(srcFilePath, { + let browseified = browserify(require.resolve('../../src/app2.js'), { standalone: 'p5' }); From 74b38bf89218d4c52bfd015b0a50b1895719c776 Mon Sep 17 00:00:00 2001 From: sakshamsaxena Date: Sat, 28 Dec 2019 03:19:24 +0530 Subject: [PATCH 2/2] rewrote the logic for combine modules --- tasks/build/combineModules.js | 58 +++++++++++++---------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/tasks/build/combineModules.js b/tasks/build/combineModules.js index 79e85a97cd..7e4f9ee241 100644 --- a/tasks/build/combineModules.js +++ b/tasks/build/combineModules.js @@ -7,6 +7,7 @@ const derequire = require('derequire'); const { format } = require('prettier'); module.exports = function(grunt) { + const tempFilePath = path.resolve('./src/customApp.js'); grunt.registerTask( 'combineModules', 'Compile and combine certain modules with Browserify', @@ -14,53 +15,35 @@ module.exports = function(grunt) { // Reading and writing files is asynchronous const done = this.async(); - // Module sources are space separated names in a single string (enter within quotes) - const temp = []; + // Modules is an array of p5 modules to be bundled. + const modules = ['core']; for (const arg of arguments) { if (arg !== 'min') { - temp.push(arg); + modules.push(arg); } } - const module_src = temp.join(', '); + const modulesList = modules.join(', '); // Render the banner for the top of the file. Includes the Module name. const bannerTemplate = `/*! Custom p5.js v<%= pkg.version %> <%= grunt.template.today("mmmm dd, yyyy") %> - Contains the following modules : ${module_src}*/`; + Contains the following modules : ${modulesList}*/`; const banner = grunt.template.process(bannerTemplate); - // Make a list of sources from app.js in that sequence only + // Make a list of sources from app.js which match our criteria + const dump = fs.readFileSync('./src/app.js', 'utf8').split('\n'); const sources = []; - const dump = fs.readFileSync('./src/app.js', 'utf8'); - const regexp = /import\s.*('.+')/g; // Used for ES6 import syntax - // const regexp = /\('.+'/g; // Used for require syntax - let match; - while ((match = regexp.exec(dump)) != null) { - let text = match[1]; // Used for ES6 import syntax - // let text = match[0]; // Used for require syntax - text = text.substring(text.indexOf('./') + 2, text.length - 1); - sources.push(text); - } - - // Populate the source file path array with concerned files' path - var srcFilePath = []; - const imp = 'import '; - for (var j = 0; j < sources.length; j++) { - var source = sources[j]; - var base = source.substring(0, source.lastIndexOf('/')); - if (base === 'core' || module_src.search(base) !== -1) { - var fullPath; - if (source === 'core/main') { - fullPath = imp + 'p5 from ' + "'./" + source + "';"; - } else { - fullPath = imp + "'./" + source + "';"; - } - srcFilePath.push(fullPath); + const regexp = new RegExp('./(' + modules.join('/|') + ')', 'g'); + dump.forEach(source => { + let match; + while ((match = regexp.exec(source)) !== null) { + sources.push(match.input); } - } + }); + sources.push('module.exports = p5;'); + + // Create a temp file with this data and feed it to browserify + fs.writeFileSync(tempFilePath, sources.join('\n'), 'utf8'); - srcFilePath = srcFilePath.join('\n'); - srcFilePath += '\nmodule.exports = p5;'; - fs.writeFileSync('./src/app2.js', srcFilePath, 'utf8'); // Check whether combineModules is minified const isMin = args === 'min'; const filename = isMin ? 'p5Custom.pre-min.js' : 'p5Custom.js'; @@ -69,7 +52,7 @@ module.exports = function(grunt) { const libFilePath = path.resolve('lib/modules/' + filename); // Invoke Browserify programatically to bundle the code - let browseified = browserify(require.resolve('../../src/app2.js'), { + let browseified = browserify(tempFilePath, { standalone: 'p5' }); @@ -105,8 +88,9 @@ module.exports = function(grunt) { }); } - // finally, write it to disk + // finally, write it to disk and remove the temp file grunt.file.write(libFilePath, code); + grunt.file.delete(tempFilePath); // Print a success message grunt.log.writeln(