@parcel/packager-js 2.0.0-beta.3.1 → 2.0.0-nightly.1006

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.
@@ -5,25 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.CJSOutputFormat = void 0;
7
7
 
8
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
-
10
- // List of engines that support object destructuring syntax
11
- const DESTRUCTURING_ENGINES = {
12
- chrome: '51',
13
- edge: '15',
14
- firefox: '53',
15
- safari: '10',
16
- node: '6.5',
17
- ios: '10',
18
- samsung: '5',
19
- opera: '38',
20
- electron: '1.2'
21
- };
22
-
23
8
  class CJSOutputFormat {
24
9
  constructor(packager) {
25
- _defineProperty(this, "packager", void 0);
26
-
27
10
  this.packager = packager;
28
11
  }
29
12
 
@@ -32,56 +15,13 @@ class CJSOutputFormat {
32
15
  let lines = 0;
33
16
 
34
17
  for (let [source, specifiers] of this.packager.externals) {
35
- let properties = [];
36
- let categories = new Set();
18
+ // CJS only supports the namespace symbol. This ensures that all accesses
19
+ // are live and the `this` binding is correct.
20
+ let namespace = specifiers.get('*');
37
21
 
38
- for (let [imported, symbol] of specifiers) {
39
- if (imported === '*') {
40
- categories.add('namespace');
41
- } else if (imported === 'default') {
42
- categories.add('default');
43
- } else {
44
- categories.add('named');
45
- properties.push({
46
- key: imported,
47
- value: symbol
48
- });
49
- }
50
- }
51
-
52
- let specifiersWildcard = specifiers.get('*');
53
- let specifiersDefault = specifiers.get('default'); // Attempt to combine require calls as much as possible. Namespace, default, and named specifiers
54
- // cannot be combined, so in the case where we have more than one type, assign the require() result
55
- // to a variable first and then create additional variables for each specifier based on that.
56
- // Otherwise, if just one category is imported, just assign and require all at once.
57
-
58
- if (categories.size > 1) {
59
- let name = specifiersWildcard || this.packager.getTopLevelName(source);
60
- res += `var ${name} = require(${JSON.stringify(source)});\n`;
22
+ if (namespace) {
23
+ res += `var ${namespace} = require(${JSON.stringify(source)});\n`;
61
24
  lines++;
62
-
63
- if (specifiersDefault) {
64
- res += `var ${specifiersDefault} = $parcel$interopDefault(${name});\n`;
65
- lines++;
66
- this.packager.usedHelpers.add('$parcel$interopDefault');
67
- }
68
-
69
- if (properties.length > 0) {
70
- let [r, l] = this.generateDestructuringAssignment(properties, name, true);
71
- res += r;
72
- lines += l;
73
- }
74
- } else if (specifiersDefault) {
75
- res += `var ${specifiersDefault} = $parcel$interopDefault(require(${JSON.stringify(source)}));\n`;
76
- lines++;
77
- this.packager.usedHelpers.add('$parcel$interopDefault');
78
- } else if (specifiersWildcard) {
79
- res += `var ${specifiersWildcard} = require(${JSON.stringify(source)});\n`;
80
- lines++;
81
- } else if (properties.length > 0) {
82
- let [r, l] = this.generateDestructuringAssignment(properties, `require(${JSON.stringify(source)})`, false);
83
- res += r;
84
- lines += l;
85
25
  } else {
86
26
  res += `require(${JSON.stringify(source)});\n`;
87
27
  lines++;
@@ -96,43 +36,8 @@ class CJSOutputFormat {
96
36
  return [res, lines];
97
37
  }
98
38
 
99
- generateDestructuringAssignment(specifiers, value, isIdentifier) {
100
- let res = '';
101
- let lines = 0; // If destructuring is not supported, generate a series of variable declarations
102
- // with member expressions for each property.
103
-
104
- if (!this.packager.bundle.env.matchesEngines(DESTRUCTURING_ENGINES)) {
105
- if (!isIdentifier && specifiers.length > 1) {
106
- let name = this.packager.getTopLevelName('temp');
107
- res += `var ${name} = ${value};\n`;
108
- lines++;
109
- value = name;
110
- }
111
-
112
- for (let specifier of specifiers) {
113
- res += `var ${specifier.value} = ${value}.${specifier.key};\n`;
114
- lines++;
115
- }
116
-
117
- return [res, lines];
118
- }
119
-
120
- let s = specifiers.map(specifier => {
121
- let s = specifier.key;
122
-
123
- if (specifier.value !== specifier.key) {
124
- s += `: ${specifier.value}`;
125
- }
126
-
127
- return s;
128
- });
129
- res += `var {${s.join(', ')}} = ${value};\n`;
130
- lines++;
131
- return [res, lines];
132
- }
133
-
134
39
  buildBundlePostlude() {
135
- return '';
40
+ return ['', 0];
136
41
  }
137
42
 
138
43
  }
@@ -55,22 +55,14 @@ function _fs() {
55
55
  return data;
56
56
  }
57
57
 
58
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
58
+ var _utils2 = require("./utils");
59
59
 
60
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
60
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
61
61
 
62
62
  const PRELUDE = _fs().default.readFileSync(_path().default.join(__dirname, 'dev-prelude.js'), 'utf8').trim().replace(/;$/, '');
63
63
 
64
64
  class DevPackager {
65
65
  constructor(options, bundleGraph, bundle, parcelRequireName) {
66
- _defineProperty(this, "options", void 0);
67
-
68
- _defineProperty(this, "bundleGraph", void 0);
69
-
70
- _defineProperty(this, "bundle", void 0);
71
-
72
- _defineProperty(this, "parcelRequireName", void 0);
73
-
74
66
  this.options = options;
75
67
  this.bundleGraph = bundleGraph;
76
68
  this.bundle = bundle;
@@ -100,11 +92,12 @@ class DevPackager {
100
92
  let map = new (_sourceMap().default)(this.options.projectRoot);
101
93
  let prefix = this.getPrefix();
102
94
  let lineOffset = (0, _utils().countLines)(prefix);
95
+ let script = null;
103
96
  this.bundle.traverse(node => {
104
97
  let wrapped = first ? '' : ',';
105
98
 
106
99
  if (node.type === 'dependency') {
107
- let resolved = this.bundleGraph.getDependencyResolution(node.value, this.bundle);
100
+ let resolved = this.bundleGraph.getResolvedAsset(node.value, this.bundle);
108
101
 
109
102
  if (resolved && resolved.type !== 'js') {
110
103
  // if this is a reference to another javascript asset, we should not include
@@ -118,15 +111,24 @@ class DevPackager {
118
111
 
119
112
  if (node.type === 'asset') {
120
113
  let asset = node.value;
121
- (0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
114
+ (0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets'); // If this is the main entry of a script rather than a module, we need to hoist it
115
+ // outside the bundle wrapper function so that its variables are exposed as globals.
116
+
117
+ if (this.bundle.env.sourceType === 'script' && asset === this.bundle.getMainEntry()) {
118
+ script = results[i++];
119
+ return;
120
+ }
121
+
122
122
  let deps = {};
123
123
  let dependencies = this.bundleGraph.getDependencies(asset);
124
124
 
125
125
  for (let dep of dependencies) {
126
- let resolved = this.bundleGraph.getDependencyResolution(dep, this.bundle);
126
+ let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
127
127
 
128
- if (resolved) {
129
- deps[dep.moduleSpecifier] = this.bundleGraph.getAssetPublicId(resolved);
128
+ if (this.bundleGraph.isDependencySkipped(dep)) {
129
+ deps[(0, _utils2.getSpecifier)(dep)] = false;
130
+ } else if (resolved) {
131
+ deps[(0, _utils2.getSpecifier)(dep)] = this.bundleGraph.getAssetPublicId(resolved);
130
132
  }
131
133
  }
132
134
 
@@ -158,7 +160,7 @@ class DevPackager {
158
160
  let entries = this.bundle.getEntryAssets();
159
161
  let mainEntry = this.bundle.getMainEntry();
160
162
 
161
- if (!this.isEntry() && this.bundle.env.outputFormat === 'global') {
163
+ if (!this.isEntry() && this.bundle.env.outputFormat === 'global' || this.bundle.env.sourceType === 'script') {
162
164
  // In async bundles we don't want the main entry to execute until we require it
163
165
  // as there might be dependencies in a sibling bundle that hasn't loaded yet.
164
166
  entries = entries.filter(a => {
@@ -169,7 +171,25 @@ class DevPackager {
169
171
  mainEntry = null;
170
172
  }
171
173
 
172
- let contents = prefix + '({' + assets + '},' + JSON.stringify(entries.map(asset => this.bundleGraph.getAssetPublicId(asset))) + ', ' + JSON.stringify(mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null) + ', ' + JSON.stringify(this.parcelRequireName) + ')' + '\n';
174
+ let contents = prefix + '({' + assets + '},' + JSON.stringify(entries.map(asset => this.bundleGraph.getAssetPublicId(asset))) + ', ' + JSON.stringify(mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null) + ', ' + JSON.stringify(this.parcelRequireName) + ')' + '\n'; // The entry asset of a script bundle gets hoisted outside the bundle wrapper function
175
+ // so that its variables become globals. We need to replace any require calls for
176
+ // runtimes with a parcelRequire call.
177
+
178
+ if (this.bundle.env.sourceType === 'script' && script) {
179
+ let entryMap;
180
+ let mapBuffer = script.mapBuffer;
181
+
182
+ if (mapBuffer) {
183
+ entryMap = new (_sourceMap().default)(this.options.projectRoot, mapBuffer);
184
+ }
185
+
186
+ contents += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, script.code, entryMap, this.parcelRequireName);
187
+
188
+ if (this.bundle.env.sourceMap && entryMap) {
189
+ map.addSourceMap(entryMap, lineOffset);
190
+ }
191
+ }
192
+
173
193
  return {
174
194
  contents,
175
195
  map
@@ -202,9 +222,7 @@ class DevPackager {
202
222
  }
203
223
 
204
224
  isEntry() {
205
- var _this$bundle$getMainE;
206
-
207
- return !this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') || this.bundle.env.isIsolated() || !!((_this$bundle$getMainE = this.bundle.getMainEntry()) !== null && _this$bundle$getMainE !== void 0 && _this$bundle$getMainE.isIsolated);
225
+ return !this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated';
208
226
  }
209
227
 
210
228
  }
@@ -5,12 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.ESMOutputFormat = void 0;
7
7
 
8
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
-
10
8
  class ESMOutputFormat {
11
9
  constructor(packager) {
12
- _defineProperty(this, "packager", void 0);
13
-
14
10
  this.packager = packager;
15
11
  }
16
12
 
@@ -27,8 +23,8 @@ class ESMOutputFormat {
27
23
  if (imported === 'default'
28
24
  /* || isCommonJS*/
29
25
  ) {
30
- defaultSpecifier = symbol;
31
- } else if (imported === '*') {
26
+ defaultSpecifier = symbol;
27
+ } else if (imported === '*') {
32
28
  namespaceSpecifier = `* as ${symbol}`;
33
29
  } else {
34
30
  let specifier = imported;
@@ -82,17 +78,26 @@ class ESMOutputFormat {
82
78
 
83
79
  buildBundlePostlude() {
84
80
  let res = '';
81
+ let lines = 0;
85
82
  let exportSpecifiers = [];
86
83
 
87
- for (let exported of this.packager.exportedSymbols.values()) {
88
- for (let {
89
- exportAs,
90
- local
91
- } of exported) {
84
+ for (let {
85
+ asset,
86
+ exportSymbol,
87
+ local,
88
+ exportAs
89
+ } of this.packager.exportedSymbols.values()) {
90
+ if (this.packager.wrappedAssets.has(asset.id)) {
91
+ let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
92
+ res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
93
+ lines++;
94
+ }
95
+
96
+ for (let as of exportAs) {
92
97
  let specifier = local;
93
98
 
94
99
  if (exportAs !== local) {
95
- specifier += ` as ${exportAs}`;
100
+ specifier += ` as ${as}`;
96
101
  }
97
102
 
98
103
  exportSpecifiers.push(specifier);
@@ -101,9 +106,10 @@ class ESMOutputFormat {
101
106
 
102
107
  if (exportSpecifiers.length > 0) {
103
108
  res += `\nexport {${exportSpecifiers.join(', ')}};`;
109
+ lines++;
104
110
  }
105
111
 
106
- return res;
112
+ return [res, lines];
107
113
  }
108
114
 
109
115
  }
@@ -5,21 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.GlobalOutputFormat = void 0;
7
7
 
8
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
9
-
10
8
  class GlobalOutputFormat {
11
9
  constructor(packager) {
12
- _defineProperty(this, "packager", void 0);
13
-
14
10
  this.packager = packager;
15
11
  }
16
12
 
17
13
  buildBundlePrelude() {
18
- return ['(function () {\n', 1];
14
+ let prelude = this.packager.bundle.env.supports('arrow-functions', true) ? '(() => {\n' : '(function () {\n';
15
+ return [prelude, 1];
19
16
  }
20
17
 
21
18
  buildBundlePostlude() {
22
- return '})();';
19
+ return ['})();', 0];
23
20
  }
24
21
 
25
22
  }