@parcel/packager-js 2.0.0-beta.3 → 2.0.0-dev.1510

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.
@@ -4,137 +4,33 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.CJSOutputFormat = void 0;
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
7
  class CJSOutputFormat {
24
8
  constructor(packager) {
25
- _defineProperty(this, "packager", void 0);
26
-
27
9
  this.packager = packager;
28
10
  }
29
-
30
11
  buildBundlePrelude() {
31
12
  let res = '';
32
13
  let lines = 0;
33
-
34
14
  for (let [source, specifiers] of this.packager.externals) {
35
- let properties = [];
36
- let categories = new Set();
37
-
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`;
61
- 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`;
15
+ // CJS only supports the namespace symbol. This ensures that all accesses
16
+ // are live and the `this` binding is correct.
17
+ let namespace = specifiers.get('*');
18
+ if (namespace) {
19
+ res += `var ${namespace} = require(${JSON.stringify(source)});\n`;
80
20
  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
21
  } else {
86
22
  res += `require(${JSON.stringify(source)});\n`;
87
23
  lines++;
88
24
  }
89
25
  }
90
-
91
26
  if (res.length > 0) {
92
27
  res += '\n';
93
28
  lines++;
94
29
  }
95
-
96
30
  return [res, lines];
97
31
  }
98
-
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
32
  buildBundlePostlude() {
135
- return '';
33
+ return ['', 0];
136
34
  }
137
-
138
35
  }
139
-
140
36
  exports.CJSOutputFormat = CJSOutputFormat;
