@parcel/utils 2.0.0-beta.3 → 2.0.0-dev.1510

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 (79) hide show
  1. package/lib/index.js +37516 -542
  2. package/lib/index.js.map +1 -0
  3. package/package.json +41 -18
  4. package/src/DefaultMap.js +1 -1
  5. package/src/PromiseQueue.js +13 -0
  6. package/src/alternatives.js +21 -6
  7. package/src/ansi-html.js +1 -1
  8. package/src/blob.js +1 -0
  9. package/src/collection.js +35 -3
  10. package/src/config.js +77 -22
  11. package/src/debounce.js +1 -1
  12. package/src/dependency-location.js +5 -5
  13. package/src/getExisting.js +1 -4
  14. package/src/getModuleParts.js +23 -0
  15. package/src/glob.js +26 -3
  16. package/src/hash.js +49 -0
  17. package/src/http-server.js +19 -7
  18. package/src/index.js +25 -10
  19. package/src/path.js +11 -1
  20. package/src/prettyDiagnostic.js +90 -40
  21. package/src/progress-message.js +22 -0
  22. package/src/replaceBundleReferences.js +49 -25
  23. package/src/schema.js +20 -19
  24. package/src/shared-buffer.js +23 -0
  25. package/src/sourcemap.js +13 -7
  26. package/src/urlJoin.js +3 -1
  27. package/test/DefaultMap.test.js +7 -4
  28. package/test/PromiseQueue.test.js +28 -0
  29. package/test/collection.test.js +13 -1
  30. package/test/config.test.js +98 -0
  31. package/test/input/config/.testrc +3 -0
  32. package/test/input/config/config.cjs +3 -0
  33. package/test/input/config/config.js +3 -0
  34. package/test/input/config/config.json +3 -0
  35. package/test/input/config/empty.json +0 -0
  36. package/test/input/config/empty.toml +0 -0
  37. package/test/replaceBundleReferences.test.js +88 -4
  38. package/test/throttle.test.js +1 -1
  39. package/test/urlJoin.test.js +11 -0
  40. package/lib/DefaultMap.js +0 -64
  41. package/lib/Deferred.js +0 -34
  42. package/lib/PromiseQueue.js +0 -144
  43. package/lib/TapStream.js +0 -46
  44. package/lib/alternatives.js +0 -151
  45. package/lib/ansi-html.js +0 -32
  46. package/lib/blob.js +0 -47
  47. package/lib/bundle-url.js +0 -43
  48. package/lib/collection.js +0 -51
  49. package/lib/config.js +0 -159
  50. package/lib/countLines.js +0 -18
  51. package/lib/debounce.js +0 -20
  52. package/lib/dependency-location.js +0 -21
  53. package/lib/escape-html.js +0 -24
  54. package/lib/generateBuildMetrics.js +0 -156
  55. package/lib/generateCertificate.js +0 -149
  56. package/lib/getCertificate.js +0 -19
  57. package/lib/getExisting.js +0 -31
  58. package/lib/getRootDir.js +0 -74
  59. package/lib/glob.js +0 -118
  60. package/lib/http-server.js +0 -110
  61. package/lib/is-url.js +0 -27
  62. package/lib/isDirectoryInside.js +0 -24
  63. package/lib/md5.js +0 -61
  64. package/lib/objectHash.js +0 -34
  65. package/lib/openInBrowser.js +0 -94
  66. package/lib/parseCSSImport.js +0 -16
  67. package/lib/path.js +0 -44
  68. package/lib/prettifyTime.js +0 -10
  69. package/lib/prettyDiagnostic.js +0 -119
  70. package/lib/relativeBundlePath.js +0 -38
  71. package/lib/relativeUrl.js +0 -32
  72. package/lib/replaceBundleReferences.js +0 -184
  73. package/lib/schema.js +0 -391
  74. package/lib/sourcemap.js +0 -155
  75. package/lib/stream.js +0 -86
  76. package/lib/throttle.js +0 -16
  77. package/lib/urlJoin.js +0 -43
  78. package/src/.babelrc +0 -3
  79. package/src/md5.js +0 -56
