Skip to content

Commit

Permalink
Reduce the number of npm dependencies needed by parcel (#7576)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jan 27, 2022
1 parent 0de7372 commit 4cbda87
Show file tree
Hide file tree
Showing 84 changed files with 565 additions and 2,732 deletions.
1 change: 1 addition & 0 deletions flow-libs/babel-core.js.flow
Expand Up @@ -171,4 +171,5 @@ declare module "@babel/core" {
| [string | { ... } | Function, { ... } | void],
{| dirname?: string, type?: "preset" | "plugin" |}
): ConfigItem;
declare export * as types from "@babel/types";
}
10 changes: 9 additions & 1 deletion gulpfile.js
Expand Up @@ -12,7 +12,15 @@ const IGNORED_PACKAGES = [
'!packages/core/is-v2-ready-yet/**',
'!packages/core/test-utils/**',
'!packages/core/types/**',
'!packages/utils/node-libs-browser/**',

// These packages are bundled.
'!packages/core/codeframe/**',
'!packages/core/fs/**',
'!packages/core/package-manager/**',
'!packages/core/utils/**',
'!packages/reporters/cli/**',
'!packages/reporters/dev-server/**',
'!packages/utils/fs-write-stream-atomic/**',
];

const paths = {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -11,7 +11,8 @@
"packages/*/*"
],
"scripts": {
"build": "cross-env NODE_ENV=production PARCEL_BUILD_ENV=production gulp",
"build": "yarn build-bundles && cross-env NODE_ENV=production PARCEL_BUILD_ENV=production gulp",
"build-bundles": "rm -rf packages/*/*/lib && cross-env NODE_ENV=production PARCEL_BUILD_ENV=production PARCEL_SELF_BUILD=true parcel build packages/core/{fs,codeframe,package-manager,utils} packages/reporters/{cli,dev-server}",
"build-ts": "lerna run build-ts && lerna run check-ts",
"build-native": "node scripts/build-native.js",
"build-native-release": "node scripts/build-native.js --release",
Expand Down Expand Up @@ -52,7 +53,7 @@
"mocha-junit-reporter": "^2.0.0",
"mocha-multi-reporters": "^1.5.1",
"prettier": "2.4.1",
"rimraf": "^2.6.3",
"rimraf": "^3.0.2",
"semver": "^5.7.1",
"sinon": "^7.3.1"
},
Expand Down
15 changes: 11 additions & 4 deletions packages/core/codeframe/package.json
Expand Up @@ -19,13 +19,20 @@
"engines": {
"node": ">= 12.0.0"
},
"targets": {
"main": {
"includeNodeModules": {
"chalk": false
}
}
},
"dependencies": {
"chalk": "^4.1.0",
"chalk": "^4.1.0"
},
"devDependencies": {
"emphasize": "^4.2.0",
"slice-ansi": "^4.0.0",
"strip-ansi": "^6.0.0",
"string-width": "^4.2.0"
},
"devDependencies": {
"strip-ansi": "^6.0.0"
}
}
1 change: 0 additions & 1 deletion packages/core/core/package.json
Expand Up @@ -45,7 +45,6 @@
"dotenv-expand": "^5.1.0",
"json-source-map": "^0.6.1",
"json5": "^2.2.0",
"micromatch": "^4.0.2",
"msgpackr": "^1.5.1",
"nullthrows": "^1.1.1",
"semver": "^5.7.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/core/src/ParcelConfig.js
Expand Up @@ -29,7 +29,7 @@ import ThrowableDiagnostic, {
} from '@parcel/diagnostic';
import json5 from 'json5';

