@anansi/core 0.16.7 → 0.16.9

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.
@@ -15,15 +15,15 @@ import tmp from 'tmp';
15
15
  import { ufs } from 'unionfs';
16
16
  import { promisify } from 'util';
17
17
  import webpack, { MultiCompiler } from 'webpack';
18
- import logging from 'webpack/lib/logging/runtime';
18
+ import logging from 'webpack/lib/logging/runtime.js';
19
19
  import WebpackDevServer from 'webpack-dev-server';
20
20
 
21
- import 'cross-fetch/dist/node-polyfill';
21
+ import 'cross-fetch/dist/node-polyfill.js';
22
22
  import { getWebpackConfig } from './getWebpackConfig.js';
23
23
  import { BoundRender } from './types.js';
24
24
 
25
25
  // run directly from node
26
- if (require.main === module) {
26
+ if ('main' in import.meta) {
27
27
  const entrypoint = process.argv[2];
28
28
 
29
29
  if (!entrypoint) {
@@ -34,11 +34,14 @@ if (require.main === module) {
34
34
  startDevServer(entrypoint);
35
35
  }
36
36
 
37
- export default function startDevServer(
37
+ let serverFileContents: Promise<string> = Promise.resolve('');
38
+ let serverEntry = '';
39
+
40
+ export default async function startDevServer(
38
41
  entrypoint: string,
39
42
  env: Record<string, unknown> = {},
40
43
  ) {
41
- const webpackConfig = getWebpackConfig();
44
+ const webpackConfig = await getWebpackConfig();
42
45
 
43
46
  const log = logging.getLogger('anansi-devserver');
44
47
 
@@ -148,12 +151,12 @@ export default function startDevServer(
148
151
  // ASSETS
149
152
  const clientManifest = clientStats.toJson();
150
153
 
151
- const serverEntry = getServerBundle(serverStats);
154
+ serverEntry = getServerBundle(serverStats);
155
+ serverFileContents = readFile(serverEntry).then(buf => buf.toString());
152
156
  // reload modules
153
157
  Object.keys(fsRequire.cache).forEach(key => {
154
158
  delete fsRequire.cache[key];
155
159
  });
156
- // eslint-disable-next-line @typescript-eslint/no-var-requires
157
160
  render = (fsRequire(serverEntry) as any).default.bind(
158
161
  undefined,
159
162
  clientManifest,
@@ -240,6 +243,24 @@ export default function startDevServer(
240
243
  importRender((multiStats as webpack.MultiStats).stats);
241
244
  } catch (e: any) {
242
245
  log.error('Failed to load serve entrypoint');
246
+ const finder = new RegExp(`${serverEntry}:([\\d]+):([\\d]+)`, 'g');
247
+ serverFileContents.then(fileText => {
248
+ const textRows = fileText.split('\n');
249
+ log.error('>>> Stack Context [serve entrypoint] <<<');
250
+ for (const match of e.stack.matchAll(finder) ?? []) {
251
+ const row = Number.parseInt(match[1]);
252
+ const col = Number.parseInt(match[2]);
253
+ log.error(path.basename(serverEntry) + ' ' + row + ':' + col);
254
+ log.error(textRows[row - 2]);
255
+ log.error(textRows[row - 1]);
256
+ log.error(Array(col).join(' ') + '^');
257
+ log.error(textRows[row]);
258
+ log.error(textRows[row + 1]);
259
+ log.error(textRows[row + 2]);
260
+ }
261
+ diskFs.writeFileSync(serverEntry, fileText);
262
+ });
263
+
243
264
  throw e;
244
265
  }
245
266
  } else {
@@ -2,3 +2,7 @@ declare module 'webpack/lib/logging/runtime' {
2
2
  const logging: any;
3
3
  export = logging;
4
4
  }
5
+ declare module 'webpack/lib/logging/runtime.js' {
6
+ const logging: any;
7
+ export = logging;
8
+ }
@@ -32,11 +32,7 @@ export default function Document({
32
32
  const policy = {
33
33
  ...csPolicy,
34
34
  };
35
- if (
36
- nonce &&
37
- // nonces negate 'unsafe-inline' so do not add it in development to keep things easier
38
- process.env.NODE_ENV === 'production'
39
- ) {
35
+ if (nonce) {
40
36
  if (typeof policy['script-src'] === 'string') {
41
37
  policy['script-src'] = [policy['script-src'], `'nonce-${nonce}'`];
42
38
  } else {
@@ -1,4 +1,5 @@
1
1
  import type { Route } from '@anansi/router';
2
+ import { Request } from 'express';
2
3
  import React from 'react';
3
4
  import { StatsChunkGroup } from 'webpack';
4
5
 
@@ -70,6 +71,13 @@ export default function DocumentSpout(options: {
70
71
  ? { href: asset, as: 'script' }
71
72
  : { href: asset },
72
73
  );
74
+ const nonce =
75
+ // nonces negate 'unsafe-inline' so do not add it in development to keep things easier
76
+ props.nonce &&
77
+ (process.env.NODE_ENV === 'production' ||
78
+ (props.req as Request)?.protocol !== 'http')
79
+ ? props.nonce
80
+ : undefined;
73
81
 
74
82
  return {
75
83
  ...nextProps,
@@ -80,7 +88,7 @@ export default function DocumentSpout(options: {
80
88
  title={nextProps.title ?? options.title}
81
89
  assets={assets}
82
90
  rootId={options.rootId}
83
- nonce={props.nonce}
91
+ nonce={nonce}
84
92
  csPolicy={options.csPolicy}
85
93
  scripts={nextProps.scripts}
86
94
  >
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1 +0,0 @@
1
- {"type":"commonjs"}