@parcel/utils 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.
- package/.eslintrc.js +6 -6
- package/lib/DefaultMap.js +0 -8
- package/lib/Deferred.js +10 -2
- package/lib/PromiseQueue.js +21 -30
- package/lib/TapStream.js +10 -10
- package/lib/alternatives.js +134 -0
- package/lib/ansi-html.js +10 -2
- package/lib/blob.js +26 -8
- package/lib/collection.js +14 -11
- package/lib/config.js +145 -35
- package/lib/countLines.js +2 -2
- package/lib/dependency-location.js +3 -3
- package/lib/generateBuildMetrics.js +148 -0
- package/lib/generateCertificate.js +33 -8
- package/lib/getExisting.js +11 -3
- package/lib/getRootDir.js +18 -7
- package/lib/glob.js +53 -19
- package/lib/hash.js +44 -0
- package/lib/http-server.js +48 -10
- package/lib/index.js +298 -224
- package/lib/is-url.js +12 -2
- package/lib/isDirectoryInside.js +24 -0
- package/lib/objectHash.js +10 -2
- package/lib/openInBrowser.js +94 -0
- package/lib/path.js +33 -6
- package/lib/prettyDiagnostic.js +107 -25
- package/lib/relativeBundlePath.js +13 -7
- package/lib/relativeUrl.js +19 -3
- package/lib/replaceBundleReferences.js +91 -35
- package/lib/schema.js +104 -33
- package/lib/shared-buffer.js +31 -0
- package/lib/sourcemap.js +147 -0
- package/lib/stream.js +38 -3
- package/lib/urlJoin.js +25 -6
- package/package.json +27 -16
- package/src/DefaultMap.js +1 -1
- package/src/PromiseQueue.js +16 -12
- package/src/alternatives.js +143 -0
- package/src/ansi-html.js +2 -2
- package/src/blob.js +4 -3
- package/src/bundle-url.js +1 -1
- package/src/collection.js +14 -14
- package/src/config.js +100 -35
- package/src/countLines.js +5 -2
- package/src/debounce.js +1 -1
- package/src/dependency-location.js +11 -6
- package/src/generateBuildMetrics.js +158 -0
- package/src/generateCertificate.js +1 -1
- package/src/getCertificate.js +1 -1
- package/src/getExisting.js +1 -4
- package/src/getRootDir.js +1 -2
- package/src/glob.js +29 -11
- package/src/hash.js +34 -0
- package/src/http-server.js +10 -12
- package/src/index.js +52 -23
- package/src/is-url.js +1 -1
- package/src/isDirectoryInside.js +11 -0
- package/src/openInBrowser.js +64 -0
- package/src/path.js +38 -6
- package/src/prettyDiagnostic.js +58 -24
- package/src/relativeBundlePath.js +8 -13
- package/src/replaceBundleReferences.js +75 -39
- package/src/schema.js +101 -44
- package/src/shared-buffer.js +24 -0
- package/src/sourcemap.js +135 -0
- package/src/stream.js +31 -1
- package/src/urlJoin.js +3 -1
- package/test/DefaultMap.test.js +7 -4
- package/test/config.test.js +50 -0
- package/test/input/config/config.json +3 -0
- package/test/input/config/empty.json +0 -0
- package/test/input/config/empty.toml +0 -0
- package/test/input/sourcemap/referenced-min.js +2 -0
- package/test/input/sourcemap/referenced-min.js.map +6 -0
- package/test/input/sourcemap/source-root.js +2 -0
- package/test/input/sourcemap/source-root.js.map +7 -0
- package/test/objectHash.test.js +33 -0
- package/test/prettifyTime.test.js +17 -0
- package/test/replaceBundleReferences.test.js +268 -0
- package/test/sourcemap.test.js +207 -0
- package/test/throttle.test.js +1 -2
- package/test/urlJoin.test.js +37 -0
- package/lib/generateBundleReport.js +0 -38
- package/lib/loadSourceMapUrl.js +0 -33
- package/lib/md5.js +0 -35
- package/lib/prettyError.js +0 -43
- package/lib/promisify.js +0 -13
- package/lib/resolve.js +0 -93
- package/lib/serializeObject.js +0 -28
- package/src/.babelrc +0 -3
- package/src/generateBundleReport.js +0 -51
- package/src/loadSourceMapUrl.js +0 -33
- package/src/md5.js +0 -44
- package/src/prettyError.js +0 -54
- package/src/promisify.js +0 -13
- package/src/resolve.js +0 -123
- package/src/serializeObject.js +0 -22
- package/test/input/sourcemap/referenced.js +0 -7
- package/test/loadSourceMapUrl.test.js +0 -37
- package/test/prettyError.test.js +0 -104
|
@@ -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
package/src/blob.js
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
import type {Blob} from '@parcel/types';
|
|
4
4
|
|
|
5
|
-
import {
|
|
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> {
|
|
9
10
|
if (blob instanceof Readable) {
|
|
10
11
|
return bufferStream(blob);
|
|
11
12
|
} else if (blob instanceof Buffer) {
|
|
12
|
-
return Buffer.from(blob);
|
|
13
|
+
return Promise.resolve(Buffer.from(blob));
|
|
13
14
|
} else {
|
|
14
|
-
return Buffer.from(blob, 'utf8');
|
|
15
|
+
return Promise.resolve(Buffer.from(blob, 'utf8'));
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
|
package/src/bundle-url.js
CHANGED
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,15 @@ 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
|
+
}
|
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 {parse as 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
|
|
18
|
-
|
|
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
|
|
25
|
+
export function resolveConfig(
|
|
25
26
|
fs: FileSystem,
|
|
26
27
|
filepath: FilePath,
|
|
27
28
|
filenames: Array<FilePath>,
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
if (
|
|
35
|
-
return
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
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
|
|
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
74
|
if (extname === 'js') {
|
|
59
|
-
|
|
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 (
|
|
88
|
+
if (parse === false) {
|
|
73
89
|
config = configContent;
|
|
74
90
|
} else {
|
|
75
|
-
let parse =
|
|
76
|
-
|
|
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
|
-
|
|
80
|
-
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;
|
|
158
|
+
}
|
|
159
|
+
}
|
package/src/countLines.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
|
|
3
|
-
export default function countLines(
|
|
3
|
+
export default function countLines(
|
|
4
|
+
string: string,
|
|
5
|
+
startIndex: number = 0,
|
|
6
|
+
): number {
|
|
4
7
|
let lines = 1;
|
|
5
|
-
for (let 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
|
@@ -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
|
-
|
|
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:
|
|
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
|
-
|
|
28
|
+
specifier.length -
|
|
24
29
|
1 +
|
|
25
30
|
importWrapperLength +
|
|
26
31
|
columnOffset,
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import type {FilePath, PackagedBundle} from '@parcel/types';
|
|
4
|
+
import type {FileSystem} from '@parcel/fs';
|
|
5
|
+
import SourceMap from '@parcel/source-map';
|
|
6
|
+
import nullthrows from 'nullthrows';
|
|
7
|
+
import path from 'path';
|
|
8
|
+
import {loadSourceMapUrl} from './';
|
|
9
|
+
|
|
10
|
+
export type AssetStats = {|
|
|
11
|
+
filePath: string,
|
|
12
|
+
size: number,
|
|
13
|
+
originalSize: number,
|
|
14
|
+
time: number,
|
|
15
|
+
|};
|
|
16
|
+
|
|
17
|
+
export type BundleStats = {|
|
|
18
|
+
filePath: string,
|
|
19
|
+
size: number,
|
|
20
|
+
time: number,
|
|
21
|
+
assets: Array<AssetStats>,
|
|
22
|
+
|};
|
|
23
|
+
|
|
24
|
+
export type BuildMetrics = {|
|
|
25
|
+
bundles: Array<BundleStats>,
|
|
26
|
+
|};
|
|
27
|
+
|
|
28
|
+
async function getSourcemapSizes(
|
|
29
|
+
filePath: FilePath,
|
|
30
|
+
fs: FileSystem,
|
|
31
|
+
projectRoot: FilePath,
|
|
32
|
+
): Promise<?Map<string, number>> {
|
|
33
|
+
let bundleContents = await fs.readFile(filePath, 'utf-8');
|
|
34
|
+
let mapUrlData = await loadSourceMapUrl(fs, filePath, bundleContents);
|
|
35
|
+
if (!mapUrlData) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let rawMap = mapUrlData.map;
|
|
40
|
+
let sourceMap = new SourceMap(projectRoot);
|
|
41
|
+
sourceMap.addVLQMap(rawMap);
|
|
42
|
+
let parsedMapData = sourceMap.getMap();
|
|
43
|
+
|
|
44
|
+
if (parsedMapData.mappings.length > 2) {
|
|
45
|
+
let sources = parsedMapData.sources.map(s =>
|
|
46
|
+
path.normalize(path.join(projectRoot, s)),
|
|
47
|
+
);
|
|
48
|
+
let currLine = 1;
|
|
49
|
+
let currColumn = 0;
|
|
50
|
+
let currMappingIndex = 0;
|
|
51
|
+
let currMapping = parsedMapData.mappings[currMappingIndex];
|
|
52
|
+
let nextMapping = parsedMapData.mappings[currMappingIndex + 1];
|
|
53
|
+
let sourceSizes = new Array(sources.length).fill(0);
|
|
54
|
+
let unknownOrigin: number = 0;
|
|
55
|
+
for (let i = 0; i < bundleContents.length; i++) {
|
|
56
|
+
let character = bundleContents[i];
|
|
57
|
+
|
|
58
|
+
while (
|
|
59
|
+
nextMapping &&
|
|
60
|
+
nextMapping.generated.line === currLine &&
|
|
61
|
+
nextMapping.generated.column <= currColumn
|
|
62
|
+
) {
|
|
63
|
+
currMappingIndex++;
|
|
64
|
+
currMapping = parsedMapData.mappings[currMappingIndex];
|
|
65
|
+
nextMapping = parsedMapData.mappings[currMappingIndex + 1];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let currentSource = currMapping.source;
|
|
69
|
+
let charSize = Buffer.byteLength(character, 'utf8');
|
|
70
|
+
if (
|
|
71
|
+
currentSource != null &&
|
|
72
|
+
currMapping.generated.line === currLine &&
|
|
73
|
+
currMapping.generated.column <= currColumn
|
|
74
|
+
) {
|
|
75
|
+
sourceSizes[currentSource] += charSize;
|
|
76
|
+
} else {
|
|
77
|
+
unknownOrigin += charSize;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (character === '\n') {
|
|
81
|
+
currColumn = 0;
|
|
82
|
+
currLine++;
|
|
83
|
+
} else {
|
|
84
|
+
currColumn++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
let sizeMap = new Map();
|
|
89
|
+
for (let i = 0; i < sourceSizes.length; i++) {
|
|
90
|
+
sizeMap.set(sources[i], sourceSizes[i]);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
sizeMap.set('', unknownOrigin);
|
|
94
|
+
|
|
95
|
+
return sizeMap;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function createBundleStats(
|
|
100
|
+
bundle: PackagedBundle,
|
|
101
|
+
fs: FileSystem,
|
|
102
|
+
projectRoot: FilePath,
|
|
103
|
+
) {
|
|
104
|
+
let filePath = bundle.filePath;
|
|
105
|
+
let sourcemapSizes = await getSourcemapSizes(filePath, fs, projectRoot);
|
|
106
|
+
|
|
107
|
+
let assets: Map<string, AssetStats> = new Map();
|
|
108
|
+
bundle.traverseAssets(asset => {
|
|
109
|
+
let filePath = path.normalize(asset.filePath);
|
|
110
|
+
assets.set(filePath, {
|
|
111
|
+
filePath,
|
|
112
|
+
size: asset.stats.size,
|
|
113
|
+
originalSize: asset.stats.size,
|
|
114
|
+
time: asset.stats.time,
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
let assetsReport: Array<AssetStats> = [];
|
|
119
|
+
if (sourcemapSizes && sourcemapSizes.size) {
|
|
120
|
+
assetsReport = Array.from(sourcemapSizes.keys()).map((filePath: string) => {
|
|
121
|
+
let foundSize = sourcemapSizes.get(filePath) || 0;
|
|
122
|
+
let stats = assets.get(filePath) || {
|
|
123
|
+
filePath,
|
|
124
|
+
size: foundSize,
|
|
125
|
+
originalSize: foundSize,
|
|
126
|
+
time: 0,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
...stats,
|
|
131
|
+
size: foundSize,
|
|
132
|
+
};
|
|
133
|
+
});
|
|
134
|
+
} else {
|
|
135
|
+
assetsReport = Array.from(assets.values());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
filePath: nullthrows(bundle.filePath),
|
|
140
|
+
size: bundle.stats.size,
|
|
141
|
+
time: bundle.stats.time,
|
|
142
|
+
assets: assetsReport.sort((a, b) => b.size - a.size),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export default async function generateBuildMetrics(
|
|
147
|
+
bundles: Array<PackagedBundle>,
|
|
148
|
+
fs: FileSystem,
|
|
149
|
+
projectRoot: FilePath,
|
|
150
|
+
): Promise<BuildMetrics> {
|
|
151
|
+
bundles.sort((a, b) => b.stats.size - a.stats.size).filter(b => !!b.filePath);
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
bundles: (
|
|
155
|
+
await Promise.all(bundles.map(b => createBundleStats(b, fs, projectRoot)))
|
|
156
|
+
).filter(e => !!e),
|
|
157
|
+
};
|
|
158
|
+
}
|
package/src/getCertificate.js
CHANGED
|
@@ -5,7 +5,7 @@ import type {FileSystem} from '@parcel/fs';
|
|
|
5
5
|
export default async function getCertificate(
|
|
6
6
|
fs: FileSystem,
|
|
7
7
|
options: HTTPSOptions,
|
|
8
|
-
) {
|
|
8
|
+
): Promise<{|cert: Buffer, key: Buffer|}> {
|
|
9
9
|
try {
|
|
10
10
|
let cert = await fs.readFile(options.cert);
|
|
11
11
|
let key = await fs.readFile(options.key);
|
package/src/getExisting.js
CHANGED
|
@@ -14,10 +14,7 @@ export default function getExisting(
|
|
|
14
14
|
return {
|
|
15
15
|
source,
|
|
16
16
|
minified: fs.existsSync(minifiedPath)
|
|
17
|
-
? fs
|
|
18
|
-
.readFileSync(minifiedPath, 'utf8')
|
|
19
|
-
.trim()
|
|
20
|
-
.replace(/;$/, '')
|
|
17
|
+
? fs.readFileSync(minifiedPath, 'utf8').trim().replace(/;$/, '')
|
|
21
18
|
: source,
|
|
22
19
|
};
|
|
23
20
|
}
|
package/src/getRootDir.js
CHANGED