@atlaspack/reporter-conditional-manifest 2.14.1-dev.131 → 2.14.1-dev.134

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.
@@ -3,7 +3,9 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.default = void 0;
6
+ exports.manifestHashes = exports.default = void 0;
7
+ exports.report = report;
8
+ exports.updateManifest = void 0;
7
9
  function _path() {
8
10
  const data = require("path");
9
11
  _path = function () {
@@ -11,6 +13,13 @@ function _path() {
11
13
  };
12
14
  return data;
13
15
  }
16
+ function _crypto() {
17
+ const data = _interopRequireDefault(require("crypto"));
18
+ _crypto = function () {
19
+ return data;
20
+ };
21
+ return data;
22
+ }
14
23
  function _plugin() {
15
24
  const data = require("@atlaspack/plugin");
16
25
  _plugin = function () {
@@ -19,6 +28,29 @@ function _plugin() {
19
28
  return data;
20
29
  }
21
30
  var _Config = require("./Config");
31
+ function _featureFlags() {
32
+ const data = require("@atlaspack/feature-flags");
33
+ _featureFlags = function () {
34
+ return data;
35
+ };
36
+ return data;
37
+ }
38
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
+ const manifestHashes = exports.manifestHashes = new Map();
40
+ const updateManifest = async (outputFS, logger, conditionalManifestFilename, conditionalManifest) => {
41
+ const hash = _crypto().default.createHash('sha1').update(conditionalManifest).digest('hex');
42
+ if (manifestHashes.get(conditionalManifestFilename) !== hash) {
43
+ manifestHashes.set(conditionalManifestFilename, hash);
44
+ await outputFS.mkdirp((0, _path().dirname)(conditionalManifestFilename));
45
+ await outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
46
+ mode: 0o666
47
+ });
48
+ logger.info({
49
+ message: `Wrote conditional manifest to ${conditionalManifestFilename}`
50
+ });
51
+ }
52
+ };
53
+ exports.updateManifest = updateManifest;
22
54
  async function report({
23
55
  event,
24
56
  options,
@@ -52,13 +84,17 @@ async function report({
52
84
  const conditionalManifest = JSON.stringify(
53
85
  // If there's no content, send an empty manifest so we can still map from it safely
54
86
  manifest[target.name] ?? {}, null, 2);
55
- await options.outputFS.mkdirp((0, _path().dirname)(conditionalManifestFilename));
56
- await options.outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
57
- mode: 0o666
58
- });
59
- logger.info({
60
- message: 'Wrote conditional manifest to ' + conditionalManifestFilename
61
- });
87
+ if ((0, _featureFlags().getFeatureFlag)('conditionalBundlingReporterDuplicateFix')) {
88
+ await updateManifest(options.outputFS, logger, conditionalManifestFilename, conditionalManifest);
89
+ } else {
90
+ await options.outputFS.mkdirp((0, _path().dirname)(conditionalManifestFilename));
91
+ await options.outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
92
+ mode: 0o666
93
+ });
94
+ logger.info({
95
+ message: 'Wrote conditional manifest to ' + conditionalManifestFilename
96
+ });
97
+ }
62
98
  }
63
99
  }
64
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/reporter-conditional-manifest",
3
- "version": "2.14.1-dev.131+14d6b0e5a",
3
+ "version": "2.14.1-dev.134+e072f4b10",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -15,9 +15,10 @@
15
15
  "node": ">= 16.0.0"
16
16
  },
17
17
  "dependencies": {
18
- "@atlaspack/plugin": "2.14.1-dev.131+14d6b0e5a",
18
+ "@atlaspack/feature-flags": "2.14.1-dev.134+e072f4b10",
19
+ "@atlaspack/plugin": "2.14.1-dev.134+e072f4b10",
19
20
  "nullthrows": "^1.1.1"
20
21
  },
21
22
  "type": "commonjs",
22
- "gitHead": "14d6b0e5abcf5554c4979b6492e3e635b8f458c7"
23
+ "gitHead": "e072f4b100a13d97c544324004819ee51cdac4a8"
23
24
  }
@@ -1,5 +1,6 @@
1
1
  // @flow strict-local
2
2
  import {relative, join, dirname} from 'path';
3
+ import crypto from 'crypto';
3
4
  import {Reporter} from '@atlaspack/plugin';
