diff --git a/brunch-config.js b/brunch-config.js index ad5aed1..3b7718e 100644 --- a/brunch-config.js +++ b/brunch-config.js @@ -1,16 +1,32 @@ // brunch-config.js for brunch-test by cxw42 // Make our wrapped files stand out a bit more in the generated source +let seen_modules = Object.create(null); + function wrapper(path, data) { - let retval = `\n// ${path} /////////////////////////////////\n`; + seen_modules[path] = (seen_modules[path] || 0) + 1; + console.log(`Wrapping ${path}; time #${seen_modules[path]}`); + + let retval = '\n// ' + `${path} #${seen_modules[path]}` + + ' /////////////////////////////////\n'; + // Add the commonjs wrapper - copied from // https://github.com/brunch/brunch/blob/95902d9c24efb61e613c6c45bc6a33b819ec51ee/lib/utils/modules.js#L7 + let path_str = `'${path}'`.replace('$','$$$$'); + if(path.match(/lib\/inner/)) { + console.log(data); + } + let new_data = data.replace(/\b__filename\b/g, path_str); + retval += ` require.register("${path}", function(exports, require, module) { - ${data} +${new_data} });\n\n` - retval += "\n/////////////////////////////////\n\n"; + retval += '\n/////////////////////////////////\n\n'; + if(path.match(/lib\/inner/)) { + console.log(`---------- Output -----\n${retval}\n---------------\n`); + } return retval; } //wrapper @@ -52,12 +68,26 @@ module.exports = { modules: { // Special wrapper that adds names - wrapper, // Note: only applies to things that get wrapped, + //wrapper, // Note: only applies to things that get wrapped, // i.e., non-`vendor`. // Map lib/foo->foo nameCleaner, }, + + plugins: { + replacer: { + dict: [ + { + key: /\b__filename\b/, + // No value needed - the custom replacer below supplies it + } + ], + replace: (str, key, value, path) => { + return str.split(key).join(`'${path}'`) + } + }, + }, }; // vi: set ts=4 sts=4 sw=4 et ai: // diff --git a/lib/simpletest/index.js b/lib/simpletest/index.js index 4b824a0..8913c12 100644 --- a/lib/simpletest/index.js +++ b/lib/simpletest/index.js @@ -1,3 +1,5 @@ +inner = require('./lib/inner'); module.exports = function() { console.log("This is a message from the simpletest package"); + inner(); } diff --git a/lib/simpletest/lib/inner.js b/lib/simpletest/lib/inner.js new file mode 100644 index 0000000..3801fb9 --- /dev/null +++ b/lib/simpletest/lib/inner.js @@ -0,0 +1,4 @@ +module.exports = ()=>{ + console.log('Hello from ' + __filename); + console.log(`This is not replaced__filename but this is ${__filename}`); +}; diff --git a/package-lock.json b/package-lock.json index e5b1d6e..db92e14 100755 --- a/package-lock.json +++ b/package-lock.json @@ -2773,6 +2773,10 @@ "integrity": "sha1-jcrkcOHIirwtYA//Sndihtp15jc=", "dev": true }, + "replacer-brunch": { + "version": "github:cxw42/replacer-brunch#9bc43cad3bd7de1bc5250c7ea1f1f93992e2a56f", + "dev": true + }, "resolve": { "version": "1.1.7", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.1.7.tgz", diff --git a/package.json b/package.json index 04693a9..b6058ab 100755 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "license": "ISC", "devDependencies": { "brunch": "^2.10.17", - "uglify-es-brunch": "^1.0.5" + "uglify-es-brunch": "^1.0.5", + "replacer-brunch": "cxw42/replacer-brunch#path" }, "dependencies": { "cowsay": "^1.3.1"