@merkur/cli 0.36.4 → 0.37.1

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 } = 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,14 +134,6 @@ emitter.on(
128
134
  );
129
135
  });
130
136
 
131
- if (runTask.length !== 0) {
132
- Object.keys(merkurConfig.task)
133
- .filter((taskName) => !runTask.includes(taskName))
134
- .forEach((taskKey) => {
135
- delete merkurConfig.task[taskKey];
136
- });
137
- }
138
-
139
137
  return merkurConfig;
140
138
  },
141
139
  );
@@ -170,8 +168,12 @@ emitter.on(
170
168
  EMITTER_EVENTS.MERKUR_CONFIG,
171
169
  function defaultEntries({ merkurConfig, cliConfig }) {
172
170
  merkurConfig.defaultEntries = {
173
- client: [`${cliConfig.projectFolder}/src/entries/client.js`],
174
- server: [`${cliConfig.projectFolder}/src/entries/server.js`],
171
+ client: [
172
+ path.resolve(`${cliConfig.projectFolder}/src/entries/client.js`),
173
+ ],
174
+ server: [
175
+ path.resolve(`${cliConfig.projectFolder}/src/entries/server.js`),
176
+ ],
175
177
  ...merkurConfig.defaultEntries,
176
178
  };
177
179
 
@@ -183,8 +185,11 @@ emitter.on(
183
185
  EMITTER_EVENTS.MERKUR_CONFIG,
184
186
  function playground({ merkurConfig, cliConfig }) {
185
187
  merkurConfig.playground = {
186
- template: `${cliConfig.cliFolder}/templates/playground.ejs`,
187
- templateFolder: `${cliConfig.cliFolder}/templates`,
188
+ template: path.resolve(`${cliConfig.cliFolder}/templates/playground.ejs`),
189
+ templateFolder: path.resolve(`${cliConfig.cliFolder}/templates`),
190
+ serverTemplateFolder: path.resolve(
191
+ `${cliConfig.projectFolder}/server/playground/templates`,
192
+ ),
188
193
  path: '/',
189
194
  widgetHandler: async (req) => {
190
195
  const { protocol, host } = merkurConfig.widgetServer;
@@ -243,6 +248,17 @@ emitter.on(
243
248
  buildFolder: path.resolve(cliConfig.projectFolder, cliConfig.buildFolder),
244
249
  clusters: cliConfig.command === COMMAND_NAME.DEV ? 0 : 3,
245
250
  ...merkurConfig.widgetServer,
251
+ cors: {
252
+ options: {
253
+ origin: [
254
+ new RegExp('^https?://localhost(:[0-9]+)?$'),
255
+ new RegExp('^https?://127\\.0\\.0\\.1(:[0-9]+)?$'),
256
+ ],
257
+ methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'],
258
+ optionsSuccessStatus: 200,
259
+ ...merkurConfig.widgetServer?.cors?.options,
260
+ },
261
+ },
246
262
  };
247
263
 
248
264
  const { origin, host, protocol } = merkurConfig.widgetServer;
@@ -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
  }),
@@ -0,0 +1,40 @@
1
+ import fs from 'node:fs/promises';
2
+
3
+ import esbuild from 'esbuild';
4
+ import { createLogger } from './logger.mjs';
5
+
6
+ export async function runBuild({ cliConfig, build, config }) {
7
+ const logger = createLogger(undefined, cliConfig);
8
+ const { watch } = cliConfig;
9
+
10
+ //es6 === es2015, es9 === es2018, es11 === es2020 es13 ===es2022
11
+ try {
12
+ const result = await (watch
13
+ ? esbuild.context(build)
14
+ : esbuild.build(build));
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
+
31
+ if (watch) {
32
+ await result.watch();
33
+ }
34
+
35
+ return result;
36
+ } catch (error) {
37
+ console.error(error);
38
+ process.exit(1);
39
+ }
40
+ }
package/src/runTask.mjs CHANGED
@@ -1,21 +1,44 @@
1
- import esbuild from 'esbuild';
1
+ import { createBuildConfig } from './buildConfig.mjs';
2
+ import { createTaskConfig } from './taskConfig.mjs';
3
+ import { runBuild } from './runBuild.mjs';
2
4
 
3
- export async function runTask({ cliConfig, build }) {
4
- const { watch } = cliConfig;
5
+ export async function runTask({ cliConfig, merkurConfig, context }) {
6
+ const { runTasks } = cliConfig;
7
+ let task = { ...merkurConfig.task };
5
8
 
6
- //es6 === es2015, es9 === es2018, es11 === es2020 es13 ===es2022
7
- try {
8
- const result = await (watch
9
- ? esbuild.context(build)
10
- : esbuild.build(build));
9
+ if (runTasks.length !== 0) {
10
+ Object.keys(task)
11
+ .filter((taskName) => !runTasks.includes(taskName))
12
+ .forEach((taskKey) => {
13
+ delete task[taskKey];
14
+ });
15
+ }
16
+
17
+ return Object.keys(task).reduce(async (result, key) => {
18
+ const definition = task[key];
11
19
 
12
- if (watch) {
13
- await result.watch();
14
- }
20
+ const config = await createTaskConfig({
21
+ definition,
22
+ merkurConfig,
23
+ cliConfig,
24
+ context,
25
+ });
26
+ const build = await createBuildConfig({
27
+ definition,
28
+ config,
29
+ merkurConfig,
30
+ cliConfig,
31
+ context,
32
+ });
33
+ result[definition.name] = await runBuild({
34
+ definition,
35
+ build,
36
+ merkurConfig,
37
+ cliConfig,
38
+ config,
39
+ context,
40
+ });
15
41
 
16
42
  return result;
17
- } catch (error) {
18
- console.error(error);
19
- process.exit(1);
20
- }
43
+ }, context.task);
21
44
  }
@@ -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(<%- escapeToJSON(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,30 @@
1
+ <script>
2
+ window.addEventListener('load', function () {
3
+ if (!window.__merkur__) {
4
+ console.error('Merkur is not available in the global scope. Make sure you have included Merkur in your project.');
5
+ return;
6
+ }
7
+
8
+ window.__merkur__.create(<%- escapeToJSON(widgetProperties) %>)
9
+ .then(function (widget) {
10
+ widget.containerSelector = widget.containerSelector ?? '.merkur-main';
11
+
12
+ if (widget.slot && Object.keys(widget.slot).length > 0) {
13
+ Object.keys(widget.slot).forEach(function (slotName) {
14
+ widget.slot[slotName].containerSelector = widget.slot[slotName].containerSelector ?? '.merkur-' + slotName;
15
+ });
16
+ }
17
+
18
+ // support for router plugin
19
+ if (widget.router) {
20
+ widget.on('@merkur/plugin-router.redirect', function (_, url) {
21
+ var parsedUrl = new URL(url.url);
22
+ window.location = parsedUrl.href;
23
+ });
24
+ }
25
+
26
+ // mount widget to DOM
27
+ widget.mount();
28
+ });
29
+ });
30
+ </script>
package/src/websocket.mjs CHANGED
@@ -16,7 +16,6 @@ export async function runSocketServer({ merkurConfig, context }) {
16
16
  try {
17
17
  const server = new WebSocketServer({
18
18
  port: merkurConfig.socketServer.port,
19
- host: merkurConfig.constant.HOST,
20
19
  });
21
20
 
22
21
  server.on('connection', function connection(ws) {
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,6 +69,9 @@ export interface WidgetServer {
66
69
  staticPath: string;
67
70
  buildFolder: string;
68
71
  clusters: number;
72
+ cors: {
73
+ options: CorsOptions;
74
+ };
69
75
  }
70
76
 
71
77
  export interface Constant {
@@ -102,7 +108,4 @@ export interface MerkurConfig {
102
108
  }) => Partial<BuildOptions>;
103
109
  }
104
110
 
105
- export function defineConfig({
106
- merkurConfig: MerkurConfig,
107
- cliConfig: CLIConfig,
108
- }): Partial<MerkurConfig>;
111
+ export function defineConfig({ cliConfig: CLIConfig }): Partial<MerkurConfig>;