Skip to content

Commit 8a094e8

Browse files
authored
Merge pull request #260 from webpack/bugfix/256
exports and imports field fixes
2 parents 0001f80 + afcd626 commit 8a094e8

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

lib/ResolverFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ exports.createResolver = function (options) {
377377
"internal",
378378
conditionNames,
379379
importsField,
380-
"final-file",
380+
"relative",
381381
"internal-resolve"
382382
)
383383
);
@@ -444,7 +444,7 @@ exports.createResolver = function (options) {
444444
"resolve-in-package",
445445
conditionNames,
446446
exportsField,
447-
"final-file"
447+
"relative"
448448
)
449449
);
450450
});

test/exportsField.js

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,13 @@ describe("ExportsFieldPlugin", () => {
13141314
const nodeFileSystem = new CachedInputFileSystem(fs, 4000);
13151315

13161316
const resolver = ResolverFactory.createResolver({
1317+
extensions: [".js"],
1318+
fileSystem: nodeFileSystem,
1319+
fullySpecified: true,
1320+
conditionNames: ["webpack"]
1321+
});
1322+
1323+
const commonjsResolver = ResolverFactory.createResolver({
13171324
extensions: [".js"],
13181325
fileSystem: nodeFileSystem,
13191326
conditionNames: ["webpack"]
@@ -1354,7 +1361,7 @@ describe("ExportsFieldPlugin", () => {
13541361
);
13551362
});
13561363

1357-
it("resolve using exports field, not a browser field #2", done => {
1364+
it("resolve using exports field and a browser alias field #2", done => {
13581365
const resolver = ResolverFactory.createResolver({
13591366
aliasFields: ["browser"],
13601367
extensions: [".js"],
@@ -1371,7 +1378,7 @@ describe("ExportsFieldPlugin", () => {
13711378
if (err) return done(err);
13721379
if (!result) throw new Error("No result");
13731380
result.should.equal(
1374-
path.resolve(fixture2, "node_modules/exports-field/lib/main.js")
1381+
path.resolve(fixture2, "node_modules/exports-field/lib/browser.js")
13751382
);
13761383
done();
13771384
}
@@ -1393,6 +1400,38 @@ describe("ExportsFieldPlugin", () => {
13931400
);
13941401
});
13951402

1403+
it("throw error if extension not provided", done => {
1404+
resolver.resolve(
1405+
{},
1406+
fixture2,
1407+
"exports-field/dist/main",
1408+
{},
1409+
(err, result) => {
1410+
if (!err) throw new Error(`expect error, got ${result}`);
1411+
err.should.be.instanceof(Error);
1412+
err.message.should.match(/Can't resolve/);
1413+
done();
1414+
}
1415+
);
1416+
});
1417+
1418+
it("should resolve extension without fullySpecified", done => {
1419+
commonjsResolver.resolve(
1420+
{},
1421+
fixture2,
1422+
"exports-field/dist/main",
1423+
{},
1424+
(err, result) => {
1425+
if (err) return done(err);
1426+
if (!result) throw new Error("No result");
1427+
result.should.equal(
1428+
path.resolve(fixture2, "node_modules/exports-field/lib/lib2/main.js")
1429+
);
1430+
done();
1431+
}
1432+
);
1433+
});
1434+
13961435
it("resolver should respect condition names", done => {
13971436
resolver.resolve(
13981437
{},
@@ -1728,10 +1767,12 @@ describe("ExportsFieldPlugin", () => {
17281767
" existing directory .../node_modules/exports-field",
17291768
" using description file: .../node_modules/exports-field/package.json (relative path: .)",
17301769
" using exports field: ./lib/lib2/browser.js",
1731-
" .../node_modules/exports-field/lib/lib2/browser.js doesn't exist",
1770+
" using description file: .../node_modules/exports-field/package.json (relative path: ./lib/lib2/browser.js)",
1771+
" .../node_modules/exports-field/lib/lib2/browser.js doesn't exist",
17321772
" using exports field: ./lib/browser.js",
1733-
" existing file: .../node_modules/exports-field/lib/browser.js",
1734-
" reporting result .../node_modules/exports-field/lib/browser.js"
1773+
" using description file: .../node_modules/exports-field/package.json (relative path: ./lib/browser.js)",
1774+
" existing file: .../node_modules/exports-field/lib/browser.js",
1775+
" reporting result .../node_modules/exports-field/lib/browser.js"
17351776
]);
17361777
done();
17371778
}

test/importsField.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,18 @@ describe("ImportsFieldPlugin", () => {
13251325
" existing directory .../node_modules/a",
13261326
" using description file: .../node_modules/a/package.json (relative path: .)",
13271327
" using exports field: ./lib/lib2/index.js",
1328-
" .../node_modules/a/lib/lib2/index.js doesn't exist",
1328+
" using description file: .../node_modules/a/package.json (relative path: ./lib/lib2/index.js)",
1329+
" no extension",
1330+
" .../node_modules/a/lib/lib2/index.js doesn't exist",
1331+
" .js",
1332+
" .../node_modules/a/lib/lib2/index.js.js doesn't exist",
1333+
" as directory",
1334+
" .../node_modules/a/lib/lib2/index.js doesn't exist",
13291335
" using exports field: ./lib/index.js",
1330-
" existing file: .../node_modules/a/lib/index.js",
1331-
" reporting result .../node_modules/a/lib/index.js"
1336+
" using description file: .../node_modules/a/package.json (relative path: ./lib/index.js)",
1337+
" no extension",
1338+
" existing file: .../node_modules/a/lib/index.js",
1339+
" reporting result .../node_modules/a/lib/index.js"
13321340
]);
13331341
done();
13341342
}

0 commit comments

Comments
 (0)