package/lib/md5.js DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.md5FromString = md5FromString;
7
- exports.md5FromReadableStream = md5FromReadableStream;
8
- exports.md5FromOrderedObject = md5FromOrderedObject;
9
- exports.md5FromObject = md5FromObject;
10
- exports.md5FromFilePath = md5FromFilePath;
11
-
12
- function _crypto() {
13
- const data = _interopRequireDefault(require("crypto"));
14
-
15
- _crypto = function () {
16
- return data;
17
- };
18
-
19
- return data;
20
- }
21
-
22
- function _collection() {
23
- const data = require("./collection");
24
-
25
- _collection = function () {
26
- return data;
27
- };
28
-
29
- return data;
30
- }
31
-
32
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
-
34
- function md5FromString(string, encoding = 'hex') {
35
- return _crypto().default.createHash('md5').update(string).digest(encoding);
36
- }
37
-
38
- function md5FromReadableStream(stream) {
39
- return new Promise((resolve, reject) => {
40
- stream.on('error', err => {
41
- reject(err);
42
- });
43
- stream.pipe(_crypto().default.createHash('md5').setEncoding('hex')).on('finish', function () {
44
- resolve(this.read());
45
- }).on('error', err => {
46
- reject(err);
47
- });
48
- });
49
- }
50
-
51
- function md5FromOrderedObject(obj, encoding = 'hex') {
52
- return md5FromString(JSON.stringify(obj), encoding);
53
- }
54
-
55
- function md5FromObject(obj, encoding = 'hex') {
56
- return md5FromString(JSON.stringify((0, _collection().objectSortedEntriesDeep)(obj)), encoding);
57
- }
58
-
59
- function md5FromFilePath(fs, filePath) {
60
- return md5FromReadableStream(fs.createReadStream(filePath));
61
- }
package/lib/objectHash.js DELETED
@@ -1,34 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = objectHash;
7
-
8
- function _crypto() {
9
- const data = _interopRequireDefault(require("crypto"));
10
-
11
- _crypto = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
- function objectHash(object) {
21
- let hash = _crypto().default.createHash('md5');
22
-
23
- for (let key of Object.keys(object).sort()) {
24
- let val = object[key];
25
-
26
- if (typeof val === 'object' && val) {
27
- hash.update(key + objectHash(val));
28
- } else {
29
- hash.update(key + val);
30
- }
31
- }
32
-
33
- return hash.digest('hex');
34
- }
@@ -1,94 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = openInBrowser;
7
-
8
- function _open() {
9
- const data = _interopRequireDefault(require("open"));
10
-
11
- _open = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _child_process() {
19
- const data = require("child_process");
20
-
21
- _child_process = function () {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _logger() {
29
- const data = _interopRequireDefault(require("@parcel/logger"));
30
-
31
- _logger = function () {
32
- return data;
33
- };
34
-
35
- return data;
36
- }
37
-
38
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
-
40
- // Chrome app name is platform dependent. we should not hard code it.
41
- // https://github.com/react-native-community/cli/blob/e2be8a905285d9b37512fc78c9755b9635ecf805/packages/cli/src/commands/server/launchDebugger.ts#L28
42
- function getChromeAppName() {
43
- switch (process.platform) {
44
- case 'darwin':
45
- return 'google chrome';
46
-
47
- case 'win32':
48
- return 'chrome';
49
-
50
- case 'linux':
51
- if (commandExistsUnixSync('google-chrome')) {
52
- return 'google-chrome';
53
- }
54
-
55
- if (commandExistsUnixSync('chromium-browser')) {
56
- return 'chromium-browser';
57
- }
58
-
59
- return 'chromium';
60
-
61
- default:
62
- return 'google-chrome';
63
- }
64
- }
65
-
66
- function commandExistsUnixSync(commandName) {
67
- try {
68
- const stdout = (0, _child_process().execSync)(`command -v ${commandName} 2>/dev/null` + ` && { echo >&1 '${commandName} found'; exit 0; }`);
69
- return !!stdout;
70
- } catch (error) {
71
- return false;
72
- }
73
- }
74
-
75
- function getAppName(appName) {
76
- if (['google', 'chrome'].includes(appName)) {
77
- return getChromeAppName();
78
- } else if (['brave', 'Brave'].includes(appName)) {
79
- return 'Brave Browser';
80
- } else return appName;
81
- }
82
-
83
- async function openInBrowser(url, browser) {
84
- try {
85
- const options = typeof browser === 'string' && browser.length > 0 ? {
86
- app: [getAppName(browser)]
87
- } : undefined;
88
- await (0, _open().default)(url, options);
89
- } catch (err) {
90
- _logger().default.error(`Unexpected error while opening in browser: ${browser}`, '@parcel/utils');
91
-
92
- _logger().default.error(err, '@parcel/utils');
93
- }
94
- }
@@ -1,16 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = parseCSSImport;
7
-
8
- function parseCSSImport(url) {
9
- if (!/^(~|\.\/|\/)/.test(url)) {
10
- return './' + url;
11
- } else if (!/^(~\/|\.\/|\/)/.test(url)) {
12
- return url.substring(1);
13
- } else {
14
- return url;
15
- }
16
- }
package/lib/path.js DELETED
@@ -1,44 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isAbsolute = isAbsolute;
7
- exports.normalizeSeparators = normalizeSeparators;
8
- exports.normalizePath = normalizePath;
9
- exports.relativePath = relativePath;
10
-
11
- function _path() {
12
- const data = _interopRequireDefault(require("path"));
13
-
14
- _path = function () {
15
- return data;
16
- };
17
-
18
- return data;
19
- }
20
-
21
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
-
23
- const ABSOLUTE_PATH_REGEX = /^([a-zA-Z]:){0,1}[\\/]+/;
24
- const SEPARATOR_REGEX = /[\\]+/g;
25
-
26
- function isAbsolute(filepath) {
27
- return ABSOLUTE_PATH_REGEX.test(filepath);
28
- }
29
-
30
- function normalizeSeparators(filePath) {
31
- return filePath.replace(SEPARATOR_REGEX, '/');
32
- }
33
-
34
- function normalizePath(filePath, leadingDotSlash = true) {
35
- if (leadingDotSlash && filePath[0] !== '.' && filePath[0] !== '/') {
36
- return normalizeSeparators('./' + filePath);
37
- } else {
38
- return normalizeSeparators(filePath);
39
- }
40
- }
41
-
42
- function relativePath(from, to, leadingDotSlash = true) {
43
- return normalizePath(_path().default.relative(from, to), leadingDotSlash);
44
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = prettifyTime;
7
-
8
- function prettifyTime(timeInMs) {
9
- return timeInMs < 1000 ? `${timeInMs}ms` : `${(timeInMs / 1000).toFixed(2)}s`;
10
- }
@@ -1,119 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = prettyDiagnostic;
7
-
8
- function _codeframe() {
9
- const data = _interopRequireDefault(require("@parcel/codeframe"));
10
-
11
- _codeframe = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _markdownAnsi() {
19
- const data = _interopRequireDefault(require("@parcel/markdown-ansi"));
20
-
21
- _markdownAnsi = function () {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _chalk() {
29
- const data = _interopRequireDefault(require("chalk"));
30
-
31
- _chalk = 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
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
-
60
- async function prettyDiagnostic(diagnostic, options, terminalWidth) {
61
- let {
62
- origin,
63
- message,
64
- stack,
65
- codeFrame,
66
- hints,
67
- filePath,
68
- language,
69
- skipFormatting
70
- } = diagnostic;
71
-
72
- if (filePath != null && options && !_path().default.isAbsolute(filePath)) {
73
- filePath = _path().default.join(options.projectRoot, filePath);
74
- }
75
-
76
- let result = {
77
- message: (0, _markdownAnsi().default)(`**${origin !== null && origin !== void 0 ? origin : 'unknown'}**: `) + (skipFormatting ? message : (0, _markdownAnsi().default)(message)),
78
- stack: '',
79
- codeframe: '',
80
- hints: []
81
- };
82
-
83
- if (codeFrame !== undefined) {
84
- var _codeFrame$code;
85
-
86
- let highlights = codeFrame.codeHighlights;
87
- let code = (_codeFrame$code = codeFrame.code) !== null && _codeFrame$code !== void 0 ? _codeFrame$code : options && (await options.inputFS.readFile((0, _nullthrows().default)(filePath), 'utf8'));
88
- let formattedCodeFrame = '';
89
-
90
- if (code != null) {
91
- formattedCodeFrame = (0, _codeframe().default)(code, highlights, {
92
- useColor: true,
93
- syntaxHighlighting: true,
94
- language: // $FlowFixMe sketchy null checks do not matter here...
95
- language || (filePath ? _path().default.extname(filePath).substr(1) : undefined),
96
- terminalWidth
97
- });
98
- }
99
-
100
- result.codeframe += typeof filePath !== 'string' ? '' : _chalk().default.underline(`${filePath}:${highlights[0].start.line}:${highlights[0].start.column}\n`);
101
- result.codeframe += formattedCodeFrame;
102
- } else if (typeof filePath === 'string') {
103
- result.codeframe += _chalk().default.underline(filePath);
104
- }
105
-
106
- if (stack != null) {
107
- result.stack = stack;
108
- } else if (filePath != null && result.codeframe == null) {
109
- result.stack = filePath;
110
- }
111
-
112
- if (Array.isArray(hints) && hints.length) {
113
- result.hints = hints.map(h => {
114
- return (0, _markdownAnsi().default)(h);
115
- });
116
- }
117
-
118
- return result;
119
- }
@@ -1,38 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.relativeBundlePath = relativeBundlePath;
7
-
8
- function _path() {
9
- const data = _interopRequireDefault(require("path"));
10
-
11
- _path = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _path2() {
19
- const data = require("./path");
20
-
21
- _path2 = function () {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
-
30
- function relativeBundlePath(from, to, opts = {
31
- leadingDotSlash: true
32
- }) {
33
- let fromPath = _path().default.join(from.target.distDir, from.name);
34
-
35
- let toPath = _path().default.join(to.target.distDir, to.name);
36
-
37
- return (0, _path2().relativePath)(_path().default.dirname(fromPath), toPath, opts.leadingDotSlash);
38
- }
@@ -1,32 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = relativeUrl;
7
-
8
- function _path() {
9
- const data = _interopRequireDefault(require("path"));
10
-
11
- _path = function () {
12
- return data;
13
- };
14
-
15
- return data;
16
- }
17
-
18
- function _url() {
19
- const data = _interopRequireDefault(require("url"));
20
-
21
- _url = function () {
22
- return data;
23
- };
24
-
25
- return data;
26
- }
27
-
28
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
-
30
- function relativeUrl(from, to) {
31
- return _url().default.format(_url().default.parse(_path().default.relative(from, to)));
32
- }
@@ -1,184 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.replaceURLReferences = replaceURLReferences;
7
- exports.replaceInlineReferences = replaceInlineReferences;
8
- exports.getURLReplacement = getURLReplacement;
9
-
10
- function _stream() {
11
- const data = require("stream");
12
-
13
- _stream = function () {
14
- return data;
15
- };
16
-
17
- return data;
18
- }
19
-
20
- function _nullthrows() {
21
- const data = _interopRequireDefault(require("nullthrows"));
22
-
23
- _nullthrows = function () {
24
- return data;
25
- };
26
-
27
- return data;
28
- }
29
-
30
- function _url() {
31
- const data = _interopRequireDefault(require("url"));
32
-
33
- _url = function () {
34
- return data;
35
- };
36
-
37
- return data;
38
- }
39
-
40
- function _() {
41
- const data = require("./");
42
-
43
- _ = function () {
44
- return data;
45
- };
46
-
47
- return data;
48
- }
49
-
50
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
51
-
52
- /*
53
- * Replaces references to dependency ids for URL dependencies with:
54
- * - in the case of an unresolvable url dependency, the original moduleSpecifier.
55
- * These are external requests that Parcel did not bundle.
56
- * - in the case of a reference to another bundle, the relative url to that
57
- * bundle from the current bundle.
58
- */
59
- function replaceURLReferences({
60
- bundle,
61
- bundleGraph,
62
- contents,
63
- map,
64
- relative = true
65
- }) {
66
- let replacements = new Map();
67
- let urlDependencies = [];
68
- bundle.traverse(node => {
69
- if (node.type === 'dependency' && node.value.isURL) {
70
- urlDependencies.push(node.value);
71
- }
72
- });
73
-
74
- for (let dependency of urlDependencies) {
75
- if (!dependency.isURL) {
76
- continue;
77
- }
78
-
79
- let resolved = bundleGraph.getReferencedBundle(dependency, bundle);
80
-
81
- if (resolved == null) {
82
- replacements.set(dependency.id, {
83
- from: dependency.id,
84
- to: dependency.moduleSpecifier
85
- });
86
- continue;
87
- }
88
-
89
- if (!resolved || resolved.isInline) {
90
- // If a bundle is inline, it should be replaced with inline contents,
91
- // not a URL.
92
- continue;
93
- }
94
-
95
- replacements.set(dependency.id, getURLReplacement({
96
- dependency,
97
- fromBundle: bundle,
98
- toBundle: resolved,
99
- relative
100
- }));
101
- }
102
-
103
- return performReplacement(replacements, contents, map);
104
- }
105
- /*
106
- * Replaces references to dependency ids for inline bundles with the packaged
107
- * contents of that bundle.
108
- */
109
-
110
-
111
- async function replaceInlineReferences({
112
- bundle,
113
- bundleGraph,
114
- contents,
115
- map,
116
- getInlineReplacement,
117
- getInlineBundleContents
118
- }) {
119
- let replacements = new Map();
120
- let dependencies = [];
121
- bundle.traverse(node => {
122
- if (node.type === 'dependency') {
123
- dependencies.push(node.value);
124
- }
125
- });
126
-
127
- for (let dependency of dependencies) {
128
- let entryBundle = bundleGraph.getReferencedBundle(dependency, bundle);
129
-
130
- if (!(entryBundle !== null && entryBundle !== void 0 && entryBundle.isInline)) {
131
- continue;
132
- }
133
-
134
- let packagedBundle = await getInlineBundleContents(entryBundle, bundleGraph);
135
- let packagedContents = (packagedBundle.contents instanceof _stream().Readable ? await (0, _().bufferStream)(packagedBundle.contents) : packagedBundle.contents).toString();
136
- let inlineType = (0, _nullthrows().default)(entryBundle.getEntryAssets()[0]).meta.inlineType;
137
-
138
- if (inlineType == null || inlineType === 'string') {
139
- replacements.set(dependency.id, getInlineReplacement(dependency, inlineType, packagedContents));
140
- }
141
- }
142
-
143
- return performReplacement(replacements, contents, map);
144
- }
145
-
146
- function getURLReplacement({
147
- dependency,
148
- fromBundle,
149
- toBundle,
150
- relative
151
- }) {
152
- let to;
153
-
154
- if (relative) {
155
- to = _url().default.format(_url().default.parse((0, _().relativeBundlePath)(fromBundle, toBundle, {
156
- leadingDotSlash: false
157
- })));
158
- } else {
159
- to = (0, _().urlJoin)(toBundle.target.publicUrl, _url().default.format(_url().default.parse((0, _nullthrows().default)(toBundle.name))));
160
- }
161
-
162
- return {
163
- from: dependency.id,
164
- to
165
- };
166
- }
167
-
168
- function performReplacement(replacements, contents, map) {
169
- let finalContents = contents;
170
-
171
- for (let {
172
- from,
173
- to
174
- } of replacements.values()) {
175
- // Perform replacement
176
- finalContents = finalContents.split(from).join(to);
177
- }
178
-
179
- return {
180
- contents: finalContents,
181
- // TODO: Update sourcemap with adjusted contents
182
- map
183
- };
184
- }