@atlaspack/packager-js 2.23.1 → 2.23.2

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.
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.helpers = exports.bundleQueuePrelude = exports.fnExpr = exports.preludeNew = exports.preludeOld = void 0;
4
+ const preludeOld = (parcelRequireName) => `
5
+ var $parcel$modules = {};
6
+ var $parcel$inits = {};
7
+
8
+ var parcelRequire = $parcel$global[${JSON.stringify(parcelRequireName)}];
9
+
10
+ if (parcelRequire == null) {
11
+ parcelRequire = function(id) {
12
+ if (id in $parcel$modules) {
13
+ return $parcel$modules[id].exports;
14
+ }
15
+ if (id in $parcel$inits) {
16
+ var init = $parcel$inits[id];
17
+ delete $parcel$inits[id];
18
+ var module = {id: id, exports: {}};
19
+ $parcel$modules[id] = module;
20
+ init.call(module.exports, module, module.exports);
21
+ return module.exports;
22
+ }
23
+ var err = new Error("Cannot find module '" + id + "'");
24
+ err.code = 'MODULE_NOT_FOUND';
25
+ throw err;
26
+ };
27
+
28
+ parcelRequire.register = function register(id, init) {
29
+ $parcel$inits[id] = init;
30
+ };
31
+
32
+ $parcel$global[${JSON.stringify(parcelRequireName)}] = parcelRequire;
33
+ }
34
+
35
+ var parcelRegister = parcelRequire.register;
36
+ `;
37
+ exports.preludeOld = preludeOld;
38
+ const preludeNew = (parcelRequireName) => `
39
+ var $parcel$modules = {};
40
+ var $parcel$inits = {};
41
+
42
+ var parcelRequire = $parcel$global[${JSON.stringify(parcelRequireName)}];
43
+
44
+ if (parcelRequire == null) {
45
+ parcelRequire = function(id) {
46
+ var mod = $parcel$modules[id];
47
+ if (mod !== undefined) {
48
+ return mod.exports;
49
+ }
50
+ var init = $parcel$inits[id];
51
+ if (init !== undefined) {
52
+ delete $parcel$inits[id];
53
+ var module = {id: id, exports: {}};
54
+ $parcel$modules[id] = module;
55
+ init.call(module.exports, module, module.exports);
56
+ return module.exports;
57
+ }
58
+ var err = new Error("Cannot find module '" + id + "'");
59
+ err.code = 'MODULE_NOT_FOUND';
60
+ throw err;
61
+ };
62
+
63
+ parcelRequire.register = function register(id, init) {
64
+ $parcel$inits[id] = init;
65
+ };
66
+
67
+ $parcel$global[${JSON.stringify(parcelRequireName)}] = parcelRequire;
68
+ }
69
+
70
+ var parcelRegister = parcelRequire.register;
71
+ `;
72
+ exports.preludeNew = preludeNew;
73
+ const fnExpr = (env, params, body) => {
74
+ let block = `{ ${body.join(' ')} }`;
75
+ if (env.supports('arrow-functions')) {
76
+ return `(${params.join(', ')}) => ${block}`;
77
+ }
78
+ return `function (${params.join(', ')}) ${block}`;
79
+ };
80
+ exports.fnExpr = fnExpr;
81
+ const bundleQueuePrelude = (env) => `
82
+ if (!$parcel$global.lb) {
83
+ // Set of loaded bundles
84
+ $parcel$global.lb = new Set();
85
+ // Queue of bundles to execute once they're dep bundles are loaded
86
+ $parcel$global.bq = [];
87
+
88
+ // Register loaded bundle
89
+ $parcel$global.rlb = ${(0, exports.fnExpr)(env, ['bundle'], ['$parcel$global.lb.add(bundle);', '$parcel$global.pq();'])}
90
+
91
+ // Run when ready
92
+ $parcel$global.rwr = ${(0, exports.fnExpr)(env,
93
+ // b = bundle public id
94
+ // r = run function to execute the bundle entry
95
+ // d = list of dependent bundles this bundle requires before executing
96
+ ['b', 'r', 'd'], ['$parcel$global.bq.push({b, r, d});', '$parcel$global.pq();'])}
97
+
98
+ // Process queue
99
+ $parcel$global.pq = ${(0, exports.fnExpr)(env, [], [
100
+ `var runnableEntry = $parcel$global.bq.find(${(0, exports.fnExpr)(env, ['i'], [
101
+ `return i.d.every(${(0, exports.fnExpr)(env, ['dep'], ['return $parcel$global.lb.has(dep);'])});`,
102
+ ])});`,
103
+ 'if (runnableEntry) {',
104
+ `$parcel$global.bq = $parcel$global.bq.filter(${(0, exports.fnExpr)(env, ['i'], ['return i.b !== runnableEntry.b;'])});`,
105
+ 'runnableEntry.r();',
106
+ '$parcel$global.pq();',
107
+ '}',
108
+ ])}
109
+ }
110
+ `;
111
+ exports.bundleQueuePrelude = bundleQueuePrelude;
112
+ const $parcel$export = `
113
+ function $parcel$export(e, n, v, s) {
114
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
115
+ }
116
+ `;
117
+ const $parcel$exportWildcard = `
118
+ function $parcel$exportWildcard(dest, source) {
119
+ Object.keys(source).forEach(function(key) {
120
+ if (key === 'default' || key === '__esModule' || Object.prototype.hasOwnProperty.call(dest, key)) {
121
+ return;
122
+ }
123
+
124
+ Object.defineProperty(dest, key, {
125
+ enumerable: true,
126
+ get: function get() {
127
+ return source[key];
128
+ }
129
+ });
130
+ });
131
+
132
+ return dest;
133
+ }
134
+ `;
135
+ const $parcel$interopDefault = `
136
+ function $parcel$interopDefault(a) {
137
+ return a && a.__esModule ? a.default : a;
138
+ }
139
+ `;
140
+ const $parcel$global = (env) => {
141
+ if (env.supports('global-this')) {
142
+ return `
143
+ var $parcel$global = globalThis;
144
+ `;
145
+ }
146
+ return `
147
+ var $parcel$global =
148
+ typeof globalThis !== 'undefined'
149
+ ? globalThis
150
+ : typeof self !== 'undefined'
151
+ ? self
152
+ : typeof window !== 'undefined'
153
+ ? window
154
+ : typeof global !== 'undefined'
155
+ ? global
156
+ : {};
157
+ `;
158
+ };
159
+ const $parcel$defineInteropFlag = `
160
+ function $parcel$defineInteropFlag(a) {
161
+ Object.defineProperty(a, '__esModule', {value: true, configurable: true});
162
+ }
163
+ `;
164
+ exports.helpers = {
165
+ $parcel$export,
166
+ $parcel$exportWildcard,
167
+ $parcel$interopDefault,
168
+ $parcel$global,
169
+ $parcel$defineInteropFlag,
170
+ };
package/dist/index.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const plugin_1 = require("@atlaspack/plugin");
7
+ const utils_1 = require("@atlaspack/utils");
8
+ const diagnostic_1 = require("@atlaspack/diagnostic");
9
+ const rust_1 = require("@atlaspack/rust");
10
+ const nullthrows_1 = __importDefault(require("nullthrows"));
11
+ const DevPackager_1 = require("./DevPackager");
12
+ const ScopeHoistingPackager_1 = require("./ScopeHoistingPackager");
13
+ const CONFIG_SCHEMA = {
14
+ type: 'object',
15
+ properties: {
16
+ unstable_asyncBundleRuntime: {
17
+ type: 'boolean',
18
+ },
19
+ },
20
+ additionalProperties: false,
21
+ };
22
+ exports.default = new plugin_1.Packager({
23
+ async loadConfig({ config, options }) {
24
+ let packageKey = '@atlaspack/packager-js';
25
+ let conf = await config.getConfigFrom(options.projectRoot + '/index', [], {
26
+ packageKey,
27
+ });
28
+ if (conf?.contents) {
29
+ utils_1.validateSchema.diagnostic(CONFIG_SCHEMA, {
30
+ data: conf?.contents,
31
+ source: await options.inputFS.readFile(conf.filePath, 'utf8'),
32
+ filePath: conf.filePath,
33
+ prependKey: `/${(0, diagnostic_1.encodeJSONKeyComponent)(packageKey)}`,
34
+ }, packageKey, `Invalid config for ${packageKey}`);
35
+ }
36
+ // Generate a name for the global parcelRequire function that is unique to this project.
37
+ // This allows multiple parcel builds to coexist on the same page.
38
+ let packageName = await config.getConfigFrom(options.projectRoot + '/index', [], {
39
+ packageKey: 'name',
40
+ });
41
+ let name = packageName?.contents ?? '';
42
+ return {
43
+ // @ts-expect-error TS2345
44
+ parcelRequireName: 'parcelRequire' + (0, rust_1.hashString)(name).slice(-4),
45
+ unstable_asyncBundleRuntime: Boolean(
46
+ // @ts-expect-error TS2339
47
+ conf?.contents?.unstable_asyncBundleRuntime),
48
+ };
49
+ },
50
+ async package({ bundle, bundleGraph, getInlineBundleContents, getSourceMapReference, config, options, logger, }) {
51
+ // If this is a non-module script, and there is only one asset with no dependencies,
52
+ // then we don't need to package at all and can pass through the original code un-wrapped.
53
+ let contents, map;
54
+ if (bundle.env.sourceType === 'script') {
55
+ let entries = bundle.getEntryAssets();
56
+ if (entries.length === 1 &&
57
+ bundleGraph.getDependencies(entries[0]).length === 0) {
58
+ contents = await entries[0].getCode();
59
+ map = await entries[0].getMap();
60
+ }
61
+ }
62
+ if (contents == null) {
63
+ let packager = bundle.env.shouldScopeHoist
64
+ ? new ScopeHoistingPackager_1.ScopeHoistingPackager(options, bundleGraph, bundle, (0, nullthrows_1.default)(config).parcelRequireName, (0, nullthrows_1.default)(config).unstable_asyncBundleRuntime, logger)
65
+ : new DevPackager_1.DevPackager(options, bundleGraph, bundle, (0, nullthrows_1.default)(config).parcelRequireName);
66
+ ({ contents, map } = await packager.package());
67
+ }
68
+ contents += '\n' + (await getSourceMapSuffix(getSourceMapReference, map));
69
+ // For library builds, we need to replace URL references with their final resolved paths.
70
+ // For non-library builds, this is handled in the JS runtime.
71
+ if (bundle.env.isLibrary) {
72
+ ({ contents, map } = (0, utils_1.replaceURLReferences)({
73
+ bundle,
74
+ bundleGraph,
75
+ contents,
76
+ map,
77
+ getReplacement: (s) => JSON.stringify(s).slice(1, -1),
78
+ }));
79
+ }
80
+ return (0, utils_1.replaceInlineReferences)({
81
+ bundle,
82
+ bundleGraph,
83
+ contents,
84
+ getInlineReplacement: (dependency, inlineType, content) => ({
85
+ from: `"${dependency.id}"`,
86
+ to: inlineType === 'string' ? JSON.stringify(content) : content,
87
+ }),
88
+ getInlineBundleContents,
89
+ map,
90
+ });
91
+ },
92
+ });
93
+ async function getSourceMapSuffix(getSourceMapReference, map) {
94
+ let sourcemapReference = await getSourceMapReference(map);
95
+ if (sourcemapReference != null) {
96
+ return '//# sourceMappingURL=' + sourcemapReference + '\n';
97
+ }
98
+ else {
99
+ return '';
100
+ }
101
+ }
package/dist/utils.js ADDED
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.replaceScriptDependencies = replaceScriptDependencies;
7
+ exports.getSpecifier = getSpecifier;
8
+ exports.isValidIdentifier = isValidIdentifier;
9
+ exports.makeValidIdentifier = makeValidIdentifier;
10
+ const nullthrows_1 = __importDefault(require("nullthrows"));
11
+ // This replaces __parcel__require__ references left by the transformer with
12
+ // parcelRequire calls of the resolved asset id. This lets runtimes work within
13
+ // script bundles, which must be outside the bundle wrapper so their variables are global.
14
+ function replaceScriptDependencies(bundleGraph, bundle, code, map, parcelRequireName) {
15
+ let entry = (0, nullthrows_1.default)(bundle.getMainEntry());
16
+ let dependencies = bundleGraph.getDependencies(entry);
17
+ let lineCount = 0;
18
+ let offset = 0;
19
+ let columnStartIndex = 0;
20
+ code = code.replace(/\n|__parcel__require__\(['"](.*?)['"]\)/g, (m, s, i) => {
21
+ if (m === '\n') {
22
+ columnStartIndex = i + offset + 1;
23
+ lineCount++;
24
+ return '\n';
25
+ }
26
+ let dep = (0, nullthrows_1.default)(dependencies.find((d) => getSpecifier(d) === s));
27
+ let resolved = (0, nullthrows_1.default)(bundleGraph.getResolvedAsset(dep, bundle));
28
+ let publicId = bundleGraph.getAssetPublicId(resolved);
29
+ let replacement = `${parcelRequireName}("${publicId}")`;
30
+ if (map) {
31
+ let lengthDifference = replacement.length - m.length;
32
+ if (lengthDifference !== 0) {
33
+ map.offsetColumns(lineCount + 1, i + offset - columnStartIndex + m.length, lengthDifference);
34
+ offset += lengthDifference;
35
+ }
36
+ }
37
+ return replacement;
38
+ });
39
+ return code;
40
+ }
41
+ function getSpecifier(dep) {
42
+ if (typeof dep.meta.placeholder === 'string') {
43
+ return dep.meta.placeholder;
44
+ }
45
+ return dep.specifier;
46
+ }
47
+ // https://262.ecma-international.org/6.0/#sec-names-and-keywords
48
+ const IDENTIFIER_RE = /^[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}]*$/u;
49
+ const ID_START_RE = /^[$_\p{ID_Start}]/u;
50
+ const NON_ID_CONTINUE_RE = /[^$_\u200C\u200D\p{ID_Continue}]/gu;
51
+ function isValidIdentifier(id) {
52
+ return IDENTIFIER_RE.test(id);
53
+ }
54
+ function makeValidIdentifier(name) {
55
+ name = name.replace(NON_ID_CONTINUE_RE, '');
56
+ if (!ID_START_RE.test(name)) {
57
+ name = '_' + name;
58
+ }
59
+ return name;
60
+ }
@@ -75,7 +75,7 @@ class ESMOutputFormat {
75
75
  local,
76
76
  exportAs
77
77
  } of this.packager.exportedSymbols.values()) {
78
- if (this.packager.wrappedAssets.has(asset.id)) {
78
+ if (this.packager.wrappedAssets.has(asset)) {
79
79
  let obj = `parcelRequire("${this.packager.bundleGraph.getAssetPublicId(asset)}")`;
80
80
  res += `\nvar ${local} = ${this.packager.getPropertyAccess(obj, exportSymbol)};`;
81
81
  lines++;