@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/src/helpers.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
export const prelude = (
|
|
3
|
+
parcelRequireName: string,
|
|
4
|
+
): string => `var $parcel$modules = {};
|
|
5
|
+
var $parcel$inits = {};
|
|
6
|
+
|
|
7
|
+
var parcelRequire = $parcel$global[${JSON.stringify(parcelRequireName)}];
|
|
8
|
+
if (parcelRequire == null) {
|
|
9
|
+
parcelRequire = function(id) {
|
|
10
|
+
if (id in $parcel$modules) {
|
|
11
|
+
return $parcel$modules[id].exports;
|
|
12
|
+
}
|
|
13
|
+
if (id in $parcel$inits) {
|
|
14
|
+
var init = $parcel$inits[id];
|
|
15
|
+
delete $parcel$inits[id];
|
|
16
|
+
var module = {id: id, exports: {}};
|
|
17
|
+
$parcel$modules[id] = module;
|
|
18
|
+
init.call(module.exports, module, module.exports);
|
|
19
|
+
return module.exports;
|
|
20
|
+
}
|
|
21
|
+
var err = new Error("Cannot find module '" + id + "'");
|
|
22
|
+
err.code = 'MODULE_NOT_FOUND';
|
|
23
|
+
throw err;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
parcelRequire.register = function register(id, init) {
|
|
27
|
+
$parcel$inits[id] = init;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
$parcel$global[${JSON.stringify(parcelRequireName)}] = parcelRequire;
|
|
31
|
+
}
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
export const helpers = {
|
|
35
|
+
$parcel$export: `function $parcel$export(e, n, v, s) {
|
|
36
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
37
|
+
}
|
|
38
|
+
`,
|
|
39
|
+
$parcel$exportWildcard: `function $parcel$exportWildcard(dest, source) {
|
|
40
|
+
Object.keys(source).forEach(function(key) {
|
|
41
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
Object.defineProperty(dest, key, {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
get: function get() {
|
|
48
|
+
return source[key];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return dest;
|
|
54
|
+
}
|
|
55
|
+
`,
|
|
56
|
+
$parcel$interopDefault: `function $parcel$interopDefault(a) {
|
|
57
|
+
return a && a.__esModule ? a.default : a;
|
|
58
|
+
}
|
|
59
|
+
`,
|
|
60
|
+
$parcel$global: `var $parcel$global =
|
|
61
|
+
typeof globalThis !== 'undefined'
|
|
62
|
+
? globalThis
|
|
63
|
+
: typeof self !== 'undefined'
|
|
64
|
+
? self
|
|
65
|
+
: typeof window !== 'undefined'
|
|
66
|
+
? window
|
|
67
|
+
: typeof global !== 'undefined'
|
|
68
|
+
? global
|
|
69
|
+
: {};
|
|
70
|
+
`,
|
|
71
|
+
$parcel$defineInteropFlag: `function $parcel$defineInteropFlag(a) {
|
|
72
|
+
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
73
|
+
}
|
|
74
|
+
`,
|
|
75
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
import type {Async} from '@parcel/types';
|
|
3
|
+
import type SourceMap from '@parcel/source-map';
|
|
4
|
+
import {Packager} from '@parcel/plugin';
|
|
5
|
+
import {replaceInlineReferences, replaceURLReferences} from '@parcel/utils';
|
|
6
|
+
import {hashString} from '@parcel/hash';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import nullthrows from 'nullthrows';
|
|
9
|
+
import {DevPackager} from './DevPackager';
|
|
10
|
+
import {ScopeHoistingPackager} from './ScopeHoistingPackager';
|
|
11
|
+
|
|
12
|
+
export default (new Packager({
|
|
13
|
+
async loadConfig({config, options}) {
|
|
14
|
+
// Generate a name for the global parcelRequire function that is unique to this project.
|
|
15
|
+
// This allows multiple parcel builds to coexist on the same page.
|
|
16
|
+
let pkg = await config.getConfigFrom(
|
|
17
|
+
path.join(options.projectRoot, 'index'),
|
|
18
|
+
['package.json'],
|
|
19
|
+
);
|
|
20
|
+
let name = pkg?.contents?.name ?? '';
|
|
21
|
+
return {
|
|
22
|
+
parcelRequireName: 'parcelRequire' + hashString(name).slice(-4),
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
async package({
|
|
26
|
+
bundle,
|
|
27
|
+
bundleGraph,
|
|
28
|
+
getInlineBundleContents,
|
|
29
|
+
getSourceMapReference,
|
|
30
|
+
config,
|
|
31
|
+
options,
|
|
32
|
+
}) {
|
|
33
|
+
// If this is a non-module script, and there is only one asset with no dependencies,
|
|
34
|
+
// then we don't need to package at all and can pass through the original code un-wrapped.
|
|
35
|
+
let contents, map;
|
|
36
|
+
if (bundle.env.sourceType === 'script') {
|
|
37
|
+
let entries = bundle.getEntryAssets();
|
|
38
|
+
if (
|
|
39
|
+
entries.length === 1 &&
|
|
40
|
+
bundleGraph.getDependencies(entries[0]).length === 0
|
|
41
|
+
) {
|
|
42
|
+
contents = await entries[0].getCode();
|
|
43
|
+
map = await entries[0].getMap();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (contents == null) {
|
|
48
|
+
let packager = bundle.env.shouldScopeHoist
|
|
49
|
+
? new ScopeHoistingPackager(
|
|
50
|
+
options,
|
|
51
|
+
bundleGraph,
|
|
52
|
+
bundle,
|
|
53
|
+
nullthrows(config).parcelRequireName,
|
|
54
|
+
)
|
|
55
|
+
: new DevPackager(
|
|
56
|
+
options,
|
|
57
|
+
bundleGraph,
|
|
58
|
+
bundle,
|
|
59
|
+
nullthrows(config).parcelRequireName,
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
({contents, map} = await packager.package());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
contents += '\n' + (await getSourceMapSuffix(getSourceMapReference, map));
|
|
66
|
+
|
|
67
|
+
// For library builds, we need to replace URL references with their final resolved paths.
|
|
68
|
+
// For non-library builds, this is handled in the JS runtime.
|
|
69
|
+
if (bundle.env.isLibrary) {
|
|
70
|
+
({contents, map} = replaceURLReferences({
|
|
71
|
+
bundle,
|
|
72
|
+
bundleGraph,
|
|
73
|
+
contents,
|
|
74
|
+
map,
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return replaceInlineReferences({
|
|
79
|
+
bundle,
|
|
80
|
+
bundleGraph,
|
|
81
|
+
contents,
|
|
82
|
+
getInlineReplacement: (dependency, inlineType, content) => ({
|
|
83
|
+
from: `"${dependency.id}"`,
|
|
84
|
+
to: inlineType === 'string' ? JSON.stringify(content) : content,
|
|
85
|
+
}),
|
|
86
|
+
getInlineBundleContents,
|
|
87
|
+
map,
|
|
88
|
+
});
|
|
89
|
+
},
|
|
90
|
+
}): Packager);
|
|
91
|
+
|
|
92
|
+
async function getSourceMapSuffix(
|
|
93
|
+
getSourceMapReference: (?SourceMap) => Async<?string>,
|
|
94
|
+
map: ?SourceMap,
|
|
95
|
+
): Promise<string> {
|
|
96
|
+
let sourcemapReference = await getSourceMapReference(map);
|
|
97
|
+
if (sourcemapReference != null) {
|
|
98
|
+
return '//# sourceMappingURL=' + sourcemapReference + '\n';
|
|
99
|
+
} else {
|
|
100
|
+
return '';
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type {BundleGraph, Dependency, NamedBundle} from '@parcel/types';
|
|
3
|
+
import type SourceMap from '@parcel/source-map';
|
|
4
|
+
import nullthrows from 'nullthrows';
|
|
5
|
+
|
|
6
|
+
// This replaces __parcel__require__ references left by the transformer with
|
|
7
|
+
// parcelRequire calls of the resolved asset id. This lets runtimes work within
|
|
8
|
+
// script bundles, which must be outside the bundle wrapper so their variables are global.
|
|
9
|
+
export function replaceScriptDependencies(
|
|
10
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
11
|
+
bundle: NamedBundle,
|
|
12
|
+
code: string,
|
|
13
|
+
map: ?SourceMap,
|
|
14
|
+
parcelRequireName: string,
|
|
15
|
+
): string {
|
|
16
|
+
let entry = nullthrows(bundle.getMainEntry());
|
|
17
|
+
let dependencies = bundleGraph.getDependencies(entry);
|
|
18
|
+
|
|
19
|
+
let lineCount = 0;
|
|
20
|
+
let offset = 0;
|
|
21
|
+
let columnStartIndex = 0;
|
|
22
|
+
code = code.replace(/\n|__parcel__require__\(['"](.*?)['"]\)/g, (m, s, i) => {
|
|
23
|
+
if (m === '\n') {
|
|
24
|
+
columnStartIndex = i + offset + 1;
|
|
25
|
+
lineCount++;
|
|
26
|
+
return '\n';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let dep = nullthrows(dependencies.find(d => getSpecifier(d) === s));
|
|
30
|
+
let resolved = nullthrows(bundleGraph.getResolvedAsset(dep, bundle));
|
|
31
|
+
let publicId = bundleGraph.getAssetPublicId(resolved);
|
|
32
|
+
let replacement = `${parcelRequireName}("${publicId}")`;
|
|
33
|
+
if (map) {
|
|
34
|
+
let lengthDifference = replacement.length - m.length;
|
|
35
|
+
if (lengthDifference !== 0) {
|
|
36
|
+
map.offsetColumns(
|
|
37
|
+
lineCount + 1,
|
|
38
|
+
i + offset - columnStartIndex + m.length,
|
|
39
|
+
lengthDifference,
|
|
40
|
+
);
|
|
41
|
+
offset += lengthDifference;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return replacement;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return code;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getSpecifier(dep: Dependency): string {
|
|
52
|
+
if (typeof dep.meta.placeholder === 'string') {
|
|
53
|
+
return dep.meta.placeholder;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return dep.specifier;
|
|
57
|
+
}
|
package/lib/JSPackager.js
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
var _assert = _interopRequireDefault(require("assert"));
|
|
9
|
-
|
|
10
|
-
var _plugin = require("@parcel/plugin");
|
|
11
|
-
|
|
12
|
-
var _fs = _interopRequireDefault(require("fs"));
|
|
13
|
-
|
|
14
|
-
var _scopeHoisting = require("@parcel/scope-hoisting");
|
|
15
|
-
|
|
16
|
-
var _sourceMap = _interopRequireDefault(require("@parcel/source-map"));
|
|
17
|
-
|
|
18
|
-
var _utils = require("@parcel/utils");
|
|
19
|
-
|
|
20
|
-
var _path = _interopRequireDefault(require("path"));
|
|
21
|
-
|
|
22
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
-
|
|
24
|
-
const PRELUDE = _fs.default.readFileSync(_path.default.join(__dirname, 'prelude.js'), 'utf8').trim().replace(/;$/, '');
|
|
25
|
-
|
|
26
|
-
var _default = new _plugin.Packager({
|
|
27
|
-
async package({
|
|
28
|
-
bundle,
|
|
29
|
-
bundleGraph,
|
|
30
|
-
getInlineBundleContents,
|
|
31
|
-
getSourceMapReference,
|
|
32
|
-
options
|
|
33
|
-
}) {
|
|
34
|
-
function replaceReferences({
|
|
35
|
-
contents,
|
|
36
|
-
map
|
|
37
|
-
}) {
|
|
38
|
-
return (0, _utils.replaceInlineReferences)({
|
|
39
|
-
bundle,
|
|
40
|
-
bundleGraph,
|
|
41
|
-
contents,
|
|
42
|
-
getInlineReplacement: (dependency, inlineType, content) => ({
|
|
43
|
-
from: `"${dependency.id}"`,
|
|
44
|
-
to: inlineType === 'string' ? JSON.stringify(content) : content
|
|
45
|
-
}),
|
|
46
|
-
getInlineBundleContents,
|
|
47
|
-
map
|
|
48
|
-
});
|
|
49
|
-
} // If scope hoisting is enabled, we use a different code path.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (options.scopeHoist) {
|
|
53
|
-
let ast = await (0, _scopeHoisting.concat)(bundle, bundleGraph);
|
|
54
|
-
ast = (0, _scopeHoisting.link)({
|
|
55
|
-
bundle,
|
|
56
|
-
bundleGraph,
|
|
57
|
-
ast,
|
|
58
|
-
options
|
|
59
|
-
});
|
|
60
|
-
return replaceReferences({
|
|
61
|
-
contents: (0, _scopeHoisting.generate)(bundleGraph, bundle, ast, options).contents,
|
|
62
|
-
map: null
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (bundle.env.outputFormat === 'esmodule') {
|
|
67
|
-
throw new Error(`esmodule output is not supported without scope hoisting.`);
|
|
68
|
-
} // For development, we just concatenate all of the code together
|
|
69
|
-
// rather then enabling scope hoisting, which would be too slow.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
let codeQueue = new _utils.PromiseQueue({
|
|
73
|
-
maxConcurrent: 32
|
|
74
|
-
});
|
|
75
|
-
let mapQueue = new _utils.PromiseQueue({
|
|
76
|
-
maxConcurrent: 32
|
|
77
|
-
});
|
|
78
|
-
bundle.traverse(node => {
|
|
79
|
-
if (node.type === 'asset') {
|
|
80
|
-
codeQueue.add(() => node.value.getCode());
|
|
81
|
-
mapQueue.add(() => node.value.getMap());
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
let [code, maps] = await Promise.all([codeQueue.run(), mapQueue.run()]);
|
|
85
|
-
let assets = '';
|
|
86
|
-
let i = 0;
|
|
87
|
-
let first = true;
|
|
88
|
-
let map = new _sourceMap.default();
|
|
89
|
-
let lineOffset = (0, _utils.countLines)(PRELUDE);
|
|
90
|
-
let stubsWritten = new Set();
|
|
91
|
-
bundle.traverse(node => {
|
|
92
|
-
let wrapped = first ? '' : ',';
|
|
93
|
-
|
|
94
|
-
if (node.type === 'dependency') {
|
|
95
|
-
let resolved = bundleGraph.getDependencyResolution(node.value);
|
|
96
|
-
|
|
97
|
-
if (resolved && resolved.type !== 'js' && !stubsWritten.has(resolved.id)) {
|
|
98
|
-
// if this is a reference to another javascript asset, we should not include
|
|
99
|
-
// its output, as its contents should already be loaded.
|
|
100
|
-
(0, _assert.default)(!bundle.hasAsset(resolved));
|
|
101
|
-
wrapped += JSON.stringify(resolved.id) + ':[function() {},{}]';
|
|
102
|
-
} else {
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (node.type === 'asset') {
|
|
108
|
-
let asset = node.value;
|
|
109
|
-
(0, _assert.default)(asset.type === 'js', 'all assets in a js bundle must be js assets');
|
|
110
|
-
let deps = {};
|
|
111
|
-
let dependencies = bundleGraph.getDependencies(asset);
|
|
112
|
-
|
|
113
|
-
for (let dep of dependencies) {
|
|
114
|
-
let resolved = bundleGraph.getDependencyResolution(dep);
|
|
115
|
-
|
|
116
|
-
if (resolved) {
|
|
117
|
-
deps[dep.moduleSpecifier] = resolved.id;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
let output = code[i] || '';
|
|
122
|
-
wrapped += JSON.stringify(asset.id) + ':[function(require,module,exports) {\n' + output + '\n},';
|
|
123
|
-
wrapped += JSON.stringify(deps);
|
|
124
|
-
wrapped += ']';
|
|
125
|
-
|
|
126
|
-
if (options.sourceMaps) {
|
|
127
|
-
var _maps$i;
|
|
128
|
-
|
|
129
|
-
let assetMap = (_maps$i = maps[i]) !== null && _maps$i !== void 0 ? _maps$i : _sourceMap.default.generateEmptyMap(_path.default.relative(options.projectRoot, asset.filePath).replace(/\\+/g, '/'), output);
|
|
130
|
-
map.addMap(assetMap, lineOffset);
|
|
131
|
-
lineOffset += (0, _utils.countLines)(output) + 1;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
i++;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
assets += wrapped;
|
|
138
|
-
first = false;
|
|
139
|
-
});
|
|
140
|
-
let entries = bundle.getEntryAssets();
|
|
141
|
-
let interpreter = null;
|
|
142
|
-
let isEntry = !bundleGraph.hasParentBundleOfType(bundle, 'js') || bundle.env.isIsolated();
|
|
143
|
-
|
|
144
|
-
if (isEntry) {
|
|
145
|
-
let entryAsset = entries[entries.length - 1]; // $FlowFixMe
|
|
146
|
-
|
|
147
|
-
interpreter = bundle.target.env.isBrowser() ? null : entryAsset.meta.interpreter;
|
|
148
|
-
} else if (bundle.env.outputFormat === 'global') {
|
|
149
|
-
// The last entry is the main entry, but in async bundles we don't want it to execute until we require it
|
|
150
|
-
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
151
|
-
entries.pop();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
let importScripts = '';
|
|
155
|
-
|
|
156
|
-
if (bundle.env.isWorker()) {
|
|
157
|
-
let bundles = bundleGraph.getSiblingBundles(bundle);
|
|
158
|
-
|
|
159
|
-
for (let b of bundles) {
|
|
160
|
-
importScripts += `importScripts("${(0, _utils.relativeBundlePath)(bundle, b)}");\n`;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
let sourceMapReference = await getSourceMapReference(map);
|
|
165
|
-
return replaceReferences({
|
|
166
|
-
contents: // If the entry asset included a hashbang, repeat it at the top of the bundle
|
|
167
|
-
(interpreter != null ? `#!${interpreter}\n` : '') + importScripts + (PRELUDE + '({' + assets + '},{},' + JSON.stringify(entries.map(asset => asset.id)) + ', ' + 'null' + ')\n\n' + '//# sourceMappingURL=' + sourceMapReference + '\n'),
|
|
168
|
-
map
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
exports.default = _default;
|
package/src/JSPackager.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
// @flow strict-local
|
|
2
|
-
|
|
3
|
-
import invariant from 'assert';
|
|
4
|
-
import {Packager} from '@parcel/plugin';
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
import {concat, link, generate} from '@parcel/scope-hoisting';
|
|
7
|
-
import SourceMap from '@parcel/source-map';
|
|
8
|
-
import {
|
|
9
|
-
countLines,
|
|
10
|
-
PromiseQueue,
|
|
11
|
-
relativeBundlePath,
|
|
12
|
-
replaceInlineReferences,
|
|
13
|
-
} from '@parcel/utils';
|
|
14
|
-
import path from 'path';
|
|
15
|
-
|
|
16
|
-
const PRELUDE = fs
|
|
17
|
-
.readFileSync(path.join(__dirname, 'prelude.js'), 'utf8')
|
|
18
|
-
.trim()
|
|
19
|
-
.replace(/;$/, '');
|
|
20
|
-
|
|
21
|
-
export default new Packager({
|
|
22
|
-
async package({
|
|
23
|
-
bundle,
|
|
24
|
-
bundleGraph,
|
|
25
|
-
getInlineBundleContents,
|
|
26
|
-
getSourceMapReference,
|
|
27
|
-
options,
|
|
28
|
-
}) {
|
|
29
|
-
function replaceReferences({contents, map}) {
|
|
30
|
-
return replaceInlineReferences({
|
|
31
|
-
bundle,
|
|
32
|
-
bundleGraph,
|
|
33
|
-
contents,
|
|
34
|
-
getInlineReplacement: (dependency, inlineType, content) => ({
|
|
35
|
-
from: `"${dependency.id}"`,
|
|
36
|
-
to: inlineType === 'string' ? JSON.stringify(content) : content,
|
|
37
|
-
}),
|
|
38
|
-
getInlineBundleContents,
|
|
39
|
-
map,
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// If scope hoisting is enabled, we use a different code path.
|
|
44
|
-
if (options.scopeHoist) {
|
|
45
|
-
let ast = await concat(bundle, bundleGraph);
|
|
46
|
-
ast = link({bundle, bundleGraph, ast, options});
|
|
47
|
-
return replaceReferences({
|
|
48
|
-
contents: generate(bundleGraph, bundle, ast, options).contents,
|
|
49
|
-
map: null,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (bundle.env.outputFormat === 'esmodule') {
|
|
54
|
-
throw new Error(
|
|
55
|
-
`esmodule output is not supported without scope hoisting.`,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// For development, we just concatenate all of the code together
|
|
60
|
-
// rather then enabling scope hoisting, which would be too slow.
|
|
61
|
-
let codeQueue = new PromiseQueue({maxConcurrent: 32});
|
|
62
|
-
let mapQueue = new PromiseQueue({maxConcurrent: 32});
|
|
63
|
-
bundle.traverse(node => {
|
|
64
|
-
if (node.type === 'asset') {
|
|
65
|
-
codeQueue.add(() => node.value.getCode());
|
|
66
|
-
mapQueue.add(() => node.value.getMap());
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
let [code, maps] = await Promise.all([codeQueue.run(), mapQueue.run()]);
|
|
71
|
-
|
|
72
|
-
let assets = '';
|
|
73
|
-
let i = 0;
|
|
74
|
-
let first = true;
|
|
75
|
-
let map = new SourceMap();
|
|
76
|
-
let lineOffset = countLines(PRELUDE);
|
|
77
|
-
|
|
78
|
-
let stubsWritten = new Set();
|
|
79
|
-
bundle.traverse(node => {
|
|
80
|
-
let wrapped = first ? '' : ',';
|
|
81
|
-
|
|
82
|
-
if (node.type === 'dependency') {
|
|
83
|
-
let resolved = bundleGraph.getDependencyResolution(node.value);
|
|
84
|
-
if (
|
|
85
|
-
resolved &&
|
|
86
|
-
resolved.type !== 'js' &&
|
|
87
|
-
!stubsWritten.has(resolved.id)
|
|
88
|
-
) {
|
|
89
|
-
// if this is a reference to another javascript asset, we should not include
|
|
90
|
-
// its output, as its contents should already be loaded.
|
|
91
|
-
invariant(!bundle.hasAsset(resolved));
|
|
92
|
-
wrapped += JSON.stringify(resolved.id) + ':[function() {},{}]';
|
|
93
|
-
} else {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (node.type === 'asset') {
|
|
99
|
-
let asset = node.value;
|
|
100
|
-
invariant(
|
|
101
|
-
asset.type === 'js',
|
|
102
|
-
'all assets in a js bundle must be js assets',
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
let deps = {};
|
|
106
|
-
let dependencies = bundleGraph.getDependencies(asset);
|
|
107
|
-
for (let dep of dependencies) {
|
|
108
|
-
let resolved = bundleGraph.getDependencyResolution(dep);
|
|
109
|
-
if (resolved) {
|
|
110
|
-
deps[dep.moduleSpecifier] = resolved.id;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
let output = code[i] || '';
|
|
115
|
-
wrapped +=
|
|
116
|
-
JSON.stringify(asset.id) +
|
|
117
|
-
':[function(require,module,exports) {\n' +
|
|
118
|
-
output +
|
|
119
|
-
'\n},';
|
|
120
|
-
wrapped += JSON.stringify(deps);
|
|
121
|
-
wrapped += ']';
|
|
122
|
-
|
|
123
|
-
if (options.sourceMaps) {
|
|
124
|
-
let assetMap =
|
|
125
|
-
maps[i] ??
|
|
126
|
-
SourceMap.generateEmptyMap(
|
|
127
|
-
path
|
|
128
|
-
.relative(options.projectRoot, asset.filePath)
|
|
129
|
-
.replace(/\\+/g, '/'),
|
|
130
|
-
output,
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
map.addMap(assetMap, lineOffset);
|
|
134
|
-
lineOffset += countLines(output) + 1;
|
|
135
|
-
}
|
|
136
|
-
i++;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
assets += wrapped;
|
|
140
|
-
first = false;
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
let entries = bundle.getEntryAssets();
|
|
144
|
-
let interpreter: ?string = null;
|
|
145
|
-
|
|
146
|
-
let isEntry =
|
|
147
|
-
!bundleGraph.hasParentBundleOfType(bundle, 'js') ||
|
|
148
|
-
bundle.env.isIsolated();
|
|
149
|
-
if (isEntry) {
|
|
150
|
-
let entryAsset = entries[entries.length - 1];
|
|
151
|
-
// $FlowFixMe
|
|
152
|
-
interpreter = bundle.target.env.isBrowser()
|
|
153
|
-
? null
|
|
154
|
-
: entryAsset.meta.interpreter;
|
|
155
|
-
} else if (bundle.env.outputFormat === 'global') {
|
|
156
|
-
// The last entry is the main entry, but in async bundles we don't want it to execute until we require it
|
|
157
|
-
// as there might be dependencies in a sibling bundle that hasn't loaded yet.
|
|
158
|
-
entries.pop();
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
let importScripts = '';
|
|
162
|
-
if (bundle.env.isWorker()) {
|
|
163
|
-
let bundles = bundleGraph.getSiblingBundles(bundle);
|
|
164
|
-
for (let b of bundles) {
|
|
165
|
-
importScripts += `importScripts("${relativeBundlePath(bundle, b)}");\n`;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
let sourceMapReference = await getSourceMapReference(map);
|
|
170
|
-
|
|
171
|
-
return replaceReferences({
|
|
172
|
-
contents:
|
|
173
|
-
// If the entry asset included a hashbang, repeat it at the top of the bundle
|
|
174
|
-
(interpreter != null ? `#!${interpreter}\n` : '') +
|
|
175
|
-
importScripts +
|
|
176
|
-
(PRELUDE +
|
|
177
|
-
'({' +
|
|
178
|
-
assets +
|
|
179
|
-
'},{},' +
|
|
180
|
-
JSON.stringify(entries.map(asset => asset.id)) +
|
|
181
|
-
', ' +
|
|
182
|
-
'null' +
|
|
183
|
-
')\n\n' +
|
|
184
|
-
'//# sourceMappingURL=' +
|
|
185
|
-
sourceMapReference +
|
|
186
|
-
'\n'),
|
|
187
|
-
map,
|
|
188
|
-
});
|
|
189
|
-
},
|
|
190
|
-
});
|