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