@parcel/utils 2.0.0-nightly.122 → 2.0.0-nightly.1221

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.
Files changed (101) hide show
  1. package/.eslintrc.js +6 -6
  2. package/lib/index.js +36742 -307
  3. package/lib/index.js.map +1 -0
  4. package/package.json +43 -20
  5. package/src/DefaultMap.js +1 -1
  6. package/src/PromiseQueue.js +16 -12
  7. package/src/alternatives.js +143 -0
  8. package/src/ansi-html.js +2 -2
  9. package/src/blob.js +2 -1
  10. package/src/bundle-url.js +1 -1
  11. package/src/collection.js +26 -14
  12. package/src/config.js +101 -36
  13. package/src/countLines.js +5 -2
  14. package/src/debounce.js +1 -1
  15. package/src/dependency-location.js +11 -6
  16. package/src/generateBuildMetrics.js +158 -0
  17. package/src/generateCertificate.js +1 -1
  18. package/src/getCertificate.js +1 -1
  19. package/src/getExisting.js +1 -4
  20. package/src/getModuleParts.js +23 -0
  21. package/src/getRootDir.js +1 -2
  22. package/src/glob.js +39 -10
  23. package/src/hash.js +34 -0
  24. package/src/http-server.js +29 -19
  25. package/src/index.js +55 -21
  26. package/src/is-url.js +1 -1
  27. package/src/isDirectoryInside.js +11 -0
  28. package/src/openInBrowser.js +64 -0
  29. package/src/path.js +38 -6
  30. package/src/prettyDiagnostic.js +70 -24
  31. package/src/progress-message.js +22 -0
  32. package/src/relativeBundlePath.js +8 -13
  33. package/src/replaceBundleReferences.js +85 -41
  34. package/src/schema.js +101 -44
  35. package/src/shared-buffer.js +24 -0
  36. package/src/sourcemap.js +135 -0
  37. package/src/stream.js +31 -1
  38. package/src/urlJoin.js +3 -1
  39. package/test/DefaultMap.test.js +7 -4
  40. package/test/config.test.js +98 -0
  41. package/test/input/config/.testrc +3 -0
  42. package/test/input/config/config.cjs +3 -0
  43. package/test/input/config/config.js +3 -0
  44. package/test/input/config/config.json +3 -0
  45. package/test/input/config/empty.json +0 -0
  46. package/test/input/config/empty.toml +0 -0
  47. package/test/input/sourcemap/referenced-min.js +2 -0
  48. package/test/input/sourcemap/referenced-min.js.map +6 -0
  49. package/test/input/sourcemap/source-root.js +2 -0
  50. package/test/input/sourcemap/source-root.js.map +7 -0
  51. package/test/replaceBundleReferences.test.js +268 -0
  52. package/test/sourcemap.test.js +207 -0
  53. package/test/throttle.test.js +1 -2
  54. package/test/urlJoin.test.js +37 -0
  55. package/lib/DefaultMap.js +0 -64
  56. package/lib/Deferred.js +0 -26
  57. package/lib/PromiseQueue.js +0 -133
  58. package/lib/TapStream.js +0 -41
  59. package/lib/ansi-html.js +0 -16
  60. package/lib/blob.js +0 -31
  61. package/lib/bundle-url.js +0 -43
  62. package/lib/collection.js +0 -62
  63. package/lib/config.js +0 -88
  64. package/lib/countLines.js +0 -18
  65. package/lib/debounce.js +0 -20
  66. package/lib/dependency-location.js +0 -21
  67. package/lib/escape-html.js +0 -24
  68. package/lib/generateBundleReport.js +0 -38
  69. package/lib/generateCertificate.js +0 -124
  70. package/lib/getCertificate.js +0 -19
  71. package/lib/getExisting.js +0 -23
  72. package/lib/getRootDir.js +0 -55
  73. package/lib/glob.js +0 -76
  74. package/lib/http-server.js +0 -64
  75. package/lib/is-url.js +0 -17
  76. package/lib/loadSourceMapUrl.js +0 -33
  77. package/lib/md5.js +0 -35
  78. package/lib/objectHash.js +0 -26
  79. package/lib/parseCSSImport.js +0 -16
  80. package/lib/path.js +0 -22
  81. package/lib/prettifyTime.js +0 -10
  82. package/lib/prettyDiagnostic.js +0 -57
  83. package/lib/promisify.js +0 -13
  84. package/lib/relativeBundlePath.js +0 -24
  85. package/lib/relativeUrl.js +0 -16
  86. package/lib/replaceBundleReferences.js +0 -151
  87. package/lib/resolve.js +0 -93
  88. package/lib/schema.js +0 -320
  89. package/lib/serializeObject.js +0 -28
  90. package/lib/stream.js +0 -51
  91. package/lib/throttle.js +0 -16
  92. package/lib/urlJoin.js +0 -27
  93. package/src/.babelrc +0 -3
  94. package/src/generateBundleReport.js +0 -51
  95. package/src/loadSourceMapUrl.js +0 -33
  96. package/src/md5.js +0 -44
  97. package/src/promisify.js +0 -13
  98. package/src/resolve.js +0 -135
  99. package/src/serializeObject.js +0 -22
  100. package/test/input/sourcemap/referenced.js +0 -7
  101. package/test/loadSourceMapUrl.test.js +0 -37
