@parcel/packager-js 2.0.0-nightly.97 → 2.1.0

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.
@@ -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, cache, entry, globalName) {
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.parcelRequire === 'function' &&
26
- globalObject.parcelRequire;
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 parcelRequire === 'function' && parcelRequire;
42
+ typeof globalObject[parcelRequireName] === 'function' &&
43
+ globalObject[parcelRequireName];
41
44
  if (!jumped && currentRequire) {
42
45
  return currentRequire(name, true);
43
46
  }
@@ -77,11 +80,13 @@
77
80
  return cache[name].exports;
78
81
 
79
82
  function localRequire(x) {
80
- return newRequire(localRequire.resolve(x));
83
+ var res = localRequire.resolve(x);
84
+ return res === false ? {} : newRequire(res);
81
85
  }
82
86
 
83
87
  function resolve(x) {
84
- return modules[name][1][x] || x;
88
+ var id = modules[name][1][x];
89
+ return id != null ? id : x;
85
90
  }
86
91
  }
87
92
 
@@ -96,25 +101,31 @@
96
101
  newRequire.modules = modules;
97
102
  newRequire.cache = cache;
98
103
  newRequire.parent = previousRequire;
99
- newRequire.register = function(id, exports) {
104
+ newRequire.register = function (id, exports) {
100
105
  modules[id] = [
101
- function(require, module) {
106
+ function (require, module) {
102
107
  module.exports = exports;
103
108
  },
104
109
  {},
105
110
  ];
106
111
  };
107
112
 
108
- globalObject.parcelRequire = newRequire;
113
+ Object.defineProperty(newRequire, 'root', {
114
+ get: function () {
115
+ return globalObject[parcelRequireName];
116
+ },
117
+ });
118
+
119
+ globalObject[parcelRequireName] = newRequire;
109
120
 
110
121
  for (var i = 0; i < entry.length; i++) {
111
122
  newRequire(entry[i]);
112
123
  }
113
124
 
114
- if (entry.length) {
125
+ if (mainEntry) {
115
126
  // Expose entry point to Node, AMD or browser globals
116
127
  // Based on https://github.com/ForbesLindesay/umd/blob/master/template.js
117
- var mainExports = newRequire(entry[entry.length - 1]);
128
+ var mainExports = newRequire(mainEntry);
118
129
 
119
130
  // CommonJS
120
131
  if (typeof exports === 'object' && typeof module !== 'undefined') {
@@ -122,7 +133,7 @@
122
133
 
123
134
  // RequireJS
124
135
  } else if (typeof define === 'function' && define.amd) {
125
- define(function() {
136
+ define(function () {
126
137
  return mainExports;
127
138
  });
128
139
 
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.0-nightly.97+a63f3fc9",
3
+ "version": "2.1.0",
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/JSPackager.js",
13
- "source": "src/JSPackager.js",
16
+ "main": "lib/index.js",
17
+ "source": "src/index.js",
14
18
  "engines": {
15
- "node": ">= 10.0.0",
16
- "parcel": "^2.0.0-alpha.1.1"
19
+ "node": ">= 12.0.0",
20
+ "parcel": "^2.1.0"
17
21
  },
18
22
  "dependencies": {
19
- "@parcel/plugin": "2.0.0-nightly.97+a63f3fc9",
20
- "@parcel/scope-hoisting": "2.0.0-nightly.97+a63f3fc9",
21
- "@parcel/source-map": "2.0.0-nightly.97+a63f3fc9",
22
- "@parcel/utils": "2.0.0-nightly.97+a63f3fc9"
23
+ "@parcel/diagnostic": "^2.1.0",
24
+ "@parcel/hash": "^2.1.0",
25
+ "@parcel/plugin": "^2.1.0",
26
+ "@parcel/source-map": "^2.0.0",
27
+ "@parcel/utils": "^2.1.0",
28
+ "globals": "^13.2.0",
29
+ "nullthrows": "^1.1.1"
23
30
  },
24
- "gitHead": "a63f3fc9726483219412920faeb255e035f90747"
31
+ "gitHead": "5a80fbe4af8797d0aab248a66f5254f42f00e83e"
25
32
  }
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "@parcel/eslint-config",
3
+ "overrides": [
4
+ {
5
+ "files": ["dev-prelude.js"],
6
+ "parser": "espree",
7
+ "parserOptions": {
8
+ "ecmaVersion": 5,
9
+ "sourceType": "script"
10
+ }
11
+ }
12
+ ]
13
+ }
@@ -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
+ }