StealJS supports conditional module loading through the steal-conditional extension;
2 types of conditionals are currently supported, string substitution and boolean.
In this guide, we'll build a small demo that uses the string substitution conditional syntax to import a random translation of the famous "Hello, World!".
Next, we need to import these modules in index.js and randomly pick a specific
translation to use its value to populate the h1 tag in index.html.
We could import each individual module and then write a simple algorithm to pick one
of the translation values, in our example, that's an ok solution since we only have 3
translation modules, but you can imagine how annoying it would be to do the same with
100 or even more modules.
The string substitution syntax
Add the following to the index.js file we created before:
import i18n from "locale/#{lang}";
document.getElementById("header").textContent = i18n.helloWorld;
This syntax might be already familiar and the reason is that it resembles the ES2015 Template Literals syntax,
with the difference that the character before the curly braces is a # sign.
The name between the curly braces (lang in our example) is a module name, called condition module, the steal-conditional extension will load it first and grab its default export; if the value is not a string it will throw an error, otherwise this value is used to replace the module name along with the # sign and the curly braces.
Let's say, lang's default export value is the string 'foo', the conditional import in index.js would be interpolated to:
import i18n from "locale/foo";
It is also possible for the conditional module to define multiple conditions via the . modifier:
const locales = ['en', 'es', 'hi'];
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
export default locales[getRandomInt(0, locales.length)];
The condition module creates a constant that holds the possible translation files to use,
in our example there are 3 possible modules, then, the getRandomInt helper is defined,
this function returns a random number between the min argument and max.
Finally, getRandomInt is used to export a value of the locales array.
Reload index.html to see your changes.
If you reload index.html a couple more times, you should see the other translations as well:
Pretty cool, don't you think?
Showing a random language is done here for demonstration purposes, in most real world applications you'd want to use the preferred language of the user.
Build a production app
Now that we've created our application we need to share it with the public. To do this we'll create a build that will concat our JavaScript and styles down to only one file, each, for faster page loads in production.
Build the app and switch to production
When we first installed our initial dependencies for myhub, one of those was steal-tools. steal-tools is a set of tools that helps with bundling assets for production use.
In your package.json "scripts" section add:
{
"scripts": {
...
"build": "steal-tools"
}
}
And then you can run:
> npm run build
To use the production artifacts rather than the development files we need to update our index.html to load them.
StealJS supports conditional module loading through the steal-conditional extension; 2 types of conditionals are currently supported, string substitution and boolean.
In this guide, we'll build a small demo that uses the string substitution conditional syntax to import a random translation of the famous "Hello, World!".
Install Prerequisites
Window Setup
Linux / Mac Setup
Setting up a new project
Create a new project folder
Create a new folder for your project and then run
npm init
. Answer all questions with their defaults.Create and host the main page
Create index.html with:
Next install and run a local fileserver. http-server handles our basic needs. We'll install it locally and then and it to our npm scripts:
Next, edit your
package.json
so that the start script looks like:This allows us to start the server with:
Open http://127.0.0.1:8080/. You should see the Hello, World! test.
Install the application dependencies
Installing these dependencies gives us everything we need to build our application.
Set up steal-conditional
Next, configure StealJS to load the
steal-conditional
extension as a configuration dependency; in yourpackage.json
add the following:Now restart your server; you can keep it on while you develop the rest of the application.
Import your first module
Create the module
Create index.js with the following:
Nothing interesting here, we grab the element with the
header
id and then set itstextContent
property toHello, World!
.Use steal.js in your page
Update index.html with:
Also, since we are setting the
textContent
property using JavaScript, go ahead and remove theHello, World!
from index.html.Reload index.html to see your changes.
Create the translation files
Our goal is to import a random translation of
Hello, World!
and set it to thetextContent
of theh1
tag we added to index.html.Create a folder name locale, and add the following, each translation in its own module:
Next, we need to import these modules in index.js and randomly pick a specific translation to use its value to populate the
h1
tag in index.html.We could import each individual module and then write a simple algorithm to pick one of the translation values, in our example, that's an ok solution since we only have 3 translation modules, but you can imagine how annoying it would be to do the same with 100 or even more modules.
The string substitution syntax
Add the following to the index.js file we created before:
This syntax might be already familiar and the reason is that it resembles the ES2015 Template Literals syntax, with the difference that the character before the curly braces is a
#
sign.The name between the curly braces (
lang
in our example) is a module name, called condition module, thesteal-conditional
extension will load it first and grab its default export; if the value is not astring
it will throw an error, otherwise this value is used to replace the module name along with the#
sign and the curly braces.Let's say,
lang
's default export value is the string'foo'
, the conditional import in index.js would be interpolated to:Create the condition module
Create lang.js with:
The condition module creates a constant that holds the possible translation files to use, in our example there are 3 possible modules, then, the
getRandomInt
helper is defined, this function returns a random number between themin
argument andmax
.Finally,
getRandomInt
is used to export a value of thelocales
array.Reload index.html to see your changes.
If you reload index.html a couple more times, you should see the other translations as well:
Pretty cool, don't you think?
Build a production app
Now that we've created our application we need to share it with the public. To do this we'll create a build that will concat our JavaScript and styles down to only one file, each, for faster page loads in production.
Build the app and switch to production
When we first installed our initial dependencies for myhub, one of those was steal-tools. steal-tools is a set of tools that helps with bundling assets for production use.
In your package.json
"scripts"
section add:And then you can run:
To use the production artifacts rather than the development files we need to update our index.html to load them.
Update index.html with:
By using
steal.production.js
instead ofsteal.js
StealJS will know to load the production files we just built.A bundle for each conditional module
During the build,
steal-conditional
will detect the possible variations andsteal-tools
will create separate individual bundles for each variation.If we take a look at the artifacts created during the build process
We can see how each of the locale modules we create gets its own bundle.
Finally, reload index.html to see your changes: