@atlaspack/reporter-conditional-manifest 2.12.1-dev.3520 → 2.12.1-dev.3565

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.
@@ -18,42 +18,48 @@ function _plugin() {
18
18
  };
19
19
  return data;
20
20
  }
21
- var _default = exports.default = new (_plugin().Reporter)({
22
- async report({
23
- event,
24
- options,
25
- logger
26
- }) {
27
- if (event.type === 'buildSuccess') {
28
- const bundles = event.bundleGraph.getConditionalBundleMapping();
29
-
30
- // Replace bundles with file paths
31
- const mapBundles = bundles => bundles.map(bundle => (0, _path().relative)(bundle.target.distDir, bundle.filePath));
32
- const manifest = {};
33
- for (const [bundle, conditions] of bundles.entries()) {
34
- const bundleInfo = {};
35
- for (const [key, cond] of conditions) {
36
- bundleInfo[key] = {
37
- ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(),
38
- ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse()
39
- };
40
- }
41
- manifest[bundle.target.name] ??= {};
42
- manifest[bundle.target.name][(0, _path().relative)(bundle.target.distDir, bundle.filePath)] = bundleInfo;
43
- }
21
+ var _Config = require("./Config");
22
+ async function report({
23
+ event,
24
+ options,
25
+ logger
26
+ }) {
27
+ if (event.type === 'buildSuccess') {
28
+ const bundles = event.bundleGraph.getConditionalBundleMapping();
44
29
 
45
- // Error if there are multiple targets in the build
46
- const targets = new Set(event.bundleGraph.getBundles().map(bundle => bundle.target));
47
- for (const target of targets) {
48
- const conditionalManifestFilename = (0, _path().join)(target.distDir, 'conditional-manifest.json');
49
- const conditionalManifest = JSON.stringify(manifest[target.name], null, 2);
50
- await options.outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
51
- mode: 0o666
52
- });
53
- logger.info({
54
- message: 'Wrote conditional manifest to ' + conditionalManifestFilename
55
- });
30
+ // Replace bundles with file paths
31
+ const mapBundles = bundles => bundles.map(bundle => (0, _path().relative)(bundle.target.distDir, bundle.filePath));
32
+ const manifest = {};
33
+ for (const [bundle, conditions] of bundles.entries()) {
34
+ const bundleInfo = {};
35
+ for (const [key, cond] of conditions) {
36
+ bundleInfo[key] = {
37
+ // Reverse bundles so we load children bundles first
38
+ ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(),
39
+ ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse()
40
+ };
56
41
  }
42
+ manifest[bundle.target.name] ??= {};
43
+ manifest[bundle.target.name][(0, _path().relative)(bundle.target.distDir, bundle.filePath)] = bundleInfo;
44
+ }
45
+ const targets = new Set(event.bundleGraph.getBundles().map(bundle => bundle.target));
46
+ const {
47
+ filename
48
+ } = await (0, _Config.getConfig)(options);
49
+ for (const target of targets) {
50
+ const conditionalManifestFilename = (0, _path().join)(target.distDir, filename);
51
+ const conditionalManifest = JSON.stringify(
52
+ // If there's no content, send an empty manifest so we can still map from it safely
53
+ manifest[target.name] ?? {}, null, 2);
54
+ await options.outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
55
+ mode: 0o666
56
+ });
57
+ logger.info({
58
+ message: 'Wrote conditional manifest to ' + conditionalManifestFilename
59
+ });
57
60
  }
58
61
  }
62
+ }
63
+ var _default = exports.default = new (_plugin().Reporter)({
64
+ report
59
65
  });
package/lib/Config.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getConfig = getConfig;
7
+ function _path() {
8
+ const data = require("path");
9
+ _path = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ async function getConfig({
15
+ env,
16
+ inputFS,
17
+ projectRoot
18
+ }) {
19
+ const packageJson = JSON.parse(await inputFS.readFile((0, _path().join)(projectRoot, 'package.json'), 'utf8'));
20
+ const config = packageJson['@atlaspack/reporter-conditional-manifest'] ?? {};
21
+ for (const [key, value] of Object.entries(config)) {
22
+ // Replace values in the format of ${VARIABLE} with their corresponding env
23
+ if (typeof value === 'string') {
24
+ config[key] = value.replace(/\${([^}]+)}/g, (_, v) => env[v] ?? '');
25
+ }
26
+ }
27
+ const {
28
+ filename
29
+ } = config;
30
+ return {
31
+ filename: filename ?? 'conditional-manifest.json'
32
+ };
33
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/reporter-conditional-manifest",
3
- "version": "2.12.1-dev.3520+8a5346e28",
3
+ "version": "2.12.1-dev.3565+b31bc6b33",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -12,12 +12,12 @@
12
12
  "main": "lib/ConditionalManifestReporter.js",
13
13
  "source": "src/ConditionalManifestReporter.js",
14
14
  "engines": {
15
- "node": ">= 16.0.0",
16
- "parcel": "^2.12.1-dev.3520+8a5346e28"
15
+ "atlaspack": "^2.12.1-dev.3565+b31bc6b33",
16
+ "node": ">= 16.0.0"
17
17
  },
18
18
  "dependencies": {
19
- "@atlaspack/plugin": "2.12.1-dev.3520+8a5346e28",
19
+ "@atlaspack/plugin": "2.12.1-dev.3565+b31bc6b33",
20
20
  "nullthrows": "^1.1.1"
21
21
  },
22
- "gitHead": "8a5346e28c1bb3b9cd40f1c4e77c66dd6666f1e4"
22
+ "gitHead": "b31bc6b33de40c158b9f4d8ff177238be0de409d"
23
23
  }
@@ -1,62 +1,75 @@
1
1
  // @flow strict-local
2
2
  import {relative, join} from 'path';
3
3
  import {Reporter} from '@atlaspack/plugin';
4
+ import type {
5
+ Async,
6
+ PluginLogger,
7
+ PluginOptions,
8
+ PluginTracer,
9
+ ReporterEvent,
10
+ } from '@atlaspack/types-internal';
11
+ import {getConfig} from './Config';
4
12
 
5
- export default (new Reporter({
6
- async report({event, options, logger}) {
7
- if (event.type === 'buildSuccess') {
8
- const bundles = event.bundleGraph.getConditionalBundleMapping();
9
-
10
- // Replace bundles with file paths
11
- const mapBundles = (bundles) =>
12
- bundles.map((bundle) =>
13
- relative(bundle.target.distDir, bundle.filePath),
14
- );
15
-
16
- const manifest = {};
17
- for (const [bundle, conditions] of bundles.entries()) {
18
- const bundleInfo = {};
19
- for (const [key, cond] of conditions) {
20
- bundleInfo[key] = {
21
- ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(),
22
- ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse(),
23
- };
24
- }
25
-
26
- manifest[bundle.target.name] ??= {};
27
- manifest[bundle.target.name][
28
- relative(bundle.target.distDir, bundle.filePath)
29
- ] = bundleInfo;
13
+ async function report({
14
+ event,
15
+ options,
16
+ logger,
17
+ }: {|
18
+ event: ReporterEvent,
19
+ options: PluginOptions,
20
+ logger: PluginLogger,
21
+ tracer: PluginTracer,
22
+ |}): Async<void> {
23
+ if (event.type === 'buildSuccess') {
24
+ const bundles = event.bundleGraph.getConditionalBundleMapping();
25
+
26
+ // Replace bundles with file paths
27
+ const mapBundles = (bundles) =>
28
+ bundles.map((bundle) => relative(bundle.target.distDir, bundle.filePath));
29
+
30
+ const manifest = {};
31
+ for (const [bundle, conditions] of bundles.entries()) {
32
+ const bundleInfo = {};
33
+ for (const [key, cond] of conditions) {
34
+ bundleInfo[key] = {
35
+ // Reverse bundles so we load children bundles first
36
+ ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(),
37
+ ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse(),
38
+ };
30
39
  }
31
40
 
32
- // Error if there are multiple targets in the build
33
- const targets = new Set(
34
- event.bundleGraph.getBundles().map((bundle) => bundle.target),
41
+ manifest[bundle.target.name] ??= {};
42
+ manifest[bundle.target.name][
43
+ relative(bundle.target.distDir, bundle.filePath)
44
+ ] = bundleInfo;
45
+ }
46
+
47
+ const targets = new Set(
48
+ event.bundleGraph.getBundles().map((bundle) => bundle.target),
49
+ );
50
+
51
+ const {filename} = await getConfig(options);
52
+
53
+ for (const target of targets) {
54
+ const conditionalManifestFilename = join(target.distDir, filename);
55
+ const conditionalManifest = JSON.stringify(
56
+ // If there's no content, send an empty manifest so we can still map from it safely
57
+ manifest[target.name] ?? {},
58
+ null,
59
+ 2,
35
60
  );
36
61
 
37
- for (const target of targets) {
38
- const conditionalManifestFilename = join(
39
- target.distDir,
40
- 'conditional-manifest.json',
41
- );
42
-
43
- const conditionalManifest = JSON.stringify(
44
- manifest[target.name],
45
- null,
46
- 2,
47
- );
48
-
49
- await options.outputFS.writeFile(
50
- conditionalManifestFilename,
51
- conditionalManifest,
52
- {mode: 0o666},
53
- );
54
-
55
- logger.info({
56
- message:
57
- 'Wrote conditional manifest to ' + conditionalManifestFilename,
58
- });
59
- }
62
+ await options.outputFS.writeFile(
63
+ conditionalManifestFilename,
64
+ conditionalManifest,
65
+ {mode: 0o666},
66
+ );
67
+
68
+ logger.info({
69
+ message: 'Wrote conditional manifest to ' + conditionalManifestFilename,
70
+ });
60
71
  }
61
- },
62
- }): Reporter);
72
+ }
73
+ }
74
+
75
+ export default (new Reporter({report}): Reporter);
package/src/Config.js ADDED
@@ -0,0 +1,31 @@
1
+ // @flow strict-local
2
+ import {join} from 'path';
3
+ import type {PluginOptions} from '@atlaspack/types-internal';
4
+
5
+ type Config = {|
6
+ filename: string,
7
+ |};
8
+
9
+ export async function getConfig({
10
+ env,
11
+ inputFS,
12
+ projectRoot,
13
+ }: PluginOptions): Promise<Config> {
14
+ const packageJson = JSON.parse(
15
+ await inputFS.readFile(join(projectRoot, 'package.json'), 'utf8'),
16
+ );
17
+
18
+ const config = packageJson['@atlaspack/reporter-conditional-manifest'] ?? {};
19
+ for (const [key, value] of Object.entries(config)) {
20
+ // Replace values in the format of ${VARIABLE} with their corresponding env
21
+ if (typeof value === 'string') {
22
+ config[key] = value.replace(/\${([^}]+)}/g, (_, v) => env[v] ?? '');
23
+ }
24
+ }
25
+
26
+ const {filename} = config;
27
+
28
+ return {
29
+ filename: filename ?? 'conditional-manifest.json',
30
+ };
31
+ }
@@ -0,0 +1,67 @@
1
+ // @flow strict-local
2
+ import type {PluginOptions, EnvMap} from '@atlaspack/types-internal';
3
+
4
+ import {NodePackageManager} from '@atlaspack/package-manager';
5
+ import {getConfig} from '../src/Config';
6
+ import {overlayFS, fsFixture} from '@atlaspack/test-utils';
7
+ import assert from 'assert';
8
+ import {DEFAULT_FEATURE_FLAGS} from '@atlaspack/feature-flags';
9
+
10
+ function getPluginOptions({env}: {env?: EnvMap, ...}): PluginOptions {
11
+ return {
12
+ mode: 'development',
13
+ parcelVersion: 'version',
14
+ serveOptions: false,
15
+ shouldBuildLazily: false,
16
+ shouldAutoInstall: false,
17
+ logLevel: 'info',
18
+ cacheDir: '.parcel-cache/',
19
+ packageManager: new NodePackageManager(overlayFS, '/'),
20
+ instanceId: 'instance-id',
21
+ featureFlags: DEFAULT_FEATURE_FLAGS,
22
+ detailedReport: undefined,
23
+ hmrOptions: undefined,
24
+ inputFS: overlayFS,
25
+ outputFS: overlayFS,
26
+ projectRoot: '/project-root',
27
+ env: env ?? {},
28
+ };
29
+ }
30
+
31
+ describe('ConditionalManifestReporter', function () {
32
+ it('should load filename from config', async function () {
33
+ const pluginOptions = getPluginOptions({});
34
+ overlayFS.mkdirp(pluginOptions.projectRoot);
35
+
36
+ await fsFixture(overlayFS, pluginOptions.projectRoot)`
37
+ package.json:
38
+ {
39
+ "@atlaspack/reporter-conditional-manifest": {
40
+ "filename": "../some-other-dir/conditional.json"
41
+ }
42
+ }
43
+ `;
44
+
45
+ const {filename} = await getConfig(pluginOptions);
46
+
47
+ assert.equal(filename, '../some-other-dir/conditional.json');
48
+ });
49
+
50
+ it('should load filename from config with env vars', async function () {
51
+ const pluginOptions = getPluginOptions({env: {USER: 'some-user'}});
52
+ overlayFS.mkdirp(pluginOptions.projectRoot);
53
+
54
+ await fsFixture(overlayFS, pluginOptions.projectRoot)`
55
+ package.json:
56
+ {
57
+ "@atlaspack/reporter-conditional-manifest": {
58
+ "filename": "../\${USER}/conditional.json"
59
+ }
60
+ }
61
+ `;
62
+
63
+ const {filename} = await getConfig(pluginOptions);
64
+
65
+ assert.equal(filename, '../some-user/conditional.json');
66
+ });
67
+ });