提交 8ebe2f80 编写于 作者: J Jamie Kyle

wip

上级 764f5689
......@@ -8,7 +8,7 @@ coverage
.cache
dist
lib
!test/**/node_modules
!**/test/**/node_modules
.vscode/
.idea/
*.min.js
......
{
"presets": [
["env", {
"loose": true,
"targets": {
"node": "8.0.0"
}
}],
"flow"
],
"plugins": [
"transform-object-rest-spread",
"transform-class-properties"
]
}
{
"private": true,
"name": "parcel-packages",
"bolt": {
"workspaces": [
"packages/*"
]
},
"scripts": {
"build": "bolt workspaces exec -- babel src -d dist",
"test": "ava ./packages/*/test/*"
},
"dependencies": {
"find-up": "^3.0.0",
"typeable-promisify": "^2.0.1"
},
"devDependencies": {
"ava": "^0.25.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-flow": "^6.23.0",
"babel-register": "^6.26.0",
"fixturez": "^1.1.0"
},
"ava": {
"require": [
"babel-register"
]
}
}
{
"name": "@parcel/resolver",
"version": "2.0.0",
"main": "dist/Resolver.js",
"dependencies": {
"find-up": "^3.0.0",
"typeable-promisify": "^2.0.1"
}
}
\ No newline at end of file
// @flow
import fs from 'fs';
import path from 'path';
import findUp from 'find-up';
import promisify from 'typeable-promisify';
function stat(filePath) {
return promisify(cb => fs.stat(filePath, cb));
}
function readFile(filePath, encoding = 'utf-8') {
return promisify(cb => fs.readFile(filePath, encoding, cb));
}
function realpath(filePath) {
return promisify(cb => fs.realpath(filePath, cb));
}
async function isFile(filePath) {
try {
let stats = await stat(filePath);
return stats.isFile() || stats.isFIFO();
} catch (err) {
if (err.code !== 'ENOENT' || err.code === 'ENOTDIR') throw err;
return false;
}
}
async function isDirectory(filePath) {
try {
let stats = await stat(filePath);
return stats.isDirectory();
} catch (err) {
if (err.code !== 'ENOENT' || err.code === 'ENOTDIR') throw err;
return false;
}
}
type FilePath = string; // "/path/to/file/or/directory"
type ModuleRequest = string; // import "./moduleRequest";
type CacheKey = FilePath & ModuleRequest; // "/path/to/file/or/directory:./moduleRequest"
type Extension = string; // .js, .json, etc.
export type ResolverOpts = {
// ...
};
export type ResolveOpts = {
extensions?: Array<Extension>,
rootDir?: string
};
const defaultExtensions = ['.js', '.json'];
export default class Resolver {
opts: ResolverOpts;
cache: Map<CacheKey, FilePath>;
constructor(opts: ResolverOpts = {}) {
this.opts = opts;
this.cache = new Map();
}
async resolve(sourcePath: FilePath, moduleRequest: ModuleRequest, opts: ResolveOpts = {}) {
let cacheKey = sourcePath + ':' + moduleRequest;
let cacheEntry = this.cache.has(cacheKey);
if (cacheEntry) return cacheEntry;
let resolved;
if (moduleRequest.startsWith('.')) {
resolved = this._resolveRelativePath(sourcePath, moduleRequest, opts);
} else if (moduleRequest.startsWith('/')) {
resolved = this._resolveAbsolutePath(sourcePath, moduleRequest, opts);
} else if (moduleRequest.startsWith('~')) {
resolved = this._resolveTildePath(sourcePath, moduleRequest, opts);
} else {
resolved = this._resolveModulePath(sourcePath, moduleRequest, opts);
}
if (!resolved) {
throw new Error(`Unable to resolve "${moduleRequest}" from "${sourcePath}"`);
}
this.cache.set(cacheKey, resolved);
return resolved;
}
async _resolveRelativePath(sourcePath, moduleRequest, opts) {
let basePath = path.resolve(path.dirname(sourcePath), moduleRequest);
return this._tryExtensions(basePath, opts);
}
async _resolveAbsolutePath(sourcePath, moduleRequest, opts) {
let basePath = path.resolve(this._getRootDir(opts), moduleRequest.slice(1));
return this._tryExtensions(basePath, opts);
}
async _resolveTildePath(sourcePath, moduleRequest, opts) {
let packageJsonPath = await this._findClosestPackageJson(sourcePath)
let projectRoot = path.dirname(packageJsonPath);
// [TODO: Get Devon's input]
}
async _resolveModulePath(sourcePath, moduleRequest, opts) {
let packageJsonPath = await this._findClosestPackageJsonPath(sourcePath);
let pkg = await this._readPkg(packageJsonPath);
let aliases = pkg.alias;
if (aliases) {
throw 'implementme';
}
let root = path.parse(sourcePath).root;
let searching = path.dirname(packageJsonPath);
let { moduleName, moduleSubPath } = this._parseModuleRequest(moduleRequest);
let moduleDir = null;
do {
let testModuleDir = path.join(searching, 'node_modules', moduleName);
if (await isDirectory(testModuleDir)) {
moduleDir = testModuleDir;
}
} while (
searching !== root &&
(searching = path.dirname(searching))
);
if (!moduleDir) {
throw new Error('...');
}
let basePath = path.resolve(moduleDir, moduleSubPath);
let resolved = await this._tryExtensions(basePath, opts);
if (resolved) {
return resolved;
}
// let nodeModulesDir = await findUp(path.join('node_modules'));
// console.log(nodeModulesDir);
// ...
// find project's package.json#alias
// if alias, rewrite moduleRequest
//
// find moduleRequest base ("package name") from node_modules
// find package.json for matching node_modules/dir
//
// resolve against package.json#(main,module,source,browser,etc)
// -
}
async _tryExtensions(basePath, opts) {
let extensions = opts.extensions || defaultExtensions;
for (let extension of extensions) {
let testPath = basePath + extension;
if (await isFile(testPath)) {
return testPath;
}
}
return null; // throw?
}
_getRootDir(opts) {
if (opts.rootDir) {
return opts.rootDir;
} else {
throw new Error('Absolute /paths require opts.rootDir to be defined');
}
}
async _findClosestPackageJsonPath(sourcePath) {
return await findUp('package.json', { cwd: sourcePath });
}
async _readPkg(pkgPath) {
return JSON.parse(await readFile(pkgPath));
}
_parseModuleRequest(moduleRequest) {
let moduleParts = path.normalize(moduleRequest).split(path.sep);
let moduleName;
let moduleSubPath;
if (moduleRequest.startsWith('@')) {
moduleName = moduleParts.slice(0, 2).join(path.sep);
moduleSubPath = moduleParts.slice(2).join(path.sep);
} else {
moduleName = moduleParts[0];
moduleSubPath = moduleParts.slice(1).join(path.sep);
}
return {
moduleName,
moduleSubPath,
};
}
}
// @flow
import test from 'ava';
import path from 'path';
import Resolver from '../src/Resolver';
import fixtures from 'fixturez';
const f = fixtures(__dirname, { root: __dirname });
test('Resolver.resolve', async t => {
let fixture = f.find('simple');
let sourcePath = path.join(fixture, 'source.js');
let targetPath = path.join(fixture, 'target.js');
let resolver = new Resolver();
let resolved = await resolver.resolve(sourcePath, './target');
t.is(resolved, targetPath);
});
test('Resolver.resolve - absolute', async t => {
let fixture = f.find('simple');
let sourcePath = path.join(fixture, 'source.js');
let targetPath = path.join(fixture, 'target.js');
let resolver = new Resolver();
let resolved = await resolver.resolve(sourcePath, '/target', {
rootDir: fixture,
});
t.is(resolved, targetPath);
});
test('Resolver.resolve - module', async t => {
let fixture = f.find('module-default-index');
let sourcePath = path.join(fixture, 'source.js');
let targetPath = path.join(fixture, 'node_modules', 'target', 'index.js');
let resolver = new Resolver();
let resolved = await resolver.resolve(sourcePath, 'target');
t.is(resolved, targetPath);
});
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
any-promise@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
typeable-promisify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/typeable-promisify/-/typeable-promisify-2.0.1.tgz#1baee82abaf13280198eb11e98589c881a6bd80d"
dependencies:
any-promise "^1.3.0"
此差异已折叠。
......@@ -8,12 +8,7 @@
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"files": [
"bin/",
"lib/",
"src/",
"index.js"
],
"files": ["bin/", "lib/", "src/", "index.js"],
"dependencies": {
"ansi-to-html": "^0.6.4",
"babel-code-frame": "^6.26.0",
......@@ -67,6 +62,7 @@
"terser": "^3.7.3",
"toml": "^2.3.3",
"tomlify-j0.4": "^3.0.0",
"typeable-promisify": "^2.0.1",
"v8-compile-cache": "^2.0.0",
"ws": "^5.1.1"
},
......@@ -114,10 +110,13 @@
"format": "prettier --write \"./{src,bin,test}/**/*.{js,json,md}\"",
"build": "yarn minify && babel src -d lib && ncp src/builtins lib/builtins",
"prepublish": "yarn build",
"minify": "terser -c -m -o src/builtins/prelude.min.js src/builtins/prelude.js && terser -c -m -o src/builtins/prelude2.min.js src/builtins/prelude2.js",
"minify":
"terser -c -m -o src/builtins/prelude.min.js src/builtins/prelude.js && terser -c -m -o src/builtins/prelude2.min.js src/builtins/prelude2.js",
"precommit": "npm run lint && lint-staged",
"lint": "eslint . && prettier \"./{src,bin,test}/**/*.{js,json,md}\" --list-different",
"postinstall": "node -e \"console.log('\\u001b[35m\\u001b[1mLove Parcel? You can now donate to our open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[34mhttps://opencollective.com/parcel/donate\\u001b[0m')\""
"lint":
"eslint . && prettier \"./{src,bin,test}/**/*.{js,json,md}\" --list-different",
"postinstall":
"node -e \"console.log('\\u001b[35m\\u001b[1mLove Parcel? You can now donate to our open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[34mhttps://opencollective.com/parcel/donate\\u001b[0m')\""
},
"bin": {
"parcel": "bin/cli.js"
......@@ -126,10 +125,7 @@
"node": ">= 6.0.0"
},
"lint-staged": {
"*.{js,json,md}": [
"prettier --write",
"git add"
]
"*.{js,json,md}": ["prettier --write", "git add"]
},
"collective": {
"type": "opencollective",
......
......@@ -166,6 +166,10 @@ any-observable@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/any-observable/-/any-observable-0.3.0.tgz#af933475e5806a67d0d7df090dd5e8bef65d119b"
any-promise@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
anymatch@^1.3.0:
version "1.3.2"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-1.3.2.tgz#553dcb8f91e3c889845dfdba34c77721b90b9d7a"
......@@ -7180,6 +7184,12 @@ type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
typeable-promisify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/typeable-promisify/-/typeable-promisify-2.0.1.tgz#1baee82abaf13280198eb11e98589c881a6bd80d"
dependencies:
any-promise "^1.3.0"
typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册