Skip to content

Commit 3532bf5

Browse files
committed
task: Re-organize plugins; Fixes to config/ .env.js (#11)
1 parent 5cbfdec commit 3532bf5

7 files changed

Lines changed: 47 additions & 34 deletions

File tree

.env.example.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
/**
22
* Environment variables for local development only.
3+
* Copy, rename to ".env.js" and edit.
4+
*
5+
* @see .github/workflow/deploy.yml - Production ENV.
36
*/
47

5-
process.env._THEME = 'mobile';
8+
process.env.DIA_THEME = 'mobile';
69
// process.env._THEME = 'original';
710
// Search API key: https://developers.google.com/custom-search/v1/introduction#identify_your_application_to_google_with_api_key
811
process.env.SEARCH_API_KEY = 'abc';

.github/workflows/deploy.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ env:
1212
SEARCH_API_KEY: ${{ secrets.SEARCH_API_KEY }}
1313
SEARCH_ID: ${{ secrets.SEARCH_ID }}
1414
ABSOLUTE_URL: https://nfreear.github.io/diveintoaccessibility
15-
_THEME: mobile
16-
# _THEME: original
15+
DIA_THEME: mobile
16+
# DIA_THEME: original
1717

1818
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
1919
permissions:

eleventy.config.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { resolve } from 'node:path';
22
import debug from 'debug';
3-
import diaPluginLoader from 'dia-plugins';
3+
import diaPluginLoader, { loadEnvJs } from 'dia-plugins';
44

55
// GitHub Pages sub-directory deployment.
66
const buildBaseUrl = '/diveintoaccessibility/';
@@ -14,7 +14,8 @@ export default async function (eleventyConfig) {
1414
const debugLog = debug('DIA:config');
1515

1616
debugLog('Loading config…');
17-
await loadEnvJs();
17+
18+
await loadEnvJs([import.meta.dirname, '.env.js']);
1819

1920
// Order matters, put this at the top of your configuration file.
2021
eleventyConfig.setInputDirectory('pages');
@@ -60,15 +61,6 @@ function getLinkFilePaths () {
6061
];
6162
}
6263

63-
async function loadEnvJs () {
64-
try {
65-
await import('./.env.js');
66-
console.log('.env.js loaded:', process.env);
67-
} catch (err) {
68-
console.warn('Warning: .env.js not loaded.', err.code, err.message);
69-
}
70-
}
71-
7264
/* export const config = {
7365
dir: {
7466
includes: '../_includes',

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
"scripts": {
1212
"dry-run": "cd plugins; npm publish --dry-run",
1313
"clean": "rm -rf _site",
14-
"build:original": "_THEME=orig npx @11ty/eleventy",
15-
"build:mobile": "_THEME=mobile npx @11ty/eleventy",
16-
"start:original": "_THEME=orig npx @11ty/eleventy --serve",
17-
"start:mobile": "_THEME=mobile npx @11ty/eleventy --serve",
1814
"build": "npx @11ty/eleventy",
1915
"start": "npx @11ty/eleventy --serve",
2016
"lint:fix": "npx eslint --fix .",

plugins/index.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import originalThemePlugin from './original-theme/originalThemePlugin.js';
22
import mobileThemePlugin from './mobile-theme/mobileThemePlugin.js';
3+
import themeSwitchPlugin from './theme-switch/themeSwitchPlugin.js';
34
import addLinkRefsPlugin from './pre-process/AddLinksPlugin.js';
45
import pageIdAndInfoPlugin from './page-id/PageIdPlugin.js';
56
import sortByDayNumberPlugin from './page-id/sortByDayNumberPlugin.js';
@@ -9,21 +10,7 @@ import importMapPlugin from './client-js/importMapPlugin.js';
910
import baseUrlShortcodePlugin from './shortcode/baseUrlShortcodePlugin.js';
1011
import searchIdShortcodePlugin from './shortcode/searchIdShortcodePlugin.js';
1112
import customCollectionsPlugin from './page-id/customCollectionsPlugin.js';
12-
13-
/**
14-
* Load original or mobile theme, depending on `_THEME` environment variable.
15-
*/
16-
function themeSwitchPlugin (eleventyConfig, options) {
17-
const isMobile = (process.env._THEME === 'mobile');
18-
19-
console.log('themeSwitchPlugin:', process.env._THEME);
20-
21-
if (isMobile) {
22-
return mobileThemePlugin(eleventyConfig, options);
23-
} else {
24-
return originalThemePlugin(eleventyConfig, options);
25-
}
26-
}
13+
import loadEnvJs from './utilities/loadEnvJs.js';
2714

2815
/**
2916
* Load all plugins in the collection.
@@ -53,7 +40,8 @@ export {
5340
originalThemePlugin, mobileThemePlugin, themeSwitchPlugin,
5441
addLinkRefsPlugin, pageIdAndInfoPlugin, sortByDayNumberPlugin,
5542
clientJsPlugin, importMapPlugin, baseUrlShortcodePlugin, debugFiltersPlugin,
56-
customCollectionsPlugin
43+
customCollectionsPlugin, searchIdShortcodePlugin,
44+
loadEnvJs
5745
};
5846

5947
export default diaPluginLoader;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import originalThemePlugin from '../original-theme/originalThemePlugin.js';
2+
import mobileThemePlugin from '../mobile-theme/mobileThemePlugin.js';
3+
4+
/**
5+
* Load original or mobile theme, depending on `_THEME` environment variable.
6+
*/
7+
export default function themeSwitchPlugin (eleventyConfig, options) {
8+
console.assert(process.env.DIA_THEME, 'Missing _THEME environment variable.');
9+
const isMobile = (process.env.DIA_THEME === 'mobile');
10+
11+
console.log('themeSwitchPlugin:', process.env.DIA_THEME);
12+
13+
if (isMobile) {
14+
return mobileThemePlugin(eleventyConfig, options);
15+
} else {
16+
return originalThemePlugin(eleventyConfig, options);
17+
}
18+
}

plugins/utilities/loadEnvJs.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { resolve } from 'node:path';
2+
3+
/**
4+
* Utility to load environment variables set in a JS file, for local development.
5+
*
6+
* @see https://github.com/motdotla/dotenv
7+
*/
8+
export default async function loadEnvJs (envJsPathParts = ['./.env.js']) {
9+
const envJsFile = resolve(...envJsPathParts);
10+
try {
11+
await import(envJsFile);
12+
console.log('.env.js loaded:', process.env);
13+
} catch (err) {
14+
console.warn('Warning: .env.js not loaded:', envJsFile, err.code, err.message);
15+
}
16+
}

0 commit comments

Comments
 (0)