Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with ESLint Import Resolver #103

Merged
merged 7 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configs/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
},
plugins: ['@babel', 'react', 'prettier', 'jsdoc', 'import', '@typescript-eslint/eslint-plugin'],
settings: {
'import/resolver': eslintResolver('src'),
'import/resolver': eslintResolver(),
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
Expand Down
3 changes: 3 additions & 0 deletions example-site/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "test-project",
"eslintConfig": {
"extends": "./../configs/eslint"
},
"dependencies": {
"prop-types": "^15.8.1"
}
Expand Down
21,465 changes: 4,091 additions & 17,374 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
"bin": {
"build-tools": "./src/cli.js"
},
"jest": {
"testPathIgnorePatterns": [
"node_modules",
".yalc"
]
},
"scripts": {
"test": "jest --verbose",
"test:watch": "jest --watch --verbose"
Expand Down
28 changes: 3 additions & 25 deletions src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const spinner = ora();
const { findAllProjectPaths, validateProject } = require('./../utils/projectpaths');
const { getPackage } = require('./../utils/get-package');
const dirsExist = require('../utils/dirs-exist');
const getProjectConfig = require('../utils/get-project-config');

global.buildCount = 0;

Expand Down Expand Up @@ -114,32 +115,9 @@ exports.handler = async ({
spinner.start('Building webpack configs.\n');

const configMap = validProjects.map((packageObject) => {
/**
* Project config holds all information about a particular project,
* rather than directly pulling out paths from files or attempting
* to build them, use what is here.
*/
const PROJECT_CONFIG = {
name: packageObject.name,
version: packageObject.json.version,
paths: {
project: path.resolve(packageObject.path),
config: path.resolve(`${__dirname}/../../configs`),
src: path.resolve(`${packageObject.path}/src`),
dist: path.resolve(`${packageObject.path}/dist`),
clean: [
`${packageObject.path}/dist/scripts`,
`${packageObject.path}/dist/styles`,
`${packageObject.path}/dist/static`,
],
node_modules: path.resolve(packageObject.path, 'node_modules'),
},
clean: true,
copy: true,
mode,
};
const projectConfig = getProjectConfig(packageObject, mode);

return webpackConfig(PROJECT_CONFIG, mode);
return webpackConfig(projectConfig, mode);
});

let previousHash = '';
Expand Down
6 changes: 3 additions & 3 deletions src/commands/build/plugins/eslint.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const ESLintPlugin = require('eslint-webpack-plugin');
const ESLintConfig = require('../../../../configs/eslint');
const ESLintConfigFunc = require('./../../../utils/esllint-config-func');
/**
* Sets the config for the ESLint webpack plugin.
* @returns ESLintPlugin instance.
*/
module.exports = () =>
module.exports = (projectConfig) =>
new ESLintPlugin({
baseConfig: ESLintConfig,
baseConfig: ESLintConfigFunc(projectConfig),
});
3 changes: 3 additions & 0 deletions src/utils/__tests__/projectpaths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const packageJson = {
};

describe('Project paths', () => {
let originalWrite;
let originalExit;

beforeEach(() => {
jest.resetModules();

Expand Down
21 changes: 21 additions & 0 deletions src/utils/esllint-config-func.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const eslintConfig = require('../../configs/eslint');
const { eslintResolver } = require('./get-alias');

/**
* Allows for the ESLint Config to be used dynamically in code
* with a passed config.
* @param {object} projectConfig The project config to add
* @returns
*/
module.exports = (projectConfig) => {
return {
...eslintConfig,
parserOptions: {
...eslintConfig.parserOptions,
settings: {
...eslintConfig.parserOptions.settings,
'import/resolver': eslintResolver(projectConfig),
}
}
}
};
10 changes: 6 additions & 4 deletions src/utils/get-alias.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const getProjectConfig = require('./get-project-config');

/**
* Get a list of aliases structured for webpack.
Expand All @@ -20,18 +21,19 @@ const webpackAlias = (src) => {
/**
* Get and map aliases to the eslint resolver structure.
*
* @param {string} src Path to the current target src directory
* @param {mixed} paths Paths to the current target directories
* @returns
*/
const eslintResolver = ({ src, project }) => {
const aliases = webpackAlias(src);
const eslintResolver = (paths = null) => {
paths = paths ?? getProjectConfig();
const aliases = webpackAlias(paths.paths.src);
const aliasMap = Object.entries(aliases);
const pathsList = Array.from(new Set(Object.values(aliases))).map((item) => item + '/*');

return {
typescript: {
alwaysTryTypes: true,
project,
project: paths?.paths?.project,
},
alias: {
map: aliasMap,
Expand Down
37 changes: 37 additions & 0 deletions src/utils/get-project-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const path = require('path');

/**
* Project config holds all information about a particular project,
* rather than directly pulling out paths from files or attempting
* to build them, use what is here.
*/
module.exports = (
packageObject = {
name: '',
json: {
version: 'v0.0.0'
},
path: './'
},
mode = 'development'
) => {
return {
name: packageObject?.name ?? '',
version: packageObject?.json?.version ?? 'v0.0.0',
paths: {
project: path.resolve(packageObject.path),
config: path.resolve(`${__dirname}/../../configs`),
src: path.resolve(`${packageObject.path}/src`),
dist: path.resolve(`${packageObject.path}/dist`),
clean: [
path.resolve(`${packageObject.path}/dist/scripts`),
path.resolve(`${packageObject.path}/dist/styles`),
path.resolve(`${packageObject.path}/dist/static`),
],
node_modules: path.resolve(packageObject.path, 'node_modules'),
},
clean: true,
copy: true,
mode,
};
}
ampersarnie marked this conversation as resolved.
Show resolved Hide resolved