@atlaspack/bundler-default 2.12.1-dev.3478 → 2.12.1-dev.3520

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.
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.addJSMonolithBundle = addJSMonolithBundle;
7
+ function _nullthrows() {
8
+ const data = _interopRequireDefault(require("nullthrows"));
9
+ _nullthrows = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ function addJSMonolithBundle(bundleGraph, entryAsset, entryDep) {
16
+ let target = (0, _nullthrows().default)(entryDep.target, 'Expected dependency to have a valid target');
17
+
18
+ // Create a single bundle to hold all JS assets
19
+ let bundle = bundleGraph.createBundle({
20
+ entryAsset,
21
+ target
22
+ });
23
+ bundleGraph.traverse(node => {
24
+ // JS assets can be added to the bundle, but the rest are ignored
25
+ if (node.type === 'asset' && node.value.type === 'js') {
26
+ bundleGraph.addAssetToBundle(node.value, bundle);
27
+ return;
28
+ }
29
+ if (node.type === 'dependency' && node.value.priority === 'lazy') {
30
+ // Any async dependencies need to be internalized into the bundle, and will
31
+ // be included by the asset check above
32
+ bundleGraph.internalizeAsyncDependency(bundle, node.value);
33
+ }
34
+ }, entryAsset, {
35
+ skipUnusedDependencies: true
36
+ });
37
+ let bundleGroup = bundleGraph.createBundleGroup(entryDep, target);
38
+ bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
39
+ }
@@ -0,0 +1,178 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.loadBundlerConfig = loadBundlerConfig;
7
+ function _diagnostic() {
8
+ const data = require("@atlaspack/diagnostic");
9
+ _diagnostic = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _utils() {
15
+ const data = require("@atlaspack/utils");
16
+ _utils = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _assert() {
22
+ const data = _interopRequireDefault(require("assert"));
23
+ _assert = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
+ function resolveModeConfig(config, mode) {
30
+ let generalConfig = {};
31
+ let modeConfig = {};
32
+ for (const key of Object.keys(config)) {
33
+ if (key === 'development' || key === 'production') {
34
+ if (key === mode) {
35
+ modeConfig = config[key];
36
+ }
37
+ } else {
38
+ generalConfig[key] = config[key];
39
+ }
40
+ }
41
+
42
+ // $FlowFixMe Not sure how to convince flow here...
43
+ return {
44
+ ...generalConfig,
45
+ ...modeConfig
46
+ };
47
+ }
48
+
49
+ // Default options by http version.
50
+ const HTTP_OPTIONS = {
51
+ '1': {
52
+ minBundles: 1,
53
+ manualSharedBundles: [],
54
+ minBundleSize: 30000,
55
+ maxParallelRequests: 6,
56
+ disableSharedBundles: false
57
+ },
58
+ '2': {
59
+ minBundles: 1,
60
+ manualSharedBundles: [],
61
+ minBundleSize: 20000,
62
+ maxParallelRequests: 25,
63
+ disableSharedBundles: false
64
+ }
65
+ };
66
+ const CONFIG_SCHEMA = {
67
+ type: 'object',
68
+ properties: {
69
+ http: {
70
+ type: 'number',
71
+ enum: Object.keys(HTTP_OPTIONS).map(k => Number(k))
72
+ },
73
+ manualSharedBundles: {
74
+ type: 'array',
75
+ items: {
76
+ type: 'object',
77
+ properties: {
78
+ name: {
79
+ type: 'string'
80
+ },
81
+ assets: {
82
+ type: 'array',
83
+ items: {
84
+ type: 'string'
85
+ }
86
+ },
87
+ types: {
88
+ type: 'array',
89
+ items: {
90
+ type: 'string'
91
+ }
92
+ },
93
+ root: {
94
+ type: 'string'
95
+ },
96
+ split: {
97
+ type: 'number'
98
+ }
99
+ },
100
+ required: ['name', 'assets'],
101
+ additionalProperties: false
102
+ }
103
+ },
104
+ minBundles: {
105
+ type: 'number'
106
+ },
107
+ minBundleSize: {
108
+ type: 'number'
109
+ },
110
+ maxParallelRequests: {
111
+ type: 'number'
112
+ },
113
+ disableSharedBundles: {
114
+ type: 'boolean'
115
+ }
116
+ },
117
+ additionalProperties: false
118
+ };
119
+ async function loadBundlerConfig(config, options, logger) {
120
+ let conf = await config.getConfig([], {
121
+ packageKey: '@atlaspack/bundler-default'
122
+ });
123
+ if (!conf) {
124
+ const modDefault = {
125
+ ...HTTP_OPTIONS['2'],
126
+ projectRoot: options.projectRoot
127
+ };
128
+ return modDefault;
129
+ }
130
+ (0, _assert().default)((conf === null || conf === void 0 ? void 0 : conf.contents) != null);
131
+ let modeConfig = resolveModeConfig(conf.contents, options.mode);
132
+
133
+ // minBundles will be ignored if shared bundles are disabled
134
+ if (modeConfig.minBundles != null && modeConfig.disableSharedBundles === true) {
135
+ logger.warn({
136
+ origin: '@atlaspack/bundler-default',
137
+ message: `The value of "${modeConfig.minBundles}" set for minBundles will not be used as shared bundles have been disabled`
138
+ });
139
+ }
140
+
141
+ // minBundleSize will be ignored if shared bundles are disabled
142
+ if (modeConfig.minBundleSize != null && modeConfig.disableSharedBundles === true) {
143
+ logger.warn({
144
+ origin: '@atlaspack/bundler-default',
145
+ message: `The value of "${modeConfig.minBundleSize}" set for minBundleSize will not be used as shared bundles have been disabled`
146
+ });
147
+ }
148
+
149
+ // maxParallelRequests will be ignored if shared bundles are disabled
150
+ if (modeConfig.maxParallelRequests != null && modeConfig.disableSharedBundles === true) {
151
+ logger.warn({
152
+ origin: '@atlaspack/bundler-default',
153
+ message: `The value of "${modeConfig.maxParallelRequests}" set for maxParallelRequests will not be used as shared bundles have been disabled`
154
+ });
155
+ }
156
+ if (modeConfig.manualSharedBundles) {
157
+ let nameArray = modeConfig.manualSharedBundles.map(a => a.name);
158
+ let nameSet = new Set(nameArray);
159
+ (0, _assert().default)(nameSet.size == nameArray.length, 'The name field must be unique for property manualSharedBundles');
160
+ }
161
+ _utils().validateSchema.diagnostic(CONFIG_SCHEMA, {
162
+ data: modeConfig,
163
+ source: await options.inputFS.readFile(conf.filePath, 'utf8'),
164
+ filePath: conf.filePath,
165
+ prependKey: `/${(0, _diagnostic().encodeJSONKeyComponent)('@atlaspack/bundler-default')}`
166
+ }, '@atlaspack/bundler-default', 'Invalid config for @atlaspack/bundler-default');
167
+ let http = modeConfig.http ?? 2;
168
+ let defaults = HTTP_OPTIONS[http];
169
+ return {
170
+ minBundles: modeConfig.minBundles ?? defaults.minBundles,
171
+ minBundleSize: modeConfig.minBundleSize ?? defaults.minBundleSize,
172
+ maxParallelRequests: modeConfig.maxParallelRequests ?? defaults.maxParallelRequests,
173
+ projectRoot: options.projectRoot,
174
+ disableSharedBundles: modeConfig.disableSharedBundles ?? defaults.disableSharedBundles,
175
+ manualSharedBundles: modeConfig.manualSharedBundles ?? defaults.manualSharedBundles,
176
+ loadConditionalBundlesInParallel: modeConfig.loadConditionalBundlesInParallel
177
+ };
178
+ }
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.decorateLegacyGraph = decorateLegacyGraph;
7
+ function _graph() {
8
+ const data = require("@atlaspack/graph");
9
+ _graph = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _assert() {
15
+ const data = _interopRequireDefault(require("assert"));
16
+ _assert = function () {
17
+ return data;
18
+ };
19
+ return data;
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
+ function decorateLegacyGraph(idealGraph, bundleGraph) {
30
+ let idealBundleToLegacyBundle = new Map();
31
+ let {
32
+ bundleGraph: idealBundleGraph,
33
+ dependencyBundleGraph,
34
+ bundleGroupBundleIds,
35
+ manualAssetToBundle
36
+ } = idealGraph;
37
+ let entryBundleToBundleGroup = new Map();
38
+ // Step Create Bundles: Create bundle groups, bundles, and shared bundles and add assets to them
39
+ for (let [bundleNodeId, idealBundle] of idealBundleGraph.nodes.entries()) {
40
+ if (!idealBundle || idealBundle === 'root') continue;
41
+ let entryAsset = idealBundle.mainEntryAsset;
42
+ let bundleGroup;
43
+ let bundle;
44
+ if (bundleGroupBundleIds.has(bundleNodeId)) {
45
+ (0, _assert().default)(idealBundle.manualSharedBundle == null, 'Processing a manualSharedBundle as a BundleGroup');
46
+ let dependencies = dependencyBundleGraph.getNodeIdsConnectedTo(dependencyBundleGraph.getNodeIdByContentKey(String(bundleNodeId)), _graph().ALL_EDGE_TYPES).map(nodeId => {
47
+ let dependency = (0, _nullthrows().default)(dependencyBundleGraph.getNode(nodeId));
48
+ (0, _assert().default)(dependency.type === 'dependency');
49
+ return dependency.value;
50
+ });
51
+ (0, _assert().default)(entryAsset != null, 'Processing a bundleGroup with no entry asset');
52
+ for (let dependency of dependencies) {
53
+ bundleGroup = bundleGraph.createBundleGroup(dependency, idealBundle.target);
54
+ }
55
+ (0, _assert().default)(bundleGroup);
56
+ entryBundleToBundleGroup.set(bundleNodeId, bundleGroup);
57
+ bundle = (0, _nullthrows().default)(bundleGraph.createBundle({
58
+ entryAsset: (0, _nullthrows().default)(entryAsset),
59
+ needsStableName: idealBundle.needsStableName,
60
+ bundleBehavior: idealBundle.bundleBehavior,
61
+ target: idealBundle.target,
62
+ manualSharedBundle: idealBundle.manualSharedBundle
63
+ }));
64
+ bundleGraph.addBundleToBundleGroup(bundle, bundleGroup);
65
+ } else if (idealBundle.sourceBundles.size > 0 && !idealBundle.mainEntryAsset) {
66
+ let uniqueKey = idealBundle.uniqueKey != null ? idealBundle.uniqueKey : [...idealBundle.assets].map(asset => asset.id).join(',');
67
+ bundle = (0, _nullthrows().default)(bundleGraph.createBundle({
68
+ uniqueKey,
69
+ needsStableName: idealBundle.needsStableName,
70
+ bundleBehavior: idealBundle.bundleBehavior,
71
+ type: idealBundle.type,
72
+ target: idealBundle.target,
73
+ env: idealBundle.env,
74
+ manualSharedBundle: idealBundle.manualSharedBundle
75
+ }));
76
+ } else if (idealBundle.uniqueKey != null) {
77
+ bundle = (0, _nullthrows().default)(bundleGraph.createBundle({
78
+ uniqueKey: idealBundle.uniqueKey,
79
+ needsStableName: idealBundle.needsStableName,
80
+ bundleBehavior: idealBundle.bundleBehavior,
81
+ type: idealBundle.type,
82
+ target: idealBundle.target,
83
+ env: idealBundle.env,
84
+ manualSharedBundle: idealBundle.manualSharedBundle
85
+ }));
86
+ } else {
87
+ (0, _assert().default)(entryAsset != null);
88
+ bundle = (0, _nullthrows().default)(bundleGraph.createBundle({
89
+ entryAsset,
90
+ needsStableName: idealBundle.needsStableName,
91
+ bundleBehavior: idealBundle.bundleBehavior,
92
+ target: idealBundle.target,
93
+ manualSharedBundle: idealBundle.manualSharedBundle
94
+ }));
95
+ }
96
+ idealBundleToLegacyBundle.set(idealBundle, bundle);
97
+ for (let asset of idealBundle.assets) {
98
+ bundleGraph.addAssetToBundle(asset, bundle);
99
+ }
100
+ }
101
+ // Step Internalization: Internalize dependencies for bundles
102
+ for (let idealBundle of idealBundleGraph.nodes) {
103
+ if (!idealBundle || idealBundle === 'root') continue;
104
+ let bundle = (0, _nullthrows().default)(idealBundleToLegacyBundle.get(idealBundle));
105
+ if (idealBundle.internalizedAssets) {
106
+ idealBundle.internalizedAssets.forEach(internalized => {
107
+ let incomingDeps = bundleGraph.getIncomingDependencies(idealGraph.assets[internalized]);
108
+ for (let incomingDep of incomingDeps) {
109
+ if (incomingDep.priority === 'lazy' && incomingDep.specifierType !== 'url' && bundle.hasDependency(incomingDep)) {
110
+ bundleGraph.internalizeAsyncDependency(bundle, incomingDep);
111
+ }
112
+ }
113
+ });
114
+ }
115
+ }
116
+ // Manual Shared Bundles
117
+ // NOTE: This only works under the assumption that manual shared bundles would have
118
+ // always already been loaded before the bundle that requires internalization.
119
+ for (let manualSharedAsset of manualAssetToBundle.keys()) {
120
+ let incomingDeps = bundleGraph.getIncomingDependencies(manualSharedAsset);
121
+ for (let incomingDep of incomingDeps) {
122
+ if (incomingDep.priority === 'lazy' && incomingDep.specifierType !== 'url') {
123
+ let bundles = bundleGraph.getBundlesWithDependency(incomingDep);
124
+ for (let bundle of bundles) {
125
+ bundleGraph.internalizeAsyncDependency(bundle, incomingDep);
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ // Step Add to BundleGroups: Add bundles to their bundle groups
132
+ idealBundleGraph.traverse((nodeId, _, actions) => {
133
+ let node = idealBundleGraph.getNode(nodeId);
134
+ if (node === 'root') {
135
+ return;
136
+ }
137
+ actions.skipChildren();
138
+ let outboundNodeIds = idealBundleGraph.getNodeIdsConnectedFrom(nodeId);
139
+ let entryBundle = (0, _nullthrows().default)(idealBundleGraph.getNode(nodeId));
140
+ (0, _assert().default)(entryBundle !== 'root');
141
+ let legacyEntryBundle = (0, _nullthrows().default)(idealBundleToLegacyBundle.get(entryBundle));
142
+ for (let id of outboundNodeIds) {
143
+ let siblingBundle = (0, _nullthrows().default)(idealBundleGraph.getNode(id));
144
+ (0, _assert().default)(siblingBundle !== 'root');
145
+ let legacySiblingBundle = (0, _nullthrows().default)(idealBundleToLegacyBundle.get(siblingBundle));
146
+ bundleGraph.createBundleReference(legacyEntryBundle, legacySiblingBundle);
147
+ }
148
+ });
149
+
150
+ // Step References: Add references to all bundles
151
+ for (let [asset, references] of idealGraph.assetReference) {
152
+ for (let [dependency, bundle] of references) {
153
+ let legacyBundle = (0, _nullthrows().default)(idealBundleToLegacyBundle.get(bundle));
154
+ bundleGraph.createAssetReference(dependency, asset, legacyBundle);
155
+ }
156
+ }
157
+ for (let {
158
+ from,
159
+ to
160
+ } of idealBundleGraph.getAllEdges()) {
161
+ let sourceBundle = (0, _nullthrows().default)(idealBundleGraph.getNode(from));
162
+ if (sourceBundle === 'root') {
163
+ continue;
164
+ }
165
+ (0, _assert().default)(sourceBundle !== 'root');
166
+ let legacySourceBundle = (0, _nullthrows().default)(idealBundleToLegacyBundle.get(sourceBundle));
167
+ let targetBundle = (0, _nullthrows().default)(idealBundleGraph.getNode(to));
168
+ if (targetBundle === 'root') {
169
+ continue;
170
+ }
171
+ (0, _assert().default)(targetBundle !== 'root');
172
+ let legacyTargetBundle = (0, _nullthrows().default)(idealBundleToLegacyBundle.get(targetBundle));
173
+ bundleGraph.createBundleReference(legacySourceBundle, legacyTargetBundle);
174
+ }
175
+ }