I would like to use Brunch to build two separate scripts, app.js and admin.js. It took me long enough to figure out how to include node_modules in app.js but I have it working properly now. My current issue is that while I am building admin.js it won’t run. The confusing part is that I can run the exact same setup with /vendor/ and it works as desired.
I can log “VENDOR!” to the console all day long but haven’t seen “ADMIN!” once. I’ve tried switching my “admin.js” regex to /^(admin|node_modules)/, no dice. I’ve tried ignore: [/admin/] to skip ES6 transpilation, no dice. I can see my console.log('ADMIN!'); in Chrome’s dev tools but it’s wrapped in some other code and never fires and no errors are thrown.
Am I missing some magic or did I overlook something obvious?
...
// Import local files
//
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".
// import socket from "./socket"
const { first } = require('../admin/first');
console.log(first);
// from file: assets/js/app.js
// CommonJS
...
// Import local files
//
// Local files can be imported directly using relative
// paths "./socket" or full ones "web/static/js/socket".
// import socket from "./socket"
import { info } from '../admin/first'; // import from export named "info"
import something from '../admin/first'; // import from default export
console.log(info.first);
const { first } = something; // destructuring assignment
console.log(first);
// from assets/js/app.js
// ES2015
In short that console.log('ADMIN/FIRST') is only run when the file is required or imported - which can only happen if the file exports something in the first place.
In app.html.eex you have to be careful to provide the dependency before it is used i.e.
Thanks for the help. I feel I have a good grasp on JavaScript modules, but I’m not trying to create a module. I just want Brunch to build a very simple file.
I’ve put together a repository to demonstrate. In the master branch, app.js logs “app.js”. In the vendor branch, vendor.js logs “vendor.js”. In the admin branch, admin.js logs…nothing.
If the answer is to switch to webpack, so be it. It just seems that what I’m attempting to do is so simple that I must be missing something obvious.
Essentially you are using webpack’s Multi Page Application setup - intended to generate bundles for separate pages - and then concatenating all three bundles in the same page.
My intention was never to run separate bundles on the same page. I only did that to keep the demo as simple as possible.
Whether the Brunch-built admin.js was on a page with app.js or all by itself it never worked. The same script works quite easily when built by webpack.
The question is whether it will keep working as you move forward (it may). From what I can tell you are using the tool in a way it wasn’t intended to be used. That’s all.
The fundamental question is: why are using a module bundler when you don’t intend to use JavaScript modules or libraries that are structured with the JavaScript modules.