@@ -4,94 +4,64 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.DevPackager = void 0;
7
-
8
7
  function _utils() {
9
8
  const data = require("@parcel/utils");
10
-
11
9
  _utils = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _sourceMap() {
19
15
  const data = _interopRequireDefault(require("@parcel/source-map"));
20
-
21
16
  _sourceMap = function () {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _assert() {
29
22
  const data = _interopRequireDefault(require("assert"));
30
-
31
23
  _assert = function () {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _path() {
39
29
  const data = _interopRequireDefault(require("path"));
40
-
41
30
  _path = function () {
42
31
  return data;
43
32
  };
44
-
45
33
  return data;
46
34
  }
47
-
48
35
  function _fs() {
49
36
  const data = _interopRequireDefault(require("fs"));
50
-
51
37
  _fs = function () {
52
38
  return data;
53
39
  };
54
-
55
40
  return data;
56
41
  }
57
-
42
+ var _utils2 = require("./utils");
58
43
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
61
-
62
44
  const PRELUDE = _fs().default.readFileSync(_path().default.join(__dirname, 'dev-prelude.js'), 'utf8').trim().replace(/;$/, '');
63
-
64
45
  class DevPackager {
65
46
  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
47
  this.options = options;
75
48
  this.bundleGraph = bundleGraph;
76
49
  this.bundle = bundle;
77
50
  this.parcelRequireName = parcelRequireName;
78
51
  }
79
-
80
52
  async package() {
81
53
  // Load assets
82
54
  let queue = new (_utils().PromiseQueue)({
83
55
  maxConcurrent: 32
84
56
  });
85
- this.bundle.traverse(node => {
86
- if (node.type === 'asset') {
87
- queue.add(async () => {
88
- let [code, mapBuffer] = await Promise.all([node.value.getCode(), this.bundle.env.sourceMap && node.value.getMapBuffer()]);
89
- return {
90
- code,
91
- mapBuffer
92
- };
93
- });
94
- }
57
+ this.bundle.traverseAssets(asset => {
58
+ queue.add(async () => {
59
+ let [code, mapBuffer] = await Promise.all([asset.getCode(), this.bundle.env.sourceMap && asset.getMapBuffer()]);
60
+ return {
61
+ code,
62
+ mapBuffer
63
+ };
64
+ });
95
65
  });
96
66
  let results = await queue.run();
97
67
  let assets = '';
@@ -100,12 +70,11 @@ class DevPackager {
100
70
  let map = new (_sourceMap().default)(this.options.projectRoot);
101
71
  let prefix = this.getPrefix();
102
72
  let lineOffset = (0, _utils().countLines)(prefix);
73
+ let script = null;
103
74
  this.bundle.traverse(node => {
104
75
  let wrapped = first ? '' : ',';
105
-
106
76
  if (node.type === 'dependency') {
107
- let resolved = this.bundleGraph.getDependencyResolution(node.value, this.bundle);
108
-
77
+ let resolved = this.bundleGraph.getResolvedAsset(node.value, this.bundle);
109
78
  if (resolved && resolved.type !== 'js') {
110
79
  // if this is a reference to another javascript asset, we should not include
111
80
  // its output, as its contents should already be loaded.
@@ -115,21 +84,29 @@ class DevPackager {
115
84
  return;
116
85
  }
117
86
  }
118
-
119
87
  if (node.type === 'asset') {
120
88
  let asset = node.value;
121
89
  (0, _assert().default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
90
+
91
+ // If this is the main entry of a script rather than a module, we need to hoist it
92
+ // outside the bundle wrapper function so that its variables are exposed as globals.
93
+ if (this.bundle.env.sourceType === 'script' && asset === this.bundle.getMainEntry()) {
94
+ script = results[i++];
95
+ return;
96
+ }
122
97
  let deps = {};
123
98
  let dependencies = this.bundleGraph.getDependencies(asset);
124
-
125
99
  for (let dep of dependencies) {
126
- let resolved = this.bundleGraph.getDependencyResolution(dep, this.bundle);
127
-
128
- if (resolved) {
129
- deps[dep.moduleSpecifier] = this.bundleGraph.getAssetPublicId(resolved);
100
+ let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
101
+ if (this.bundleGraph.isDependencySkipped(dep)) {
102
+ deps[(0, _utils2.getSpecifier)(dep)] = false;
103
+ } else if (resolved) {
104
+ deps[(0, _utils2.getSpecifier)(dep)] = this.bundleGraph.getAssetPublicId(resolved);
105
+ } else {
106
+ // An external module - map placeholder to original specifier.
107
+ deps[(0, _utils2.getSpecifier)(dep)] = dep.specifier;
130
108
  }
131
109
  }
132
-
133
110
  let {
134
111
  code,
135
112
  mapBuffer
@@ -138,75 +115,78 @@ class DevPackager {
138
115
  wrapped += JSON.stringify(this.bundleGraph.getAssetPublicId(asset)) + ':[function(require,module,exports) {\n' + output + '\n},';
139
116
  wrapped += JSON.stringify(deps);
140
117
  wrapped += ']';
141
-
118
+ if (this.bundle.env.isNode() && asset.meta.has_node_replacements === true) {
119
+ const relPath = (0, _utils().normalizeSeparators)(_path().default.relative(this.bundle.target.distDir, _path().default.dirname(asset.filePath)));
120
+ wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
121
+ wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
122
+ }
142
123
  if (this.bundle.env.sourceMap) {
143
124
  if (mapBuffer) {
144
125
  map.addBuffer(mapBuffer, lineOffset);
145
126
  } else {
146
127
  map.addEmptyMap(_path().default.relative(this.options.projectRoot, asset.filePath).replace(/\\+/g, '/'), output, lineOffset);
147
128
  }
148
-
149
129
  lineOffset += (0, _utils().countLines)(output) + 1;
150
130
  }
151
-
152
131
  i++;
153
132
  }
154
-
155
133
  assets += wrapped;
156
134
  first = false;
157
135
  });
158
136
  let entries = this.bundle.getEntryAssets();
159
137
  let mainEntry = this.bundle.getMainEntry();
160
-
161
- if (!this.isEntry() && this.bundle.env.outputFormat === 'global') {
138
+ if (!this.isEntry() && this.bundle.env.outputFormat === 'global' || this.bundle.env.sourceType === 'script') {
162
139
  // In async bundles we don't want the main entry to execute until we require it
163
140
  // as there might be dependencies in a sibling bundle that hasn't loaded yet.
164
141
  entries = entries.filter(a => {
165
142
  var _mainEntry;
166
-
167
143
  return a.id !== ((_mainEntry = mainEntry) === null || _mainEntry === void 0 ? void 0 : _mainEntry.id);
168
144
  });
169
145
  mainEntry = null;
170
146
  }
171
-
172
147
  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';
148
+
149
+ // The entry asset of a script bundle gets hoisted outside the bundle wrapper function
150
+ // so that its variables become globals. We need to replace any require calls for
151
+ // runtimes with a parcelRequire call.
152
+ if (this.bundle.env.sourceType === 'script' && script) {
153
+ let entryMap;
154
+ let mapBuffer = script.mapBuffer;
155
+ if (mapBuffer) {
156
+ entryMap = new (_sourceMap().default)(this.options.projectRoot, mapBuffer);
157
+ }
158
+ contents += (0, _utils2.replaceScriptDependencies)(this.bundleGraph, this.bundle, script.code, entryMap, this.parcelRequireName);
159
+ if (this.bundle.env.sourceMap && entryMap) {
160
+ map.addSourceMap(entryMap, lineOffset);
161
+ }
162
+ }
173
163
  return {
174
164
  contents,
175
165
  map
176
166
  };
177
167
  }
178
-
179
168
  getPrefix() {
180
169
  let interpreter;
181
170
  let mainEntry = this.bundle.getMainEntry();
182
-
183
171
  if (mainEntry && this.isEntry() && !this.bundle.target.env.isBrowser()) {
184
172
  let _interpreter = mainEntry.meta.interpreter;
185
173
  (0, _assert().default)(_interpreter == null || typeof _interpreter === 'string');
186
174
  interpreter = _interpreter;
187
175
  }
188
-
189
176
  let importScripts = '';
190
-
191
177
  if (this.bundle.env.isWorker()) {
192
178
  let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
193
-
194
179
  for (let b of bundles) {
195
180
  importScripts += `importScripts("${(0, _utils().relativeBundlePath)(this.bundle, b)}");\n`;
196
181
  }
197
182
  }
198
-
199
- return (// If the entry asset included a hashbang, repeat it at the top of the bundle
183
+ return (
184
+ // If the entry asset included a hashbang, repeat it at the top of the bundle
200
185
  (interpreter != null ? `#!${interpreter}\n` : '') + importScripts + PRELUDE
201
186
  );
202
187
  }
203
-
204
188
  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);
189
+ return !this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') || this.bundle.env.isIsolated() || this.bundle.bundleBehavior === 'isolated';
208
190
  }
209
-
210
191
  }
211
-
212
192
  exports.DevPackager = DevPackager;
@@ -4,65 +4,49 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.ESMOutputFormat = void 0;
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
7
  class ESMOutputFormat {
11
8
  constructor(packager) {
12
- _defineProperty(this, "packager", void 0);
13
-
14
9
  this.packager = packager;
15
10
  }
16
-
17
11
  buildBundlePrelude() {
18
12
  let res = '';
19
13
  let lines = 0;
20
-
21
14
  for (let [source, specifiers] of this.packager.externals) {
22
15
  let defaultSpecifier = null;
23
16
  let namespaceSpecifier = null;
24
17
  let namedSpecifiers = [];
25
-
26
18
  for (let [imported, symbol] of specifiers) {
27
- if (imported === 'default'
28
- /* || isCommonJS*/
29
- ) {
30
- defaultSpecifier = symbol;
31
- } else if (imported === '*') {
19
+ if (imported === 'default' /* || isCommonJS*/) {
20
+ defaultSpecifier = symbol;
21
+ } else if (imported === '*') {
32
22
  namespaceSpecifier = `* as ${symbol}`;
33
23
  } else {
34
24
  let specifier = imported;
35
-
36
25
  if (symbol !== imported) {
37
26
  specifier += ` as ${symbol}`;
38
27
  }
39
-
40
28
  namedSpecifiers.push(specifier);
41
29
  }
42
- } // ESModule syntax allows combining default and namespace specifiers, or default and named, but not all three.
30
+ }
43
31
 
32
+ // ESModule syntax allows combining default and namespace specifiers, or default and named, but not all three.
44
33
 
45
34
  let imported = '';
46
-
47
35
  if (namespaceSpecifier) {
48
36
  let s = namespaceSpecifier;
49
-
50
37
  if (defaultSpecifier) {
51
38
  s = `${defaultSpecifier}, ${namespaceSpecifier}`;
52
39
  }
53
-
54
40
  res += `import ${s} from ${JSON.stringify(source)};\n`;
55
41
  lines++;
56
42
  } else if (defaultSpecifier) {
57
43
  imported = defaultSpecifier;
58
-
59
44
  if (namedSpecifiers.length > 0) {
60
45
  imported += `, {${namedSpecifiers.join(', ')}}`;
61
46
  }
62
47
  } else if (namedSpecifiers.length > 0) {
63
48
  imported = `{${namedSpecifiers.join(', ')}}`;
64
49
  }
65
-
66
50
  if (imported.length > 0) {
67
51
  res += `import ${imported} from ${JSON.stringify(source)};\n`;
68
52
  lines++;
@@ -71,41 +55,45 @@ class ESMOutputFormat {
71
55
  lines++;
72
56
  }
73
57
  }
74
-
75
58
  if (res.length > 0) {
76
59
  res += '\n';
77
60
  lines++;
78
61
  }
79
-
80
62
  return [res, lines];
81
63
  }
82
-
83
64
  buildBundlePostlude() {
84
65
  let res = '';
66
+ let lines = 0;
85
67
  let exportSpecifiers = [];
86
-
87
- for (let exported of this.packager.exportedSymbols.values()) {
88
- for (let {
89
- exportAs,
90
- local
91
- } of exported) {
68
+ for (let {
69
+ asset,
70
+ exportSymbol,
71
+ local,
72
+ exportAs
73
+ } of this.packager.exportedSymbols.values()) {
74
+ if (this.packager.wrappedAssets.has(asset.id)) {
75
+ let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
76
+ res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
77
+ lines++;
78
+ }
79
+ for (let as of exportAs) {
92
80
  let specifier = local;
93
-
94
81
  if (exportAs !== local) {
95
- specifier += ` as ${exportAs}`;
82
+ specifier += ` as ${as}`;
96
83
  }
97
-
98
84
  exportSpecifiers.push(specifier);
99
85
  }
100
86
  }
101
-
102
87
  if (exportSpecifiers.length > 0) {
103
88
  res += `\nexport {${exportSpecifiers.join(', ')}};`;
89
+ lines++;
104
90
  }
105
-
106
- return res;
91
+ if (this.packager.needsPrelude && this.packager.shouldBundleQueue(this.packager.bundle)) {
92
+ // Should be last thing the bundle executes on intial eval
93
+ res += `\n$parcel$global.rlb(${JSON.stringify(this.packager.bundle.publicId)})`;
94
+ lines++;
95
+ }
96
+ return [res, lines];
107
97
  }
108
-
109
98
  }
110
-
111
99
  exports.ESMOutputFormat = ESMOutputFormat;
@@ -4,24 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.GlobalOutputFormat = void 0;
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
7
  class GlobalOutputFormat {
11
8
  constructor(packager) {
12
- _defineProperty(this, "packager", void 0);
13
-
14
9
  this.packager = packager;
15
10
  }
16
-
17
11
  buildBundlePrelude() {
18
- return ['(function () {\n', 1];
12
+ let prelude = this.packager.bundle.env.supports('arrow-functions', true) ? '(() => {\n' : '(function () {\n';
13
+ return [prelude, 1];
19
14
  }
20
-
21
15
  buildBundlePostlude() {
22
- return '})();';
16
+ return ['})();', 0];
23
17
  }
24
-
25
18
  }
26
-
27
19
  exports.GlobalOutputFormat = GlobalOutputFormat;