import {makeRe} from 'micromatch';
import {globToRegex} from '@parcel/utils';
import {basename} from 'path';
import loadPlugin from './loadParcelPlugin';
import {
Expand Down Expand Up @@ -347,7 +347,7 @@ export default class ParcelConfig {

let re = this.regexCache.get(patternGlob);
if (!re) {
re = makeRe(patternGlob, {dot: true, nocase: true});
re = globToRegex(patternGlob, {dot: true, nocase: true});
this.regexCache.set(patternGlob, re);
}

Expand Down
6 changes: 4 additions & 2 deletions packages/core/core/src/requests/TargetRequest.js
Expand Up @@ -18,6 +18,7 @@ import type {ConfigAndCachePath} from './ParcelConfigRequest';
import ThrowableDiagnostic, {
generateJSONCodeHighlights,
getJSONSourceLocation,
encodeJSONKeyComponent,
md,
} from '@parcel/diagnostic';
import path from 'path';
Expand Down Expand Up @@ -503,11 +504,12 @@ export class TargetResolver {
if (
targetName === 'browser' &&
pkg[targetName] != null &&
typeof pkg[targetName] === 'object'
typeof pkg[targetName] === 'object' &&
pkg.name
) {
// The `browser` field can be a file path or an alias map.
_targetDist = pkg[targetName][pkg.name];
pointer = `/${targetName}/${pkg.name}`;
pointer = `/${targetName}/${encodeJSONKeyComponent(pkg.name)}`;
} else {
_targetDist = pkg[targetName];
pointer = `/${targetName}`;
Expand Down
32 changes: 28 additions & 4 deletions packages/core/fs/package.json
Expand Up @@ -20,28 +20,52 @@
"engines": {
"node": ">= 12.0.0"
},
"targets": {
"types": false,
"main": {
"includeNodeModules": {
"@parcel/core": false,
"@parcel/fs-search": false,
"@parcel/types": false,
"@parcel/utils": false,
"@parcel/watcher": false,
"@parcel/workers": false
}
},
"browser": {
"includeNodeModules": {
"@parcel/core": false,
"@parcel/fs-search": false,
"@parcel/types": false,
"@parcel/utils": false,
"@parcel/watcher": false,
"@parcel/workers": false
}
}
},
"scripts": {
"build-ts": "mkdir -p lib && flow-to-ts src/types.js > lib/types.d.ts",
"check-ts": "tsc --noEmit index.d.ts"
},
"dependencies": {
"@parcel/fs-search": "^2.2.1",
"@parcel/fs-write-stream-atomic": "^2.2.1",
"@parcel/types": "^2.2.1",
"@parcel/utils": "^2.2.1",
"@parcel/watcher": "^2.0.0",
"@parcel/workers": "^2.2.1",
"@parcel/workers": "^2.2.1"
},
"devDependencies": {
"@parcel/fs-write-stream-atomic": "^2.2.1",
"graceful-fs": "^4.2.4",
"mkdirp": "^0.5.1",
"ncp": "^2.0.0",
"nullthrows": "^1.1.1",
"rimraf": "^3.0.2",
"utility-types": "^3.10.0"
},
"peerDependencies": {
"@parcel/core": "^2.2.1"
},
"browser": {
"@parcel/fs": "./lib/browser.js",
"./src/NodeFS.js": "./src/NodeFS.browser.js"
}
}
31 changes: 27 additions & 4 deletions packages/core/fs/src/NodeFS.js
Expand Up @@ -10,9 +10,8 @@ import type {
} from '@parcel/watcher';

import fs from 'graceful-fs';
import nativeFS from 'fs';
import ncp from 'ncp';
import mkdirp from 'mkdirp';
import rimraf from 'rimraf';
import {promisify} from 'util';
import {registerSerializableClass} from '@parcel/core';
import fsWriteStreamAtomic from '@parcel/fs-write-stream-atomic';
Expand All @@ -37,8 +36,6 @@ export class NodeFS implements FileSystem {
readdir: any = promisify(fs.readdir);
unlink: any = promisify(fs.unlink);
utimes: any = promisify(fs.utimes);
mkdirp: any = promisify(mkdirp);
rimraf: any = promisify(rimraf);
ncp: any = promisify(ncp);
createReadStream: (path: string, options?: any) => ReadStream =
fs.createReadStream;
Expand Down Expand Up @@ -133,6 +130,32 @@ export class NodeFS implements FileSystem {
serialize(): null {
return null;
}

async mkdirp(filePath: FilePath): Promise<void> {
await nativeFS.promises.mkdir(filePath, {recursive: true});
}

async rimraf(filePath: FilePath): Promise<void> {
if (fs.promises.rm) {
await fs.promises.rm(filePath, {recursive: true, force: true});
return;
}

// fs.promises.rm is not supported in node 12...
let stat;
try {
stat = await this.stat(filePath);
} catch (err) {
return;
}

if (stat.isDirectory()) {
// $FlowFixMe
await nativeFS.promises.rmdir(filePath, {recursive: true});
} else {
await nativeFS.promises.unlink(filePath);
}
}
}

registerSerializableClass(`${packageJSON.version}:NodeFS`, NodeFS);
Expand Down
1 change: 0 additions & 1 deletion packages/core/integration-tests/package.json
Expand Up @@ -51,7 +51,6 @@
"preact": "^10.5.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"rimraf": "^2.6.1",
"sugarss": "^3.0.3",
"tailwindcss": "^3.0.2",
"tempy": "^0.3.0",
Expand Down
1 change: 1 addition & 0 deletions packages/core/integration-tests/test/babel.js
Expand Up @@ -395,6 +395,7 @@ describe('babel', function () {
let distFile = await fs.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(distFile.includes('hello there'));
await fs.copyFile(differentPath, configPath);
await new Promise(resolve => setTimeout(resolve, 100));
// On Windows only, `fs.utimes` arguments must be instances of `Date`,
// otherwise it fails. For Mac instances on Azure CI, using a Date instance
// does not update the utime correctly, so for all other platforms, use a
Expand Down
19 changes: 18 additions & 1 deletion packages/core/package-manager/package.json
Expand Up @@ -24,17 +24,34 @@
"build-ts": "mkdir -p lib && flow-to-ts src/types.js > lib/types.d.ts",
"check-ts": "tsc --noEmit index.d.ts"
},
"targets": {
"types": false,
"main": {
"includeNodeModules": {
"@parcel/core": false,
"@parcel/diagnostic": false,
"@parcel/fs": false,
"@parcel/logger": false,
"@parcel/types": false,
"@parcel/utils": false,
"@parcel/workers": false,
"semver": false
}
}
},
"dependencies": {
"@parcel/diagnostic": "^2.2.1",
"@parcel/fs": "^2.2.1",
"@parcel/logger": "^2.2.1",
"@parcel/types": "^2.2.1",
"@parcel/utils": "^2.2.1",
"@parcel/workers": "^2.2.1",
"semver": "^5.7.1"
},
"devDependencies": {
"command-exists": "^1.2.6",
"cross-spawn": "^6.0.4",
"nullthrows": "^1.1.1",
"semver": "^5.7.1",
"split2": "^3.1.1"
},
"peerDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion packages/core/parcel/src/bin.js
Expand Up @@ -2,7 +2,10 @@

'use strict';

if (process.env.PARCEL_BUILD_ENV !== 'production') {
if (
process.env.PARCEL_BUILD_ENV !== 'production' ||
process.env.PARCEL_SELF_BUILD
) {
require('@parcel/babel-register');
}

Expand Down
25 changes: 19 additions & 6 deletions packages/core/utils/package.json
Expand Up @@ -19,16 +19,32 @@
"engines": {
"node": ">= 12.0.0"
},
"targets": {
"main": {
"includeNodeModules": {
"@parcel/codeframe": false,
"@parcel/diagnostic": false,
"@parcel/hash": false,
"@parcel/logger": false,
"@parcel/markdown-ansi": false,
"@parcel/source-map": false,
"chalk": false
}
}
},
"dependencies": {
"@iarna/toml": "^2.2.0",
"@parcel/codeframe": "^2.2.1",
"@parcel/diagnostic": "^2.2.1",
"@parcel/hash": "^2.2.1",
"@parcel/logger": "^2.2.1",
"@parcel/markdown-ansi": "^2.2.1",
"@parcel/source-map": "^2.0.0",
"chalk": "^4.1.0"
},
"devDependencies": {
"@babel/plugin-transform-flow-strip-types": "^7.2.0",
"@iarna/toml": "^2.2.0",
"ansi-html-community": "0.0.8",
"chalk": "^4.1.0",
"clone": "^2.1.1",
"fast-glob": "3.1.1",
"fastest-levenshtein": "^1.0.8",
Expand All @@ -40,12 +56,9 @@
"node-forge": "^1.2.1",
"nullthrows": "^1.1.1",
"open": "^7.0.3",
"random-int": "^1.0.0",
"terminal-link": "^2.1.1"
},
"devDependencies": {
"@babel/plugin-transform-flow-strip-types": "^7.2.0",
"random-int": "^1.0.0"
},
"browser": {
"./src/generateCertificate.js": false,
"./src/http-server.js": false,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/utils/src/config.js
Expand Up @@ -5,7 +5,7 @@ import type {FileSystem} from '@parcel/fs';
import ThrowableDiagnostic from '@parcel/diagnostic';
import path from 'path';
import clone from 'clone';
import {parse as json5} from 'json5';
import json5 from 'json5';
import {parse as toml} from '@iarna/toml';
import LRU from 'lru-cache';

Expand Down Expand Up @@ -154,6 +154,6 @@ function getParser(extname) {
return toml;
case 'json':
default:
return json5;
return json5.parse;
}
}
17 changes: 14 additions & 3 deletions packages/core/utils/src/glob.js
Expand Up @@ -5,15 +5,26 @@ import type {FileSystem} from '@parcel/fs';

import _isGlob from 'is-glob';
import fastGlob, {type FastGlobOptions} from 'fast-glob';
import {isMatch} from 'micromatch';
import {isMatch, makeRe, type Options} from 'micromatch';
import {normalizeSeparators} from './path';

export function isGlob(p: FilePath): any {
return _isGlob(normalizeSeparators(p));
}

export function isGlobMatch(filePath: FilePath, glob: Glob): any {
return isMatch(filePath, normalizeSeparators(glob));
export function isGlobMatch(
filePath: FilePath,
glob: Glob | Array<Glob>,
opts?: Options,
): any {
glob = Array.isArray(glob)
? glob.map(normalizeSeparators)
: normalizeSeparators(glob);
return isMatch(filePath, glob, opts);
}

export function globToRegex(glob: Glob, opts?: Options): RegExp {
return makeRe(glob, opts);
}

export function globSync(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/utils/src/index.js
Expand Up @@ -41,7 +41,7 @@ export {
export {resolveConfig, resolveConfigSync, loadConfig} from './config';
export {DefaultMap, DefaultWeakMap} from './DefaultMap';
export {makeDeferredWithPromise} from './Deferred';
export {isGlob, isGlobMatch, globSync, glob} from './glob';
export {isGlob, isGlobMatch, globSync, glob, globToRegex} from './glob';
export {hashStream, hashObject, hashFile} from './hash';
export {SharedBuffer} from './shared-buffer';
export {fuzzySearch} from './schema';
Expand Down

0 comments on commit 4cbda87

Please sign in to comment.