Skip to content

Commit

Permalink
webpack resolver handles
Browse files Browse the repository at this point in the history
- jsnext (import-js#103)
- resolve.packageMains (defers to jsnext:main, regardless)
  • Loading branch information
benmosher committed Nov 30, 2015
1 parent dc669d1 commit 39a5a75
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 0 deletions.
31 changes: 31 additions & 0 deletions resolvers/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var findRoot = require('find-root')
, path = require('path')
, resolve = require('resolve')
, get = require('lodash.get')
, find = require('array-find')

var resolveAlias = require('./resolve-alias')

Expand Down Expand Up @@ -60,6 +61,7 @@ exports.resolveImport = function resolveImport(source, file, settings) {
|| ['web_modules', 'node_modules'],

paths: paths,
packageFilter: packageFilter.bind(null, webpackConfig),
})
}

Expand All @@ -85,3 +87,32 @@ function findExternal(source, externals) {
// else, vanilla object
return Object.keys(externals).some(function (e) { return source === e })
}

/**
* webpack defaults: http://webpack.github.io/docs/configuration.html#resolve-packagemains
* @type {Array}
*/
var defaultMains = [
'webpack', 'browser', 'web', 'browserify', ['jam', 'main'], 'main',
]

function packageFilter(config, pkg) {
var altMain

// check for rollup-style first
if (pkg['jsnext:main']) {
pkg['main'] = pkg['jsnext:main']
} else {
// check for configured/default alternative main fields
altMain = find(
get(config, ['resolve', 'packageMains']) || defaultMains,
function (m) { return get(pkg, m) })

if (altMain) {
pkg['main'] = pkg[altMain]
}
}


return pkg
}
1 change: 1 addition & 0 deletions resolvers/webpack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"homepage": "https://github.com/benmosher/eslint-plugin-import#readme",
"dependencies": {
"array-find": "^1.0.0",
"find-root": "^0.1.1",
"lodash.get": "^3.7.0",
"resolve": "^1.1.6"
Expand Down
4 changes: 4 additions & 0 deletions resolvers/webpack/test/package-mains/jsnext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "lib/index.js",
"jsnext:main": "src/index.js"
}
Empty file.
1 change: 1 addition & 0 deletions resolvers/webpack/test/package-mains/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "dummy": true }
3 changes: 3 additions & 0 deletions resolvers/webpack/test/package-mains/webpack.alt.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.resolve = {
packageMains: ["main"], // override
}
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions resolvers/webpack/test/package-mains/webpack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"webpack": "webpack.js",
"main": "index.js"
}
Empty file.
32 changes: 32 additions & 0 deletions resolvers/webpack/test/packageMains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var chai = require('chai')
, expect = chai.expect
, path = require('path')

var webpack = require('../')

var file = path.join(__dirname, 'package-mains', 'dummy.js')


describe("packageMains", function () {

it("captures jsnext", function () {
expect(webpack.resolveImport('./jsnext', file))
.to.equal(path.join(__dirname, 'package-mains', 'jsnext', 'src', 'index.js'))
})

it("captures webpack", function () {
expect(webpack.resolveImport('./webpack', file))
.to.equal(path.join(__dirname, 'package-mains', 'webpack', 'webpack.js'))
})

it("uses configured packageMains, if provided", function () {
expect(webpack.resolveImport('./webpack', file, { config: 'webpack.alt.config.js' }))
.to.equal(path.join(__dirname, 'package-mains', 'webpack', 'index.js'))
})

it("always defers to jsnext:main, regardless of config", function () {
expect(webpack.resolveImport('./jsnext', file, { config: 'webpack.alt.config.js' }))
.to.equal(path.join(__dirname, 'package-mains', 'jsnext', 'src', 'index.js'))
})

})

0 comments on commit 39a5a75

Please sign in to comment.