Skip to content

Commit

Permalink
From CommonJS to ESM, while also using SWC with Jest in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paazmaya committed May 9, 2024
1 parent bedd812 commit 45bcf8d
Show file tree
Hide file tree
Showing 9 changed files with 450 additions and 61 deletions.
53 changes: 33 additions & 20 deletions .eleventy.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,52 @@
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
module.exports = function(eleventyConfig) {
/***************
* NAGINATA.fi *
***************
* Juga Paazmaya <paazmaya@yahoo.com>
* License: Attribution-ShareAlike 4.0 Unported
* http://creativecommons.org/licenses/by-sa/4.0/
*/
import facebook from './lib/facebook-meta';
import flickrImageList from './lib/flickr-image-list';

eleventyConfig.addPassthroughCopy({"src/img": "img"});
eleventyConfig.addPassthroughCopy({"src/icons": "icons"});
eleventyConfig.addPassthroughCopy({"assets": "/"});
/** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
export default function(eleventyConfig) {

const facebook = require("./lib/facebook-meta");
eleventyConfig.addGlobalData("facebook", facebook());
eleventyConfig.addPassthroughCopy({
'src/img': 'img'
});
eleventyConfig.addPassthroughCopy({
'src/icons': 'icons'
});
eleventyConfig.addPassthroughCopy({
assets: '/'
});

const flickrImageList = require("./lib/flickr-image-list");
eleventyConfig.addGlobalData("prefetch", flickrImageList());
eleventyConfig.addGlobalData('facebook', facebook());
eleventyConfig.addGlobalData('prefetch', flickrImageList());

// Create language specific collections, to reduce complexity in templates
eleventyConfig.addCollection("en", function(api) {
eleventyConfig.addCollection('en', function(api) {
return api.getFilteredByGlob('*/en/*.md');
});
eleventyConfig.addCollection("fi", function(api) {
eleventyConfig.addCollection('fi', function(api) {
return api.getFilteredByGlob('*/fi/*.md');
});
eleventyConfig.addCollection("ja", function(api) {
eleventyConfig.addCollection('ja', function(api) {
return api.getFilteredByGlob('*/ja/*.md');
});

return {
dir: {

input: "content",
includes: "../views", // relative to dir.input
input: 'content',
includes: '../views', // relative to dir.input

output: "dist"
output: 'dist'
},
data: {
layout: "index.html",
layout: 'index.html'
},
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk"
}
};
markdownTemplateEngine: 'njk',
htmlTemplateEngine: 'njk'
};
}
8 changes: 5 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
export default {
// All imported modules in your tests should be mocked automatically
// automock: false,

Expand Down Expand Up @@ -132,7 +132,7 @@ module.exports = {
// snapshotSerializers: [],

// The test environment that will be used for testing
testEnvironment: 'node'
testEnvironment: 'node',

// Options that will be passed to the testEnvironment
// testEnvironmentOptions: {},
Expand Down Expand Up @@ -167,7 +167,9 @@ module.exports = {
// timers: "real",

// A map from regular expressions to paths to transformers
// transform: null,
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest'
}

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation

Check warning on line 174 in jest.config.js

View workflow job for this annotation

GitHub Actions / build

This line has a length of 126. Maximum allowed is 120
// transformIgnorePatterns: [
Expand Down
4 changes: 2 additions & 2 deletions lib/facebook-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ADMINS = 'paazmaya';
* Facebook Open Graph Meta data.
* @returns {Array} List of meta data objects to be inserted in head element
*/
module.exports = function facebookMeta() {
export default function facebookMeta() {

Check warning on line 16 in lib/facebook-meta.js

View workflow job for this annotation

GitHub Actions / build

Function 'facebookMeta' has too many lines (42). Maximum allowed is 22
// property, name
const meta = [
// http://ogp.me/
Expand Down Expand Up @@ -55,4 +55,4 @@ module.exports = function facebookMeta() {
];

return meta;
};
}
14 changes: 6 additions & 8 deletions lib/flickr-image-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
* License: Attribution-ShareAlike 4.0 Unported
* http://creativecommons.org/licenses/by-sa/4.0/
*/

const fs = require('fs'),
path = require('path');

import fs from 'node:fs';
import path from 'node:path';

// If any of the files in 'content/*/*.md' has changed, update the whole cache.
const FLICKR_MATCHER = /(https:\/\/.+\.static\.?flickr\.com\S+\.jpg)/gu;
Expand All @@ -20,7 +18,7 @@ const FLICKR_MATCHER = /(https:\/\/.+\.static\.?flickr\.com\S+\.jpg)/gu;
* @param {array} list The array that is being filtered
* @returns {bool} The given item is the last found
*/
const filterDuplicate = function filterDuplicate(value, index, list) {
export const filterDuplicate = function filterDuplicate(value, index, list) {
return list.lastIndexOf(value) === index;
};

Expand All @@ -29,7 +27,7 @@ const filterDuplicate = function filterDuplicate(value, index, list) {
* @param {string} dirpath Directory path to be searched
* @returns {array} List of images found
*/
const findImages = function findImages(dirpath) {
export const findImages = function findImages(dirpath) {
const list = [];
let files = fs.readdirSync(dirpath);

Expand Down Expand Up @@ -57,7 +55,7 @@ const findImages = function findImages(dirpath) {
* Iterate all pages for the current language and get a list of unique Flick images.
* @returns {Array.<string>} List of images
*/
module.exports = function flickrImageList() {
export default function flickrImageList() {

// Loop all Markdown files under content/*/
const dir = path.join(__dirname, '../content/');
Expand Down Expand Up @@ -85,4 +83,4 @@ module.exports = function flickrImageList() {
images = images.filter(filterDuplicate);

return images;
};
}
Loading

0 comments on commit 45bcf8d

Please sign in to comment.