Skip to content

Commit

Permalink
feat: add proper support for pnp
Browse files Browse the repository at this point in the history
using the new resolve hook
  • Loading branch information
sergioramos committed Sep 21, 2020
1 parent 4fac7ca commit 391c91d
Show file tree
Hide file tree
Showing 170 changed files with 44,948 additions and 151,709 deletions.
111 changes: 82 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const archiver = require('archiver');
const { readFileSync, lstatSync, realpathSync, ...fs } = require('fs');
const { exists, stat: statAsync } = require('mz/fs');
const { exists, existsSync, stat: statAsync } = require('mz/fs');
const { nodeFileTrace: FileTrace } = require('@vercel/nft');
const { default: resDep } = require('@vercel/nft/out/resolve-dependency.js');
const Flatten = require('lodash.flatten');
const { default: Find } = require('apr-find');
const Globby = require('globby');
const { default: Map } = require('apr-map');
const nanomatch = require('nanomatch');
const Parallel = require('apr-parallel');
const { basename, dirname, extname, sep } = require('path');
const { basename, dirname, extname, isAbsolute, sep, join } = require('path');
const { normalize, relative, resolve } = require('path');
const PathIsInside = require('path-is-inside');
const SortBy = require('lodash.sortby');
Expand All @@ -19,6 +20,20 @@ const Uniq = require('lodash.uniq');
const zipService = require('serverless/lib/plugins/package/lib/zipService');
const packageService = require('serverless/lib/plugins/package/lib/packageService');

const EXTENSIONS = ['.tsx', '.ts', '.node', '.mjs', '.cjs', '.js'];
const NODE_BUILTINS = [
...require('repl')._builtinLibs,
'constants',
'module',
'timers',
'console',
'_stream_writable',
'_stream_readable',
'_stream_duplex',
'process',
'sys',
];

try {
// eslint-disable-next-line no-var
var {
Expand Down Expand Up @@ -307,57 +322,36 @@ module.exports = class {
);

const entrypoint = await Find(
['.tsx', '.ts', '.js'].map((ext) => entry.concat(ext)),
EXTENSIONS.map((ext) => entry.concat(ext)),
(filename) => exists(filename),
);

const getRealPath = (pathanme) => {
try {
return realpathSync(pathanme);
// eslint-disable-next-line no-unused-vars
} catch (err) {
return pathanme;
}
};

const readFile = (fullpath) => {
try {
return readFileSync(fullpath, 'utf-8');
// eslint-disable-next-line no-unused-vars
} catch (err) {
try {
return readFileSync(require.resolve(fullpath), 'utf-8');
// eslint-disable-next-line no-unused-vars
} catch (err) {}
}
};

const handleFile = (pathname) => {
const realpath = getRealPath(pathname);
const realpath = realpathSync(pathname);

const cache = this.files.find(([src]) => src === realpath);

if (Array.isArray(cache)) {
const [fullpath] = cache;
if (fullpath) {
return readFile(fullpath);
return readFileSync(fullpath, 'utf-8');
}
}

// eslint-disable-next-line block-scoped-var
if (!tsAvailable) {
return readFile(pathname);
return readFileSync(pathname, 'utf-8');
}

if (!['.ts', '.tsx'].includes(extname(pathname))) {
return readFile(pathname);
return readFileSync(pathname, 'utf-8');
}

if (
!PathIsInside(pathname, this.servicePath) ||
pathname.split(sep).includes(['node_modules'])
) {
return readFile(pathname);
return readFileSync(pathname, 'utf-8');
}

// eslint-disable-next-line block-scoped-var
Expand All @@ -375,10 +369,69 @@ module.exports = class {
return outputText;
};

const nativeResolve = (id, parent) => {
return require.resolve(id, {
paths: [parent, dirname(parent)],
});
};

const fallbackResolve = (id, parent, { previous } = {}) => {
const pathStack = parent.split(sep);
if (pathStack.includes('node_modules') || pathStack.includes('.yarn')) {
return nativeResolve(id, parent);
}

if (
isAbsolute(id) ||
id === '.' ||
id === '..' ||
id.startsWith('./') ||
id.startsWith('../')
) {
const ext = extname(id);
const entry = resolve(dirname(parent), ext ? basename(id, ext) : id);
const resolved = EXTENSIONS.map((ext) => entry.concat(ext)).find(
(filename) => {
return existsSync(filename);
},
);

if (resolved) {
return resolved;
}

if (!resolved && previous) {
return nativeResolve(previous, parent);
}

return fallbackResolve(join(id, 'index'), parent, {
tryAgain: false,
previous: id,
});
}

return nativeResolve(id, parent);
};

const { fileList = [] } = await FileTrace([entrypoint], {
base: this.servicePath,
readLink: handleFile,
readFile: handleFile,
resolve: (id, parent, job, isESM) => {
if (NODE_BUILTINS.includes(id)) {
return `node:${id}`;
}

const [, defaultResolve] = (() => {
try {
return [null, resDep(id, parent, job, isESM)];
} catch (err) {
return [err];
}
})();

return defaultResolve ? defaultResolve : fallbackResolve(id, parent);
},
});

const patterns = excludes
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
"test": "NODE_ENV=test ava --timeout=5m"
},
"dependencies": {
"@vercel/nft": "^0.9.1",
"@vercel/nft": "^0.9.3",
"apr-find": "^3.0.3",
"apr-map": "^3.0.3",
"apr-parallel": "^3.0.3",
"archiver": "^5.0.1",
"archiver": "^5.0.2",
"globby": "^11.0.1",
"lodash.flatten": "^4.4.0",
"lodash.sortby": "^4.7.0",
"lodash.topairs": "^4.3.0",
"lodash.uniq": "^4.5.0",
Expand All @@ -50,13 +51,12 @@
"apr-reduce": "^3.0.3",
"ava": "^3.12.1",
"directory-tree": "^2.2.4",
"eslint": "^7.8.1",
"eslint": "^7.9.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-xo-space": "^0.25.0",
"execa": "^4.0.3",
"hasha": "^5.2.0",
"husky": "^4.3.0",
"lint-staged": "^10.3.0",
"lint-staged": "^10.4.0",
"markdown-table": "^2.0.0",
"path-type": "^4.0.0",
"prettier": "^2.1.2",
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions test/__fixtures__/js-nm-individually/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodeLinker: node-modules
yarnPath: .yarn/releases/yarn-berry.js
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { statusCode } = require('out');

module.exports = async () => {
return { statusCode };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { statusCode } = require('out');

module.exports = async () => {
return { statusCode };
};
4 changes: 4 additions & 0 deletions test/__fixtures__/js-nm-individually/handlers/hello/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { http } = require('framework');
const handler = require('./handler');

module.exports.handler = http(handler);
4 changes: 4 additions & 0 deletions test/__fixtures__/js-nm-individually/handlers/world/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { http } = require('framework');
const handler = require('../handler');

module.exports.handler = http(handler);
File renamed without changes.
File renamed without changes.

0 comments on commit 391c91d

Please sign in to comment.