@esbuild-plugins/node-resolve
Advanced tools
Comparing version 0.0.15 to 0.1.0
# @esbuild-plugins/node-resolve | ||
## 0.1.0 | ||
### Minor Changes | ||
- Resolve to real path and not virtuals when using yarn pnp | ||
## 0.0.15 | ||
@@ -4,0 +10,0 @@ |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -22,29 +11,2 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
@@ -61,13 +23,2 @@ var t = {}; | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -78,160 +29,128 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
exports.cleanUrl = exports.queryRE = exports.NodeResolvePlugin = exports.resolveAsync = void 0; | ||
var escape_string_regexp_1 = __importDefault(require("escape-string-regexp")); | ||
var fs_1 = __importDefault(require("fs")); | ||
var module_1 = require("module"); | ||
var path_1 = __importDefault(require("path")); | ||
var resolve_1 = __importDefault(require("resolve")); | ||
var util_1 = require("util"); | ||
var NAME = 'node-resolve'; | ||
var debug = require('debug')(NAME); | ||
var resolveAsync = function (id, _a) { return __awaiter(void 0, void 0, void 0, function () { | ||
const escape_string_regexp_1 = __importDefault(require("escape-string-regexp")); | ||
const fs_1 = __importDefault(require("fs")); | ||
const module_1 = require("module"); | ||
const path_1 = __importDefault(require("path")); | ||
const resolve_1 = __importDefault(require("resolve")); | ||
const util_1 = require("util"); | ||
const NAME = 'node-resolve'; | ||
const debug = require('debug')(NAME); | ||
let pnpapi; | ||
try { | ||
pnpapi = require('pnpapi'); | ||
} | ||
catch (_a) { } | ||
const promisifiedResolve = util_1.promisify(resolve_1.default); | ||
const resolveAsync = (id, _b) => __awaiter(void 0, void 0, void 0, function* () { | ||
var { mainFields } = _b, _opts = __rest(_b, ["mainFields"]); | ||
function packageFilter(packageJSON) { | ||
var e_1, _a; | ||
if (!(mainFields === null || mainFields === void 0 ? void 0 : mainFields.length)) { | ||
return packageJSON; | ||
} | ||
try { | ||
// changes the main field to be another field | ||
for (var mainFields_1 = __values(mainFields), mainFields_1_1 = mainFields_1.next(); !mainFields_1_1.done; mainFields_1_1 = mainFields_1.next()) { | ||
var mainField = mainFields_1_1.value; | ||
if (mainField === 'main') { | ||
break; | ||
} | ||
var newMain = packageJSON[mainField]; | ||
if (newMain && typeof newMain === 'string') { | ||
debug("set main to '" + mainField); | ||
packageJSON['main'] = newMain; | ||
break; | ||
} | ||
// changes the main field to be another field | ||
for (let mainField of mainFields) { | ||
if (mainField === 'main') { | ||
break; | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (mainFields_1_1 && !mainFields_1_1.done && (_a = mainFields_1.return)) _a.call(mainFields_1); | ||
const newMain = packageJSON[mainField]; | ||
if (newMain && typeof newMain === 'string') { | ||
debug(`set main to '${mainField}`); | ||
packageJSON['main'] = newMain; | ||
break; | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return packageJSON; | ||
} | ||
var opts, res; | ||
var mainFields = _a.mainFields, _opts = __rest(_a, ["mainFields"]); | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
opts = __assign(__assign({}, _opts), { preserveSymlinks: false, packageFilter: packageFilter }); | ||
return [4 /*yield*/, util_1.promisify(resolve_1.default)(id, opts)]; | ||
case 1: | ||
res = _b.sent(); | ||
return [2 /*return*/, res]; | ||
const opts = Object.assign({ preserveSymlinks: false, packageFilter }, _opts); | ||
const res = yield promisifiedResolve(id, opts); | ||
if (res && pnpapi) { | ||
const realPath = pnpapi.resolveVirtual(res); | ||
if (realPath) { | ||
return realPath; | ||
} | ||
}); | ||
}); }; | ||
} | ||
return res; | ||
}); | ||
exports.resolveAsync = resolveAsync; | ||
function NodeResolvePlugin(_a) { | ||
var onNonResolved = _a.onNonResolved, namespace = _a.namespace, extensions = _a.extensions, onResolved = _a.onResolved, resolveOptions = _a.resolveOptions, mainFields = _a.mainFields, _b = _a.name, name = _b === void 0 ? NAME : _b, isExtensionRequiredInImportPath = _a.isExtensionRequiredInImportPath, resolveSynchronously = _a.resolveSynchronously; | ||
var builtinsSet = new Set(module_1.builtinModules); | ||
function NodeResolvePlugin({ onNonResolved, namespace, extensions, onResolved, resolveOptions, mainFields, name = NAME, isExtensionRequiredInImportPath, resolveSynchronously, }) { | ||
const builtinsSet = new Set(module_1.builtinModules); | ||
debug('setup'); | ||
var filter = new RegExp('(' + extensions.map(escape_string_regexp_1.default).join('|') + ')(\\?.*)?$'); | ||
const filter = new RegExp('(' + extensions.map(escape_string_regexp_1.default).join('|') + ')(\\?.*)?$'); | ||
return { | ||
name: name, | ||
setup: function setup(_a) { | ||
var _this = this; | ||
var onLoad = _a.onLoad, onResolve = _a.onResolve; | ||
onLoad({ filter: filter, namespace: namespace }, function (args) { return __awaiter(_this, void 0, void 0, function () { | ||
var contents, resolveDir, e_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
if (builtinsSet.has(args.path)) { | ||
return [2 /*return*/]; | ||
} | ||
return [4 /*yield*/, fs_1.default.promises.readFile(args.path)]; // do not convert to string to support images and other assets | ||
case 1: | ||
contents = _a.sent() // do not convert to string to support images and other assets | ||
; | ||
resolveDir = path_1.default.dirname(args.path); | ||
debug('onLoad'); | ||
return [2 /*return*/, { | ||
loader: 'default', | ||
contents: contents, | ||
resolveDir: resolveDir, | ||
}]; | ||
case 2: | ||
e_2 = _a.sent(); | ||
return [2 /*return*/, null]; | ||
case 3: return [2 /*return*/]; | ||
name, | ||
setup: function setup({ onLoad, onResolve }) { | ||
onLoad({ filter, namespace }, (args) => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
if (builtinsSet.has(args.path)) { | ||
return; | ||
} | ||
}); | ||
}); }); | ||
const contents = yield fs_1.default.promises.readFile(args.path); // do not convert to string to support images and other assets | ||
let resolveDir = path_1.default.dirname(args.path); | ||
debug('onLoad'); | ||
return { | ||
loader: 'default', | ||
contents, | ||
resolveDir, | ||
}; | ||
} | ||
catch (e) { | ||
return null; | ||
} | ||
})); | ||
onResolve({ filter: isExtensionRequiredInImportPath ? filter : /.*/ }, function resolver(args) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var resolved, options, _a, e_3, res, res; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
args.path = exports.cleanUrl(args.path); | ||
if (builtinsSet.has(args.path)) { | ||
return [2 /*return*/, null]; | ||
} | ||
if (args.path.startsWith('data:')) { | ||
return [2 /*return*/, null]; | ||
} | ||
_b.label = 1; | ||
case 1: | ||
_b.trys.push([1, 5, , 9]); | ||
options = __assign({ basedir: args.resolveDir, preserveSymlinks: false, extensions: extensions, | ||
mainFields: mainFields }, resolveOptions); | ||
if (!resolveSynchronously) return [3 /*break*/, 2]; | ||
_a = resolve_1.default.sync(args.path, options); | ||
return [3 /*break*/, 4]; | ||
case 2: return [4 /*yield*/, exports.resolveAsync(args.path, options)]; | ||
case 3: | ||
_a = _b.sent(); | ||
_b.label = 4; | ||
case 4: | ||
resolved = _a; | ||
return [3 /*break*/, 9]; | ||
case 5: | ||
e_3 = _b.sent(); | ||
debug("not resolved " + args.path); | ||
if (!onNonResolved) return [3 /*break*/, 7]; | ||
return [4 /*yield*/, onNonResolved(args.path, args.importer)]; | ||
case 6: | ||
res = _b.sent(); | ||
return [2 /*return*/, res || null]; | ||
case 7: return [2 /*return*/, null]; | ||
case 8: return [3 /*break*/, 9]; | ||
case 9: | ||
// resolved = path.relative(resolved, process.cwd()) | ||
debug("resolved '" + resolved + "'"); | ||
if (!(resolved && onResolved)) return [3 /*break*/, 11]; | ||
return [4 /*yield*/, onResolved(resolved, args.importer)]; | ||
case 10: | ||
res = _b.sent(); | ||
if (typeof res === 'string') { | ||
return [2 /*return*/, { | ||
path: res, | ||
namespace: namespace, | ||
}]; | ||
} | ||
if ((res === null || res === void 0 ? void 0 : res.path) != null || | ||
(res === null || res === void 0 ? void 0 : res.external) != null || | ||
(res === null || res === void 0 ? void 0 : res.namespace) != null || | ||
(res === null || res === void 0 ? void 0 : res.errors) != null) { | ||
return [2 /*return*/, res]; | ||
} | ||
_b.label = 11; | ||
case 11: | ||
debug('onResolve'); | ||
if (resolved) { | ||
return [2 /*return*/, { | ||
path: resolved, | ||
namespace: namespace, | ||
}]; | ||
} | ||
return [2 /*return*/]; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
args.path = exports.cleanUrl(args.path); | ||
if (builtinsSet.has(args.path)) { | ||
return null; | ||
} | ||
if (args.path.startsWith('data:')) { | ||
return null; | ||
} | ||
let resolved; | ||
try { | ||
const options = Object.assign({ basedir: args.resolveDir, preserveSymlinks: false, extensions, | ||
mainFields }, resolveOptions); | ||
resolved = resolveSynchronously | ||
? resolve_1.default.sync(args.path, options) | ||
: yield exports.resolveAsync(args.path, options); | ||
} | ||
catch (e) { | ||
debug(`not resolved ${args.path}`); | ||
if (onNonResolved) { | ||
let res = yield onNonResolved(args.path, args.importer); | ||
return res || null; | ||
} | ||
}); | ||
else { | ||
return null; | ||
} | ||
} | ||
// resolved = path.relative(resolved, process.cwd()) | ||
debug(`resolved '${resolved}'`); | ||
if (resolved && onResolved) { | ||
const res = yield onResolved(resolved, args.importer); | ||
if (typeof res === 'string') { | ||
return { | ||
path: res, | ||
namespace, | ||
}; | ||
} | ||
if ((res === null || res === void 0 ? void 0 : res.path) != null || | ||
(res === null || res === void 0 ? void 0 : res.external) != null || | ||
(res === null || res === void 0 ? void 0 : res.namespace) != null || | ||
(res === null || res === void 0 ? void 0 : res.errors) != null) { | ||
return res; | ||
} | ||
} | ||
if (resolveSynchronously && resolved && pnpapi) { | ||
const realPath = pnpapi.resolveVirtual(resolved); | ||
if (realPath) { | ||
return { path: realPath, namespace }; | ||
} | ||
} | ||
if (resolved) { | ||
return { | ||
path: resolved, | ||
namespace, | ||
}; | ||
} | ||
}); | ||
@@ -245,4 +164,4 @@ }); | ||
exports.queryRE = /\?.*$/; | ||
var cleanUrl = function (url) { return url.replace(exports.queryRE, ''); }; | ||
const cleanUrl = (url) => url.replace(exports.queryRE, ''); | ||
exports.cleanUrl = cleanUrl; | ||
//# sourceMappingURL=index.js.map |
@@ -11,45 +11,2 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -59,211 +16,164 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var esbuild_1 = require("esbuild"); | ||
var test_support_1 = require("test-support"); | ||
var _1 = __importDefault(require(".")); | ||
var slash_1 = __importDefault(require("slash")); | ||
var path_1 = __importDefault(require("path")); | ||
const esbuild_1 = require("esbuild"); | ||
const test_support_1 = require("test-support"); | ||
const _1 = __importDefault(require(".")); | ||
const slash_1 = __importDefault(require("slash")); | ||
const path_1 = __importDefault(require("path")); | ||
require('debug').enable(require('../package.json').name); | ||
test('works', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, base, _b, ENTRY, called, resolved, res, expected; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "import {x} from './utils'; console.log(x);", | ||
'utils.ts': "import mod from 'mod'; export const x = mod('x');", | ||
'node_modules/mod/index.js': 'export default () => {}', | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, base = _a.base, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
called = 0; | ||
resolved = []; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
onNonResolved: function (p) { | ||
throw new Error("cannot resolve " + p); | ||
}, | ||
onResolved: function (x) { | ||
resolved.push(x); | ||
called++; | ||
return x; | ||
}, | ||
}), | ||
], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
expect(called).toBe(3); | ||
expected = ['entry.ts', 'utils.ts', 'node_modules/mod/index.js']; | ||
expect(resolved.map(normalize(base))).toEqual(expected); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
test('works', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, base, paths: [ENTRY], } = yield test_support_1.writeFiles({ | ||
'entry.ts': `import {x} from './utils'; console.log(x);`, | ||
'utils.ts': `import mod from 'mod'; export const x = mod('x');`, | ||
'node_modules/mod/index.js': 'export default () => {}', | ||
}); | ||
}); }); | ||
test('all resolved paths are absolute', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, base, _b, ENTRY, resolved, res; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "import {x} from './utils'; console.log(x);", | ||
'utils.ts': "import mod from 'mod'; export const x = mod('x');", | ||
'node_modules/mod/index.js': 'export default () => {}', | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, base = _a.base, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
resolved = []; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
onResolved: function (x) { | ||
resolved.push(x); | ||
return x; | ||
}, | ||
}), | ||
], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
expect(resolved.filter(function (x) { return path_1.default.isAbsolute(x); }).length).toEqual(3); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
let called = 0; | ||
let resolved = []; | ||
const res = yield esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
onNonResolved: (p) => { | ||
throw new Error(`cannot resolve ${p}`); | ||
}, | ||
onResolved: (x) => { | ||
resolved.push(x); | ||
called++; | ||
return x; | ||
}, | ||
}), | ||
], | ||
}); | ||
}); }); | ||
test('does not throw when onUnresolved', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, _b, ENTRY, called; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "import {x} from 'xxx'; console.log(x);", | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
called = false; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
onNonResolved: function () { | ||
called = true; | ||
return { | ||
external: true, | ||
}; | ||
}, | ||
}), | ||
], | ||
})]; | ||
case 2: | ||
_c.sent(); | ||
expect(called).toBeTruthy(); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
expect(called).toBe(3); | ||
const expected = ['entry.ts', 'utils.ts', 'node_modules/mod/index.js']; | ||
expect(resolved.map(normalize(base))).toEqual(expected); | ||
unlink(); | ||
// console.log(formatEsbuildOutput(res)) | ||
})); | ||
test('all resolved paths are absolute', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, base, paths: [ENTRY], } = yield test_support_1.writeFiles({ | ||
'entry.ts': `import {x} from './utils'; console.log(x);`, | ||
'utils.ts': `import mod from 'mod'; export const x = mod('x');`, | ||
'node_modules/mod/index.js': 'export default () => {}', | ||
}); | ||
}); }); | ||
test('uses mainFields option', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, base, _b, ENTRY, resolved; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'main.ts': "import mod from 'mod'; export const x = mod('x');", | ||
'node_modules/mod/module.js': 'export default () => {}', | ||
'node_modules/mod/package.json': JSON.stringify({ | ||
name: 'mod', | ||
version: '0.0.0', | ||
module: 'module.js', | ||
main: 'module.js', | ||
}), | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, base = _a.base, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
resolved = []; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts', '.json'], | ||
mainFields: ['module', 'main'], | ||
onNonResolved: function (p) { | ||
throw new Error("cannot resolve " + p); | ||
}, | ||
onResolved: function (p) { | ||
resolved.push(p); | ||
}, | ||
}), | ||
], | ||
})]; | ||
case 2: | ||
_c.sent(); | ||
expect(resolved.map(normalize(base))).toEqual([ | ||
'main.ts', | ||
'node_modules/mod/module.js', | ||
]); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
let resolved = []; | ||
const res = yield esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
onResolved: (x) => { | ||
resolved.push(x); | ||
return x; | ||
}, | ||
}), | ||
], | ||
}); | ||
}); }); | ||
test('isExtensionRequiredInImportPath', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, unlink, base, _b, ENTRY, called, resolved, res, expected; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, test_support_1.writeFiles({ | ||
'entry.ts': "import {x} from './utils.ts'; console.log(x);", | ||
'utils.ts': "export * from './another.ts?query'", | ||
'another.ts': "import mod from 'mod'; export const x = mod('x');", | ||
'node_modules/mod/index.js': 'export default () => {}', | ||
})]; | ||
case 1: | ||
_a = _c.sent(), unlink = _a.unlink, base = _a.base, _b = __read(_a.paths, 1), ENTRY = _b[0]; | ||
called = 0; | ||
resolved = []; | ||
return [4 /*yield*/, esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
isExtensionRequiredInImportPath: true, | ||
onNonResolved: function (p) { | ||
throw new Error("cannot resolve " + p); | ||
}, | ||
onResolved: function (x) { | ||
resolved.push(x); | ||
called++; | ||
return x; | ||
}, | ||
}), | ||
], | ||
})]; | ||
case 2: | ||
res = _c.sent(); | ||
expect(called).toBe(3); | ||
expected = ['entry.ts', 'utils.ts', 'another.ts']; | ||
expect(resolved.map(normalize(base))).toEqual(expected); | ||
unlink(); | ||
return [2 /*return*/]; | ||
} | ||
expect(resolved.filter((x) => path_1.default.isAbsolute(x)).length).toEqual(3); | ||
unlink(); | ||
// console.log(formatEsbuildOutput(res)) | ||
})); | ||
test('does not throw when onUnresolved', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, paths: [ENTRY], } = yield test_support_1.writeFiles({ | ||
'entry.ts': `import {x} from 'xxx'; console.log(x);`, | ||
}); | ||
}); }); | ||
var normalize = function (base) { return function (x) { return slash_1.default(path_1.default.relative(base, x)); }; }; | ||
let called = false; | ||
yield esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
onNonResolved: () => { | ||
called = true; | ||
return { | ||
external: true, | ||
}; | ||
}, | ||
}), | ||
], | ||
}); | ||
expect(called).toBeTruthy(); | ||
unlink(); | ||
})); | ||
test('uses mainFields option', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, base, paths: [ENTRY], } = yield test_support_1.writeFiles({ | ||
'main.ts': `import mod from 'mod'; export const x = mod('x');`, | ||
'node_modules/mod/module.js': 'export default () => {}', | ||
'node_modules/mod/package.json': JSON.stringify({ | ||
name: 'mod', | ||
version: '0.0.0', | ||
module: 'module.js', | ||
main: 'module.js', | ||
}), | ||
}); | ||
let resolved = []; | ||
yield esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts', '.json'], | ||
mainFields: ['module', 'main'], | ||
onNonResolved: (p) => { | ||
throw new Error(`cannot resolve ${p}`); | ||
}, | ||
onResolved(p) { | ||
resolved.push(p); | ||
}, | ||
}), | ||
], | ||
}); | ||
expect(resolved.map(normalize(base))).toEqual([ | ||
'main.ts', | ||
'node_modules/mod/module.js', | ||
]); | ||
unlink(); | ||
})); | ||
test('isExtensionRequiredInImportPath', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const { unlink, base, paths: [ENTRY], } = yield test_support_1.writeFiles({ | ||
'entry.ts': `import {x} from './utils.ts'; console.log(x);`, | ||
'utils.ts': `export * from './another.ts?query'`, | ||
'another.ts': `import mod from 'mod'; export const x = mod('x');`, | ||
'node_modules/mod/index.js': 'export default () => {}', | ||
}); | ||
let called = 0; | ||
let resolved = []; | ||
const res = yield esbuild_1.build({ | ||
entryPoints: [ENTRY], | ||
write: false, | ||
format: 'esm', | ||
target: 'es2017', | ||
bundle: true, | ||
plugins: [ | ||
_1.default({ | ||
extensions: ['.js', '.ts'], | ||
isExtensionRequiredInImportPath: true, | ||
onNonResolved: (p) => { | ||
throw new Error(`cannot resolve ${p}`); | ||
}, | ||
onResolved: (x) => { | ||
resolved.push(x); | ||
called++; | ||
return x; | ||
}, | ||
}), | ||
], | ||
}); | ||
expect(called).toBe(3); | ||
const expected = ['entry.ts', 'utils.ts', 'another.ts']; | ||
expect(resolved.map(normalize(base))).toEqual(expected); | ||
unlink(); | ||
// console.log(formatEsbuildOutput(res)) | ||
})); | ||
const normalize = (base) => (x) => slash_1.default(path_1.default.relative(base, x)); | ||
//# sourceMappingURL=index.test.js.map |
{ | ||
"name": "@esbuild-plugins/node-resolve", | ||
"version": "0.0.15", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -12,4 +12,11 @@ import { OnResolveArgs, OnResolveResult, Plugin } from 'esbuild' | ||
let pnpapi | ||
try { | ||
pnpapi = require('pnpapi') | ||
} catch {} | ||
type ResolveAsyncOpts = ResolveOpts & { mainFields?: string[] } | ||
const promisifiedResolve = promisify(resolve as any) | ||
export const resolveAsync: ( | ||
@@ -38,7 +45,13 @@ id: string, | ||
const opts: ResolveOpts = { | ||
..._opts, | ||
preserveSymlinks: false, | ||
packageFilter, | ||
..._opts, | ||
} | ||
const res = await promisify(resolve as any)(id, opts) | ||
const res = await promisifiedResolve(id, opts) | ||
if (res && pnpapi) { | ||
const realPath = pnpapi.resolveVirtual(res) | ||
if (realPath) { | ||
return realPath | ||
} | ||
} | ||
return res | ||
@@ -54,3 +67,2 @@ } | ||
isExtensionRequiredInImportPath?: boolean | ||
// TODO add an importsNeedExtension to only match imports with given extension, useful to resolve css and assets only if they match regex | ||
namespace?: string | undefined | ||
@@ -160,3 +172,8 @@ onNonResolved?: ( | ||
debug('onResolve') | ||
if (resolveSynchronously && resolved && pnpapi) { | ||
const realPath = pnpapi.resolveVirtual(resolved) | ||
if (realPath) { | ||
return { path: realPath, namespace } | ||
} | ||
} | ||
if (resolved) { | ||
@@ -163,0 +180,0 @@ return { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57474
994