Skip to content

Commit

Permalink
conf: direct file imports (globally unique file names) (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 29, 2020
1 parent 1c8b93b commit a10ae8a
Show file tree
Hide file tree
Showing 226 changed files with 1,151 additions and 654 deletions.
21 changes: 20 additions & 1 deletion .eslintrc.js
@@ -1,4 +1,9 @@
const skipWords = require('./config/eslint/spellCheckerSkip');
const jsconfig = require('./jsconfig.json');

const paths = jsconfig.compilerOptions.paths;

const aliases = Object.keys(paths);

module.exports = {
env: {
Expand All @@ -20,6 +25,15 @@ module.exports = {
ENV_DEVELOPMENT: true,
},
ignorePatterns: ['playground'],
overrides: [
{
files: ['./packages/*/src/**/*.js'],
excludedFiles: '*.test.js',
rules: {
'import/no-relative-parent-imports': 2,
},
},
],
parser: 'babel-eslint',
parserOptions: {
babelOptions: {
Expand All @@ -34,15 +48,21 @@ module.exports = {
plugins: ['jest', 'spellcheck'],

rules: {
'import/extensions': [2, 'never'],
'import/first': 2,
'import/newline-after-import': 2,
'import/no-self-import': 2,
'import/no-unresolved': [2, { ignore: aliases }],
'import/no-useless-path-segments': 2,

'import/order': [
'error',
{
alphabetize: {
order: 'asc',
},
'newlines-between': 'always',
pathGroups: [{ pattern: `*(${aliases.join('|')})`, group: 'internal' }],
pathGroupsExcludedImportTypes: ['builtin'],
},
],
Expand All @@ -65,7 +85,6 @@ module.exports = {
'no-var': 2,
'no-warning-comments': 2,
'object-shorthand': [2, 'always', { avoidQuotes: true }],
'prefer-arrow-callback': 2,
'prefer-const': 2,
'sort-keys': [
1,
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -11,4 +11,7 @@ packages/*/esm
packages/*/*.js
packages/*/*.js.map
packages/*/*.d.ts
packages/*/*.mjs
packages/*/*.mjs

# Unignore direct js files
!jest.config.js
40 changes: 29 additions & 11 deletions CONTRIBUTING.md
Expand Up @@ -19,6 +19,23 @@ When making changes to packages other than vest, you should also add the name of

`fix: [eslint-plugin-vest] fix edge case bug.`

## Absolute import and `src` file naming

All the files under `src` are unique, and their imports are aliased for absolute imports so you can import the a module using only its filename:

```js
import state from 'state';
```
Instead of
```js
import state from '../../state';
```

### IDE support

Most modern IDEs will support those aliases using the jsconfig.json file in the project root. If you move files around, or create new files during local development, you can run `yarn dev` to watch your changes and automatically generate the jsconfig file.


## Repository structure

Vest’s structure is fairly simple. To help you familiarize with the project, see this mapping:
Expand All @@ -30,17 +47,17 @@ packages/
├── vest/ # Main Vest package.
│ ├── src/ # Vest's prebuilt source code.
│ │ └── core/ # Modules required for vest's functionality.
│ │ │ └── context/ # Vest's shared runtime. Used across the whole library.
│ │ │ └── produce/ # Generates the out put object and callbaks.
│ │ │ └── state/ # Vest's persistent state. Used across the whole library.
│ │ │ └── test/ # Contains the test function and its lifecycle.
│ │ │ └── suite/ # Contains all the suite modules and methods
│ │ │ └── create/ # Initializes the suite and creates a context and state.
│ │ └── hooks/ # Functions that extend vest's functionality. They all use context.
│ │ │ └── draft/ # Allows access to the intermediate test result.
│ │ │ └── exclusive/ # Allows including or excluding fields in runtime.
│ │ │ └── warn/ # Allows setting warn-only fields.
│ │ │ └── group/ # Adds another nesting level within suites.
│ │ │ └── ctx # Vest's shared runtime. Used across the whole library.
│ │ │ └── produce/ # Generates the out put object and callbaks.
│ │ │ └── state # Vest's persistent state. Used across the whole library.
│ │ │ └── test/ # Contains the test function and its lifecycle.
│ │ │ └── suite/ # Contains all the suite modules and methods
│ │ │ └── createSuite # Initializes the suite and creates a context and state.
│ │ └── hooks/ # Functions that extend vest's functionality. They all use context.
│ │ │ └── draft # Allows access to the intermediate test result.
│ │ │ └── exclusive # Allows including or excluding fields in runtime.
│ │ │ └── warn # Allows setting warn-only fields.
│ │ │ └── group # Adds another nesting level within suites.
│ │ └── lib/ # Shared helper functions.
│ │ └── testUtils/ # Test helper functions.
│ │ └── typings/ # Contains typescript declaration files for the exported modules.
Expand All @@ -52,6 +69,7 @@ packages/
├── eslint-plugin-vest/ # Eslint plugin with vest specific rules
│ ├── lib/ # Contains all rules
├── n4s/ # Assertion library used by vest
├── __shared/ # Code used by multiple packages
```

## Branching strategy
Expand Down
4 changes: 3 additions & 1 deletion config/babel/babel.config.js
@@ -1,3 +1,5 @@
const { DIR_NAME_PACKAGES } = require('../../util/filePaths');

const env = api => {
const conf = {
targets: {},
Expand Down Expand Up @@ -31,7 +33,7 @@ module.exports = api => {
].filter(Boolean);

return {
include: [/src/, /testUtils/, /node_modules/],
include: [new RegExp(DIR_NAME_PACKAGES), /testUtils/, /node_modules/],
presets,
plugins,
};
Expand Down
@@ -1,4 +1,4 @@
const isDeepCopy = (source, clone) => {
module.exports = (source, clone) => {
const queue = [[source, clone]];

outer: while (queue.length) {
Expand Down Expand Up @@ -64,38 +64,3 @@ const isDeepCopy = (source, clone) => {

return { pass: true };
};

export const SAMPLE_DEEP_OBJECT = [
{
_id: '5eb4784ee29b8b3003688425',
index: 0,
name: {
first: 'Velasquez',
last: 'Lara',
},
range: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
tags: ['Lorem', 'ullamco', 'minim', 'ut', 'ad'],
},
{
_id: '5eb4784e64618155ef167791',
index: 1,
name: {
first: 'Mcconnell',
last: 'Dennis',
},
range: [
{
a: 1,
b: [
{
a: 2,
c: ['one', 'two', 'three', 'four'],
},
],
},
],
tags: ['nulla', 'ex', 'et', 'sint', 'aliqua'],
},
];

export default isDeepCopy;
@@ -1,5 +1,6 @@
import _ from 'lodash';
import isDeepCopy, { SAMPLE_DEEP_OBJECT } from '.';

import isDeepCopy from '.';

describe('Sanity (testing isDeepCopy)', () => {
it('Should fail when same value', () => {
Expand Down Expand Up @@ -31,3 +32,36 @@ describe('Sanity (testing isDeepCopy)', () => {
).toBe(true);
});
});

const SAMPLE_DEEP_OBJECT = [
{
_id: '5eb4784ee29b8b3003688425',
index: 0,
name: {
first: 'Velasquez',
last: 'Lara',
},
range: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
tags: ['Lorem', 'ullamco', 'minim', 'ut', 'ad'],
},
{
_id: '5eb4784e64618155ef167791',
index: 1,
name: {
first: 'Mcconnell',
last: 'Dennis',
},
range: [
{
a: 1,
b: [
{
a: 2,
c: ['one', 'two', 'three', 'four'],
},
],
},
],
tags: ['nulla', 'ex', 'et', 'sint', 'aliqua'],
},
];
16 changes: 12 additions & 4 deletions config/jest/jest.config.js
@@ -1,15 +1,23 @@
const path = require('path');
const { BABEL_CONFIG_PATH, CONFIG_PATH } = require('..');

const { moduleAliases, filePaths } = require('../../util');

const moduleNameMapper = moduleAliases.reduce(
(aliases, { name, absolute }) =>
Object.assign(aliases, { [`^${name}$`]: absolute }),
{}
);

module.exports = (options = {}) => ({
clearMocks: true,
moduleNameMapper,
rootDir: '.',
roots: ['<rootDir>'],
setupFilesAfterEnv: [path.join(CONFIG_PATH, 'jest/jest.setup.js')],
setupFilesAfterEnv: [path.join(filePaths.CONFIG_PATH, 'jest/jest.setup.js')],
testEnvironment: 'node',
testMatch: ['**/*/(spec|test).js', '**/*.(spec|test).js'],
testMatch: ['**/*.(spec|test).js'],
transform: {
'\\.js$': ['babel-jest', { configFile: BABEL_CONFIG_PATH }],
'^.+\\.js$': ['babel-jest', { configFile: filePaths.BABEL_CONFIG_PATH }],
},
...options,
});
3 changes: 2 additions & 1 deletion config/jest/jest.setup.js
@@ -1,8 +1,9 @@
const glob = require('glob');

const { default: isDeepCopy } = require('../../shared/testUtils/isDeepCopy');
const { packagePath } = require('../../util');

const isDeepCopy = require('./isDeepCopy');

global.isWatchMode = (process.argv || []).some(
arg => arg && arg.includes('--watch')
);
Expand Down
1 change: 1 addition & 0 deletions config/rollup/addEsmDir.js
@@ -1,4 +1,5 @@
const path = require('path');

const fs = require('fs-extra');

const DIR_ESM = 'esm';
Expand Down
1 change: 1 addition & 0 deletions config/rollup/genConfig.js
@@ -1,6 +1,7 @@
const path = require('path');

const { packageJson, packagePath } = require('../../util');

const getPlugins = require('./getPlugins');
const nameByFormat = require('./nameByFormat');

Expand Down
13 changes: 11 additions & 2 deletions config/rollup/getPlugins.js
@@ -1,12 +1,20 @@
const compiler = require('@ampproject/rollup-plugin-closure-compiler');
const alias = require('@rollup/plugin-alias');
const { default: babel } = require('@rollup/plugin-babel');
const commonjs = require('@rollup/plugin-commonjs');
const { default: resolve } = require('@rollup/plugin-node-resolve');
const replace = require('@rollup/plugin-replace');
const { terser } = require('rollup-plugin-terser');

const { moduleAliases, filePaths } = require('../../util');

// This is not the real path. Find a way to fix it.
const { BABEL_CONFIG_PATH } = require('../../config');

const aliases = moduleAliases.reduce(
(aliases, { name: find, absolute: replacement }) =>
aliases.concat({ find, replacement }),
[]
);

const NAME_ES5 = 'es5';
const FORMAT_UMD = 'umd';
Expand All @@ -23,12 +31,13 @@ module.exports = function ({ dev = false, format, min, libraryName, version }) {
const envName = babelEnv();

const PLUGINS = [
alias({ entries: aliases }),
resolve(),
commonjs({
include: /node_modules\/(anyone|n4s)/,
}),
babel({
configFile: BABEL_CONFIG_PATH,
configFile: filePaths.BABEL_CONFIG_PATH,
babelHelpers: 'bundled',
envName,
}),
Expand Down

0 comments on commit a10ae8a

Please sign in to comment.