mikel-press 0.20.1 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +44 -39
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -35,9 +35,13 @@ const press = (config = {}) => {
35
35
  });
36
36
  });
37
37
  // 2. transform nodes
38
- const transformPlugins = getPlugins("transform");
39
- context.nodes.forEach((node, _, allNodes) => {
40
- transformPlugins.forEach(plugin => {
38
+ getPlugins("transform").forEach(plugin => {
39
+ // special hook to initialize the transform plugin
40
+ if (typeof plugin.beforeTransform === "function") {
41
+ plugin.beforeTransform(context);
42
+ }
43
+ // run the transform in all nodes
44
+ context.nodes.forEach((node, _, allNodes) => {
41
45
  return plugin.transform(context, node, allNodes);
42
46
  });
43
47
  });
@@ -53,11 +57,15 @@ const press = (config = {}) => {
53
57
  return plugin.beforeEmit(context);
54
58
  });
55
59
  // 5. emit each node
56
- const emitPlugins = getPlugins("emit");
57
- filteredNodes.forEach((node, _, allNodes) => {
58
- emitPlugins.forEach(plugin => {
59
- return plugin.emit(context, node, allNodes);
60
- });
60
+ filteredNodes.forEach(node => {
61
+ // 1. if node has been processed (aka node.content is an string), write the file
62
+ if (typeof node.content === "string") {
63
+ press.utils.write(path.join(context.destination, node.path), node.content);
64
+ }
65
+ // 2. if node has not been processed, just copy the file
66
+ else if (fs.existsSync(node.source)) {
67
+ press.utils.copy(node.source, path.join(context.destination, node.path));
68
+ }
61
69
  });
62
70
  };
63
71
 
@@ -118,31 +126,39 @@ press.LABEL_PARTIAL = "asset/partial";
118
126
 
119
127
  // @description source plugin
120
128
  press.SourcePlugin = (options = {}) => {
129
+ const shouldEmit = options?.shouldEmit ?? true;
130
+ const processedNodes = new Set();
121
131
  return {
122
132
  name: "SourcePlugin",
123
133
  load: context => {
124
134
  const folder = path.join(context.source, options?.folder || ".");
125
135
  const extensions = options?.extensions || context.extensions;
126
136
  const exclude = options?.exclude || context.exclude;
127
- return press.utils.readdir(folder, extensions, exclude).map(file => ({
128
- source: path.join(folder, file),
129
- label: options.label || press.LABEL_PAGE,
130
- path: path.join(options?.basePath || ".", file),
131
- url: path.normalize("/" + path.join(options?.basePath || ".", file)),
132
- content: press.utils.read(path.join(folder, file)),
133
- }));
137
+ return press.utils.readdir(folder, extensions, exclude).map(file => {
138
+ processedNodes.add(path.join(folder, file)); // register this node
139
+ return {
140
+ source: path.join(folder, file),
141
+ label: options.label || press.LABEL_PAGE,
142
+ path: path.join(options?.basePath || ".", file),
143
+ url: path.normalize("/" + path.join(options?.basePath || ".", file)),
144
+ content: press.utils.read(path.join(folder, file)),
145
+ };
146
+ });
147
+ },
148
+ shouldEmit: (context, node) => {
149
+ return !processedNodes.has(node.source) || shouldEmit;
134
150
  },
135
151
  };
136
152
  };
137
153
 
138
154
  // @description data plugin
139
155
  press.DataPlugin = (options = {}) => {
140
- return press.SourcePlugin({folder: "./data", extensions: [".json"], label: press.LABEL_DATA, ...options});
156
+ return press.SourcePlugin({folder: "./data", shouldEmit: false, extensions: [".json"], label: press.LABEL_DATA, ...options});
141
157
  };
142
158
 
143
159
  // @description partials plugin
144
160
  press.PartialsPlugin = (options = {}) => {
145
- return press.SourcePlugin({folder: "./partials", extensions: [".html"], label: press.LABEL_PARTIAL, ...options});
161
+ return press.SourcePlugin({folder: "./partials", shouldEmit: false, extensions: [".html"], label: press.LABEL_PARTIAL, ...options});
146
162
  };
147
163
 
148
164
  // @description assets plugin
@@ -157,14 +173,6 @@ press.AssetsPlugin = (options = {}) => {
157
173
  path: path.join(options?.basePath || ".", file),
158
174
  }));
159
175
  },
160
- emit: (context, node) => {
161
- if (node.label === press.LABEL_ASSET && typeof node.content === "string") {
162
- press.utils.write(path.join(context.destination, node.path), node.content);
163
- }
164
- else if (node.label === press.LABEL_ASSET) {
165
- press.utils.copy(node.source, path.join(context.destination, node.path));
166
- }
167
- },
168
176
  };
169
177
  };
170
178
 
@@ -191,10 +199,7 @@ press.FrontmatterPlugin = () => {
191
199
  press.ContentPagePlugin = (siteData = {}) => {
192
200
  return {
193
201
  name: "ContentPagePlugin",
194
- shouldEmit: (context, node) => {
195
- return ![press.LABEL_DATA, press.LABEL_PARTIAL].includes(node.label);
196
- },
197
- beforeEmit: context => {
202
+ beforeTransform: context => {
198
203
  const getNodes = label => context.nodes.filter(n => n.label === label);
199
204
  // 1. prepare site data
200
205
  Object.assign(siteData, context.config, {
@@ -213,14 +218,12 @@ press.ContentPagePlugin = (siteData = {}) => {
213
218
  });
214
219
  });
215
220
  },
216
- emit: (context, node) => {
221
+ transform: (context, node) => {
217
222
  if (node.label === press.LABEL_PAGE && typeof node.content === "string") {
218
223
  context.template.use(ctx => {
219
224
  ctx.tokens = mikel.tokenize(node.content || "");
220
225
  });
221
- // compile and write the template
222
- const result = context.template({site: siteData, page: node});
223
- press.utils.write(path.join(context.destination, node.path), result);
226
+ node.content = context.template({site: siteData, page: node});
224
227
  }
225
228
  },
226
229
  };
@@ -230,12 +233,14 @@ press.ContentPagePlugin = (siteData = {}) => {
230
233
  press.CopyAssetsPlugin = (options = {}) => {
231
234
  return {
232
235
  name: "CopyAssetsPlugin",
233
- beforeEmit: context => {
234
- return (options.patterns || []).forEach(item => {
235
- if (item.from && fs.existsSync(item.from)) {
236
- press.utils.copy(item.from, path.join(context.destination, options.basePath || ".", item.to || path.basename(item.from)));
237
- }
238
- });
236
+ load: () => {
237
+ return (options?.patterns || [])
238
+ .filter(item => item.from && fs.existsSync(path.resolve(item.from)))
239
+ .map(item => ({
240
+ source: path.resolve(item.from),
241
+ path: path.join(options?.basePath || ".", item.to || path.basename(item.from)),
242
+ label: options?.label || press.LABEL_ASSET,
243
+ }));
239
244
  },
240
245
  };
241
246
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mikel-press",
3
3
  "description": "A tiny and fast static site generator based on mikel templating",
4
- "version": "0.20.1",
4
+ "version": "0.21.0",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {
@@ -19,7 +19,7 @@
19
19
  "node": ">=20"
20
20
  },
21
21
  "dependencies": {
22
- "mikel": "^0.20.1"
22
+ "mikel": "^0.21.0"
23
23
  },
24
24
  "files": [
25
25
  "README.md",