tiddlyhost / tiddlyhost-com

Rails application for creating and hosting TiddlyWiki sites, plus resources for deploying it to https://tiddlyhost.com/
Other
184 stars 17 forks source link

Learn how to use the uglify to plugin to compress the tiddlywikicore js file #281

Closed simonbaird closed 1 year ago

simonbaird commented 1 year ago

Suggested by @telumire.

https://flibbles.github.io/tw5-uglify/uglified.html https://flibbles.github.io/tw5-uglify/ https://github.com/Telumire/TW/raw/main/tiddlywikicore-5.2.3-uglified.js

simonbaird commented 1 year ago

I used terser without any compression or mangling to create https://tiddlyhost.com/tiddlywikicore-5.2.3.min.js . It basically just strips comments and whitespace. It's not able to look inside plugin json content, which I think @flibbles' plugin can do. Also I'm scared to introduce bugs from heavy-handed compression/mangling, so I like the idea of using the existing plugin.

That said I'm hoping for a node command line that can produce the tiddlywikicore-5.2.3.uglified.js output file rather than doing it manually.

(If you have an idea how to do that, please share!)

simonbaird commented 1 year ago

See also #171.

flibbles commented 1 year ago

If you have uglify installed on your node server, any files you generate or servers you host should automatically create compressed content.

simonbaird commented 1 year ago

I'm not running the server, but I'm using node to generate a core js file like this:

node tiddlywiki.js editions/empty --output corejs --render '$:/core/templates/tiddlywiki5.js' '[[tiddlywikicore-]addsuffix<version>addsuffix[.js]]' 'text/plain'

I'm thinking maybe if I created an "empty-with-uglify" edition with the plugin installed, then a similar command would do what I want, e.g something like:

node tiddlywiki.js editions/empty-with-uglify --output corejs --render '$:/core/templates/tiddlywiki5.js' '[[tiddlywikicore-]addsuffix<version>addsuffix[.ugly.js]]' 'text/plain'

(Not sure if that would work but it might.)

flibbles commented 1 year ago
node tiddlywiki.js +plugins/flibbles/uglify editions/empty --output corejs --render '$:/core/templates/tiddlywiki5.js' '[[tiddlywikicore-]addsuffix<version>addsuffix[.js]]' 'text/plain'

That should be it, assuming you've got uglify on your tiddlywiki plugin path.

flibbles commented 1 year ago

Or if you use editions/empty-with-uglify, then you could just add uglify to the list of plugins in the tiddlywiki.info file in that directory. Then that second command you listed would work fine. Super easy.

simonbaird commented 1 year ago

Thanks!

simonbaird commented 1 year ago

Here's what I have so far. It's close but not quite there:

  export TIDDLYWIKI_PLUGIN_PATH=../tw5-uglify && node tiddlywiki.js +plugins/plugins/uglify editions/empty \
    --output output/external-core \                                                                                                                                            
    --render '$:/core/templates/tiddlywiki5.js' '[[tiddlywikicore-]addsuffix<version>addsuffix[.min.js]]' 'text/plain' \
    --rendertiddler '$:/core/save/offline-external-js' 'index.html' 'text/plain'

It does uglify the contents of tiddlywikicore-5.2.3.min.js , but not the contents of the $:/core plugin tiddler which shows up like this:

$tw.preloadTiddlerArray([{"title":"$:/core","name":"Core" ... very long json...

So the trick would be to somehow uglify the core plugin before its content gets inserted into the tiddlywikicore.json.

flibbles commented 1 year ago

All right, the fault here is not with you. You're doing everything right.

The offline-external-js template is rendering core using the jsontiddlers macro, which is different from the jsontiddler widget which I thought all the exporting templates used. It slipped me by.

I'll have to push a fix for this. Shouldn't be hard, but probably won't be able to get to it for a couple days. Just know that the command you've written should work fine once a fixed version of uglify is available.

simonbaird commented 1 year ago

Ack, no worries and thanks again for the help.

flibbles commented 1 year ago

A new version of Uglify (1.5.5) is pushed where your command should work.

simonbaird commented 1 year ago

Works nicely. Thanks!