Hexo has a scripts
folder, and the files under which are loaded by Hexo
when start. I usually uses this folder as the development folder for my plug-in scripts. And extract them into independent package after polished it into package-ready quality.
Usually, the files under scripts should be javascripts
. But as I’m a fan of Coffee Script, so I wish to use coffee-script
to write the plug-ins. For the formal package, I compile the coffee scripts
into javascripts
before release. But for development, I wish to use coffee script
directly.
In node.js
, it is possible to require coffee-script
directly, if you registered the coffee-script
runtime compiler:
|
|
And as how node.js require
function is implemented, you cannot register coffee-script
runtime compiler in .coffee
file. Or the compiler will complain:
|
|
Theoretically, it is possible to put the coffee-script
registration code in a javascript
file and also put it under /scripts
folder. SO Hexo
will load it when start-up.
Well, this approach doesn’t really work. If you try it, it is very likely to get the exactly same error as before. The reason is related to Hexo
implementation. Hexo
uses Scripts Loader to require files under /scripts
. The loader doesn’t really provide an explicit way to specify which file is loaded before another. So the registration file is guaranteed to be loaded before any .coffee
.
So far, it seems that there is no cure to this problem! But actually it does. There is undocumented feature will help to solve this issue.
Hexo uses [Script Loader] to load the scripts. In Scripts Loader use hexo.util.file2 to populate the source files under /scripts
. And hexo.util.file2 use fs.readdir to actully populate the file system entries. For fs.readdir, there is a undocumented feature, that the populated entries are actually sorted alphabetically, which means a.js
is loaded before b.coffee
.
With this feature, we can put our coffee-script
registration in a file with lower alphabetic-order name. Personally, I’d like called ___register_coffeescript.js
, since _
is smaller than any letter or number.
⚠️WARNING: fs.readdir yielding sorted files is an undocumented behavior, which means it is not guaranteed either to work across platforms or not get changed in the future. So for, it works on Mac, and I expect it behaves similar on Linux. But not sure about Windows, since fs
uses a different native binding on Windows.