@parcel/packager-js 2.0.0-nightly.133 → 2.0.0-nightly.1332

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.
@@ -6,7 +6,7 @@
6
6
  // anything defined in a previous bundle is accessed via the
7
7
  // orig method which is the require for previous bundles
8
8
 
9
- (function(modules, cache, entry, globalName) {
9
+ (function (modules, entry, mainEntry, parcelRequireName, globalName) {
10
10
  /* eslint-disable no-undef */
11
11
  var globalObject =
12
12
  typeof globalThis !== 'undefined'
@@ -22,8 +22,10 @@
22
22
 
23
23
  // Save the require from previous bundle to this closure if any
24
24
  var previousRequire =
25
- typeof globalObject.parcelRequire === 'function' &&
26
- globalObject.parcelRequire;
25
+ typeof globalObject[parcelRequireName] === 'function' &&
26
+ globalObject[parcelRequireName];
27
+
28
+ var cache = previousRequire.cache || {};
27
29
  // Do not use `require` to prevent Webpack from trying to bundle this call
28
30
  var nodeRequire =
29
31
  typeof module !== 'undefined' &&
@@ -37,7 +39,8 @@
37
39
  // cache jump to the current global require ie. the last bundle
38
40
  // that was added to the page.
39
41
  var currentRequire =
40
- typeof parcelRequire === 'function' && parcelRequire;
42
+ typeof globalObject[parcelRequireName] === 'function' &&
43
+ globalObject[parcelRequireName];
41
44
  if (!jumped && currentRequire) {
42
45
  return currentRequire(name, true);
43
46
  }
@@ -77,11 +80,13 @@
77
80
  return cache[name].exports;
78
81
 
79
82
  function localRequire(x) {
80
- return newRequire(localRequire.resolve(x));
83
+ var res = localRequire.resolve(x);
84
+ return res === false ? {} : newRequire(res);
81
85
  }
82
86
 
83
87
  function resolve(x) {
84
- return modules[name][1][x] || x;
88
+ var id = modules[name][1][x];
89
+ return id != null ? id : x;
85
90
  }
86
91
  }
87
92
 
@@ -96,25 +101,31 @@
96
101
  newRequire.modules = modules;
97
102
  newRequire.cache = cache;
98
103
  newRequire.parent = previousRequire;
99
- newRequire.register = function(id, exports) {
104
+ newRequire.register = function (id, exports) {
100
105
  modules[id] = [
101
- function(require, module) {
106
+ function (require, module) {
102
107
  module.exports = exports;
103
108
  },
104
109
  {},
105
110
  ];
106
111
  };
107
112
 
108
- globalObject.parcelRequire = newRequire;
113
+ Object.defineProperty(newRequire, 'root', {
114
+ get: function () {
115
+ return globalObject[parcelRequireName];
116
+ },
117
+ });
118
+
119
+ globalObject[parcelRequireName] = newRequire;
109
120
 
110
121
  for (var i = 0; i < entry.length; i++) {
111
122
  newRequire(entry[i]);
112
123
  }
113
124
 
114
- if (entry.length) {
125
+ if (mainEntry) {
115
126
  // Expose entry point to Node, AMD or browser globals
116
127
  // Based on https://github.com/ForbesLindesay/umd/blob/master/template.js
117
- var mainExports = newRequire(entry[entry.length - 1]);
128
+ var mainExports = newRequire(mainEntry);
118
129
 
119
130
  // CommonJS
120
131
  if (typeof exports === 'object' && typeof module !== 'undefined') {
@@ -122,7 +133,7 @@
122
133
 
123
134
  // RequireJS
124
135
  } else if (typeof define === 'function' && define.amd) {
125
- define(function() {
136
+ define(function () {
126
137
  return mainExports;
127
138
  });
128
139
 
package/lib/helpers.js ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.prelude = exports.helpers = void 0;
7
+ const prelude = parcelRequireName => `var $parcel$modules = {};
8
+ var $parcel$inits = {};
9
+
10
+ var parcelRequire = $parcel$global[${JSON.stringify(parcelRequireName)}];
11
+ if (parcelRequire == null) {
12
+ parcelRequire = function(id) {
13
+ if (id in $parcel$modules) {
14
+ return $parcel$modules[id].exports;
15
+ }
16
+ if (id in $parcel$inits) {
17
+ var init = $parcel$inits[id];
18
+ delete $parcel$inits[id];
19
+ var module = {id: id, exports: {}};
20
+ $parcel$modules[id] = module;
21
+ init.call(module.exports, module, module.exports);
22
+ return module.exports;
23
+ }
24
+ var err = new Error("Cannot find module '" + id + "'");
25
+ err.code = 'MODULE_NOT_FOUND';
26
+ throw err;
27
+ };
28
+
29
+ parcelRequire.register = function register(id, init) {
30
+ $parcel$inits[id] = init;
31
+ };
32
+
33
+ $parcel$global[${JSON.stringify(parcelRequireName)}] = parcelRequire;
34
+ }
35
+ `;
36
+ exports.prelude = prelude;
37
+ const helpers = {
38
+ $parcel$export: `function $parcel$export(e, n, v, s) {
39
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
40
+ }
41
+ `,
42
+ $parcel$exportWildcard: `function $parcel$exportWildcard(dest, source) {
43
+ Object.keys(source).forEach(function(key) {
44
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
45
+ return;
46
+ }
47
+
48
+ Object.defineProperty(dest, key, {
49
+ enumerable: true,
50
+ get: function get() {
51
+ return source[key];
52
+ }
53
+ });
54
+ });
55
+
56
+ return dest;
57
+ }
58
+ `,
59
+ $parcel$interopDefault: `function $parcel$interopDefault(a) {
60
+ return a && a.__esModule ? a.default : a;
61
+ }
62
+ `,
63
+ $parcel$global: `var $parcel$global =
64
+ typeof globalThis !== 'undefined'
65
+ ? globalThis
66
+ : typeof self !== 'undefined'
67
+ ? self
68
+ : typeof window !== 'undefined'
69
+ ? window
70
+ : typeof global !== 'undefined'
71
+ ? global
72
+ : {};
73
+ `,
74
+ $parcel$defineInteropFlag: `function $parcel$defineInteropFlag(a) {
75
+ Object.defineProperty(a, '__esModule', {value: true, configurable: true});
76
+ }
77
+ `
78
+ };
79
+ exports.helpers = helpers;
package/lib/index.js ADDED
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ function _plugin() {
8
+ const data = require("@parcel/plugin");
9
+ _plugin = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _utils() {
15
+ const data = require("@parcel/utils");
16
+ _utils = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _hash() {
22
+ const data = require("@parcel/hash");
23
+ _hash = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
28
+ function _path() {
29
+ const data = _interopRequireDefault(require("path"));
30
+ _path = function () {
31
+ return data;
32
+ };
33
+ return data;
34
+ }
35
+ function _nullthrows() {
36
+ const data = _interopRequireDefault(require("nullthrows"));
37
+ _nullthrows = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
42
+ var _DevPackager = require("./DevPackager");
43
+ var _ScopeHoistingPackager = require("./ScopeHoistingPackager");
44
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
45
+ var _default = new (_plugin().Packager)({
46
+ async loadConfig({
47
+ config,
48
+ options
49
+ }) {
50
+ var _pkg$contents$name, _pkg$contents;
51
+ // Generate a name for the global parcelRequire function that is unique to this project.
52
+ // This allows multiple parcel builds to coexist on the same page.
53
+ let pkg = await config.getConfigFrom(_path().default.join(options.projectRoot, 'index'), ['package.json']);
54
+ let name = (_pkg$contents$name = pkg === null || pkg === void 0 ? void 0 : (_pkg$contents = pkg.contents) === null || _pkg$contents === void 0 ? void 0 : _pkg$contents.name) !== null && _pkg$contents$name !== void 0 ? _pkg$contents$name : '';
55
+ return {
56
+ parcelRequireName: 'parcelRequire' + (0, _hash().hashString)(name).slice(-4)
57
+ };
58
+ },
59
+ async package({
60
+ bundle,
61
+ bundleGraph,
62
+ getInlineBundleContents,
63
+ getSourceMapReference,
64
+ config,
65
+ options
66
+ }) {
67
+ // If this is a non-module script, and there is only one asset with no dependencies,
68
+ // then we don't need to package at all and can pass through the original code un-wrapped.
69
+ let contents, map;
70
+ if (bundle.env.sourceType === 'script') {
71
+ let entries = bundle.getEntryAssets();
72
+ if (entries.length === 1 && bundleGraph.getDependencies(entries[0]).length === 0) {
73
+ contents = await entries[0].getCode();
74
+ map = await entries[0].getMap();
75
+ }
76
+ }
77
+ if (contents == null) {
78
+ let packager = bundle.env.shouldScopeHoist ? new _ScopeHoistingPackager.ScopeHoistingPackager(options, bundleGraph, bundle, (0, _nullthrows().default)(config).parcelRequireName) : new _DevPackager.DevPackager(options, bundleGraph, bundle, (0, _nullthrows().default)(config).parcelRequireName);
79
+ ({
80
+ contents,
81
+ map
82
+ } = await packager.package());
83
+ }
84
+ contents += '\n' + (await getSourceMapSuffix(getSourceMapReference, map));
85
+
86
+ // For library builds, we need to replace URL references with their final resolved paths.
87
+ // For non-library builds, this is handled in the JS runtime.
88
+ if (bundle.env.isLibrary) {
89
+ ({
90
+ contents,
91
+ map
92
+ } = (0, _utils().replaceURLReferences)({
93
+ bundle,
94
+ bundleGraph,
95
+ contents,
96
+ map,
97
+ getReplacement: s => JSON.stringify(s).slice(1, -1)
98
+ }));
99
+ }
100
+ return (0, _utils().replaceInlineReferences)({
101
+ bundle,
102
+ bundleGraph,
103
+ contents,
104
+ getInlineReplacement: (dependency, inlineType, content) => ({
105
+ from: `"${dependency.id}"`,
106
+ to: inlineType === 'string' ? JSON.stringify(content) : content
107
+ }),
108
+ getInlineBundleContents,
109
+ map
110
+ });
111
+ }
112
+ });
113
+ exports.default = _default;
114
+ async function getSourceMapSuffix(getSourceMapReference, map) {
115
+ let sourcemapReference = await getSourceMapReference(map);
116
+ if (sourcemapReference != null) {
117
+ return '//# sourceMappingURL=' + sourcemapReference + '\n';
118
+ } else {
119
+ return '';
120
+ }
121
+ }
package/lib/utils.js ADDED
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getSpecifier = getSpecifier;
7
+ exports.replaceScriptDependencies = replaceScriptDependencies;
8
+ function _nullthrows() {
9
+ const data = _interopRequireDefault(require("nullthrows"));
10
+ _nullthrows = function () {
11
+ return data;
12
+ };
13
+ return data;
14
+ }
15
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
+ // This replaces __parcel__require__ references left by the transformer with
17
+ // parcelRequire calls of the resolved asset id. This lets runtimes work within
18
+ // script bundles, which must be outside the bundle wrapper so their variables are global.
19
+ function replaceScriptDependencies(bundleGraph, bundle, code, map, parcelRequireName) {
20
+ let entry = (0, _nullthrows().default)(bundle.getMainEntry());
21
+ let dependencies = bundleGraph.getDependencies(entry);
22
+ let lineCount = 0;
23
+ let offset = 0;
24
+ let columnStartIndex = 0;
25
+ code = code.replace(/\n|__parcel__require__\(['"](.*?)['"]\)/g, (m, s, i) => {
26
+ if (m === '\n') {
27
+ columnStartIndex = i + offset + 1;
28
+ lineCount++;
29
+ return '\n';
30
+ }
31
+ let dep = (0, _nullthrows().default)(dependencies.find(d => getSpecifier(d) === s));
32
+ let resolved = (0, _nullthrows().default)(bundleGraph.getResolvedAsset(dep, bundle));
33
+ let publicId = bundleGraph.getAssetPublicId(resolved);
34
+ let replacement = `${parcelRequireName}("${publicId}")`;
35
+ if (map) {
36
+ let lengthDifference = replacement.length - m.length;
37
+ if (lengthDifference !== 0) {
38
+ map.offsetColumns(lineCount + 1, i + offset - columnStartIndex + m.length, lengthDifference);
39
+ offset += lengthDifference;
40
+ }
41
+ }
42
+ return replacement;
43
+ });
44
+ return code;
45
+ }
46
+ function getSpecifier(dep) {
47
+ if (typeof dep.meta.placeholder === 'string') {
48
+ return dep.meta.placeholder;
49
+ }
50
+ return dep.specifier;
51
+ }
package/package.json CHANGED
@@ -1,25 +1,32 @@
1
1
  {
2
2
  "name": "@parcel/packager-js",
3
- "version": "2.0.0-nightly.133+e8a4b8b7",
3
+ "version": "2.0.0-nightly.1332+76e710092",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
8
+ "funding": {
9
+ "type": "opencollective",
10
+ "url": "https://opencollective.com/parcel"
11
+ },
8
12
  "repository": {
9
13
  "type": "git",
10
14
  "url": "https://github.com/parcel-bundler/parcel.git"
11
15
  },
12
- "main": "lib/JSPackager.js",
13
- "source": "src/JSPackager.js",
16
+ "main": "lib/index.js",
17
+ "source": "src/index.js",
14
18
  "engines": {
15
- "node": ">= 10.0.0",
16
- "parcel": "^2.0.0-alpha.1.1"
19
+ "node": ">= 12.0.0",
20
+ "parcel": "2.0.0-nightly.1330+76e710092"
17
21
  },
18
22
  "dependencies": {
19
- "@parcel/plugin": "2.0.0-nightly.133+e8a4b8b7",
20
- "@parcel/scope-hoisting": "2.0.0-nightly.133+e8a4b8b7",
21
- "@parcel/source-map": "2.0.0-nightly.133+e8a4b8b7",
22
- "@parcel/utils": "2.0.0-nightly.133+e8a4b8b7"
23
+ "@parcel/diagnostic": "2.0.0-nightly.1332+76e710092",
24
+ "@parcel/hash": "2.9.3-nightly.2955+76e710092",
25
+ "@parcel/plugin": "2.0.0-nightly.1332+76e710092",
26
+ "@parcel/source-map": "^2.1.1",
27
+ "@parcel/utils": "2.0.0-nightly.1332+76e710092",
28
+ "globals": "^13.2.0",
29
+ "nullthrows": "^1.1.1"
23
30
  },
24
- "gitHead": "e8a4b8b7e54c33c38c6702e1c5b03f93af7a58bc"
31
+ "gitHead": "76e7100922bd0688c1ed3ce56f21c78d8eeb5d65"
25
32
  }
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "@parcel/eslint-config",
3
+ "overrides": [
4
+ {
5
+ "files": ["dev-prelude.js"],
6
+ "parser": "espree",
7
+ "parserOptions": {
8
+ "ecmaVersion": 5,
9
+ "sourceType": "script"
10
+ }
11
+ }
12
+ ]
13
+ }
@@ -0,0 +1,42 @@
1
+ // @flow
2
+ import type {
3
+ ScopeHoistingPackager,
4
+ OutputFormat,
5
+ } from './ScopeHoistingPackager';
6
+
7
+ export class CJSOutputFormat implements OutputFormat {
8
+ packager: ScopeHoistingPackager;
9
+
10
+ constructor(packager: ScopeHoistingPackager) {
11
+ this.packager = packager;
12
+ }
13
+
14
+ buildBundlePrelude(): [string, number] {
15
+ let res = '';
16
+ let lines = 0;
17
+
18
+ for (let [source, specifiers] of this.packager.externals) {
19
+ // CJS only supports the namespace symbol. This ensures that all accesses
20
+ // are live and the `this` binding is correct.
21
+ let namespace = specifiers.get('*');
22
+ if (namespace) {
23
+ res += `var ${namespace} = require(${JSON.stringify(source)});\n`;
24
+ lines++;
25
+ } else {
26
+ res += `require(${JSON.stringify(source)});\n`;
27
+ lines++;
28
+ }
29
+ }
30
+
31
+ if (res.length > 0) {
32
+ res += '\n';
33
+ lines++;
34
+ }
35
+
36
+ return [res, lines];
37
+ }
38
+
39
+ buildBundlePostlude(): [string, number] {
40
+ return ['', 0];
41
+ }
42
+ }
@@ -0,0 +1,252 @@
1
+ // @flow strict-local
2
+ import type {BundleGraph, PluginOptions, NamedBundle} from '@parcel/types';
3
+
4
+ import {
5
+ PromiseQueue,
6
+ relativeBundlePath,
7
+ countLines,
8
+ normalizeSeparators,
9
+ } from '@parcel/utils';
10
+ import SourceMap from '@parcel/source-map';
11
+ import invariant from 'assert';
12
+ import path from 'path';
13
+ import fs from 'fs';
14
+ import {replaceScriptDependencies, getSpecifier} from './utils';
15
+
16
+ const PRELUDE = fs
17
+ .readFileSync(path.join(__dirname, 'dev-prelude.js'), 'utf8')
18
+ .trim()
19
+ .replace(/;$/, '');
20
+
21
+ export class DevPackager {
22
+ options: PluginOptions;
23
+ bundleGraph: BundleGraph<NamedBundle>;
24
+ bundle: NamedBundle;
25
+ parcelRequireName: string;
26
+
27
+ constructor(
28
+ options: PluginOptions,
29
+ bundleGraph: BundleGraph<NamedBundle>,
30
+ bundle: NamedBundle,
31
+ parcelRequireName: string,
32
+ ) {
33
+ this.options = options;
34
+ this.bundleGraph = bundleGraph;
35
+ this.bundle = bundle;
36
+ this.parcelRequireName = parcelRequireName;
37
+ }
38
+
39
+ async package(): Promise<{|contents: string, map: ?SourceMap|}> {
40
+ // Load assets
41
+ let queue = new PromiseQueue({maxConcurrent: 32});
42
+ this.bundle.traverseAssets(asset => {
43
+ queue.add(async () => {
44
+ let [code, mapBuffer] = await Promise.all([
45
+ asset.getCode(),
46
+ this.bundle.env.sourceMap && asset.getMapBuffer(),
47
+ ]);
48
+ return {code, mapBuffer};
49
+ });
50
+ });
51
+
52
+ let results = await queue.run();
53
+
54
+ let assets = '';
55
+ let i = 0;
56
+ let first = true;
57
+ let map = new SourceMap(this.options.projectRoot);
58
+
59
+ let prefix = this.getPrefix();
60
+ let lineOffset = countLines(prefix);
61
+ let script: ?{|code: string, mapBuffer: ?Buffer|} = null;
62
+
63
+ this.bundle.traverse(node => {
64
+ let wrapped = first ? '' : ',';
65
+
66
+ if (node.type === 'dependency') {
67
+ let resolved = this.bundleGraph.getResolvedAsset(
68
+ node.value,
69
+ this.bundle,
70
+ );
71
+ if (resolved && resolved.type !== 'js') {
72
+ // if this is a reference to another javascript asset, we should not include
73
+ // its output, as its contents should already be loaded.
74
+ invariant(!this.bundle.hasAsset(resolved));
75
+ wrapped +=
76
+ JSON.stringify(this.bundleGraph.getAssetPublicId(resolved)) +
77
+ ':[function() {},{}]';
78
+ } else {
79
+ return;
80
+ }
81
+ }
82
+
83
+ if (node.type === 'asset') {
84
+ let asset = node.value;
85
+ invariant(
86
+ asset.type === 'js',
87
+ 'all assets in a js bundle must be js assets',
88
+ );
89
+
90
+ // If this is the main entry of a script rather than a module, we need to hoist it
91
+ // outside the bundle wrapper function so that its variables are exposed as globals.
92
+ if (
93
+ this.bundle.env.sourceType === 'script' &&
94
+ asset === this.bundle.getMainEntry()
95
+ ) {
96
+ script = results[i++];
97
+ return;
98
+ }
99
+
100
+ let deps = {};
101
+ let dependencies = this.bundleGraph.getDependencies(asset);
102
+ for (let dep of dependencies) {
103
+ let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
104
+ if (this.bundleGraph.isDependencySkipped(dep)) {
105
+ deps[getSpecifier(dep)] = false;
106
+ } else if (resolved) {
107
+ deps[getSpecifier(dep)] =
108
+ this.bundleGraph.getAssetPublicId(resolved);
109
+ } else {
110
+ // An external module - map placeholder to original specifier.
111
+ deps[getSpecifier(dep)] = dep.specifier;
112
+ }
113
+ }
114
+
115
+ let {code, mapBuffer} = results[i];
116
+ let output = code || '';
117
+ wrapped +=
118
+ JSON.stringify(this.bundleGraph.getAssetPublicId(asset)) +
119
+ ':[function(require,module,exports) {\n' +
120
+ output +
121
+ '\n},';
122
+ wrapped += JSON.stringify(deps);
123
+ wrapped += ']';
124
+
125
+ if (
126
+ this.bundle.env.isNode() &&
127
+ asset.meta.has_node_replacements === true
128
+ ) {
129
+ const relPath = normalizeSeparators(
130
+ path.relative(
131
+ this.bundle.target.distDir,
132
+ path.dirname(asset.filePath),
133
+ ),
134
+ );
135
+ wrapped = wrapped.replace('$parcel$dirnameReplace', relPath);
136
+ wrapped = wrapped.replace('$parcel$filenameReplace', relPath);
137
+ }
138
+
139
+ if (this.bundle.env.sourceMap) {
140
+ if (mapBuffer) {
141
+ map.addBuffer(mapBuffer, lineOffset);
142
+ } else {
143
+ map.addEmptyMap(
144
+ path
145
+ .relative(this.options.projectRoot, asset.filePath)
146
+ .replace(/\\+/g, '/'),
147
+ output,
148
+ lineOffset,
149
+ );
150
+ }
151
+
152
+ lineOffset += countLines(output) + 1;
153
+ }
154
+ i++;
155
+ }
156
+
157
+ assets += wrapped;
158
+ first = false;
159
+ });
160
+
161
+ let entries = this.bundle.getEntryAssets();
162
+ let mainEntry = this.bundle.getMainEntry();
163
+ if (
164
+ (!this.isEntry() && this.bundle.env.outputFormat === 'global') ||
165
+ this.bundle.env.sourceType === 'script'
166
+ ) {
167
+ // In async bundles we don't want the main entry to execute until we require it
168
+ // as there might be dependencies in a sibling bundle that hasn't loaded yet.
169
+ entries = entries.filter(a => a.id !== mainEntry?.id);
170
+ mainEntry = null;
171
+ }
172
+
173
+ let contents =
174
+ prefix +
175
+ '({' +
176
+ assets +
177
+ '},' +
178
+ JSON.stringify(
179
+ entries.map(asset => this.bundleGraph.getAssetPublicId(asset)),
180
+ ) +
181
+ ', ' +
182
+ JSON.stringify(
183
+ mainEntry ? this.bundleGraph.getAssetPublicId(mainEntry) : null,
184
+ ) +
185
+ ', ' +
186
+ JSON.stringify(this.parcelRequireName) +
187
+ ')' +
188
+ '\n';
189
+
190
+ // The entry asset of a script bundle gets hoisted outside the bundle wrapper function
191
+ // so that its variables become globals. We need to replace any require calls for
192
+ // runtimes with a parcelRequire call.
193
+ if (this.bundle.env.sourceType === 'script' && script) {
194
+ let entryMap;
195
+ let mapBuffer = script.mapBuffer;
196
+ if (mapBuffer) {
197
+ entryMap = new SourceMap(this.options.projectRoot, mapBuffer);
198
+ }
199
+ contents += replaceScriptDependencies(
200
+ this.bundleGraph,
201
+ this.bundle,
202
+ script.code,
203
+ entryMap,
204
+ this.parcelRequireName,
205
+ );
206
+ if (this.bundle.env.sourceMap && entryMap) {
207
+ map.addSourceMap(entryMap, lineOffset);
208
+ }
209
+ }
210
+
211
+ return {
212
+ contents,
213
+ map,
214
+ };
215
+ }
216
+
217
+ getPrefix(): string {
218
+ let interpreter: ?string;
219
+ let mainEntry = this.bundle.getMainEntry();
220
+ if (mainEntry && this.isEntry() && !this.bundle.target.env.isBrowser()) {
221
+ let _interpreter = mainEntry.meta.interpreter;
222
+ invariant(_interpreter == null || typeof _interpreter === 'string');
223
+ interpreter = _interpreter;
224
+ }
225
+
226
+ let importScripts = '';
227
+ if (this.bundle.env.isWorker()) {
228
+ let bundles = this.bundleGraph.getReferencedBundles(this.bundle);
229
+ for (let b of bundles) {
230
+ importScripts += `importScripts("${relativeBundlePath(
231
+ this.bundle,
232
+ b,
233
+ )}");\n`;
234
+ }
235
+ }
236
+
237
+ return (
238
+ // If the entry asset included a hashbang, repeat it at the top of the bundle
239
+ (interpreter != null ? `#!${interpreter}\n` : '') +
240
+ importScripts +
241
+ PRELUDE
242
+ );
243
+ }
244
+
245
+ isEntry(): boolean {
246
+ return (
247
+ !this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') ||
248
+ this.bundle.env.isIsolated() ||
249
+ this.bundle.bundleBehavior === 'isolated'
250
+ );
251
+ }
252
+ }