diff --git a/.gitignore b/.gitignore index 6df16bc..51a93a4 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ npm-debug.log* yarn-debug.log* yarn-error.log* + +tsconfig.dev.json diff --git a/jest.config.js b/jest.config.js index 358e9f8..b81f848 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,3 +1,5 @@ +const aliases = require('./scripts/create-symlinks/config.json'); + // For a detailed explanation regarding each configuration property, visit: // 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 - moduleNameMapper: { - '^_api(.*)$': '/src/core/api$1', - '^_blocks(.*)$': '/src/core/blocks$1', - '^_consts(.*)$': '/src/core/consts$1', - '^_hooks(.*)$': '/src/core/hooks$1', - '^_hoks(.*)$': '/src/core/hoks$1', - '^_services(.*)$': '/src/core/services$1', - '^_types(.*)$': '/src/core/types$1', - '^_utils(.*)$': '/src/core/utils$1', - '^_enums(.*)$': '/src/core/enums$1', - '^_referers(.*)$': '/src/core/referers$1', - '^_pages(.*)$': '/src/pages$1', - }, + moduleNameMapper: Object.entries(aliases).reduce((acc, [key, aliasPath]) => ({ + ...acc, + [`^${key}(.*)$`]: `/${aliasPath}$1`, + }), {}), // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader // modulePathIgnorePatterns: [], diff --git a/package.json b/package.json index 5e6c9e5..629f8d0 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,9 @@ "typescript": "^4.0.3" }, "scripts": { + "symlinks": "node scripts/create-symlinks/index", "start": "webpack serve", - "init": "npm run clean && npm i", + "init": "npm run clean && npm i && npm run symlinks", "dev": "webpack serve --open false", "build": "webpack --mode=production", "eslint": "eslint -c .eslintrc.json src --fix", diff --git a/scripts/create-symlinks/config.json b/scripts/create-symlinks/config.json new file mode 100644 index 0000000..0c3d101 --- /dev/null +++ b/scripts/create-symlinks/config.json @@ -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" +} diff --git a/scripts/create-symlinks/index.js b/scripts/create-symlinks/index.js new file mode 100644 index 0000000..970be0f --- /dev/null +++ b/scripts/create-symlinks/index.js @@ -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(); diff --git a/scripts/find-unused-code/index.js b/scripts/find-unused-code/index.js index 84025da..3c1b2bd 100644 --- a/scripts/find-unused-code/index.js +++ b/scripts/find-unused-code/index.js @@ -1,10 +1,9 @@ -const configurator = require("ts-prune/lib/configurator"); const runner = require("ts-prune/lib/runner"); const {ignore} = require('./ignore-files'); const error = []; -runner.run(configurator.getConfig(), (text) => { +runner.run({ project: 'tsconfig.dev.json' }, (text) => { if (ignore.every(ign => !text.includes(ign))) { error.push(text); } diff --git a/src/core/api/usersTestApi.ts b/src/core/api/usersTestApi.ts index 0700e9d..0850523 100644 --- a/src/core/api/usersTestApi.ts +++ b/src/core/api/usersTestApi.ts @@ -1,5 +1,5 @@ import {makeApi} from '_utils/makeApi'; -import {http} from '../infrastructure/Http'; +import {http} from '_infrastructure/Http'; type User = { id: number; diff --git a/tsconfig.json b/tsconfig.json index b01425c..edb775d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,21 +20,8 @@ "noEmit": false, "jsx": "react", "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": [ - "src/**/*.ts", - "src/**/*.tsx" - ] + "include": ["src/**/*.ts", "src/**/*.tsx"] } diff --git a/webpack.config.js b/webpack.config.js index 66d2e06..44eb4cb 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -3,6 +3,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin'); const {CleanWebpackPlugin} = require('clean-webpack-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const webpack = require('webpack'); +const aliases = require('./scripts/create-symlinks/config.json'); module.exports = { mode: 'development', @@ -28,19 +29,10 @@ module.exports = { }, resolve: { extensions: ['.tsx', '.ts', '.js'], - alias: { - _types: path.resolve(__dirname, 'src/core/types/'), - _api: path.resolve(__dirname, 'src/core/api/'), - _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/'), - } + alias: Object.entries(aliases).reduce((acc, [key, aliasPath]) => ({ + ...acc, + [key]: path.resolve(__dirname, `${aliasPath}/`), + }), {}), }, optimization: { splitChunks: {