@graphcommerce/next-config 5.1.0-canary.3 → 5.1.0-canary.4

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.
@@ -4,7 +4,5 @@ export declare class InterceptorPlugin {
4
4
  private interceptorByDepependency;
5
5
  private resolveDependency;
6
6
  constructor();
7
- private watched;
8
- watchList(): string[];
9
7
  apply(compiler: Compiler): void;
10
8
  }
@@ -11,39 +11,23 @@ const generateInterceptors_1 = require("./generateInterceptors");
11
11
  const writeInterceptors_1 = require("./writeInterceptors");
12
12
  class InterceptorPlugin {
13
13
  constructor() {
14
- this.watched = [];
15
14
  this.resolveDependency = (0, resolveDependency_1.resolveDependency)();
16
- this.watched = this.watchList();
17
15
  this.interceptors = (0, generateInterceptors_1.generateInterceptors)((0, findPlugins_1.findPlugins)(), this.resolveDependency);
18
16
  this.interceptorByDepependency = Object.fromEntries(Object.values(this.interceptors).map((i) => [i.dependency, i]));
19
17
  (0, writeInterceptors_1.writeInterceptors)(this.interceptors);
20
18
  }
21
- watchList() {
22
- return [
23
- ...new Set((0, findPlugins_1.findPlugins)()
24
- .map((p) => this.resolveDependency(p.plugin))
25
- .map((p) => p.fromRoot)),
26
- ];
27
- }
28
19
  apply(compiler) {
29
20
  const logger = compiler.getInfrastructureLogger('InterceptorPlugin');
30
21
  // After the compilation has succeeded we watch all possible plugin locations.
31
22
  compiler.hooks.afterCompile.tap('InterceptorPlugin', (compilation) => {
32
- const watchList = this.watchList();
33
- const added = watchList.filter((d) => !this.watched.includes(d));
34
- const removed = this.watched.filter((d) => !watchList.includes(d));
35
- this.watched = watchList;
36
- if (added.length > 0) {
37
- added.forEach((context) => compilation.contextDependencies.add(context));
38
- }
39
- if (removed.length > 0) {
40
- removed.forEach((context) => compilation.contextDependencies.delete(context));
41
- }
42
- if (added.length > 0 || removed.length > 0) {
43
- this.interceptors = (0, generateInterceptors_1.generateInterceptors)((0, findPlugins_1.findPlugins)(), this.resolveDependency);
44
- this.interceptorByDepependency = Object.fromEntries(Object.values(this.interceptors).map((i) => [i.dependency, i]));
45
- (0, writeInterceptors_1.writeInterceptors)(this.interceptors);
46
- }
23
+ const plugins = (0, findPlugins_1.findPlugins)();
24
+ plugins.forEach((p) => {
25
+ const absoluteFilePath = `${path_1.default.join(process.cwd(), this.resolveDependency(p.plugin).fromRoot)}.tsx`;
26
+ compilation.fileDependencies.add(absoluteFilePath);
27
+ });
28
+ this.interceptors = (0, generateInterceptors_1.generateInterceptors)(plugins, this.resolveDependency);
29
+ this.interceptorByDepependency = Object.fromEntries(Object.values(this.interceptors).map((i) => [i.dependency, i]));
30
+ (0, writeInterceptors_1.writeInterceptors)(this.interceptors);
47
31
  });
48
32
  compiler.hooks.normalModuleFactory.tap('InterceptorPlugin', (nmf) => {
49
33
  nmf.hooks.beforeResolve.tap('InterceptorPlugin', (resource) => {
@@ -47,9 +47,7 @@ function parseStructure(file) {
47
47
  // console.log('hallo', node)
48
48
  }
49
49
  });
50
- return exports.component && exports.exported
51
- ? exports
52
- : undefined;
50
+ return exports;
53
51
  }
54
52
  function findPlugins(cwd = process.cwd()) {
55
53
  const dependencies = (0, resolveDependenciesSync_1.resolveDependenciesSync)(cwd);
@@ -61,8 +59,7 @@ function findPlugins(cwd = process.cwd()) {
61
59
  const result = parseStructure(file);
62
60
  if (!result)
63
61
  return;
64
- if (result.ifEnv && !process.env[result.ifEnv])
65
- return;
62
+ // if (result.ifEnv && !process.env[result.ifEnv]) return
66
63
  plugins.push({ ...result, plugin: file.replace(dependency, path).replace('.tsx', '') });
67
64
  }
68
65
  catch (e) {
@@ -1,7 +1,7 @@
1
1
  import { ResolveDependency, ResolveDependencyReturn } from '../utils/resolveDependency';
2
2
  export type PluginConfig = {
3
- component: string;
4
- exported: string;
3
+ component?: string;
4
+ exported?: string;
5
5
  plugin: string;
6
6
  ifEnv?: string;
7
7
  };
@@ -4,7 +4,7 @@ exports.generateInterceptors = exports.generateInterceptor = void 0;
4
4
  function generateInterceptor(plugin) {
5
5
  const { fromModule, dependency, components } = plugin;
6
6
  const pluginImports = Object.entries(components)
7
- .map(([_, plugins]) => {
7
+ .map(([, plugins]) => {
8
8
  const duplicateImports = new Set();
9
9
  return plugins
10
10
  .sort((a, b) => a.plugin.localeCompare(b.plugin))
@@ -74,7 +74,9 @@ exports.generateInterceptor = generateInterceptor;
74
74
  function generateInterceptors(plugins, resolve) {
75
75
  // todo: Do not use reduce as we're passing the accumulator to the next iteration
76
76
  const byExportedComponent = plugins.reduce((acc, plug) => {
77
- const { exported } = plug;
77
+ const { exported, component } = plug;
78
+ if (!exported || !component)
79
+ return acc;
78
80
  const resolved = resolve(exported);
79
81
  if (!acc[resolved.fromRoot])
80
82
  acc[resolved.fromRoot] = {
@@ -82,9 +84,9 @@ function generateInterceptors(plugins, resolve) {
82
84
  target: `${resolved.fromRoot}.interceptor`,
83
85
  components: {},
84
86
  };
85
- if (!acc[resolved.fromRoot].components[plug.component])
86
- acc[resolved.fromRoot].components[plug.component] = [];
87
- acc[resolved.fromRoot].components[plug.component].push(plug);
87
+ if (!acc[resolved.fromRoot].components[component])
88
+ acc[resolved.fromRoot].components[component] = [];
89
+ acc[resolved.fromRoot].components[component].push(plug);
88
90
  return acc;
89
91
  }, {});
90
92
  return Object.fromEntries(Object.entries(byExportedComponent).map(([target, plg]) => [target, generateInterceptor(plg)]));
@@ -6,14 +6,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.writeInterceptors = void 0;
7
7
  const node_fs_1 = __importDefault(require("node:fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
+ const glob_1 = __importDefault(require("glob"));
10
+ const resolveDependenciesSync_1 = require("../utils/resolveDependenciesSync");
9
11
  function writeInterceptors(interceptors, cwd = process.cwd()) {
10
- Object.entries(interceptors).forEach(([target, plugin]) => {
11
- // eslint-disable-next-line no-console
12
- const fileToWrite = `${path_1.default.join(cwd, plugin.fromRoot)}.interceptor.tsx`;
13
- if (!node_fs_1.default.existsSync(fileToWrite) ||
14
- node_fs_1.default.readFileSync(fileToWrite, 'utf8').toString() !== plugin.template) {
15
- node_fs_1.default.writeFileSync(fileToWrite, plugin.template);
12
+ const dependencies = (0, resolveDependenciesSync_1.resolveDependenciesSync)(cwd);
13
+ const existing = [];
14
+ dependencies.forEach((dependency) => {
15
+ const files = glob_1.default.sync(`${dependency}/**/*.interceptor.tsx`, { cwd });
16
+ existing.push(...files);
17
+ });
18
+ Object.entries(interceptors).forEach(([, plugin]) => {
19
+ const relativeFile = `${plugin.fromRoot}.interceptor.tsx`;
20
+ if (existing.includes(relativeFile)) {
21
+ delete existing[existing.indexOf(relativeFile)];
16
22
  }
23
+ const fileToWrite = path_1.default.join(cwd, relativeFile);
24
+ const isSame = node_fs_1.default.existsSync(fileToWrite) &&
25
+ node_fs_1.default.readFileSync(fileToWrite, 'utf8').toString() === plugin.template;
26
+ if (!isSame)
27
+ node_fs_1.default.writeFileSync(fileToWrite, plugin.template);
17
28
  });
29
+ // Cleanup unused interceptors
30
+ existing.forEach((file) => node_fs_1.default.unlinkSync(file));
18
31
  }
19
32
  exports.writeInterceptors = writeInterceptors;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graphcommerce/next-config",
3
3
  "homepage": "https://www.graphcommerce.org/",
4
4
  "repository": "github:graphcommerce-org/graphcommerce",
5
- "version": "5.1.0-canary.3",
5
+ "version": "5.1.0-canary.4",
6
6
  "type": "commonjs",
7
7
  "main": "dist/index.js",
8
8
  "typings": "dist/index.d.ts",
@@ -15,7 +15,6 @@ export class InterceptorPlugin {
15
15
  constructor() {
16
16
  this.resolveDependency = resolveDependency()
17
17
 
18
- this.watched = this.watchList()
19
18
  this.interceptors = generateInterceptors(findPlugins(), this.resolveDependency)
20
19
  this.interceptorByDepependency = Object.fromEntries(
21
20
  Object.values(this.interceptors).map((i) => [i.dependency, i]),
@@ -24,41 +23,26 @@ export class InterceptorPlugin {
24
23
  writeInterceptors(this.interceptors)
25
24
  }
26
25
 
27
- private watched: string[] = []
28
-
29
- watchList() {
30
- return [
31
- ...new Set(
32
- findPlugins()
33
- .map((p) => this.resolveDependency(p.plugin))
34
- .map((p) => p.fromRoot),
35
- ),
36
- ]
37
- }
38
-
39
26
  apply(compiler: Compiler): void {
40
27
  const logger = compiler.getInfrastructureLogger('InterceptorPlugin')
41
28
 
42
29
  // After the compilation has succeeded we watch all possible plugin locations.
43
30
  compiler.hooks.afterCompile.tap('InterceptorPlugin', (compilation) => {
44
- const watchList = this.watchList()
45
- const added = watchList.filter((d) => !this.watched.includes(d))
46
- const removed = this.watched.filter((d) => !watchList.includes(d))
47
- this.watched = watchList
31
+ const plugins = findPlugins()
48
32
 
49
- if (added.length > 0) {
50
- added.forEach((context) => compilation.contextDependencies.add(context))
51
- }
52
- if (removed.length > 0) {
53
- removed.forEach((context) => compilation.contextDependencies.delete(context))
54
- }
55
- if (added.length > 0 || removed.length > 0) {
56
- this.interceptors = generateInterceptors(findPlugins(), this.resolveDependency)
57
- this.interceptorByDepependency = Object.fromEntries(
58
- Object.values(this.interceptors).map((i) => [i.dependency, i]),
59
- )
60
- writeInterceptors(this.interceptors)
61
- }
33
+ plugins.forEach((p) => {
34
+ const absoluteFilePath = `${path.join(
35
+ process.cwd(),
36
+ this.resolveDependency(p.plugin).fromRoot,
37
+ )}.tsx`
38
+ compilation.fileDependencies.add(absoluteFilePath)
39
+ })
40
+
41
+ this.interceptors = generateInterceptors(plugins, this.resolveDependency)
42
+ this.interceptorByDepependency = Object.fromEntries(
43
+ Object.values(this.interceptors).map((i) => [i.dependency, i]),
44
+ )
45
+ writeInterceptors(this.interceptors)
62
46
  })
63
47
 
64
48
  compiler.hooks.normalModuleFactory.tap('InterceptorPlugin', (nmf) => {
@@ -4,12 +4,12 @@ import { resolveDependenciesSync } from '../utils/resolveDependenciesSync'
4
4
  import type { PluginConfig } from './generateInterceptors'
5
5
 
6
6
  type ParseResult = {
7
- component: string
8
- exported: string
7
+ component?: string
8
+ exported?: string
9
9
  ifEnv?: string
10
10
  }
11
11
 
12
- function parseStructure(file: string): ParseResult | undefined {
12
+ function parseStructure(file: string): ParseResult {
13
13
  const ast = parseFileSync(file, { syntax: 'typescript', tsx: true })
14
14
  // const ast = swc.parseFileSync(file, { syntax: 'typescript' })
15
15
 
@@ -52,9 +52,7 @@ function parseStructure(file: string): ParseResult | undefined {
52
52
  }
53
53
  })
54
54
 
55
- return exports.component && exports.exported
56
- ? (exports as { component: string; exported: string })
57
- : undefined
55
+ return exports as ParseResult
58
56
  }
59
57
 
60
58
  export function findPlugins(cwd: string = process.cwd()) {
@@ -67,7 +65,7 @@ export function findPlugins(cwd: string = process.cwd()) {
67
65
  try {
68
66
  const result = parseStructure(file)
69
67
  if (!result) return
70
- if (result.ifEnv && !process.env[result.ifEnv]) return
68
+ // if (result.ifEnv && !process.env[result.ifEnv]) return
71
69
 
72
70
  plugins.push({ ...result, plugin: file.replace(dependency, path).replace('.tsx', '') })
73
71
  } catch (e) {
@@ -75,5 +73,6 @@ export function findPlugins(cwd: string = process.cwd()) {
75
73
  }
76
74
  })
77
75
  })
76
+
78
77
  return plugins
79
78
  }
@@ -1,8 +1,8 @@
1
1
  import { ResolveDependency, ResolveDependencyReturn } from '../utils/resolveDependency'
2
2
 
3
3
  export type PluginConfig = {
4
- component: string
5
- exported: string
4
+ component?: string
5
+ exported?: string
6
6
  plugin: string
7
7
  ifEnv?: string
8
8
  }
@@ -19,7 +19,7 @@ export function generateInterceptor(plugin: Plugin): MaterializedPlugin {
19
19
  const { fromModule, dependency, components } = plugin
20
20
 
21
21
  const pluginImports = Object.entries(components)
22
- .map(([_, plugins]) => {
22
+ .map(([, plugins]) => {
23
23
  const duplicateImports = new Set()
24
24
  return plugins
25
25
  .sort((a, b) => a.plugin.localeCompare(b.plugin))
@@ -109,7 +109,9 @@ export function generateInterceptors(
109
109
  ): GenerateInterceptorsReturn {
110
110
  // todo: Do not use reduce as we're passing the accumulator to the next iteration
111
111
  const byExportedComponent = plugins.reduce((acc, plug) => {
112
- const { exported } = plug
112
+ const { exported, component } = plug
113
+ if (!exported || !component) return acc
114
+
113
115
  const resolved = resolve(exported)
114
116
 
115
117
  if (!acc[resolved.fromRoot])
@@ -119,10 +121,10 @@ export function generateInterceptors(
119
121
  components: {},
120
122
  } as Plugin
121
123
 
122
- if (!acc[resolved.fromRoot].components[plug.component])
123
- acc[resolved.fromRoot].components[plug.component] = []
124
+ if (!acc[resolved.fromRoot].components[component])
125
+ acc[resolved.fromRoot].components[component] = []
124
126
 
125
- acc[resolved.fromRoot].components[plug.component].push(plug)
127
+ acc[resolved.fromRoot].components[component].push(plug)
126
128
  return acc
127
129
  }, {} as Record<string, Plugin>)
128
130
 
@@ -1,20 +1,35 @@
1
1
  import fs from 'node:fs'
2
2
  import path from 'path'
3
+ import glob from 'glob'
4
+ import { resolveDependenciesSync } from '../utils/resolveDependenciesSync'
3
5
  import { GenerateInterceptorsReturn } from './generateInterceptors'
4
6
 
5
7
  export function writeInterceptors(
6
8
  interceptors: GenerateInterceptorsReturn,
7
9
  cwd: string = process.cwd(),
8
10
  ) {
9
- Object.entries(interceptors).forEach(([target, plugin]) => {
10
- // eslint-disable-next-line no-console
11
- const fileToWrite = `${path.join(cwd, plugin.fromRoot)}.interceptor.tsx`
11
+ const dependencies = resolveDependenciesSync(cwd)
12
+ const existing: string[] = []
13
+ dependencies.forEach((dependency) => {
14
+ const files = glob.sync(`${dependency}/**/*.interceptor.tsx`, { cwd })
15
+ existing.push(...files)
16
+ })
17
+
18
+ Object.entries(interceptors).forEach(([, plugin]) => {
19
+ const relativeFile = `${plugin.fromRoot}.interceptor.tsx`
12
20
 
13
- if (
14
- !fs.existsSync(fileToWrite) ||
15
- fs.readFileSync(fileToWrite, 'utf8').toString() !== plugin.template
16
- ) {
17
- fs.writeFileSync(fileToWrite, plugin.template)
21
+ if (existing.includes(relativeFile)) {
22
+ delete existing[existing.indexOf(relativeFile)]
18
23
  }
24
+ const fileToWrite = path.join(cwd, relativeFile)
25
+
26
+ const isSame =
27
+ fs.existsSync(fileToWrite) &&
28
+ fs.readFileSync(fileToWrite, 'utf8').toString() === plugin.template
29
+
30
+ if (!isSame) fs.writeFileSync(fileToWrite, plugin.template)
19
31
  })
32
+
33
+ // Cleanup unused interceptors
34
+ existing.forEach((file) => fs.unlinkSync(file))
20
35
  }
@@ -1 +0,0 @@
1
- export declare function rmInterceptors(cwd?: string): string[];
@@ -1,22 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.rmInterceptors = void 0;
7
- const node_fs_1 = __importDefault(require("node:fs"));
8
- const glob_1 = __importDefault(require("glob"));
9
- const resolveDependenciesSync_1 = require("../utils/resolveDependenciesSync");
10
- function rmInterceptors(cwd = process.cwd()) {
11
- const dependencies = (0, resolveDependenciesSync_1.resolveDependenciesSync)(cwd);
12
- const removed = [];
13
- dependencies.forEach((dependency) => {
14
- const files = glob_1.default.sync(`${dependency}/**/*.interceptor.tsx`, { cwd });
15
- files.forEach((file) => {
16
- node_fs_1.default.unlinkSync(file);
17
- removed.push(file);
18
- });
19
- });
20
- return removed;
21
- }
22
- exports.rmInterceptors = rmInterceptors;
@@ -1,17 +0,0 @@
1
- import fs from 'node:fs'
2
- import glob from 'glob'
3
- import { resolveDependenciesSync } from '../utils/resolveDependenciesSync'
4
-
5
- export function rmInterceptors(cwd: string = process.cwd()): string[] {
6
- const dependencies = resolveDependenciesSync(cwd)
7
-
8
- const removed: string[] = []
9
- dependencies.forEach((dependency) => {
10
- const files = glob.sync(`${dependency}/**/*.interceptor.tsx`, { cwd })
11
- files.forEach((file) => {
12
- fs.unlinkSync(file)
13
- removed.push(file)
14
- })
15
- })
16
- return removed
17
- }