4
5
  import type {
5
6
  Async,
@@ -7,10 +8,40 @@ import type {
7
8
  PluginOptions,
8
9
  PluginTracer,
9
10
  ReporterEvent,
11
+ FileSystem,
12
+ FilePath,
10
13
  } from '@atlaspack/types-internal';
11
14
  import {getConfig} from './Config';
15
+ import {getFeatureFlag} from '@atlaspack/feature-flags';
12
16
 
13
- async function report({
17
+ export const manifestHashes: Map<FilePath, string> = new Map();
18
+
19
+ export const updateManifest = async (
20
+ outputFS: FileSystem,
21
+ logger: PluginLogger,
22
+ conditionalManifestFilename: FilePath,
23
+ conditionalManifest: string,
24
+ ) => {
25
+ const hash = crypto
26
+ .createHash('sha1')
27
+ .update(conditionalManifest)
28
+ .digest('hex');
29
+
30
+ if (manifestHashes.get(conditionalManifestFilename) !== hash) {
31
+ manifestHashes.set(conditionalManifestFilename, hash);
32
+
33
+ await outputFS.mkdirp(dirname(conditionalManifestFilename));
34
+ await outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
35
+ mode: 0o666,
36
+ });
37
+
38
+ logger.info({
39
+ message: `Wrote conditional manifest to ${conditionalManifestFilename}`,
40
+ });
41
+ }
42
+ };
43
+
44
+ export async function report({
14
45
  event,
15
46
  options,
16
47
  logger,
@@ -59,17 +90,26 @@ async function report({
59
90
  2,
60
91
  );
61
92
 
62
- await options.outputFS.mkdirp(dirname(conditionalManifestFilename));
93
+ if (getFeatureFlag('conditionalBundlingReporterDuplicateFix')) {
94
+ await updateManifest(
95
+ options.outputFS,
96
+ logger,
97
+ conditionalManifestFilename,
98
+ conditionalManifest,
99
+ );
100
+ } else {
101
+ await options.outputFS.mkdirp(dirname(conditionalManifestFilename));
102
+ await options.outputFS.writeFile(
103
+ conditionalManifestFilename,
104
+ conditionalManifest,
105
+ {mode: 0o666},
106
+ );
63
107
 
64
- await options.outputFS.writeFile(
65
- conditionalManifestFilename,
66
- conditionalManifest,
67
- {mode: 0o666},
68
- );
69
-
70
- logger.info({
71
- message: 'Wrote conditional manifest to ' + conditionalManifestFilename,
72
- });
108
+ logger.info({
109
+ message:
110
+ 'Wrote conditional manifest to ' + conditionalManifestFilename,
111
+ });
112
+ }
73
113
  }
74
114
  }
75
115
  }
@@ -0,0 +1,114 @@
1
+ // @flow strict-local
2
+ import {
3
+ manifestHashes,
4
+ updateManifest,
5
+ } from '../src/ConditionalManifestReporter';
6
+ import sinon from 'sinon';
7
+ import assert from 'assert';
8
+
9
+ let createMockOverlayFS = () => ({
10
+ // Mock filesystem methods
11
+ writeFile: sinon.stub(),
12
+ readFile: sinon.stub(),
13
+ exists: sinon.stub(),
14
+ chdir: sinon.stub(),
15
+ copyFile: sinon.stub(),
16
+ createReadStream: sinon.stub(),
17
+ createWriteStream: sinon.stub(),
18
+ cwd: sinon.stub(),
19
+ existsSync: sinon.stub(),
20
+ findAncestorFile: sinon.stub(),
21
+ findFirstFile: sinon.stub(),
22
+ findNodeModule: sinon.stub(),
23
+ getEventsSince: sinon.stub(),
24
+ mkdirp: sinon.stub(),
25
+ ncp: sinon.stub(),
26
+ readFileSync: sinon.stub(),
27
+ readdir: sinon.stub(),
28
+ readdirSync: sinon.stub(),
29
+ realpath: sinon.stub(),
30
+ realpathSync: sinon.stub(),
31
+ rimraf: sinon.stub(),
32
+ stat: sinon.stub(),
33
+ statSync: sinon.stub(),
34
+ symlink: sinon.stub(),
35
+ unlink: sinon.stub(),
36
+ watch: sinon.stub(),
37
+ writeSnapshot: sinon.stub(),
38
+ });
39
+
40
+ let createMockLogger = () => ({
41
+ // Mock logger methods
42
+ warn: sinon.stub(),
43
+ info: sinon.stub(),
44
+ verbose: sinon.stub(),
45
+ error: sinon.stub(),
46
+ log: sinon.stub(),
47
+ });
48
+
49
+ describe('ConditionalManifestReporter', function () {
50
+ beforeEach(() => {
51
+ manifestHashes.clear();
52
+ });
53
+
54
+ it('should write the manifest to file', async function () {
55
+ let logger = createMockLogger();
56
+ let overlayFS = createMockOverlayFS();
57
+
58
+ const conditionalManifestFilename =
59
+ '/project-root/dist/conditional-manifest.json';
60
+ const conditionalManifest = JSON.stringify({test: 'manifest'});
61
+
62
+ await updateManifest(
63
+ overlayFS,
64
+ logger,
65
+ conditionalManifestFilename,
66
+ conditionalManifest,
67
+ );
68
+
69
+ // Verify that writeFile was called with the correct arguments
70
+ assert(
71
+ overlayFS.writeFile.calledWith(
72
+ conditionalManifestFilename,
73
+ conditionalManifest,
74
+ {mode: 0o666},
75
+ ),
76
+ );
77
+
78
+ // Verify that logger.info was called with the correct message
79
+ assert(
80
+ logger.info.calledWith({
81
+ message: `Wrote conditional manifest to ${conditionalManifestFilename}`,
82
+ }),
83
+ );
84
+ });
85
+
86
+ it('should not write the manifest if it has not changed', async function () {
87
+ let logger = createMockLogger();
88
+ let overlayFS = createMockOverlayFS();
89
+
90
+ let conditionalManifestFilename =
91
+ '/project-root/dist/conditional-manifest.json';
92
+ let conditionalManifest = JSON.stringify({test: 'manifest'});
93
+
94
+ await updateManifest(
95
+ overlayFS,
96
+ logger,
97
+ conditionalManifestFilename,
98
+ conditionalManifest,
99
+ );
100
+
101
+ await updateManifest(
102
+ overlayFS,
103
+ logger,
104
+ conditionalManifestFilename,
105
+ conditionalManifest,
106
+ );
107
+
108
+ // Verify that writeFile was called once
109
+ assert(overlayFS.writeFile.calledOnce);
110
+
111
+ // Verify that logger.info was called with the correct message
112
+ assert(logger.info.calledOnce);
113
+ });
114
+ });