@merkur/cli 0.36.3 → 0.37.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.
@@ -16,10 +16,12 @@ export async function createMerkurConfig({ cliConfig, context, args } = {}) {
16
16
 
17
17
  try {
18
18
  logger.debug(
19
- `Load merkur config on path ${projectFolder}/${MERKUR_CONFIG_FILE}`,
19
+ `Load merkur config on path ${path.resolve(`${projectFolder}/${MERKUR_CONFIG_FILE}`)}`,
20
20
  );
21
21
 
22
- const file = await import(`${projectFolder}/${MERKUR_CONFIG_FILE}`);
22
+ const file = await import(
23
+ path.resolve(`${projectFolder}/${MERKUR_CONFIG_FILE}`)
24
+ );
23
25
  merkurConfig = await file.default({
24
26
  cliConfig,
25
27
  context,
@@ -28,6 +30,7 @@ export async function createMerkurConfig({ cliConfig, context, args } = {}) {
28
30
  });
29
31
  } catch (error) {
30
32
  logger.error(error);
33
+ process.exit(1);
31
34
  }
32
35
 
33
36
  cliConfig = { ...cliConfig, ...(merkurConfig?.cliConfig ?? {}), ...args };
@@ -84,7 +87,7 @@ async function registerHooks({ merkurConfig }) {
84
87
  emitter.on(
85
88
  EMITTER_EVENTS.MERKUR_CONFIG,
86
89
  function defaultTask({ merkurConfig, cliConfig }) {
87
- const { staticFolder, runTask } = cliConfig;
90
+ const { staticFolder, runTasks } = cliConfig;
88
91
 
89
92
  merkurConfig.task = merkurConfig.task ?? {};
90
93
 
@@ -94,22 +97,25 @@ emitter.on(
94
97
  build: {
95
98
  platform: 'node',
96
99
  write: true,
100
+ metafile: false,
97
101
  },
98
102
  },
99
103
  es13: {
100
104
  name: 'es13',
105
+ folder: 'es13',
101
106
  build: {
102
107
  platform: 'browser',
103
- outdir: `${staticFolder}/es13`,
108
+ outdir: path.resolve(`${staticFolder}/es13`),
104
109
  plugins: [devPlugin],
105
110
  },
106
111
  },
107
112
  es9: {
108
113
  name: 'es9',
114
+ folder: 'es9',
109
115
  build: {
110
116
  platform: 'browser',
111
117
  target: 'es2018',
112
- outdir: `${staticFolder}/es9`,
118
+ outdir: path.resolve(`${staticFolder}/es9`),
113
119
  },
114
120
  },
115
121
  };
@@ -128,9 +134,9 @@ emitter.on(
128
134
  );
129
135
  });
130
136
 
131
- if (runTask.length !== 0) {
137
+ if (runTasks.length !== 0) {
132
138
  Object.keys(merkurConfig.task)
133
- .filter((taskName) => !runTask.includes(taskName))
139
+ .filter((taskName) => !runTasks.includes(taskName))
134
140
  .forEach((taskKey) => {
135
141
  delete merkurConfig.task[taskKey];
136
142
  });
@@ -170,8 +176,12 @@ emitter.on(
170
176
  EMITTER_EVENTS.MERKUR_CONFIG,
171
177
  function defaultEntries({ merkurConfig, cliConfig }) {
172
178
  merkurConfig.defaultEntries = {
173
- client: [`${cliConfig.projectFolder}/src/entries/client.js`],
174
- server: [`${cliConfig.projectFolder}/src/entries/server.js`],
179
+ client: [
180
+ path.resolve(`${cliConfig.projectFolder}/src/entries/client.js`),
181
+ ],
182
+ server: [
183
+ path.resolve(`${cliConfig.projectFolder}/src/entries/server.js`),
184
+ ],
175
185
  ...merkurConfig.defaultEntries,
176
186
  };
177
187
 
@@ -183,8 +193,11 @@ emitter.on(
183
193
  EMITTER_EVENTS.MERKUR_CONFIG,
184
194
  function playground({ merkurConfig, cliConfig }) {
185
195
  merkurConfig.playground = {
186
- template: `${cliConfig.cliFolder}/templates/playground.ejs`,
187
- templateFolder: `${cliConfig.cliFolder}/templates`,
196
+ template: path.resolve(`${cliConfig.cliFolder}/templates/playground.ejs`),
197
+ templateFolder: path.resolve(`${cliConfig.cliFolder}/templates`),
198
+ serverTemplateFolder: path.resolve(
199
+ `${cliConfig.projectFolder}/server/playground/templates`,
200
+ ),
188
201
  path: '/',
189
202
  widgetHandler: async (req) => {
190
203
  const { protocol, host } = merkurConfig.widgetServer;
@@ -243,6 +256,17 @@ emitter.on(
243
256
  buildFolder: path.resolve(cliConfig.projectFolder, cliConfig.buildFolder),
244
257
  clusters: cliConfig.command === COMMAND_NAME.DEV ? 0 : 3,
245
258
  ...merkurConfig.widgetServer,
259
+ cors: {
260
+ options: {
261
+ origin: [
262
+ new RegExp('^https?://localhost(:[0-9]+)?$'),
263
+ new RegExp('^https?://127\\.0\\.0\\.1(:[0-9]+)?$'),
264
+ ],
265
+ methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
266
+ optionsSuccessStatus: 200,
267
+ ...merkurConfig.widgetServer?.cors?.options,
268
+ },
269
+ },
246
270
  };
247
271
 
248
272
  const { origin, host, protocol } = merkurConfig.widgetServer;
@@ -253,8 +277,20 @@ emitter.on(
253
277
  return merkurConfig;
254
278
  },
255
279
  );
256
- emitter.on(EMITTER_EVENTS.MERKUR_CONFIG, function devServer({ merkurConfig }) {
257
- merkurConfig.HMR = merkurConfig?.HMR ?? true;
280
+
281
+ emitter.on(EMITTER_EVENTS.MERKUR_CONFIG, function hmr({ merkurConfig }) {
282
+ merkurConfig.HMR = merkurConfig.HMR ?? true;
283
+
284
+ return merkurConfig;
285
+ });
286
+
287
+ emitter.on(EMITTER_EVENTS.MERKUR_CONFIG, function constant({ merkurConfig }) {
288
+ merkurConfig.constant = {
289
+ ...{
290
+ HOST: 'localhost',
291
+ },
292
+ ...merkurConfig.constant,
293
+ };
258
294
 
259
295
  return merkurConfig;
260
296
  });
@@ -0,0 +1,26 @@
1
+ import { createLogger } from '../logger.mjs';
2
+
3
+ import chalk from 'chalk';
4
+
5
+ export function aliasPlugin({ definition, cliConfig }) {
6
+ const { projectFolder } = cliConfig;
7
+ const logger = createLogger('aliasPlugin', cliConfig);
8
+ return {
9
+ name: 'aliasPlugin',
10
+ setup(build) {
11
+ logger.debug(`Setup plugin for "${chalk.cyan(definition.name)}" task.`);
12
+
13
+ build.onResolve({ filter: /^@\// }, async (args) => {
14
+ const result = await build.resolve(args.path.replace(/^@\//, `./`), {
15
+ kind: 'import-statement',
16
+ resolveDir: `${projectFolder}/src/`,
17
+ });
18
+
19
+ if (result.errors.length > 0) {
20
+ return { errors: result.errors };
21
+ }
22
+ return { path: result.path };
23
+ });
24
+ },
25
+ };
26
+ }
@@ -72,7 +72,14 @@ export function devPlugin({ definition, merkurConfig, cliConfig }) {
72
72
  });
73
73
 
74
74
  client.on('open', function open() {
75
- if (merkurConfig.HMR) {
75
+ if (
76
+ merkurConfig.HMR &&
77
+ !(
78
+ cliConfig.writeToDisk &&
79
+ changed.length === 0 &&
80
+ errors.length === 0
81
+ )
82
+ ) {
76
83
  client.send(
77
84
  JSON.stringify({
78
85
  to: 'browser',
@@ -0,0 +1,19 @@
1
+ import fs from 'node:fs/promises';
2
+
3
+ export function excludeVendorsFromSourceMapPlugin() {
4
+ return {
5
+ name: 'excludeVendorsFromSourceMapPlugin',
6
+ setup(build) {
7
+ build.onLoad({ filter: /node_modules/ }, async (args) => {
8
+ const contents = await fs.readFile(args.path, { encoding: 'utf8' });
9
+
10
+ return {
11
+ contents:
12
+ contents +
13
+ '\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIiJdLCJtYXBwaW5ncyI6IkEifQ==',
14
+ loader: 'default',
15
+ };
16
+ });
17
+ },
18
+ };
19
+ }
@@ -1,4 +1,5 @@
1
1
  import fs from 'node:fs/promises';
2
+ import path from 'node:path';
2
3
 
3
4
  import { createLogger } from '../logger.mjs';
4
5
  import { time } from '../utils.mjs';
@@ -31,7 +32,9 @@ export function metaPlugin({ definition, config, cliConfig }) {
31
32
 
32
33
  metaInformation = await Promise.all(
33
34
  generatedFiles.map(async (file) => {
34
- const stat = await fs.stat(`${projectFolder}/${file}`);
35
+ const stat = await fs.stat(
36
+ path.resolve(`${projectFolder}/${file}`),
37
+ );
35
38
 
36
39
  return { stat, file };
37
40
  }),
package/src/runTask.mjs CHANGED
@@ -1,6 +1,10 @@
1
+ import fs from 'node:fs/promises';
2
+
1
3
  import esbuild from 'esbuild';
4
+ import { createLogger } from './logger.mjs';
2
5
 
3
- export async function runTask({ cliConfig, build }) {
6
+ export async function runTask({ cliConfig, build, config }) {
7
+ const logger = createLogger(undefined, cliConfig);
4
8
  const { watch } = cliConfig;
5
9
 
6
10
  //es6 === es2015, es9 === es2018, es11 === es2020 es13 ===es2022
@@ -9,6 +13,21 @@ export async function runTask({ cliConfig, build }) {
9
13
  ? esbuild.context(build)
10
14
  : esbuild.build(build));
11
15
 
16
+ if (config.analyze && result.metafile) {
17
+ logger.log(
18
+ await esbuild.analyzeMetafile(result.metafile, {
19
+ verbose: cliConfig.verbose,
20
+ }),
21
+ );
22
+
23
+ if (build.outdir) {
24
+ await fs.writeFile(
25
+ `${build.outdir}/${build.entryNames ?? config.name}.meta.json`,
26
+ JSON.stringify(result.metafile),
27
+ );
28
+ }
29
+ }
30
+
12
31
  if (watch) {
13
32
  await result.watch();
14
33
  }
@@ -9,6 +9,8 @@ export async function createTaskConfig({
9
9
  config: {
10
10
  isServer: definition?.build?.platform === 'node',
11
11
  writeToDisk: definition?.build?.write ?? cliConfig.writeToDisk,
12
+ sourcemap: definition?.build?.sourcemap ?? cliConfig.sourcemap,
13
+ analyze: definition?.build?.metafile ?? cliConfig.analyze,
12
14
  ...definition.config,
13
15
  },
14
16
  definition,
@@ -1,18 +1,6 @@
1
1
  <% if(widgetProperties?.slot?.headline?.html){ %>
2
- <div class="headline-view"><%- widgetProperties && widgetProperties.slot && widgetProperties.slot.headline &&
3
- widgetProperties.slot.headline.html %></div>
2
+ <div class="<%= (widgetProperties?.slot?.headline?.containerSelector ?? 'merkur-headline').replace('.', '') %>">
3
+ <%- widgetProperties?.slot?.headline?.html %>
4
+ </div>
4
5
  <% } %>
5
- <div class="merkur-view"><%- html %></div>
6
- <script>
7
- window.addEventListener('load', function () {
8
- __merkur__.create(<%- JSON.stringify(widgetProperties) %>)
9
- .then(function (widget) {
10
- widget.containerSelector = '.merkur-view';
11
- if (widget?.slot?.headline) {
12
- widget.slot.headline.containerSelector = '.headline-view';
13
- }
14
-
15
- widget.mount();
16
- });
17
- });
18
- </script>
6
+ <div class="<%= (widgetProperties?.containerSelector ?? 'merkur-main').replace('.', '') %>"><%- html %></div>
@@ -0,0 +1,25 @@
1
+ <script>
2
+ window.addEventListener('load', function () {
3
+ __merkur__.create(<%- escapeToJSON(widgetProperties) %>)
4
+ .then(function (widget) {
5
+ widget.containerSelector = widget.containerSelector ?? '.merkur-main';
6
+
7
+ if (widget.slot && Object.keys(widget.slot).length > 0) {
8
+ Object.keys(widget.slot).forEach(function (slotName) {
9
+ widget.slot[slotName].containerSelector = widget.slot[slotName].containerSelector ?? '.merkur-' + slotName;
10
+ });
11
+ }
12
+
13
+ // support for router plugin
14
+ if (widget.router) {
15
+ widget.on('@merkur/plugin-router.redirect', function (_, url) {
16
+ var parsedUrl = new URL(url.url);
17
+ window.location = parsedUrl.href;
18
+ });
19
+ }
20
+
21
+ // mount widget to DOM
22
+ widget.mount();
23
+ });
24
+ });
25
+ </script>
@@ -1,8 +1,8 @@
1
1
  <script>
2
2
  window.__merkur_dev__ = window.__merkur_dev__ || {};
3
- window.__merkur_dev__.merkurConfig = <%- JSON.stringify(merkurConfig) %>;
4
- window.__merkur_dev__.assets = <%- JSON.stringify(assets) %>;
5
- window.__merkur_dev__.widgetProperties = <%- JSON.stringify(widgetProperties) %>;
3
+ window.__merkur_dev__.merkurConfig = <%- escapeToJSON(merkurConfig) %>;
4
+ window.__merkur_dev__.assets = <%- escapeToJSON(assets) %>;
5
+ window.__merkur_dev__.widgetProperties = <%- escapeToJSON(widgetProperties) %>;
6
6
  <%- devClient %>
7
7
  </script>
8
8
  <% assets.forEach((asset)=> { %>
package/types.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { BuildOptions } from 'esbuild';
2
2
  import type { Request, Response } from '@types/express';
3
+ import type { CorsOptions } from '@types/cors';
3
4
 
4
5
  export interface CLIConfig {
5
6
  environment: string;
@@ -30,6 +31,7 @@ export type Extend =
30
31
 
31
32
  export interface Task {
32
33
  name: 'es13' | 'es9' | 'node' | string;
34
+ folder: 'es13' | 'es9' | string;
33
35
  build: BuildOptions;
34
36
  [key: string]: any;
35
37
  }
@@ -46,6 +48,7 @@ export interface DevServer {
46
48
  export interface Playground {
47
49
  template: string;
48
50
  templateFolder: string;
51
+ serverTemplateFolder: string;
49
52
  path: string;
50
53
  widgetHandler(req: Request, res: Response): Record<string, unknown>;
51
54
  widgetParams(req: Request): URLSearchParams;
@@ -66,10 +69,18 @@ export interface WidgetServer {
66
69
  staticPath: string;
67
70
  buildFolder: string;
68
71
  clusters: number;
72
+ cors: {
73
+ options: CorsOptions;
74
+ };
75
+ }
76
+
77
+ export interface Constant {
78
+ HOST: string;
69
79
  }
70
80
 
71
81
  export interface MerkurConfig {
72
82
  HMR: boolean;
83
+ constant: Constant;
73
84
  cliConfig: CLIConfig;
74
85
  extends: Extend[];
75
86
  task: {