@anansi/core 0.16.8 → 0.16.10

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.
@@ -1,22 +1,20 @@
1
- import { create as createResolve } from 'enhanced-resolve';
1
+ import { default as enhanced } from 'enhanced-resolve';
2
2
  import webpack from 'webpack';
3
3
 
4
- const resolve = createResolve.sync({
4
+ const resolve = enhanced.create.sync({
5
5
  modules: ['.'],
6
6
  // or resolve.create.sync
7
7
  extensions: ['.js', '.cjs', '.mjs'],
8
8
  // see more options below
9
9
  });
10
10
 
11
- export function getWebpackConfig(): (
12
- env: any,
13
- argv: any,
14
- ) => webpack.Configuration {
11
+ export async function getWebpackConfig(): Promise<
12
+ (env: any, argv: any) => webpack.Configuration
13
+ > {
15
14
  const configPath = resolve({}, process.cwd(), 'webpack.config');
16
15
  if (!configPath) {
17
16
  throw new Error('Cannot find webpack.config in ' + process.cwd());
18
17
  }
19
- // eslint-disable-next-line @typescript-eslint/no-var-requires
20
- const webpackConfig = require(configPath);
21
- return webpackConfig;
18
+
19
+ return (await import(configPath)).default;
22
20
  }
@@ -13,13 +13,13 @@ import path from 'path';
13
13
  import { promisify } from 'util';
14
14
  import webpack from 'webpack';
15
15
 
16
- import 'cross-fetch/dist/node-polyfill';
16
+ import 'cross-fetch/dist/node-polyfill.js';
17
17
  import getProxyMiddlewares from './getProxyMiddlewares.js';
18
18
  import { getWebpackConfig } from './getWebpackConfig.js';
19
19
  import { Render } from './types.js';
20
20
 
21
21
  // run directly from node
22
- if (require.main === module) {
22
+ if ('main' in import.meta) {
23
23
  const entrypoint = process.argv[2];
24
24
 
25
25
  if (!entrypoint) {
@@ -29,7 +29,7 @@ if (require.main === module) {
29
29
  serve(entrypoint);
30
30
  }
31
31
 
32
- export default function serve(
32
+ export default async function serve(
33
33
  entrypoint: string,
34
34
  options: { serveAssets?: boolean; serveProxy?: boolean } = {},
35
35
  ) {
@@ -37,7 +37,7 @@ export default function serve(
37
37
 
38
38
  const loader = ora('Initializing').start();
39
39
 
40
- const webpackConfig = getWebpackConfig();
40
+ const webpackConfig = await getWebpackConfig();
41
41
 
42
42
  const manifestPath = getManifestPathFromWebpackconfig(
43
43
  webpackConfig({}, { mode: 'production' }),
@@ -66,7 +66,7 @@ export default function serve(
66
66
  }
67
67
 
68
68
  // Start the express server after the first compilation
69
- function initializeApp(clientManifest: webpack.StatsCompilation) {
69
+ async function initializeApp(clientManifest: webpack.StatsCompilation) {
70
70
  loader.info('Launching server');
71
71
  if (!clientManifest) {
72
72
  loader.fail('Manifest not found');
@@ -88,7 +88,7 @@ export default function serve(
88
88
  next: NextFunction,
89
89
  ) => {
90
90
  const filename =
91
- req.url?.substr(
91
+ req.url?.substring(
92
92
  (process.env.WEBPACK_PUBLIC_PATH as string).length,
93
93
  ) ?? '';
94
94
  const assetPath = path.join(
@@ -101,9 +101,7 @@ export default function serve(
101
101
  !diskFs.lstatSync(assetPath).isDirectory()
102
102
  ) {
103
103
  try {
104
- const fileContent = (await readFile(assetPath)).toString();
105
- res.contentType(filename);
106
- res.send(fileContent);
104
+ res.sendFile(assetPath);
107
105
  } catch (e) {
108
106
  return next();
109
107
  }
@@ -129,11 +127,12 @@ export default function serve(
129
127
  }
130
128
 
131
129
  // SERVER SIDE RENDERING
132
- // eslint-disable-next-line @typescript-eslint/no-var-requires
133
- const render: Render = require(path.join(
134
- process.cwd(),
135
- entrypoint,
136
- )).default;
130
+ let render: Render = (await import(path.join(process.cwd(), entrypoint)))
131
+ .default;
132
+
133
+ if ('default' in render) {
134
+ render = render.default as any;
135
+ }
137
136
 
138
137
  if (typeof render !== 'function') {
139
138
  throw new Error(
@@ -187,8 +186,14 @@ export default function serve(
187
186
  });
188
187
  }
189
188
 
190
- // eslint-disable-next-line @typescript-eslint/no-var-requires
191
- initializeApp(require(manifestPath));
189
+ let manifest = await import(manifestPath, {
190
+ assert: { type: 'json' },
191
+ });
192
+ // handle inconsistent import conditions
193
+ if ('default' in manifest) {
194
+ manifest = manifest.default;
195
+ }
196
+ await initializeApp(manifest);
192
197
 
193
198
  process.on('SIGINT', () => {
194
199
  loader.warn('Received SIGINT, devserver shutting down');
@@ -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
+ }
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1 +0,0 @@
1
- {"type":"commonjs"}