@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.
@@ -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
- return newRequire(localRequire.resolve(x));
83
+ var res = localRequire.resolve(x);
84
+ return res === false ? {} : newRequire(res);
84
85
  }
85
86
 
86
87
  function resolve(x) {
87
- return modules[name][1][x] || x;
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
@@ -11,9 +11,9 @@ if (parcelRequire == null) {
11
11
  return $parcel$modules[id].exports;
12
12
  }
13
13
  if (id in $parcel$inits) {
14
- let init = $parcel$inits[id];
14
+ var init = $parcel$inits[id];
15
15
  delete $parcel$inits[id];
16
- let module = {id, exports: {}};
16
+ var module = {id: id, exports: {}};
17
17
  $parcel$modules[id] = module;
18
18
  init.call(module.exports, module, module.exports);
19
19
  return module.exports;
@@ -38,7 +38,7 @@ export const helpers = {
38
38
  `,
39
39
  $parcel$exportWildcard: `function $parcel$exportWildcard(dest, source) {
40
40
  Object.keys(source).forEach(function(key) {
41
- if (key === 'default' || key === '__esModule') {
41
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
42
42
  return;
43
43
  }
44
44
 
@@ -46,7 +46,7 @@ export const helpers = {
46
46
  enumerable: true,
47
47
  get: function get() {
48
48
  return source[key];
49
- },
49
+ }
50
50
  });
51
51
  });
52
52
 
package/src/index.js CHANGED
@@ -2,32 +2,24 @@
2
2
  import type {Async} from '@parcel/types';
3
3
  import type SourceMap from '@parcel/source-map';
4
4
  import {Packager} from '@parcel/plugin';
5
- import {
6
- replaceInlineReferences,
7
- md5FromString,
8
- loadConfig,
9
- } from '@parcel/utils';
5
+ import {replaceInlineReferences, replaceURLReferences} from '@parcel/utils';
6
+ import {hashString} from '@parcel/hash';
10
7
  import path from 'path';
11
8
  import nullthrows from 'nullthrows';
12
9
  import {DevPackager} from './DevPackager';
13
10
  import {ScopeHoistingPackager} from './ScopeHoistingPackager';
14
11
 
15
12
  export default (new Packager({
16
- async loadConfig({options}) {
13
+ async loadConfig({config, options}) {
17
14
  // Generate a name for the global parcelRequire function that is unique to this project.
18
15
  // This allows multiple parcel builds to coexist on the same page.
19
- let pkg = await loadConfig(
20
- options.inputFS,
21
- path.join(options.entryRoot, 'index'),
16
+ let pkg = await config.getConfigFrom(
17
+ path.join(options.projectRoot, 'index'),
22
18
  ['package.json'],
23
- options.projectRoot,
24
19
  );
25
- let name = pkg?.config.name ?? '';
20
+ let name = pkg?.contents?.name ?? '';
26
21
  return {
27
- config: {
28
- parcelRequireName: 'parcelRequire' + md5FromString(name).slice(-4),
29
- },
30
- files: pkg?.files ?? [],
22
+ parcelRequireName: 'parcelRequire' + hashString(name).slice(-4),
31
23
  };
32
24
  },
33
25
  async package({
@@ -38,23 +30,51 @@ export default (new Packager({
38
30
  config,
39
31
  options,
40
32
  }) {
41
- let packager = bundle.env.shouldScopeHoist
42
- ? new ScopeHoistingPackager(
43
- options,
44
- bundleGraph,
45
- bundle,
46
- nullthrows(config).parcelRequireName,
47
- )
48
- : new DevPackager(
49
- options,
50
- bundleGraph,
51
- bundle,
52
- nullthrows(config).parcelRequireName,
53
- );
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
+ }
54
64
 
55
- let {contents, map} = await packager.package();
56
65
  contents += '\n' + (await getSourceMapSuffix(getSourceMapReference, map));
57
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
+
58
78
  return replaceInlineReferences({
59
79
  bundle,
60
80
  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
+ }