package/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "@parcel/utils",
3
- "version": "2.0.0-nightly.122+04f2e662",
3
+ "version": "2.0.0-nightly.1221+3437f64e4",
4
4
  "description": "Blazing fast, zero configuration web application bundler",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
+ "funding": {
10
+ "type": "opencollective",
11
+ "url": "https://opencollective.com/parcel"
12
+ },
9
13
  "repository": {
10
14
  "type": "git",
11
15
  "url": "https://github.com/parcel-bundler/parcel.git"
@@ -13,32 +17,51 @@
13
17
  "main": "lib/index.js",
14
18
  "source": "src/index.js",
15
19
  "engines": {
16
- "node": ">= 10.0.0"
20
+ "node": ">= 12.0.0"
21
+ },
22
+ "targets": {
23
+ "main": {
24
+ "includeNodeModules": {
25
+ "@parcel/codeframe": false,
26
+ "@parcel/diagnostic": false,
27
+ "@parcel/hash": false,
28
+ "@parcel/logger": false,
29
+ "@parcel/markdown-ansi": false,
30
+ "@parcel/source-map": false,
31
+ "chalk": false
32
+ }
33
+ }
17
34
  },
18
35
  "dependencies": {
36
+ "@parcel/codeframe": "2.0.0-nightly.1221+3437f64e4",
37
+ "@parcel/diagnostic": "2.0.0-nightly.1221+3437f64e4",
38
+ "@parcel/hash": "2.8.3-nightly.2844+3437f64e4",
39
+ "@parcel/logger": "2.0.0-nightly.1221+3437f64e4",
40
+ "@parcel/markdown-ansi": "2.0.0-nightly.1221+3437f64e4",
41
+ "@parcel/source-map": "^2.1.1",
42
+ "chalk": "^4.1.0"
43
+ },
44
+ "devDependencies": {
19
45
  "@iarna/toml": "^2.2.0",
20
- "@parcel/codeframe": "2.0.0-nightly.122+04f2e662",
21
- "@parcel/diagnostic": "2.0.0-nightly.122+04f2e662",
22
- "@parcel/logger": "2.0.0-nightly.122+04f2e662",
23
- "@parcel/markdown-ansi": "2.0.0-nightly.122+04f2e662",
24
- "ansi-html": "^0.0.7",
25
- "chalk": "^2.4.2",
46
+ "ansi-html-community": "0.0.8",
26
47
  "clone": "^2.1.1",
27
- "fast-glob": "^3.0.4",
48
+ "fast-glob": "3.1.1",
49
+ "fastest-levenshtein": "^1.0.8",
28
50
  "is-glob": "^4.0.0",
29
51
  "is-url": "^1.2.2",
30
- "js-levenshtein": "^1.1.6",
31
- "json5": "^1.0.1",
32
- "micromatch": "^4.0.2",
33
- "node-forge": "^0.8.1",
52
+ "json5": "^2.2.0",
53
+ "lru-cache": "^6.0.0",
54
+ "micromatch": "^4.0.4",
55
+ "node-forge": "^1.2.1",
34
56
  "nullthrows": "^1.1.1",
35
- "resolve": "^1.12.0",
36
- "serialize-to-js": "^3.0.1",
37
- "terser": "^3.7.3"
57
+ "open": "^7.0.3",
58
+ "random-int": "^1.0.0",
59
+ "terminal-link": "^2.1.1"
38
60
  },
39
- "devDependencies": {
40
- "@babel/plugin-transform-flow-strip-types": "^7.2.0",
41
- "random-int": "^1.0.0"
61
+ "browser": {
62
+ "./src/generateCertificate.js": false,
63
+ "./src/http-server.js": false,
64
+ "./src/openInBrowser.js": false
42
65
  },
43
- "gitHead": "04f2e662b571a9caba58363aa0ce3303a282e281"
66
+ "gitHead": "3437f64e4c6f57d911614c480b742e229149f200"
44
67
  }
package/src/DefaultMap.js CHANGED
@@ -24,7 +24,7 @@ export class DefaultMap<K, V> extends Map<K, V> {
24
24
 
25
25
  // Duplicated from DefaultMap implementation for Flow
26
26
  // Roughly mirrors https://github.com/facebook/flow/blob/2eb5a78d92c167117ba9caae070afd2b9f598599/lib/core.js#L617
27
- export class DefaultWeakMap<K: {...}, V> extends WeakMap<K, V> {
27
+ export class DefaultWeakMap<K: interface {}, V> extends WeakMap<K, V> {
28
28
  _getDefault: K => V;
29
29
 
30
30
  constructor(getDefault: K => V, entries?: Iterable<[K, V]>) {
@@ -10,6 +10,7 @@ export default class PromiseQueue<T> {
10
10
  _numRunning: number = 0;
11
11
  _queue: Array<() => Promise<void>> = [];
12
12
  _runPromise: ?Promise<Array<T>> = null;
13
+ _error: mixed;
13
14
  _count: number = 0;
14
15
  _results: Array<T> = [];
15
16
 
@@ -74,7 +75,7 @@ export default class PromiseQueue<T> {
74
75
  if (this._queue.length) {
75
76
  this._next();
76
77
  } else if (this._numRunning === 0) {
77
- this._resolve();
78
+ this._done();
78
79
  }
79
80
  }
80
81
 
@@ -82,10 +83,15 @@ export default class PromiseQueue<T> {
82
83
  this._numRunning++;
83
84
  try {
84
85
  await fn();
85
- this._numRunning--;
86
86
  } catch (e) {
87
- this._reject(e);
88
- // rejecting resets state so numRunning is reset to 0 here
87
+ // Only store the first error that occurs.
88
+ // We don't reject immediately so that any other concurrent
89
+ // requests have time to complete.
90
+ if (this._error == null) {
91
+ this._error = e;
92
+ }
93
+ } finally {
94
+ this._numRunning--;
89
95
  }
90
96
  }
91
97
 
@@ -98,17 +104,15 @@ export default class PromiseQueue<T> {
98
104
  this._deferred = null;
99
105
  }
100
106
 
101
- _reject(err: mixed): void {
107
+ _done(): void {
102
108
  if (this._deferred != null) {
103
- this._deferred.reject(err);
109
+ if (this._error != null) {
110
+ this._deferred.reject(this._error);
111
+ } else {
112
+ this._deferred.resolve(this._results);
113
+ }
104
114
  }
105
- this._resetState();
106
- }
107
115
 
108
- _resolve(): void {
109
- if (this._deferred != null) {
110
- this._deferred.resolve(this._results);
111
- }
112
116
  this._resetState();
113
117
  }
114
118
  }
@@ -0,0 +1,143 @@
1
+ // @flow
2
+ import path from 'path';
3
+ import type {FileSystem} from '@parcel/fs';
4
+ import {fuzzySearch} from './schema';
5
+ import {relativePath} from './path';
6
+ import {resolveConfig} from './config';
7
+
8
+ export async function findAlternativeNodeModules(
9
+ fs: FileSystem,
10
+ moduleName: string,
11
+ dir: string,
12
+ ): Promise<Array<string>> {
13
+ let potentialModules: Array<string> = [];
14
+ let root = path.parse(dir).root;
15
+ let isOrganisationModule = moduleName.startsWith('@');
16
+
17
+ while (dir !== root) {
18
+ // Skip node_modules directories
19
+ if (path.basename(dir) === 'node_modules') {
20
+ dir = path.dirname(dir);
21
+ }
22
+
23
+ try {
24
+ let modulesDir = path.join(dir, 'node_modules');
25
+ let stats = await fs.stat(modulesDir);
26
+ if (stats.isDirectory()) {
27
+ let dirContent = (await fs.readdir(modulesDir)).sort();
28
+
29
+ // Filter out the modules that interest us
30
+ let modules = dirContent.filter(i =>
31
+ isOrganisationModule ? i.startsWith('@') : !i.startsWith('@'),
32
+ );
33
+
34
+ // If it's an organisation module, loop through all the modules of that organisation
35
+ if (isOrganisationModule) {
36
+ await Promise.all(
37
+ modules.map(async item => {
38
+ let orgDirPath = path.join(modulesDir, item);
39
+ let orgDirContent = (await fs.readdir(orgDirPath)).sort();
40
+
41
+ // Add all org packages
42
+ potentialModules.push(...orgDirContent.map(i => `${item}/${i}`));
43
+ }),
44
+ );
45
+ }
46
+ }
47
+ } catch (err) {
48
+ // ignore
49
+ }
50
+
51
+ // Move up a directory
52
+ dir = path.dirname(dir);
53
+ }
54
+
55
+ return fuzzySearch(potentialModules.sort(), moduleName).slice(0, 2);
56
+ }
57
+
58
+ async function findAllFilesUp({
59
+ fs,
60
+ dir,
61
+ root,
62
+ basedir,
63
+ maxlength,
64
+ collected,
65
+ leadingDotSlash = true,
66
+ includeDirectories = true,
67
+ }: {|
68
+ fs: FileSystem,
69
+ dir: string,
70
+ root: string,
71
+ basedir: string,
72
+ maxlength: number,
73
+ collected: Array<string>,
74
+ leadingDotSlash?: boolean,
75
+ includeDirectories?: boolean,
76
+ |}): Promise<mixed> {
77
+ let dirContent = (await fs.readdir(dir)).sort();
78
+ return Promise.all(
79
+ dirContent.map(async item => {
80
+ let fullPath = path.join(dir, item);
81
+ let relativeFilePath = relativePath(basedir, fullPath, leadingDotSlash);
82
+ if (relativeFilePath.length < maxlength) {
83
+ let stats = await fs.stat(fullPath);
84
+ let isDir = stats.isDirectory();
85
+ if ((isDir && includeDirectories) || stats.isFile()) {
86
+ collected.push(relativeFilePath);
87
+ }
88
+
89
+ // If it's a directory, run over each item within said directory...
90
+ if (isDir) {
91
+ return findAllFilesUp({
92
+ fs,
93
+ dir: fullPath,
94
+ root,
95
+ basedir,
96
+ maxlength,
97
+ collected,
98
+ });
99
+ }
100
+ }
101
+ }),
102
+ );
103
+ }
104
+
105
+ export async function findAlternativeFiles(
106
+ fs: FileSystem,
107
+ fileSpecifier: string,
108
+ dir: string,
109
+ projectRoot: string,
110
+ leadingDotSlash?: boolean = true,
111
+ includeDirectories?: boolean = true,
112
+ includeExtension?: boolean = false,
113
+ ): Promise<Array<string>> {
114
+ let potentialFiles: Array<string> = [];
115
+ // Find our root, we won't recommend files above the package root as that's bad practise
116
+ let pkg = await resolveConfig(
117
+ fs,
118
+ path.join(dir, 'index'),
119
+ ['package.json'],
120
+ projectRoot,
121
+ );
122
+
123
+ let pkgRoot = pkg ? path.dirname(pkg) : projectRoot;
124
+ await findAllFilesUp({
125
+ fs,
126
+ dir: pkgRoot,
127
+ root: pkgRoot,
128
+ basedir: dir,
129
+ maxlength: fileSpecifier.length + 10,
130
+ collected: potentialFiles,
131
+ leadingDotSlash,
132
+ includeDirectories,
133
+ });
134
+
135
+ if (path.extname(fileSpecifier) === '' && !includeExtension) {
136
+ potentialFiles = potentialFiles.map(p => {
137
+ let ext = path.extname(p);
138
+ return ext.length > 0 ? p.slice(0, -ext.length) : p;
139
+ });
140
+ }
141
+
142
+ return fuzzySearch(potentialFiles, fileSpecifier).slice(0, 2);
143
+ }
package/src/ansi-html.js CHANGED
@@ -1,5 +1,5 @@
1
- // @flow
2
- import ansiHTML from 'ansi-html';
1
+ // @flow strict-local
2
+ import ansiHTML from 'ansi-html-community';
3
3
  import {escapeHTML} from './escape-html';
4
4
 
5
5
  export function ansiHtml(ansi: string): string {
package/src/blob.js CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  import type {Blob} from '@parcel/types';
4
4
 
5
- import {bufferStream} from '../';
5
+ import {Buffer} from 'buffer';
6
+ import {bufferStream} from './';
6
7
  import {Readable} from 'stream';
7
8
 
8
9
  export function blobToBuffer(blob: Blob): Promise<Buffer> {
package/src/bundle-url.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // @flow strict-local
2
2
 
3
3
  let bundleURL: ?string = null;
4
- function getBundleURLCached() {
4
+ function getBundleURLCached(): string {
5
5
  if (bundleURL == null) {
6
6
  bundleURL = _getBundleURL();
7
7
  }
package/src/collection.js CHANGED
@@ -4,28 +4,16 @@ export function unique<T>(array: Array<T>): Array<T> {
4
4
  return [...new Set(array)];
5
5
  }
6
6
 
7
- export function flatMap<T, U>(
8
- array: Array<T>,
9
- projectFn: (T, number, Array<T>) => Array<U>,
10
- ): Array<U> {
11
- let out = [];
12
-
13
- for (let arr of array.map(projectFn)) {
14
- out.push(...arr);
15
- }
16
- return out;
17
- }
18
-
19
7
  export function objectSortedEntries(obj: {
20
8
  +[string]: mixed,
21
- ...,
9
+ ...
22
10
  }): Array<[string, mixed]> {
23
11
  return Object.entries(obj).sort(([keyA], [keyB]) => keyA.localeCompare(keyB));
24
12
  }
25
13
 
26
14
  export function objectSortedEntriesDeep(object: {
27
15
  +[string]: mixed,
28
- ...,
16
+ ...
29
17
  }): Array<[string, mixed]> {
30
18
  let sortedEntries = objectSortedEntries(object);
31
19
  for (let i = 0; i < sortedEntries.length; i++) {
@@ -55,3 +43,27 @@ export function setDifference<T>(a: Set<T>, b: Set<T>): Set<T> {
55
43
  }
56
44
  return difference;
57
45
  }
46
+
47
+ export function setIntersect<T>(a: Set<T>, b: Set<T>): void {
48
+ for (let entry of a) {
49
+ if (!b.has(entry)) {
50
+ a.delete(entry);
51
+ }
52
+ }
53
+ }
54
+
55
+ export function setUnion<T>(a: Iterable<T>, b: Iterable<T>): Set<T> {
56
+ return new Set([...a, ...b]);
57
+ }
58
+
59
+ export function setEqual<T>(a: Set<T>, b: Set<T>): boolean {
60
+ if (a.size != b.size) {
61
+ return false;
62
+ }
63
+ for (let entry of a) {
64
+ if (!b.has(entry)) {
65
+ return false;
66
+ }
67
+ }
68
+ return true;
69
+ }
package/src/config.js CHANGED
@@ -2,87 +2,137 @@
2
2
 
3
3
  import type {ConfigResult, File, FilePath} from '@parcel/types';
4
4
  import type {FileSystem} from '@parcel/fs';
5
+ import ThrowableDiagnostic from '@parcel/diagnostic';
5
6
  import path from 'path';
6
7
  import clone from 'clone';
8
+ import json5 from 'json5';
9
+ import {parse as toml} from '@iarna/toml';
10
+ import LRU from 'lru-cache';
7
11
 
8
- type ConfigOutput = {|
12
+ export type ConfigOutput = {|
9
13
  config: ConfigResult,
10
14
  files: Array<File>,
11
15
  |};
12
16
 
13
- type ConfigOptions = {|
17
+ export type ConfigOptions = {|
14
18
  parse?: boolean,
19
+ parser?: string => any,
15
20
  |};
16
21
 
17
- const PARSERS = {
18
- json: require('json5').parse,
19
- toml: require('@iarna/toml').parse,
20
- };
21
-
22
- const existsCache = new Map();
22
+ const configCache = new LRU<FilePath, ConfigOutput>({max: 500});
23
+ const resolveCache = new Map();
23
24
 
24
- export async function resolveConfig(
25
+ export function resolveConfig(
25
26
  fs: FileSystem,
26
27
  filepath: FilePath,
27
28
  filenames: Array<FilePath>,
28
- opts: ?ConfigOptions,
29
- root: FilePath = path.parse(filepath).root,
30
- ): Promise<FilePath | null> {
31
- filepath = await fs.realpath(path.dirname(filepath));
32
-
33
- // Don't traverse above the module root
34
- if (filepath === root || path.basename(filepath) === 'node_modules') {
35
- return null;
29
+ projectRoot: FilePath,
30
+ ): Promise<?FilePath> {
31
+ // Cache the result of resolving config for this directory.
32
+ // This is automatically invalidated at the end of the current build.
33
+ let key = path.dirname(filepath) + filenames.join(',');
34
+ let cached = resolveCache.get(key);
35
+ if (cached !== undefined) {
36
+ return Promise.resolve(cached);
36
37
  }
37
38
 
38
- for (const filename of filenames) {
39
- let file = path.join(filepath, filename);
40
- if ((await fs.exists(file)) && (await fs.stat(file)).isFile()) {
41
- return file;
42
- }
43
- }
39
+ let resolved = fs.findAncestorFile(
40
+ filenames,
41
+ path.dirname(filepath),
42
+ projectRoot,
43
+ );
44
+ resolveCache.set(key, resolved);
45
+ return Promise.resolve(resolved);
46
+ }
44
47
 
45
- return resolveConfig(fs, filepath, filenames, opts);
48
+ export function resolveConfigSync(
49
+ fs: FileSystem,
50
+ filepath: FilePath,
51
+ filenames: Array<FilePath>,
52
+ projectRoot: FilePath,
53
+ ): ?FilePath {
54
+ return fs.findAncestorFile(filenames, path.dirname(filepath), projectRoot);
46
55
  }
47
56
 
48
57
  export async function loadConfig(
49
58
  fs: FileSystem,
50
59
  filepath: FilePath,
51
60
  filenames: Array<FilePath>,
61
+ projectRoot: FilePath,
52
62
  opts: ?ConfigOptions,
53
63
  ): Promise<ConfigOutput | null> {
54
- let configFile = await resolveConfig(fs, filepath, filenames, opts);
64
+ let parse = opts?.parse ?? true;
65
+ let configFile = await resolveConfig(fs, filepath, filenames, projectRoot);
55
66
  if (configFile) {
67
+ let cachedOutput = configCache.get(String(parse) + configFile);
68
+ if (cachedOutput) {
69
+ return cachedOutput;
70
+ }
71
+
56
72
  try {
57
73
  let extname = path.extname(configFile).slice(1);
58
- if (extname === 'js') {
59
- return {
74
+ if (extname === 'js' || extname === 'cjs') {
75
+ let output = {
60
76
  // $FlowFixMe
61
77
  config: clone(require(configFile)),
62
78
  files: [{filePath: configFile}],
63
79
  };
80
+
81
+ configCache.set(configFile, output);
82
+ return output;
64
83
  }
65
84
 
66
85
  let configContent = await fs.readFile(configFile, 'utf8');
67
- if (!configContent) {
68
- return null;
69
- }
70
86
 
71
87
  let config;
72
- if (opts && opts.parse === false) {
88
+ if (parse === false) {
73
89
  config = configContent;
74
90
  } else {
75
- let parse = PARSERS[extname] || PARSERS.json;
76
- config = parse(configContent);
91
+ let parse = opts?.parser ?? getParser(extname);
92
+ try {
93
+ config = parse(configContent);
94
+ } catch (e) {
95
+ if (extname !== '' && extname !== 'json') {
96
+ throw e;
97
+ }
98
+
99
+ let pos = {
100
+ line: e.lineNumber,
101
+ column: e.columnNumber,
102
+ };
103
+
104
+ throw new ThrowableDiagnostic({
105
+ diagnostic: {
106
+ message: `Failed to parse ${path.basename(configFile)}`,
107
+ origin: '@parcel/utils',
108
+ codeFrames: [
109
+ {
110
+ language: 'json5',
111
+ filePath: configFile,
112
+ code: configContent,
113
+ codeHighlights: [
114
+ {
115
+ start: pos,
116
+ end: pos,
117
+ message: e.message,
118
+ },
119
+ ],
120
+ },
121
+ ],
122
+ },
123
+ });
124
+ }
77
125
  }
78
126
 
79
- return {
80
- config: config,
127
+ let output = {
128
+ config,
81
129
  files: [{filePath: configFile}],
82
130
  };
131
+
132
+ configCache.set(String(parse) + configFile, output);
133
+ return output;
83
134
  } catch (err) {
84
135
  if (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT') {
85
- existsCache.delete(configFile);
86
136
  return null;
87
137
  }
88
138
 
@@ -92,3 +142,18 @@ export async function loadConfig(
92
142
 
93
143
  return null;
94
144
  }
145
+
146
+ loadConfig.clear = () => {
147
+ configCache.reset();
148
+ resolveCache.clear();
149
+ };
150
+
151
+ function getParser(extname) {
152
+ switch (extname) {
153
+ case 'toml':
154
+ return toml;
155
+ case 'json':
156
+ default:
157
+ return json5.parse;
158
+ }
159
+ }
package/src/countLines.js CHANGED
@@ -1,8 +1,11 @@
1
1
  // @flow strict-local
2
2
 
3
- export default function countLines(string: string): number {
3
+ export default function countLines(
4
+ string: string,
5
+ startIndex: number = 0,
6
+ ): number {
4
7
  let lines = 1;
5
- for (let i = 0; i < string.length; i++) {
8
+ for (let i = startIndex; i < string.length; i++) {
6
9
  if (string.charAt(i) === '\n') {
7
10
  lines++;
8
11
  }
package/src/debounce.js CHANGED
@@ -6,7 +6,7 @@ export default function debounce<TArgs: Array<mixed>>(
6
6
  ): (...args: TArgs) => void {
7
7
  let timeout;
8
8
 
9
- return function(...args: TArgs) {
9
+ return function (...args: TArgs) {
10
10
  if (timeout) {
11
11
  clearTimeout(timeout);
12
12
  }
@@ -1,17 +1,22 @@
1
1
  // @flow
2
+
2
3
  export default function createDependencyLocation(
3
- start: {|
4
+ start: interface {
4
5
  line: number,
5
6
  column: number,
6
- |},
7
- moduleSpecifier: string,
7
+ },
8
+ specifier: string,
8
9
  lineOffset: number = 0,
9
10
  columnOffset: number = 0,
10
11
  // Imports are usually wrapped in quotes
11
12
  importWrapperLength: number = 2,
12
- ) {
13
+ ): {|
14
+ end: {|column: number, line: number|},
15
+ filePath: string,
16
+ start: {|column: number, line: number|},
17
+ |} {
13
18
  return {
14
- filePath: moduleSpecifier,
19
+ filePath: specifier,
15
20
  start: {
16
21
  line: start.line + lineOffset,
17
22
  column: start.column + columnOffset,
@@ -20,7 +25,7 @@ export default function createDependencyLocation(
20
25
  line: start.line + lineOffset,
21
26
  column:
22
27
  start.column +
23
- moduleSpecifier.length -
28
+ specifier.length -
24
29
  1 +
25
30
  importWrapperLength +
26
31
  columnOffset,