How to build Dojo into a single file, given a list of dependencies?

后端 未结 2 1449
醉酒成梦
醉酒成梦 2020-12-30 07:58

I have a simple Dojo application, that does only one require call, loading all the dependencies. The trouble is, while it is extremely simple, it still ends up

相关标签:
2条回答
  • 2020-12-30 08:27

    I needed to do the same thing and this is how I solved it.

    If you read the documentation about how to create custom builds (http://dojotoolkit.org/documentation/tutorials/1.8/build/), in the Layers section they talk about creating custom layers. You can add all the modules you need there. Here is an example of my custom layer file:

    layers : {
    "dojo/dojo" : {
        include : [
                "dojo/dojo",
                "dojo/_base/window",//
                "dojo/dom",//
                "dojo/dom-class",//
                "dojo/ready",//
                "dojo/store/Memory"
        ],
        customBase : true,
        boot : true
    }
    }
    

    What this will do is build only those modules into dojo.js, along with the bootstrap loader so that you can load other modules on the fly.

    0 讨论(0)
  • 2020-12-30 08:42

    If you're using Dojo's require() loader, there are build tools that you can use to combine files and minify. According to the site, the build tools aren't included in an official release, so you'll have to get them from the development version (specifically, look in the buildscripts directory).

    The Dojo documentation contains some info on its build system that you may also find useful.

    As a proof of concept, here are the steps I took:

    1. Go to the download page, and download the Source Dojo Toolkit SDK (it's the only one that contains the util scripts needed for a build).

    2. Extract to a location (for the sake of this post, let's say it's /opt/dojo-toolkit).

    3. From the Dojo toolkit directory (i.e. /opt/dojo-toolkit), run the build util: ./util/buildscripts/build.sh action=release htmlFiles=/path/to/my/index.html (careful, this slowed my 5-year-old dual-core to a crawl)

    Example of index.html (this one is exactly inside the dojo-toolkit directory):

    ...
    <head>
        <script src="dojo/dojo.js"></script>
        <script>
        dojo.require("my.test");
        </script>
    </head>
    ...
    

    The require() call looks for nested modules (I couldn't get it to work with a top-level module), so in this case, I've got a my directory inside of dojo-toolkit which contains a test.js file. That file is the main "bootstrap" file which loads in all of the dependencies. I just put random require() calls in mine:

    dojo.require('dijit.ProgressBar');
    dojo.require('dijit.Tree');
    

    And that should do it. Basically, running the build utility against your HTML file (the one that contains the reference to dojo.js) makes sure that all of the dependencies are found, starting from the top.

    Note: the build system create a release directory with the built output, but it looks a little misleading at first - it appears to have minified each individual file, but if you look at your actual bootstrap file (my/test.js, in this case), it will be a combined, minified file with (I presume) everything you need to run your app.


    Otherwise, if you're using AMD style require()'s (as in require.js), you can use its optimization tool. According to the site, it will:

    1. Combine all dependent files (including require.js itself) into a single file. It analyzes the require() call to figure out which files it needs to combine.

    2. Minify your JavaScript using either UglifyJS (default) or Closure Compiler.

    0 讨论(0)
提交回复
热议问题