Добавление симлинков для импортов (#56)

This commit is contained in:
Nikolay
2021-01-06 11:54:40 +03:00
committed by GitHub
parent 8b1c830f56
commit 0f5175ab79
9 changed files with 83 additions and 45 deletions

2
.gitignore vendored
View File

@ -23,3 +23,5 @@
npm-debug.log* npm-debug.log*
yarn-debug.log* yarn-debug.log*
yarn-error.log* yarn-error.log*
tsconfig.dev.json

View File

@ -1,3 +1,5 @@
const aliases = require('./scripts/create-symlinks/config.json');
// For a detailed explanation regarding each configuration property, visit: // For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html // https://jestjs.io/docs/en/configuration.html
@ -79,19 +81,10 @@ module.exports = {
// ], // ],
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
moduleNameMapper: { moduleNameMapper: Object.entries(aliases).reduce((acc, [key, aliasPath]) => ({
'^_api(.*)$': '<rootDir>/src/core/api$1', ...acc,
'^_blocks(.*)$': '<rootDir>/src/core/blocks$1', [`^${key}(.*)$`]: `<rootDir>/${aliasPath}$1`,
'^_consts(.*)$': '<rootDir>/src/core/consts$1', }), {}),
'^_hooks(.*)$': '<rootDir>/src/core/hooks$1',
'^_hoks(.*)$': '<rootDir>/src/core/hoks$1',
'^_services(.*)$': '<rootDir>/src/core/services$1',
'^_types(.*)$': '<rootDir>/src/core/types$1',
'^_utils(.*)$': '<rootDir>/src/core/utils$1',
'^_enums(.*)$': '<rootDir>/src/core/enums$1',
'^_referers(.*)$': '<rootDir>/src/core/referers$1',
'^_pages(.*)$': '<rootDir>/src/pages$1',
},
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [], // modulePathIgnorePatterns: [],

View File

@ -22,8 +22,9 @@
"typescript": "^4.0.3" "typescript": "^4.0.3"
}, },
"scripts": { "scripts": {
"symlinks": "node scripts/create-symlinks/index",
"start": "webpack serve", "start": "webpack serve",
"init": "npm run clean && npm i", "init": "npm run clean && npm i && npm run symlinks",
"dev": "webpack serve --open false", "dev": "webpack serve --open false",
"build": "webpack --mode=production", "build": "webpack --mode=production",
"eslint": "eslint -c .eslintrc.json src --fix", "eslint": "eslint -c .eslintrc.json src --fix",

View File

@ -0,0 +1,14 @@
{
"_api": "src/core/api",
"_blocks": "src/core/blocks",
"_consts": "src/core/consts",
"_hooks": "src/core/hooks",
"_hoks": "src/core/hoks",
"_services": "src/core/services",
"_types": "src/core/types",
"_utils": "src/core/utils",
"_infrastructure": "src/core/infrastructure",
"_enums": "src/core/enums",
"_referers": "src/core/referers",
"_pages": "src/pages"
}

View File

@ -0,0 +1,50 @@
const fs = require('fs');
const path = require('path');
const aliases = require('./config.json');
const tsconfig = require('../../tsconfig.json');
const CURRENT_FOLDER = process.cwd();
function createTsConfigDev() {
const tsConfigDev = {
...tsconfig,
compilerOptions: {
...tsconfig.compilerOptions,
paths: Object.entries(aliases).reduce((acc, [key, value]) => ({
...acc,
[`${key}/*`]: [`./${value}/*`],
}), {}),
}
};
fs.writeFileSync(path.resolve('tsconfig.dev.json'), JSON.stringify(tsConfigDev, null, 4));
}
function createSymlinks() {
if (!fs.existsSync('./node_modules')) {
fs.mkdirSync('node_modules');
}
try {
for (const alias in aliases) {
const folder = aliases[alias];
const pathInPackage = path.resolve(CURRENT_FOLDER, folder);
if (!fs.existsSync(folder)) {
continue;
}
const symlinkPath = path.resolve(`./node_modules/${alias}`);
if (fs.existsSync(symlinkPath)) {
continue;
}
fs.symlinkSync(pathInPackage, symlinkPath);
console.info(`${symlinkPath} --> ${pathInPackage}`);
}
} catch (e) {
console.info(`${e}`);
}
}
createSymlinks();
createTsConfigDev();

View File

@ -1,10 +1,9 @@
const configurator = require("ts-prune/lib/configurator");
const runner = require("ts-prune/lib/runner"); const runner = require("ts-prune/lib/runner");
const {ignore} = require('./ignore-files'); const {ignore} = require('./ignore-files');
const error = []; const error = [];
runner.run(configurator.getConfig(), (text) => { runner.run({ project: 'tsconfig.dev.json' }, (text) => {
if (ignore.every(ign => !text.includes(ign))) { if (ignore.every(ign => !text.includes(ign))) {
error.push(text); error.push(text);
} }

View File

@ -1,5 +1,5 @@
import {makeApi} from '_utils/makeApi'; import {makeApi} from '_utils/makeApi';
import {http} from '../infrastructure/Http'; import {http} from '_infrastructure/Http';
type User = { type User = {
id: number; id: number;

View File

@ -20,21 +20,8 @@
"noEmit": false, "noEmit": false,
"jsx": "react", "jsx": "react",
"paths": { "paths": {
"_api/*": ["./src/core/api/*"], "*": ["*"]
"_blocks/*": ["./src/core/blocks/*"],
"_consts/*": ["./src/core/consts/*"],
"_hooks/*": ["./src/core/hooks/*"],
"_hoks/*": ["./src/core/hoks/*"],
"_services/*": ["./src/core/services/*"],
"_types/*": ["./src/core/types/*"],
"_utils/*": ["./src/core/utils/*"],
"_enums/*": ["./src/core/enums/*"],
"_referers/*": ["./src/core/referers/*"],
"_pages/*": ["./src/pages/*"],
} }
}, },
"include": [ "include": ["src/**/*.ts", "src/**/*.tsx"]
"src/**/*.ts",
"src/**/*.tsx"
]
} }

View File

@ -3,6 +3,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin'); const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const webpack = require('webpack'); const webpack = require('webpack');
const aliases = require('./scripts/create-symlinks/config.json');
module.exports = { module.exports = {
mode: 'development', mode: 'development',
@ -28,19 +29,10 @@ module.exports = {
}, },
resolve: { resolve: {
extensions: ['.tsx', '.ts', '.js'], extensions: ['.tsx', '.ts', '.js'],
alias: { alias: Object.entries(aliases).reduce((acc, [key, aliasPath]) => ({
_types: path.resolve(__dirname, 'src/core/types/'), ...acc,
_api: path.resolve(__dirname, 'src/core/api/'), [key]: path.resolve(__dirname, `${aliasPath}/`),
_blocks: path.resolve(__dirname, 'src/core/blocks/'), }), {}),
_consts: path.resolve(__dirname, 'src/core/consts/'),
_hooks: path.resolve(__dirname, 'src/core/hooks/'),
_hoks: path.resolve(__dirname, 'src/core/hoks/'),
_services: path.resolve(__dirname, 'src/core/services/'),
_utils: path.resolve(__dirname, 'src/core/utils/'),
_enums: path.resolve(__dirname, 'src/core/enums/'),
_referers: path.resolve(__dirname, 'src/core/referers/'),
_pages: path.resolve(__dirname, 'src/pages/'),
}
}, },
optimization: { optimization: {
splitChunks: { splitChunks: {