@parcel/packager-js 2.0.0-beta.3.1 → 2.0.0-canary.1521
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 -110
- package/lib/DevPackager.js +50 -70
- package/lib/ESMOutputFormat.js +26 -38
- package/lib/GlobalOutputFormat.js +3 -11
- package/lib/ScopeHoistingPackager.js +535 -492
- package/lib/dev-prelude.js +10 -8
- package/lib/helpers.js +78 -30
- package/lib/index.js +71 -52
- package/lib/utils.js +51 -0
- package/package.json +9 -7
- package/src/.eslintrc.json +13 -0
- package/src/CJSOutputFormat.js +7 -119
- package/src/DevPackager.js +76 -22
- package/src/ESMOutputFormat.js +34 -5
- package/src/GlobalOutputFormat.js +6 -3
- package/src/ScopeHoistingPackager.js +640 -337
- package/src/dev-prelude.js +10 -8
- package/src/helpers.js +121 -27
- package/src/index.js +89 -26
- package/src/utils.js +57 -0
package/src/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/src/helpers.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
): string => `
|
|
2
|
+
import type {Environment} from '@parcel/types';
|
|
3
|
+
|
|
4
|
+
export const prelude = (parcelRequireName: string): string => `
|
|
5
|
+
var $parcel$modules = {};
|
|
5
6
|
var $parcel$inits = {};
|
|
6
7
|
|
|
7
8
|
var parcelRequire = $parcel$global[${JSON.stringify(parcelRequireName)}];
|
|
9
|
+
|
|
8
10
|
if (parcelRequire == null) {
|
|
9
11
|
parcelRequire = function(id) {
|
|
10
12
|
if (id in $parcel$modules) {
|
|
11
13
|
return $parcel$modules[id].exports;
|
|
12
14
|
}
|
|
13
15
|
if (id in $parcel$inits) {
|
|
14
|
-
|
|
16
|
+
var init = $parcel$inits[id];
|
|
15
17
|
delete $parcel$inits[id];
|
|
16
|
-
|
|
18
|
+
var module = {id: id, exports: {}};
|
|
17
19
|
$parcel$modules[id] = module;
|
|
18
20
|
init.call(module.exports, module, module.exports);
|
|
19
21
|
return module.exports;
|
|
@@ -29,16 +31,88 @@ if (parcelRequire == null) {
|
|
|
29
31
|
|
|
30
32
|
$parcel$global[${JSON.stringify(parcelRequireName)}] = parcelRequire;
|
|
31
33
|
}
|
|
34
|
+
|
|
35
|
+
var parcelRegister = parcelRequire.register;
|
|
32
36
|
`;
|
|
33
37
|
|
|
34
|
-
export const
|
|
35
|
-
|
|
38
|
+
export const fnExpr = (
|
|
39
|
+
env: Environment,
|
|
40
|
+
params: Array<string>,
|
|
41
|
+
body: Array<string>,
|
|
42
|
+
): string => {
|
|
43
|
+
let block = `{ ${body.join(' ')} }`;
|
|
44
|
+
|
|
45
|
+
if (env.supports('arrow-functions')) {
|
|
46
|
+
return `(${params.join(', ')}) => ${block}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return `function (${params.join(', ')}) ${block}`;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const bundleQueuePrelude = (env: Environment): string => `
|
|
53
|
+
if (!$parcel$global.lb) {
|
|
54
|
+
// Set of loaded bundles
|
|
55
|
+
$parcel$global.lb = new Set();
|
|
56
|
+
// Queue of bundles to execute once they're dep bundles are loaded
|
|
57
|
+
$parcel$global.bq = [];
|
|
58
|
+
|
|
59
|
+
// Register loaded bundle
|
|
60
|
+
$parcel$global.rlb = ${fnExpr(
|
|
61
|
+
env,
|
|
62
|
+
['bundle'],
|
|
63
|
+
['$parcel$global.lb.add(bundle);', '$parcel$global.pq();'],
|
|
64
|
+
)}
|
|
65
|
+
|
|
66
|
+
// Run when ready
|
|
67
|
+
$parcel$global.rwr = ${fnExpr(
|
|
68
|
+
env,
|
|
69
|
+
// b = bundle public id
|
|
70
|
+
// r = run function to execute the bundle entry
|
|
71
|
+
// d = list of dependent bundles this bundle requires before executing
|
|
72
|
+
['b', 'r', 'd'],
|
|
73
|
+
['$parcel$global.bq.push({b, r, d});', '$parcel$global.pq();'],
|
|
74
|
+
)}
|
|
75
|
+
|
|
76
|
+
// Process queue
|
|
77
|
+
$parcel$global.pq = ${fnExpr(
|
|
78
|
+
env,
|
|
79
|
+
[],
|
|
80
|
+
[
|
|
81
|
+
`var runnableEntry = $parcel$global.bq.find(${fnExpr(
|
|
82
|
+
env,
|
|
83
|
+
['i'],
|
|
84
|
+
[
|
|
85
|
+
`return i.d.every(${fnExpr(
|
|
86
|
+
env,
|
|
87
|
+
['dep'],
|
|
88
|
+
['return $parcel$global.lb.has(dep);'],
|
|
89
|
+
)});`,
|
|
90
|
+
],
|
|
91
|
+
)});`,
|
|
92
|
+
'if (runnableEntry) {',
|
|
93
|
+
`$parcel$global.bq = $parcel$global.bq.filter(${fnExpr(
|
|
94
|
+
env,
|
|
95
|
+
['i'],
|
|
96
|
+
['return i.b !== runnableEntry.b;'],
|
|
97
|
+
)});`,
|
|
98
|
+
'runnableEntry.r();',
|
|
99
|
+
'$parcel$global.pq();',
|
|
100
|
+
'}',
|
|
101
|
+
],
|
|
102
|
+
)}
|
|
103
|
+
}
|
|
104
|
+
`;
|
|
105
|
+
|
|
106
|
+
const $parcel$export = `
|
|
107
|
+
function $parcel$export(e, n, v, s) {
|
|
36
108
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
37
109
|
}
|
|
38
|
-
|
|
39
|
-
|
|
110
|
+
`;
|
|
111
|
+
|
|
112
|
+
const $parcel$exportWildcard = `
|
|
113
|
+
function $parcel$exportWildcard(dest, source) {
|
|
40
114
|
Object.keys(source).forEach(function(key) {
|
|
41
|
-
if (key === 'default' || key === '__esModule') {
|
|
115
|
+
if (key === 'default' || key === '__esModule' || Object.prototype.hasOwnProperty.call(dest, key)) {
|
|
42
116
|
return;
|
|
43
117
|
}
|
|
44
118
|
|
|
@@ -46,30 +120,50 @@ export const helpers = {
|
|
|
46
120
|
enumerable: true,
|
|
47
121
|
get: function get() {
|
|
48
122
|
return source[key];
|
|
49
|
-
}
|
|
123
|
+
}
|
|
50
124
|
});
|
|
51
125
|
});
|
|
52
126
|
|
|
53
127
|
return dest;
|
|
54
128
|
}
|
|
55
|
-
|
|
56
|
-
|
|
129
|
+
`;
|
|
130
|
+
|
|
131
|
+
const $parcel$interopDefault = `
|
|
132
|
+
function $parcel$interopDefault(a) {
|
|
57
133
|
return a && a.__esModule ? a.default : a;
|
|
58
134
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
135
|
+
`;
|
|
136
|
+
|
|
137
|
+
const $parcel$global = (env: Environment): string => {
|
|
138
|
+
if (env.supports('global-this')) {
|
|
139
|
+
return `
|
|
140
|
+
var $parcel$global = globalThis;
|
|
141
|
+
`;
|
|
142
|
+
}
|
|
143
|
+
return `
|
|
144
|
+
var $parcel$global =
|
|
145
|
+
typeof globalThis !== 'undefined'
|
|
146
|
+
? globalThis
|
|
147
|
+
: typeof self !== 'undefined'
|
|
148
|
+
? self
|
|
149
|
+
: typeof window !== 'undefined'
|
|
150
|
+
? window
|
|
151
|
+
: typeof global !== 'undefined'
|
|
152
|
+
? global
|
|
153
|
+
: {};
|
|
154
|
+
`;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const $parcel$defineInteropFlag = `
|
|
158
|
+
function $parcel$defineInteropFlag(a) {
|
|
72
159
|
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
73
160
|
}
|
|
74
|
-
|
|
161
|
+
`;
|
|
162
|
+
|
|
163
|
+
export const helpers = {
|
|
164
|
+
$parcel$export,
|
|
165
|
+
$parcel$exportWildcard,
|
|
166
|
+
$parcel$interopDefault,
|
|
167
|
+
$parcel$global,
|
|
168
|
+
$parcel$defineInteropFlag,
|
|
75
169
|
};
|
package/src/index.js
CHANGED
|
@@ -4,30 +4,63 @@ import type SourceMap from '@parcel/source-map';
|
|
|
4
4
|
import {Packager} from '@parcel/plugin';
|
|
5
5
|
import {
|
|
6
6
|
replaceInlineReferences,
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
replaceURLReferences,
|
|
8
|
+
validateSchema,
|
|
9
|
+
type SchemaEntity,
|
|
9
10
|
} from '@parcel/utils';
|
|
11
|
+
import {encodeJSONKeyComponent} from '@parcel/diagnostic';
|
|
12
|
+
import {hashString} from '@parcel/rust';
|
|
10
13
|
import path from 'path';
|
|
11
14
|
import nullthrows from 'nullthrows';
|
|
12
15
|
import {DevPackager} from './DevPackager';
|
|
13
16
|
import {ScopeHoistingPackager} from './ScopeHoistingPackager';
|
|
14
17
|
|
|
18
|
+
type JSPackagerConfig = {|
|
|
19
|
+
parcelRequireName: string,
|
|
20
|
+
unstable_asyncBundleRuntime: boolean,
|
|
21
|
+
|};
|
|
22
|
+
|
|
23
|
+
const CONFIG_SCHEMA: SchemaEntity = {
|
|
24
|
+
type: 'object',
|
|
25
|
+
properties: {
|
|
26
|
+
unstable_asyncBundleRuntime: {
|
|
27
|
+
type: 'boolean',
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
additionalProperties: false,
|
|
31
|
+
};
|
|
32
|
+
|
|
15
33
|
export default (new Packager({
|
|
16
|
-
async loadConfig({options}) {
|
|
34
|
+
async loadConfig({config, options}): Promise<JSPackagerConfig> {
|
|
17
35
|
// Generate a name for the global parcelRequire function that is unique to this project.
|
|
18
36
|
// This allows multiple parcel builds to coexist on the same page.
|
|
19
|
-
let pkg = await
|
|
20
|
-
options.
|
|
21
|
-
path.join(options.entryRoot, 'index'),
|
|
37
|
+
let pkg = await config.getConfigFrom(
|
|
38
|
+
path.join(options.projectRoot, 'index'),
|
|
22
39
|
['package.json'],
|
|
23
|
-
options.projectRoot,
|
|
24
40
|
);
|
|
25
|
-
|
|
41
|
+
|
|
42
|
+
let packageKey = '@parcel/packager-js';
|
|
43
|
+
|
|
44
|
+
if (pkg?.contents[packageKey]) {
|
|
45
|
+
validateSchema.diagnostic(
|
|
46
|
+
CONFIG_SCHEMA,
|
|
47
|
+
{
|
|
48
|
+
data: pkg?.contents[packageKey],
|
|
49
|
+
source: await options.inputFS.readFile(pkg.filePath, 'utf8'),
|
|
50
|
+
filePath: pkg.filePath,
|
|
51
|
+
prependKey: `/${encodeJSONKeyComponent(packageKey)}`,
|
|
52
|
+
},
|
|
53
|
+
packageKey,
|
|
54
|
+
`Invalid config for ${packageKey}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let name = pkg?.contents?.name ?? '';
|
|
26
59
|
return {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
60
|
+
parcelRequireName: 'parcelRequire' + hashString(name).slice(-4),
|
|
61
|
+
unstable_asyncBundleRuntime: Boolean(
|
|
62
|
+
pkg?.contents[packageKey]?.unstable_asyncBundleRuntime,
|
|
63
|
+
),
|
|
31
64
|
};
|
|
32
65
|
},
|
|
33
66
|
async package({
|
|
@@ -38,23 +71,53 @@ export default (new Packager({
|
|
|
38
71
|
config,
|
|
39
72
|
options,
|
|
40
73
|
}) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
74
|
+
// If this is a non-module script, and there is only one asset with no dependencies,
|
|
75
|
+
// then we don't need to package at all and can pass through the original code un-wrapped.
|
|
76
|
+
let contents, map;
|
|
77
|
+
if (bundle.env.sourceType === 'script') {
|
|
78
|
+
let entries = bundle.getEntryAssets();
|
|
79
|
+
if (
|
|
80
|
+
entries.length === 1 &&
|
|
81
|
+
bundleGraph.getDependencies(entries[0]).length === 0
|
|
82
|
+
) {
|
|
83
|
+
contents = await entries[0].getCode();
|
|
84
|
+
map = await entries[0].getMap();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (contents == null) {
|
|
89
|
+
let packager = bundle.env.shouldScopeHoist
|
|
90
|
+
? new ScopeHoistingPackager(
|
|
91
|
+
options,
|
|
92
|
+
bundleGraph,
|
|
93
|
+
bundle,
|
|
94
|
+
nullthrows(config).parcelRequireName,
|
|
95
|
+
nullthrows(config).unstable_asyncBundleRuntime,
|
|
96
|
+
)
|
|
97
|
+
: new DevPackager(
|
|
98
|
+
options,
|
|
99
|
+
bundleGraph,
|
|
100
|
+
bundle,
|
|
101
|
+
nullthrows(config).parcelRequireName,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
({contents, map} = await packager.package());
|
|
105
|
+
}
|
|
54
106
|
|
|
55
|
-
let {contents, map} = await packager.package();
|
|
56
107
|
contents += '\n' + (await getSourceMapSuffix(getSourceMapReference, map));
|
|
57
108
|
|
|
109
|
+
// 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
|
+
if (bundle.env.isLibrary) {
|
|
112
|
+
({contents, map} = replaceURLReferences({
|
|
113
|
+
bundle,
|
|
114
|
+
bundleGraph,
|
|
115
|
+
contents,
|
|
116
|
+
map,
|
|
117
|
+
getReplacement: s => JSON.stringify(s).slice(1, -1),
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
|
|
58
121
|
return replaceInlineReferences({
|
|
59
122
|
bundle,
|
|
60
123
|
bundleGraph,
|
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
|
+
}
|