@atlaspack/reporter-conditional-manifest 2.12.1-dev.3502 → 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,45 +18,39 @@ function _plugin() {
18
18
  };
19
19
  return data;
20
20
  }
21
- function _nullthrows() {
22
- const data = _interopRequireDefault(require("nullthrows"));
23
- _nullthrows = function () {
24
- return data;
25
- };
26
- return data;
27
- }
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
- var _default = exports.default = new (_plugin().Reporter)({
30
- async report({
31
- event,
32
- options,
33
- logger
34
- }) {
35
- if (event.type === 'buildSuccess') {
36
- const bundles = event.bundleGraph.getConditionalBundleMapping();
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();
37
29
 
38
- // Replace bundles with file paths
39
- const mapBundles = bundles => bundles.map(bundle => (0, _path().relative)(bundle.target.distDir, bundle.filePath));
40
- const manifest = {};
41
- for (const [bundle, conditions] of bundles.entries()) {
42
- const bundleInfo = {};
43
- for (const [key, cond] of conditions) {
44
- bundleInfo[key] = {
45
- ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(),
46
- ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse()
47
- };
48
- }
49
- manifest[(0, _path().relative)(bundle.target.distDir, bundle.filePath)] = bundleInfo;
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
+ };
50
41
  }
51
- const conditionalManifest = JSON.stringify(manifest, null, 2);
52
-
53
- // Error if there are multiple targets in the build
54
- const targets = new Set(event.bundleGraph.getBundles().map(bundle => bundle.target));
55
- if (targets.size > 1) {
56
- throw new Error('Conditional bundling does not support multiple targets');
57
- }
58
- const target = targets.values().next().value;
59
- const conditionalManifestFilename = (0, _path().join)((0, _nullthrows().default)(target === null || target === void 0 ? void 0 : target.distDir, 'distDir not found in target'), 'conditional-manifest.json');
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);
60
54
  await options.outputFS.writeFile(conditionalManifestFilename, conditionalManifest, {
61
55
  mode: 0o666
62
56
  });
@@ -65,4 +59,7 @@ var _default = exports.default = new (_plugin().Reporter)({
65
59
  });
66
60
  }
67
61
  }
62
+ }
63
+ var _default = exports.default = new (_plugin().Reporter)({
64
+ report
68
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.3502+c2daeab5a",
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.3502+c2daeab5a"
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.3502+c2daeab5a",
19
+ "@atlaspack/plugin": "2.12.1-dev.3565+b31bc6b33",
20
20
  "nullthrows": "^1.1.1"
21
21
  },
22
- "gitHead": "c2daeab5a12461903159dec34df5671eaaa9b749"
22
+ "gitHead": "b31bc6b33de40c158b9f4d8ff177238be0de409d"
23
23
  }
@@ -1,49 +1,64 @@
1
1
  // @flow strict-local
2
2
  import {relative, join} from 'path';
3
3
  import {Reporter} from '@atlaspack/plugin';
4
- import nullthrows from 'nullthrows';
5
-
6
- export default (new Reporter({
7
- async report({event, options, logger}) {
8
- if (event.type === 'buildSuccess') {
9
- const bundles = event.bundleGraph.getConditionalBundleMapping();
10
-
11
- // Replace bundles with file paths
12
- const mapBundles = (bundles) =>
13
- bundles.map((bundle) =>
14
- relative(bundle.target.distDir, bundle.filePath),
15
- );
16
-
17
- const manifest = {};
18
- for (const [bundle, conditions] of bundles.entries()) {
19
- const bundleInfo = {};
20
- for (const [key, cond] of conditions) {
21
- bundleInfo[key] = {
22
- ifTrueBundles: mapBundles(cond.ifTrueBundles).reverse(),
23
- ifFalseBundles: mapBundles(cond.ifFalseBundles).reverse(),
24
- };
25
- }
26
-
27
- manifest[relative(bundle.target.distDir, bundle.filePath)] = bundleInfo;
28
- }
4
+ import type {
5
+ Async,
6
+ PluginLogger,
7
+ PluginOptions,
8
+ PluginTracer,
9
+ ReporterEvent,
10
+ } from '@atlaspack/types-internal';
11
+ import {getConfig} from './Config';
29
12
 
30
- const conditionalManifest = JSON.stringify(manifest, null, 2);
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();
31
25
 
32
- // Error if there are multiple targets in the build
33
- const targets = new Set(
34
- event.bundleGraph.getBundles().map((bundle) => bundle.target),
35
- );
36
- if (targets.size > 1) {
37
- throw new Error(
38
- 'Conditional bundling does not support multiple targets',
39
- );
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
+ };
40
39
  }
41
40
 
42
- const target = targets.values().next().value;
43
- const conditionalManifestFilename = join(
44
- nullthrows(target?.distDir, 'distDir not found in target'),
45
- 'conditional-manifest.json',
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,
46
60
  );
61
+
47
62
  await options.outputFS.writeFile(
48
63
  conditionalManifestFilename,
49
64
  conditionalManifest,
@@ -54,5 +69,7 @@ export default (new Reporter({
54
69
  message: 'Wrote conditional manifest to ' + conditionalManifestFilename,
55
70
  });
56
71
  }
57
- },
58
- }): 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
+ });