@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.
- package/lib/CJSOutputFormat.js +6 -101
- package/lib/DevPackager.js +38 -20
- package/lib/ESMOutputFormat.js +19 -13
- package/lib/GlobalOutputFormat.js +3 -6
- package/lib/ScopeHoistingPackager.js +321 -267
- package/lib/dev-prelude.js +10 -8
- package/lib/helpers.js +5 -5
- package/lib/index.js +52 -30
- package/lib/utils.js +62 -0
- package/package.json +8 -7
- package/src/.eslintrc.json +13 -0
- package/src/CJSOutputFormat.js +7 -119
- package/src/DevPackager.js +45 -11
- package/src/ESMOutputFormat.js +23 -5
- package/src/GlobalOutputFormat.js +6 -3
- package/src/ScopeHoistingPackager.js +383 -257
- package/src/dev-prelude.js +10 -8
- package/src/helpers.js +4 -4
- package/src/index.js +49 -29
- package/src/utils.js +57 -0
package/lib/dev-prelude.js
CHANGED
|
@@ -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, entry, mainEntry, parcelRequireName, globalName) {
|
|
9
|
+
(function (modules, entry, mainEntry, parcelRequireName, globalName) {
|
|
10
10
|
/* eslint-disable no-undef */
|
|
11
11
|
var globalObject =
|
|
12
12
|
typeof globalThis !== 'undefined'
|
|
@@ -73,18 +73,20 @@
|
|
|
73
73
|
localRequire,
|
|
74
74
|
module,
|
|
75
75
|
module.exports,
|
|
76
|
-
this
|
|
76
|
+
this
|
|
77
77
|
);
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
return cache[name].exports;
|
|
81
81
|
|
|
82
82
|
function localRequire(x) {
|
|
83
|
-
|
|
83
|
+
var res = localRequire.resolve(x);
|
|
84
|
+
return res === false ? {} : newRequire(res);
|
|
84
85
|
}
|
|
85
86
|
|
|
86
87
|
function resolve(x) {
|
|
87
|
-
|
|
88
|
+
var id = modules[name][1][x];
|
|
89
|
+
return id != null ? id : x;
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
92
|
|
|
@@ -99,9 +101,9 @@
|
|
|
99
101
|
newRequire.modules = modules;
|
|
100
102
|
newRequire.cache = cache;
|
|
101
103
|
newRequire.parent = previousRequire;
|
|
102
|
-
newRequire.register = function(id, exports) {
|
|
104
|
+
newRequire.register = function (id, exports) {
|
|
103
105
|
modules[id] = [
|
|
104
|
-
function(require, module) {
|
|
106
|
+
function (require, module) {
|
|
105
107
|
module.exports = exports;
|
|
106
108
|
},
|
|
107
109
|
{},
|
|
@@ -109,7 +111,7 @@
|
|
|
109
111
|
};
|
|
110
112
|
|
|
111
113
|
Object.defineProperty(newRequire, 'root', {
|
|
112
|
-
get: function() {
|
|
114
|
+
get: function () {
|
|
113
115
|
return globalObject[parcelRequireName];
|
|
114
116
|
},
|
|
115
117
|
});
|
|
@@ -131,7 +133,7 @@
|
|
|
131
133
|
|
|
132
134
|
// RequireJS
|
|
133
135
|
} else if (typeof define === 'function' && define.amd) {
|
|
134
|
-
define(function() {
|
|
136
|
+
define(function () {
|
|
135
137
|
return mainExports;
|
|
136
138
|
});
|
|
137
139
|
|
package/lib/helpers.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.prelude = exports.helpers = void 0;
|
|
7
7
|
|
|
8
8
|
const prelude = parcelRequireName => `var $parcel$modules = {};
|
|
9
9
|
var $parcel$inits = {};
|
|
@@ -15,9 +15,9 @@ if (parcelRequire == null) {
|
|
|
15
15
|
return $parcel$modules[id].exports;
|
|
16
16
|
}
|
|
17
17
|
if (id in $parcel$inits) {
|
|
18
|
-
|
|
18
|
+
var init = $parcel$inits[id];
|
|
19
19
|
delete $parcel$inits[id];
|
|
20
|
-
|
|
20
|
+
var module = {id: id, exports: {}};
|
|
21
21
|
$parcel$modules[id] = module;
|
|
22
22
|
init.call(module.exports, module, module.exports);
|
|
23
23
|
return module.exports;
|
|
@@ -43,7 +43,7 @@ const helpers = {
|
|
|
43
43
|
`,
|
|
44
44
|
$parcel$exportWildcard: `function $parcel$exportWildcard(dest, source) {
|
|
45
45
|
Object.keys(source).forEach(function(key) {
|
|
46
|
-
if (key === 'default' || key === '__esModule') {
|
|
46
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
|
|
@@ -51,7 +51,7 @@ const helpers = {
|
|
|
51
51
|
enumerable: true,
|
|
52
52
|
get: function get() {
|
|
53
53
|
return source[key];
|
|
54
|
-
}
|
|
54
|
+
}
|
|
55
55
|
});
|
|
56
56
|
});
|
|
57
57
|
|
package/lib/index.js
CHANGED
|
@@ -25,63 +25,55 @@ function _utils() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
function
|
|
29
|
-
const data =
|
|
28
|
+
function _hash() {
|
|
29
|
+
const data = require("@parcel/hash");
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
_hash = function () {
|
|
32
32
|
return data;
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
return data;
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
function
|
|
39
|
-
const data = _interopRequireDefault(require("
|
|
38
|
+
function _path() {
|
|
39
|
+
const data = _interopRequireDefault(require("path"));
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
_path = function () {
|
|
42
42
|
return data;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
return data;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
function
|
|
49
|
-
const data = require("
|
|
48
|
+
function _nullthrows() {
|
|
49
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
_nullthrows = function () {
|
|
52
52
|
return data;
|
|
53
53
|
};
|
|
54
54
|
|
|
55
55
|
return data;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
const data = require("./ScopeHoistingPackager");
|
|
58
|
+
var _DevPackager = require("./DevPackager");
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
return data;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
return data;
|
|
66
|
-
}
|
|
60
|
+
var _ScopeHoistingPackager = require("./ScopeHoistingPackager");
|
|
67
61
|
|
|
68
62
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
69
63
|
|
|
70
64
|
var _default = new (_plugin().Packager)({
|
|
71
65
|
async loadConfig({
|
|
66
|
+
config,
|
|
72
67
|
options
|
|
73
68
|
}) {
|
|
74
|
-
var _pkg$
|
|
69
|
+
var _pkg$contents$name, _pkg$contents;
|
|
75
70
|
|
|
76
71
|
// Generate a name for the global parcelRequire function that is unique to this project.
|
|
77
72
|
// This allows multiple parcel builds to coexist on the same page.
|
|
78
|
-
let pkg = await
|
|
79
|
-
let name = (_pkg$
|
|
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 : '';
|
|
80
75
|
return {
|
|
81
|
-
|
|
82
|
-
parcelRequireName: 'parcelRequire' + (0, _utils().md5FromString)(name).slice(-4)
|
|
83
|
-
},
|
|
84
|
-
files: (_pkg$files = pkg === null || pkg === void 0 ? void 0 : pkg.files) !== null && _pkg$files !== void 0 ? _pkg$files : []
|
|
76
|
+
parcelRequireName: 'parcelRequire' + (0, _hash().hashString)(name).slice(-4)
|
|
85
77
|
};
|
|
86
78
|
},
|
|
87
79
|
|
|
@@ -93,12 +85,42 @@ var _default = new (_plugin().Packager)({
|
|
|
93
85
|
config,
|
|
94
86
|
options
|
|
95
87
|
}) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
+
|
|
102
124
|
return (0, _utils().replaceInlineReferences)({
|
|
103
125
|
bundle,
|
|
104
126
|
bundleGraph,
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getSpecifier = getSpecifier;
|
|
7
|
+
exports.replaceScriptDependencies = replaceScriptDependencies;
|
|
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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/packager-js",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-nightly.1006+18b038d5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -17,15 +17,16 @@
|
|
|
17
17
|
"source": "src/index.js",
|
|
18
18
|
"engines": {
|
|
19
19
|
"node": ">= 12.0.0",
|
|
20
|
-
"parcel": "
|
|
20
|
+
"parcel": "2.0.0-nightly.1004+18b038d5"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@parcel/diagnostic": "2.0.0-
|
|
24
|
-
"@parcel/
|
|
25
|
-
"@parcel/
|
|
26
|
-
"@parcel/
|
|
23
|
+
"@parcel/diagnostic": "2.0.0-nightly.1006+18b038d5",
|
|
24
|
+
"@parcel/hash": "2.3.3-nightly.2629+18b038d5",
|
|
25
|
+
"@parcel/plugin": "2.0.0-nightly.1006+18b038d5",
|
|
26
|
+
"@parcel/source-map": "^2.0.0",
|
|
27
|
+
"@parcel/utils": "2.0.0-nightly.1006+18b038d5",
|
|
27
28
|
"globals": "^13.2.0",
|
|
28
29
|
"nullthrows": "^1.1.1"
|
|
29
30
|
},
|
|
30
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "18b038d57043965afb31077db1f46dadb11a6a78"
|
|
31
32
|
}
|
package/src/CJSOutputFormat.js
CHANGED
|
@@ -4,19 +4,6 @@ import type {
|
|
|
4
4
|
OutputFormat,
|
|
5
5
|
} from './ScopeHoistingPackager';
|
|
6
6
|
|
|
7
|
-
// List of engines that support object destructuring syntax
|
|
8
|
-
const DESTRUCTURING_ENGINES = {
|
|
9
|
-
chrome: '51',
|
|
10
|
-
edge: '15',
|
|
11
|
-
firefox: '53',
|
|
12
|
-
safari: '10',
|
|
13
|
-
node: '6.5',
|
|
14
|
-
ios: '10',
|
|
15
|
-
samsung: '5',
|
|
16
|
-
opera: '38',
|
|
17
|
-
electron: '1.2',
|
|
18
|
-
};
|
|
19
|
-
|
|
20
7
|
export class CJSOutputFormat implements OutputFormat {
|
|
21
8
|
packager: ScopeHoistingPackager;
|
|
22
9
|
|
|
@@ -29,70 +16,12 @@ export class CJSOutputFormat implements OutputFormat {
|
|
|
29
16
|
let lines = 0;
|
|
30
17
|
|
|
31
18
|
for (let [source, specifiers] of this.packager.externals) {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
} else if (imported === 'default') {
|
|
38
|
-
categories.add('default');
|
|
39
|
-
} else {
|
|
40
|
-
categories.add('named');
|
|
41
|
-
properties.push({
|
|
42
|
-
key: imported,
|
|
43
|
-
value: symbol,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
let specifiersWildcard = specifiers.get('*');
|
|
49
|
-
let specifiersDefault = specifiers.get('default');
|
|
50
|
-
|
|
51
|
-
// Attempt to combine require calls as much as possible. Namespace, default, and named specifiers
|
|
52
|
-
// cannot be combined, so in the case where we have more than one type, assign the require() result
|
|
53
|
-
// to a variable first and then create additional variables for each specifier based on that.
|
|
54
|
-
// Otherwise, if just one category is imported, just assign and require all at once.
|
|
55
|
-
if (categories.size > 1) {
|
|
56
|
-
let name = specifiersWildcard || this.packager.getTopLevelName(source);
|
|
57
|
-
res += `var ${name} = require(${JSON.stringify(source)});\n`;
|
|
58
|
-
lines++;
|
|
59
|
-
|
|
60
|
-
if (specifiersDefault) {
|
|
61
|
-
res += `var ${specifiersDefault} = $parcel$interopDefault(${name});\n`;
|
|
62
|
-
lines++;
|
|
63
|
-
this.packager.usedHelpers.add('$parcel$interopDefault');
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (properties.length > 0) {
|
|
67
|
-
let [r, l] = this.generateDestructuringAssignment(
|
|
68
|
-
properties,
|
|
69
|
-
name,
|
|
70
|
-
true,
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
res += r;
|
|
74
|
-
lines += l;
|
|
75
|
-
}
|
|
76
|
-
} else if (specifiersDefault) {
|
|
77
|
-
res += `var ${specifiersDefault} = $parcel$interopDefault(require(${JSON.stringify(
|
|
78
|
-
source,
|
|
79
|
-
)}));\n`;
|
|
80
|
-
lines++;
|
|
81
|
-
this.packager.usedHelpers.add('$parcel$interopDefault');
|
|
82
|
-
} else if (specifiersWildcard) {
|
|
83
|
-
res += `var ${specifiersWildcard} = require(${JSON.stringify(
|
|
84
|
-
source,
|
|
85
|
-
)});\n`;
|
|
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`;
|
|
86
24
|
lines++;
|
|
87
|
-
} else if (properties.length > 0) {
|
|
88
|
-
let [r, l] = this.generateDestructuringAssignment(
|
|
89
|
-
properties,
|
|
90
|
-
`require(${JSON.stringify(source)})`,
|
|
91
|
-
false,
|
|
92
|
-
);
|
|
93
|
-
|
|
94
|
-
res += r;
|
|
95
|
-
lines += l;
|
|
96
25
|
} else {
|
|
97
26
|
res += `require(${JSON.stringify(source)});\n`;
|
|
98
27
|
lines++;
|
|
@@ -107,48 +36,7 @@ export class CJSOutputFormat implements OutputFormat {
|
|
|
107
36
|
return [res, lines];
|
|
108
37
|
}
|
|
109
38
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
value: string,
|
|
113
|
-
isIdentifier: boolean,
|
|
114
|
-
): [string, number] {
|
|
115
|
-
let res = '';
|
|
116
|
-
let lines = 0;
|
|
117
|
-
|
|
118
|
-
// If destructuring is not supported, generate a series of variable declarations
|
|
119
|
-
// with member expressions for each property.
|
|
120
|
-
if (!this.packager.bundle.env.matchesEngines(DESTRUCTURING_ENGINES)) {
|
|
121
|
-
if (!isIdentifier && specifiers.length > 1) {
|
|
122
|
-
let name = this.packager.getTopLevelName('temp');
|
|
123
|
-
res += `var ${name} = ${value};\n`;
|
|
124
|
-
lines++;
|
|
125
|
-
value = name;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
for (let specifier of specifiers) {
|
|
129
|
-
res += `var ${specifier.value} = ${value}.${specifier.key};\n`;
|
|
130
|
-
lines++;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return [res, lines];
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
let s = specifiers.map(specifier => {
|
|
137
|
-
let s = specifier.key;
|
|
138
|
-
if (specifier.value !== specifier.key) {
|
|
139
|
-
s += `: ${specifier.value}`;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return s;
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
res += `var {${s.join(', ')}} = ${value};\n`;
|
|
146
|
-
lines++;
|
|
147
|
-
|
|
148
|
-
return [res, lines];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
buildBundlePostlude(): string {
|
|
152
|
-
return '';
|
|
39
|
+
buildBundlePostlude(): [string, number] {
|
|
40
|
+
return ['', 0];
|
|
153
41
|
}
|
|
154
42
|
}
|
package/src/DevPackager.js
CHANGED
|
@@ -6,6 +6,7 @@ import SourceMap from '@parcel/source-map';
|
|
|
6
6
|
import invariant from 'assert';
|
|
7
7
|
import path from 'path';
|
|
8
8
|
import fs from 'fs';
|
|
9
|
+
import {replaceScriptDependencies, getSpecifier} from './utils';
|
|
9
10
|
|
|
10
11
|
const PRELUDE = fs
|
|
11
12
|
.readFileSync(path.join(__dirname, 'dev-prelude.js'), 'utf8')
|
|
@@ -54,12 +55,13 @@ export class DevPackager {
|
|
|
54
55
|
|
|
55
56
|
let prefix = this.getPrefix();
|
|
56
57
|
let lineOffset = countLines(prefix);
|
|
58
|
+
let script: ?{|code: string, mapBuffer: ?Buffer|} = null;
|
|
57
59
|
|
|
58
60
|
this.bundle.traverse(node => {
|
|
59
61
|
let wrapped = first ? '' : ',';
|
|
60
62
|
|
|
61
63
|
if (node.type === 'dependency') {
|
|
62
|
-
let resolved = this.bundleGraph.
|
|
64
|
+
let resolved = this.bundleGraph.getResolvedAsset(
|
|
63
65
|
node.value,
|
|
64
66
|
this.bundle,
|
|
65
67
|
);
|
|
@@ -82,17 +84,25 @@ export class DevPackager {
|
|
|
82
84
|
'all assets in a js bundle must be js assets',
|
|
83
85
|
);
|
|
84
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
|
+
|
|
85
97
|
let deps = {};
|
|
86
98
|
let dependencies = this.bundleGraph.getDependencies(asset);
|
|
87
99
|
for (let dep of dependencies) {
|
|
88
|
-
let resolved = this.bundleGraph.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
resolved,
|
|
95
|
-
);
|
|
100
|
+
let resolved = this.bundleGraph.getResolvedAsset(dep, this.bundle);
|
|
101
|
+
if (this.bundleGraph.isDependencySkipped(dep)) {
|
|
102
|
+
deps[getSpecifier(dep)] = false;
|
|
103
|
+
} else if (resolved) {
|
|
104
|
+
deps[getSpecifier(dep)] =
|
|
105
|
+
this.bundleGraph.getAssetPublicId(resolved);
|
|
96
106
|
}
|
|
97
107
|
}
|
|
98
108
|
|
|
@@ -130,7 +140,10 @@ export class DevPackager {
|
|
|
130
140
|
|
|
131
141
|
let entries = this.bundle.getEntryAssets();
|
|
132
142
|
let mainEntry = this.bundle.getMainEntry();
|
|
133
|
-
if (
|
|
143
|
+
if (
|
|
144
|
+
(!this.isEntry() && this.bundle.env.outputFormat === 'global') ||
|
|
145
|
+
this.bundle.env.sourceType === 'script'
|
|
146
|
+
) {
|
|
134
147
|
// In async bundles we don't want the main entry to execute until we require it
|
|
135
148
|
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
136
149
|
entries = entries.filter(a => a.id !== mainEntry?.id);
|
|
@@ -154,6 +167,27 @@ export class DevPackager {
|
|
|
154
167
|
')' +
|
|
155
168
|
'\n';
|
|
156
169
|
|
|
170
|
+
// The entry asset of a script bundle gets hoisted outside the bundle wrapper function
|
|
171
|
+
// so that its variables become globals. We need to replace any require calls for
|
|
172
|
+
// runtimes with a parcelRequire call.
|
|
173
|
+
if (this.bundle.env.sourceType === 'script' && script) {
|
|
174
|
+
let entryMap;
|
|
175
|
+
let mapBuffer = script.mapBuffer;
|
|
176
|
+
if (mapBuffer) {
|
|
177
|
+
entryMap = new SourceMap(this.options.projectRoot, mapBuffer);
|
|
178
|
+
}
|
|
179
|
+
contents += replaceScriptDependencies(
|
|
180
|
+
this.bundleGraph,
|
|
181
|
+
this.bundle,
|
|
182
|
+
script.code,
|
|
183
|
+
entryMap,
|
|
184
|
+
this.parcelRequireName,
|
|
185
|
+
);
|
|
186
|
+
if (this.bundle.env.sourceMap && entryMap) {
|
|
187
|
+
map.addSourceMap(entryMap, lineOffset);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
157
191
|
return {
|
|
158
192
|
contents,
|
|
159
193
|
map,
|
|
@@ -192,7 +226,7 @@ export class DevPackager {
|
|
|
192
226
|
return (
|
|
193
227
|
!this.bundleGraph.hasParentBundleOfType(this.bundle, 'js') ||
|
|
194
228
|
this.bundle.env.isIsolated() ||
|
|
195
|
-
|
|
229
|
+
this.bundle.bundleBehavior === 'isolated'
|
|
196
230
|
);
|
|
197
231
|
}
|
|
198
232
|
}
|
package/src/ESMOutputFormat.js
CHANGED
|
@@ -70,14 +70,31 @@ export class ESMOutputFormat implements OutputFormat {
|
|
|
70
70
|
return [res, lines];
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
buildBundlePostlude(): string {
|
|
73
|
+
buildBundlePostlude(): [string, number] {
|
|
74
74
|
let res = '';
|
|
75
|
+
let lines = 0;
|
|
75
76
|
let exportSpecifiers = [];
|
|
76
|
-
for (let
|
|
77
|
-
|
|
77
|
+
for (let {
|
|
78
|
+
asset,
|
|
79
|
+
exportSymbol,
|
|
80
|
+
local,
|
|
81
|
+
exportAs,
|
|
82
|
+
} of this.packager.exportedSymbols.values()) {
|
|
83
|
+
if (this.packager.wrappedAssets.has(asset.id)) {
|
|
84
|
+
let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(
|
|
85
|
+
asset,
|
|
86
|
+
)}")`;
|
|
87
|
+
res += `\nvar ${local} = ${this.packager.getPropertyAccess(
|
|
88
|
+
obj,
|
|
89
|
+
exportSymbol,
|
|
90
|
+
)};`;
|
|
91
|
+
lines++;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (let as of exportAs) {
|
|
78
95
|
let specifier = local;
|
|
79
96
|
if (exportAs !== local) {
|
|
80
|
-
specifier += ` as ${
|
|
97
|
+
specifier += ` as ${as}`;
|
|
81
98
|
}
|
|
82
99
|
|
|
83
100
|
exportSpecifiers.push(specifier);
|
|
@@ -86,8 +103,9 @@ export class ESMOutputFormat implements OutputFormat {
|
|
|
86
103
|
|
|
87
104
|
if (exportSpecifiers.length > 0) {
|
|
88
105
|
res += `\nexport {${exportSpecifiers.join(', ')}};`;
|
|
106
|
+
lines++;
|
|
89
107
|
}
|
|
90
108
|
|
|
91
|
-
return res;
|
|
109
|
+
return [res, lines];
|
|
92
110
|
}
|
|
93
111
|
}
|
|
@@ -12,10 +12,13 @@ export class GlobalOutputFormat implements OutputFormat {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
buildBundlePrelude(): [string, number] {
|
|
15
|
-
|
|
15
|
+
let prelude = this.packager.bundle.env.supports('arrow-functions', true)
|
|
16
|
+
? '(() => {\n'
|
|
17
|
+
: '(function () {\n';
|
|
18
|
+
return [prelude, 1];
|
|
16
19
|
}
|
|
17
20
|
|
|
18
|
-
buildBundlePostlude(): string {
|
|
19
|
-
return '})();';
|
|
21
|
+
buildBundlePostlude(): [string, number] {
|
|
22
|
+
return ['})();', 0];
|
|
20
23
|
}
|
|
21
24
|
}
|