General developer forum

Can I skip uglifying an AMD module in my custom plugin?

Picture of Zak Kolar
Can I skip uglifying an AMD module in my custom plugin?

I'm working on a plugin that involves quite a bit of JavaScript/TypeScript, and I'm using Webpack to compile it. Everything compiles correctly to an AMD module, but I'm having trouble getting it to play nicely with Moodle's default Grunt workflow. Originally, I was compiling my JS into a module and placing it in plugin/amd/src. That works fine when I'm running Moodle in development mode, but when I run Moodle's grunt task, the uglifier messes up the module. There are no errors during the task itself, but the .min.js script always has errors when it's loaded in the browser that aren't present in the pre-uglify version. Webpack minifies the file for me ahead of time so I don't really need to run the grunt version, but I can't figure out a good way to get grunt to skip this file. I was able to skip the JS lint stage by adding a comment to the top of the file, but I cant find a similar way to skip uglifying.

I tried outputting the Webpack generated file directly into the plugin/amd/build directory. This works when the site is in production mode, but it makes development a pain. Since the module is not present in the amd/src file, it doesn't get loaded in development mode which means my only option would be to leave the cache on and manually purge it each time I update the file. I could try putting the webpack-generated file in both places, but I don't want the build version to be overwritten if another developer runs the global Grunt task.

Does anyone know of a self-contained way that I can put my Webpack-compiled file into both /amd/src and /amd/build and have Moodle's grunt process ignore it? I'm hoping to avoid changing the Moodle-level Gruntfile so that my plugin can be installed without modifying existing files.

Alternatively, is there a way that I can force Moodle to look in the /amd/build folder for this particular file even when I'm in development mode? Then I could skip placing it in the /src folder all together and Grunt won't even be aware it exists.

Average of ratings: -
Dan at desk in Moodle HQ, Perth
Re: Can I skip uglifying an AMD module in my custom plugin?
Group Core developersGroup Moodle Course Creator Certificate holdersGroup Moodle HQGroup Particularly helpful MoodlersGroup Plugin developersGroup Testers

I think that the hacky solution might be to avoid putting your source file in src and just include the built file in build.

But it might be worth creating an issue for a better way for us to handle this.

Average of ratings: -