Amber Smalltalk - Creating a single .js file for deployment
Asked Answered
S

4

9

I've seen this topic come up a couple times, but I don't think I've seen any definitive solution posted.

I've tried the route of combining all my Foo.deploy.js, Bar.deploy.js into a single .js file, and then including that in the loadAmber() call. This /seems/ to work reasonably well, but the majority of the download size still comes from Amber internals.

In my application, the worst offenders in size are:

  • 200K - jQuery UI
  • 95K - Kernel-Collections
  • 90K - jQuery
  • 87K - Kernel-Objects
  • 50K - Canvas
  • 40K - MyApp
  • 20K - Kernel-Classes

I can't do too much about the size of jQuery UI, but I can do a lot about the size of the Amber core, and the number of HTTP requests needed to fetch them. The only problem is I cannot figure out how to tell Amber not to auto-matically fetch Kernel-Objects.deploy.js, etc.

Has anybody managed to package their entire Amber javascript into a single .js file successfully?

  • R. Tyler Croy

Code: http://github.com/rtyler Chatter: http://twitter.com/agentdero

Skid answered 28/3, 2012 at 16:26 Comment(1)
Please note that as of December 2014 and Amber version 0.13 the last answer which currently has 0 upvotes is the correct one.Antipasto
S
6

The Amber compiler can do quite a bit of stuff:

http://github.com/NicolasPetton/amber/blob/master/bin/amberc#L18-90

The following command will compile jQuery and the listed packages and generate amber-deploy.js:

./bin/amberc -l js/lib/jQuery/jquery-1.6.4.min,Kernel-Objects.deploy,Kernel-Classes.deploy,Kernel-Methods.deploy,Kernel-Collections.deploy,Kernel-Exceptions.deploy,Canvas.deploy amber-deploy

Note the lack of spaces between listed filenames.

Also - you can download the Google Closure compiler http://closure-compiler.googlecode.com/files/compiler-latest.zip and put it in your home directory (~/compiler.jar) and then run (notice the -O option):

./bin/amberc -O -l js/lib/jQuery/jquery-1.6.4.min,Kernel-Objects.deploy,Kernel-Classes.deploy,Kernel-Methods.deploy,Kernel-Collections.deploy,Kernel-Exceptions.deploy,Canvas.deploy amber-deploy

Then in your html you can just put:

<script src='amber-deploy.js'></script>
Skid answered 5/4, 2012 at 15:36 Comment(3)
I'm receiving a amber not defined error. Maybe it's needed boot.js too? what about amber.js itself? beside your own code, what else would be necessary to load?Disloyal
I assume this answer is no longer valid with the current development version, right?Antipasto
This approach no longer works for version 0.12. See Amber mailing list.Antipasto
U
3

Starting in Amber 0.13, you can use the RequireJS optimizer to create one minimized JS file, including your own code, Amber, and all dependencies.

To load Amber 0.13 alpha:

(sudo) npm -g install amber-cli@~0.13.0  

The key is the build file. All RequireJS setup that used to go directly into index.html goes here. For example, a slightly customized app.build.js:

({
    mainConfigFile: "config.js",
    paths: {
        ... skipped items there by default
        'lib/jquery-migrate': 'bower_components/jquery/jquery-migrate'
    },
    "shim": {
        'lib/jquery-migrate': [ 'jquery' ]
    },
    include: [
        'amber/requirejs/require.min',
        'amber/deploy',
        'my-namespace/MyPackage',
        'lib/jquery-migrate'
    ],
    out: "all-in-1.js"
})

Now, running r.js -o app.build.js will now produce a single minified JS file named all-in-1.js, which is the only file you need to load from index.html

Umbilication answered 30/7, 2014 at 22:27 Comment(2)
@Hannes - yes, but according to the readme of github.com/herby/amd-config-builder, it seems you can break it into one file per external dependency. HTHUmbilication
Amber 0.13 has been released. Deployment now goes with a predefined `` grunt deploy`` commandAntipasto
T
1

I believe the incantation is now supposed to be like this -

./bin/amberc -m Counter st/Examples.st Onefile

... where I'm creating a single file distribution for the "Counter" example without actually instantiating a counter. The output file will be "Onefile.js" which contains the necessary amber kernel classes as well as the Counter example class.

However, when I try to load Onefile.js, I get multiple "Maximum call stack size exceeded" errors in the JS console in Chrome and Safari on MacOSX. While I feel the "one file to deploy" scenario is close to reality, there are still some warts to be healed.

Btw adding the "-O" flag to the above command like will result in the output file being passed through the google closure compiler whose path is expected to be in "~/compiler.jar". i.e. -

./bin/amberc -O -m Counter st/Examples.st Onefile

will result in a minimized single JS output file, which is nice.

Teacup answered 23/10, 2013 at 3:4 Comment(1)
suggestion: post this as a new question for Amber version 0.12.4. Additional things have changed since Oct. 2013.Antipasto
A
1

Amber version 0.13 released

Amber Version 0.13 has been released.

In this version the command

amber init

creates creates among a number of other files a Gruntfile.js (http://gruntjs.com/).

With

 grunt deploy

on the command line your JavaScript file called the.js is changed to contain everything which is needed for deployment.

This means for deployment you only need

  • index.html
  • the.js

to put in the target directory.

the.js contains only JavaScript. No Smalltalk sources.

With

grunt develop

you switch back to development mode. You get a short version of the.js

Source: https://github.com/amber-smalltalk/amber/wiki/Deploying-Amber

Antipasto answered 15/10, 2014 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.