Skip to content
This repository has been archived by the owner on May 2, 2020. It is now read-only.

Commit

Permalink
Add nested module in lib, and __filename
Browse files Browse the repository at this point in the history
 - `lib/simpletest` now requires `./lib/inner`
 - Replace `__filename` with the filename using tkesgar/replacer-brunch#1
  • Loading branch information
Chris White committed Sep 8, 2018
1 parent ee3f565 commit 9f680f2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
38 changes: 34 additions & 4 deletions brunch-config.js
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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: //
2 changes: 2 additions & 0 deletions lib/simpletest/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
inner = require('./lib/inner');
module.exports = function() {
console.log("This is a message from the simpletest package");
inner();
}
4 changes: 4 additions & 0 deletions lib/simpletest/lib/inner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = ()=>{
console.log('Hello from ' + __filename);
console.log(`This is not replaced__filename but this is ${__filename}`);
};
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9f680f2

Please sign in to comment.