Skip to content

Commit 21677e8

Browse files
committed
Added support for data files
1 parent 5307985 commit 21677e8

File tree

6 files changed

+144
-10
lines changed

6 files changed

+144
-10
lines changed

.eleventy.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = function (config) {
1919
});
2020

2121
config.setUseGitIgnore(false);
22+
config.setDataDeepMerge(true);
2223

2324
config.addTransform('jsx', (content) => {
2425
if (isValidElement(content)) {
@@ -54,6 +55,7 @@ module.exports = function (config) {
5455
dir: {
5556
input: 'src/_js',
5657
output: 'dist',
58+
data: 'data',
5759
layouts: 'layouts',
5860
includes: '_includes',
5961
},

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,31 @@ For a working example, [take a look at the `home` module here](https://github.co
102102

103103
## Data
104104

105-
All official 11ty methods to gather data from an API or otherwise, will work here. There are many great examples of how to do this in the official 11ty documentation, including the use of GraphQL: https://www.11ty.dev/docs/data-js/
105+
All official 11ty methods to gather data from an API or otherwise, will work here. There are many great examples of how to do this in the official 11ty documentation, including the use of GraphQL: https://www.11ty.dev/docs/data-js/.
106+
107+
To define _global_ data, add either JSON, JS or TypeScript files to the `./src/data` folder. These will then be parsed by 11ty and added via the [data cascade](https://www.11ty.dev/docs/data-cascade/). You can access these directly in your `.11ty.ts*` files.
108+
109+
For example, if you were to add a `global.ts` file to `./src/data`, you would access this via a `global` property in your pages argument object:
110+
111+
```tsx
112+
interface IProps {
113+
global: {
114+
title: string;
115+
};
116+
}
117+
118+
/*[...]*/
119+
120+
function Page({ global }: IProps) {
121+
return (
122+
<main>
123+
<h1>{global.title}</h1>
124+
</main>
125+
);
126+
}
127+
```
128+
129+
To add local data, e.g data specific to a module, add an `.11tydata.ts` file within the relevant module folder. This will then be accessible in exactly the same way as shown above, but only for that page. For example, if you added `home.11tydata.ts` to `./src/modules/home`, your home page `11ty.ts` file would have access to the values held within that data file.
106130

107131
## Installation
108132

src/data/siteMeta.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* -----------------------------------
2+
*
3+
* Meta
4+
*
5+
* -------------------------------- */
6+
7+
module.exports = {
8+
pageTitle: '11tyby',
9+
};

src/modules/home/home.11ty.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { h } from 'preact';
2-
import { IPage } from '../shared/model/page.model';
2+
import { IPage, IData } from '../shared/model/page.model';
33
import style from './home.module.scss';
44

55
/* -----------------------------------
@@ -17,12 +17,12 @@ import { Form } from '@/modules/home/components/form';
1717
*
1818
* -------------------------------- */
1919

20-
function Page(this: IPage) {
20+
function Page(this: IPage, { siteMeta }: IData) {
2121
const inlineCss = this.getAssetContents('home/home.11ty.css');
2222

2323
return (
2424
<Html
25-
title="Home - 11ty"
25+
title={siteMeta.pageTitle}
2626
summary="11ty demo"
2727
inlineCss={inlineCss}
2828
jsPath="home/home.entry.js"

src/modules/shared/model/page.model.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,30 @@
55
* -------------------------------- */
66

77
interface IPage {
8+
slug: string;
9+
url: string;
10+
getCollectionItem: any;
11+
getPreviousCollectionItem: any;
12+
getNextCollectionItem: any;
813
getAssetContents: (file: string) => string;
914
}
1015

16+
/* -----------------------------------
17+
*
18+
* IData
19+
*
20+
* -------------------------------- */
21+
22+
interface IData {
23+
siteMeta: {
24+
pageTitle: string;
25+
};
26+
}
27+
1128
/* -----------------------------------
1229
*
1330
* Export
1431
*
1532
* -------------------------------- */
1633

17-
export { IPage };
34+
export { IPage, IData };

webpack.config.js

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,69 @@ const sassLoader = {
3333
},
3434
};
3535

36+
/* -----------------------------------
37+
*
38+
* Data
39+
*
40+
* -------------------------------- */
41+
42+
const data = {
43+
mode: RELEASE ? 'production' : 'development',
44+
entry: getDataFiles(),
45+
context: path.join(__dirname, '/src/'),
46+
cache: true,
47+
target: 'node',
48+
externals: fs.readdirSync('node_modules'),
49+
output: {
50+
path: path.join(__dirname, '/src/_js'),
51+
filename: '[name].js',
52+
libraryTarget: 'umd',
53+
},
54+
resolve: {
55+
extensions: ['.js', '.ts', '.tsx', '.json', '.scss'],
56+
alias: {
57+
'@': path.resolve(__dirname, `./src/`),
58+
},
59+
},
60+
plugins: [
61+
new CopyPlugin({
62+
patterns: [
63+
{
64+
from: 'data',
65+
to: 'data',
66+
noErrorOnMissing: true,
67+
globOptions: {
68+
ignore: ['**/*.ts'],
69+
},
70+
},
71+
],
72+
}),
73+
],
74+
module: {
75+
rules: [
76+
{
77+
test: /\.tsx?$/,
78+
use: [
79+
{
80+
loader: 'ts-loader',
81+
},
82+
],
83+
},
84+
],
85+
},
86+
optimization: {
87+
minimizer: [
88+
new TerserPlugin({
89+
cache: !RELEASE,
90+
parallel: true,
91+
terserOptions: {
92+
keep_fnames: true,
93+
},
94+
}),
95+
],
96+
},
97+
};
98+
3699
/* -----------------------------------
37100
*
38101
* Pages
@@ -41,7 +104,7 @@ const sassLoader = {
41104

42105
const pages = {
43106
mode: RELEASE ? 'production' : 'development',
44-
entry: glob.sync(`${__dirname}/src/**/*.11ty.ts*`).reduce(getModuleFile, {}),
107+
entry: glob.sync(`${__dirname}/src/**/*.11ty.ts*`).reduce(getSourceFile, {}),
45108
context: path.join(__dirname, '/src/'),
46109
cache: true,
47110
target: 'node',
@@ -165,7 +228,7 @@ const pages = {
165228

166229
const entry = {
167230
mode: RELEASE ? 'production' : 'development',
168-
entry: glob.sync(`${__dirname}/src/modules/**/*.entry.ts*`).reduce(getModuleFile, {}),
231+
entry: glob.sync(`${__dirname}/src/modules/**/*.entry.ts*`).reduce(getSourceFile, {}),
169232
context: path.join(__dirname, '/src/'),
170233
cache: true,
171234
target: 'web',
@@ -270,11 +333,30 @@ const entry = {
270333

271334
/* -----------------------------------
272335
*
273-
* Module
336+
* Data
337+
*
338+
* -------------------------------- */
339+
340+
function getDataFiles() {
341+
const globalData = glob.sync(`${__dirname}/src/data/*.ts`).reduce(getSourceFile, {});
342+
343+
const moduleData = glob
344+
.sync(`${__dirname}/src/**/*.11tydata.ts`)
345+
.reduce(getSourceFile, {});
346+
347+
const dataFiles = { ...globalData, ...moduleData };
348+
const totalItems = Object.keys(dataFiles).length;
349+
350+
return dataFiles;
351+
}
352+
353+
/* -----------------------------------
354+
*
355+
* Source
274356
*
275357
* -------------------------------- */
276358

277-
function getModuleFile(result, file) {
359+
function getSourceFile(result, file) {
278360
let [name] = file.split('src/').slice(-1);
279361

280362
if (file.includes('modules/')) {
@@ -308,4 +390,4 @@ function splitVendorChunks(module, chunks) {
308390
*
309391
* -------------------------------- */
310392

311-
module.exports = [pages, entry];
393+
module.exports = [data, pages, entry];

0 commit comments

Comments
 (0)