Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
fix(babel): Support latest babel 7 only (#78)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This resolver now requires babel 7
  • Loading branch information
danez authored and tleunen committed Dec 11, 2017
1 parent 4ed3438 commit f2804c5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 65 deletions.
4 changes: 2 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"presets": [
["env", {
["@babel/env", {
"targets": {
"node": 6
},
Expand All @@ -9,6 +9,6 @@
}]
],
"plugins": [
["transform-object-rest-spread", { "useBuiltIns": true }]
["@babel/proposal-object-rest-spread", { "useBuiltIns": true }]
]
}
20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
"resolve": "^1.4.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"@babel/cli": "^7.0.0-beta.34",
"@babel/core": "^7.0.0-beta.34",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0-beta.34",
"@babel/preset-env": "^7.0.0-beta.34",
"babel-core": "^7.0.0-0",
"babel-jest": "^21.2.0",
"babel-plugin-module-resolver": "^3.0.0-beta.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-preset-env": "^2.0.0-beta.2",
"babel-plugin-module-resolver": "^3.0.0",
"eslint": "^4.8.0",
"eslint-config-airbnb-base": "^12.0.2",
"eslint-plugin-import": "^2.7.0",
Expand All @@ -50,17 +51,14 @@
"standard-version": "^4.2.0"
},
"peerDependencies": {
"babel-core": "^6.0.0",
"babel-plugin-module-resolver": "^3.0.0-beta"
"@babel/core": "^7.0.0-0",
"babel-plugin-module-resolver": "^3.0.0"
},
"scripts": {
"lint": "eslint src test",
"compile": "babel src --out-dir lib",
"babel:clean": "rimraf node_modules/babel-cli node_modules/babel-core node_modules/babel-plugin-transform-object-rest-spread",
"babel:6": "npm run babel:clean && npm i babel-cli@^6 babel-core@^6 babel-plugin-transform-object-rest-spread@^6",
"test:babel:6": "npm run babel:6 && npm run test:suite",
"pretest": "npm run lint",
"test": "npm run test:babel:6",
"test": "npm run test:suite --coverage",
"test:suite": "jest",
"test:watch": "jest --watch",
"prepare": "npm run compile",
Expand Down
40 changes: 23 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const resolve = require('resolve');
const pkgUp = require('pkg-up');
const { resolvePath } = require('babel-plugin-module-resolver');
const { OptionManager } = require('babel-core');
const { OptionManager } = require('@babel/core');

function getPlugins(file) {
try {
Expand All @@ -12,10 +12,7 @@ function getPlugins(file) {
filename: file,
});

return result.plugins.filter((plugin) => {
const plug = OptionManager.memoisedPlugins.find(item => item.plugin === plugin[0]);
return plug && plug.plugin && plug.plugin.key === 'module-resolver';
});
return result.plugins.filter(plugin => plugin.key === 'module-resolver');
} catch (err) {
// This error should only occur if something goes wrong with babel's
// internals. Dump it to console so people know what's going on,
Expand All @@ -26,6 +23,20 @@ function getPlugins(file) {
}
}

function getPluginOptions(file, defaultOptions) {
const instances = getPlugins(file);

return instances.reduce(
(config, plugin) => ({
cwd: plugin.options.cwd || config.cwd,
root: config.root.concat(plugin.options.root || []),
alias: Object.assign(config.alias, plugin.options.alias || {}),
extensions: plugin.options.extensions || config.extensions,
}),
defaultOptions,
);
}

function stripWebpack(src) {
let source = src;

Expand All @@ -46,6 +57,8 @@ function stripWebpack(src) {

exports.interfaceVersion = 2;

const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];

/**
* Find the full path to 'source', given 'file' as a full reference path.
*
Expand All @@ -62,15 +75,8 @@ exports.resolve = (source, file, opts) => {
const projectRootDir = path.dirname(pkgUp.sync(file));

try {
const instances = getPlugins(file);

const pluginOpts = instances.reduce(
(config, plugin) => ({
cwd: plugin[1] && plugin[1].cwd ? plugin[1].cwd : config.cwd,
root: config.root.concat(plugin[1] && plugin[1].root ? plugin[1].root : []),
alias: Object.assign(config.alias, plugin[1] ? plugin[1].alias : {}),
extensions: plugin[1] && plugin[1].extensions ? plugin[1].extensions : config.extensions,
}),
const pluginOptions = getPluginOptions(
file,
{
// if .babelrc doesn't exist, try to get the configuration information from `options`,
// which gets defined by the eslint configuration file.
Expand All @@ -84,14 +90,14 @@ exports.resolve = (source, file, opts) => {
cwd: options.cwd || projectRootDir,
root: options.root || [],
alias: options.alias || {},
extensions: options.extensions || ['.js', '.jsx', '.es', '.es6', '.mjs'],
extensions: options.extensions || defaultExtensions,
},
);

const finalSource = stripWebpack(source);
const src = resolvePath(finalSource, file, pluginOpts);
const src = resolvePath(finalSource, file, pluginOptions);

const extensions = options.extensions || pluginOpts.extensions;
const extensions = options.extensions || pluginOptions.extensions;

return {
found: true,
Expand Down
4 changes: 2 additions & 2 deletions test/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"root": ["./test/examples/**"],
"alias": {
"components": "./test/examples/components",
"old-bcore": "babel-core"
"babel-plugin": "babel-plugin-module-resolver"
},
"extensions": [".js", ".customExt"]
}]
],
"env": {
"test": {
"testenv": {
"plugins": [
["babel-plugin-module-resolver", {
"alias": {
Expand Down
37 changes: 4 additions & 33 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
'use strict';

const path = require('path');
const { OptionManager } = require('babel-core');
const resolverPlugin = require('../src/index');

const opts = {};
Expand All @@ -17,10 +16,10 @@ describe('eslint-import-resolver-module-resolver', () => {
});

it('should return `true` when mapped to a npm module', () => {
expect(resolverPlugin.resolve('old-bcore', path.resolve('./test/examples/file1'), opts))
expect(resolverPlugin.resolve('babel-plugin', path.resolve('./test/examples/file1'), opts))
.toEqual({
found: true,
path: path.resolve(__dirname, '../node_modules/babel-core/index.js'),
path: path.resolve(__dirname, '../node_modules/babel-plugin-module-resolver/lib/index.js'),
});
});

Expand Down Expand Up @@ -141,7 +140,7 @@ describe('eslint-import-resolver-module-resolver', () => {

it('should return `true` when the mapping exists in the "env"', () => {
const oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';
process.env.NODE_ENV = 'testenv';

expect(resolverPlugin.resolve('subsub/c2', path.resolve('./test/examples/components/c1'), opts))
.toEqual({
Expand All @@ -166,7 +165,7 @@ describe('eslint-import-resolver-module-resolver', () => {

it('should return `true` when the mapping exists in the "env"', () => {
const oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'test';
process.env.NODE_ENV = 'testenv';

expect(resolverPlugin.resolve('subsub/c2', path.resolve('./test/examples/components/sub/envonly/yo'), opts))
.toEqual({
Expand All @@ -178,32 +177,4 @@ describe('eslint-import-resolver-module-resolver', () => {
});
});
});

describe('babel internals', () => {
let oldInit;
let error;

beforeEach(() => {
oldInit = OptionManager.prototype.init;
// eslint-disable-next-line prefer-destructuring
error = console.error;
});
afterEach(() => {
OptionManager.prototype.init = oldInit;
console.error = error;
});

it('should survive babel blowing up', () => {
console.error = jest.fn();

OptionManager.prototype.init = undefined;

expect(resolverPlugin.resolve('underscore', path.resolve('./test/examples/file1'), opts))
.toEqual({
found: false,
});

expect(console.error).toBeCalled();
});
});
});

0 comments on commit f2804c5

Please sign in to comment.