Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Eleventy - Create a global production flag
Rob OLeary
Rob OLeary

Posted on • Originally published atroboleary.net

     

Eleventy - Create a global production flag

A production flag enables you to run activities in dev or production such as minifying assets, showing draft posts, etc. There isn't a built-in flag or function that comes witheleventy (11ty) specifically for this. However we have this info at our fingertips.

What is the simplest way to add a production flag to our eleventy project?

Make it global

I think the best choice is to put the functionality in aglobal data file. This makes the flag available in every template. You can use in your eleventy config and other JavaScript files if yourequire the data file. Other methods often led to some duplication.

man with a glass globe in his hand with a flag stuck into it

A handier way of doing things ;-)

By default, global data files are located in the_data folder. I will create a JavaScript file calledproduction.js in that folder to create a globalproduction variable.

Code for the globalproduction flag

Node.js exposes environment variables throughprocess.env. Eleventy supplies its own environment variables toprocess.env also.

Since version 2.0, eleventy has made aprocess.env.ELEVENTY_RUN_MODE variable available. It has a value ofbuild,serve, orwatch. We can use this variable to make our production flag. Thebuild value equates to our production mode since we only build when we deploy our project to production.

In ourpackage.json, we can add npm scripts for running dev and production modes. The "build" script is our production mode -- this is what is run when we deploy our eleventy website to production.

{"name":"eleventy-production-flag","scripts":{"dev":"eleventy --serve --incremental","build":"eleventy",},"dependencies":{"@11ty/eleventy":"^2.0.1",}}
Enter fullscreen modeExit fullscreen mode

I will add the functionality to_data/production.js. Its sole mission is to return a boolean value --true if it is running in production mode, otherwisefalse. I will use anIIFE (Immediately Invoked Function Expression) that returns the boolean value.

// _data/production.jsmodule.exports=(function(){returnprocess.env.ELEVENTY_RUN_MODE==="build";})();
Enter fullscreen modeExit fullscreen mode

Now, you can now use theproduction variable throughout your project!

The nice thing about this methodology is that it is totally portable -- it works across all operating systems, and youdon't need to setany environment variable yourself anywhere, anytime.

Useproduction in layouts and includes

You can use theproduction variable in layouts and includes. You don't need to do anything else thanks to the data cascade! 🥰

For example, we could have a differenttitle on pages when you are running in dev versus production.

two tabs open in a browser. the left tab shows the title 'dev - home page' for the dev version of the website, and the right tab has the title 'home page' for the production version of the website

This can help when you are working on your website and occasionally checking deployments in the browser. It may prevent you mistaking a tab of the dev website for a tab of the production website. This is all the code you need in yourhead.njk include:

<!-- head.njk --><head>    {% if production %}        <title>{{ title}}</title>    {% else %}        <title>DEV - {{ title}}</title>    {% endif %}    <!-- other stuff --></head>
Enter fullscreen modeExit fullscreen mode

I do a variation on this theme with favicons for my own website. I have a bright green square favicon in dev mode to catch my eye, whereas production mode it is my actual favicon.

two tabs open in a browser. the left tab shows a bright green square as the favicon for the dev version of website, and the right tab shows rob's actual favicon for the prod version of the website

Useproduction in the eleventy config

You can use theproduction variable in your eleventy config but you mustrequire the file.

For example, you can use it to exclude some files from the build. Here I am excluding my draft posts from the build.

// eleventy.config.jsconstproduction=require("./_data/production");module.exports=function(config){if(production){// e.g. exclude draft filesconfig.ignores.add("./src/drafts/*.md");}};
Enter fullscreen modeExit fullscreen mode

If you split your eleventy config into multiple files, you can do the exact same thing in each of those files and it will work the same way.

Source code

The source code is available in thisGitHub repo.

You will find the project in theproduction-flag subfolder.

Did you enjoy this tutorial? If you did, could you give the repo a star to let me know please? 🌟🫶


Written byRob O'Leary

Subscribe to web feed for the latest articles.

© Rob OLeary 2024

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hacker, traveller, photographer, tinkerer
  • Joined

More fromRob OLeary

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp