Skip to content

Instantly share code, notes, and snippets.

@nerdyman
Last active June 19, 2025 19:38
Show Gist options
  • Select an option

  • Save nerdyman/2f97b24ab826623bff9202750013f99e to your computer and use it in GitHub Desktop.

Select an option

Save nerdyman/2f97b24ab826623bff9202750013f99e to your computer and use it in GitHub Desktop.

Revisions

  1. nerdyman revised this gist Oct 4, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve-tsconfig-path-to-webpack-alias.js
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ function resolveTsconfigPathsToAlias({

    Object.keys(paths).forEach((item) => {
    const key = item.replace('/*', '');
    const value = resolve(webpackConfigBasePath, paths[item][0].replace('/*', ''));
    const value = resolve(webpackConfigBasePath, paths[item][0].replace('/*', '').replace('*', ''));

    aliases[key] = value;
    });
  2. nerdyman revised this gist Oct 3, 2019. 2 changed files with 4 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions resolve-tsconfig-path-to-webpack-alias.js
    Original file line number Diff line number Diff line change
    @@ -3,13 +3,13 @@ const { resolve } = require('path');
    /**
    * Resolve tsconfig.json paths to Webpack aliases
    * @param {string} tsconfigPath - Path to tsconfig
    * @param {string} webpackConfigBasePath - Relative base path from tsconfig to Webpack config to create absolute aliases
    * @param {string} webpackConfigBasePath - Path from tsconfig to Webpack config to create absolute aliases
    * @return {object} - Webpack alias config
    */
    function resolveTsconfigPathsToAlias({
    tsconfigPath = './tsconfig.json',
    webpackConfigBasePath = './'} = {},
    ) {
    webpackConfigBasePath = __dirname,
    } = {}) {
    const { paths } = require(tsconfigPath).compilerOptions;

    const aliases = {};
    1 change: 1 addition & 0 deletions tsconfig.json
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,7 @@
    "es2017.object"
    ],
    "paths": {
    "Root/*": ["src/*"],
    "Components/*": ["src/components/*"],
    "Data/*": ["src/data/*"],
    }
  3. nerdyman revised this gist Sep 7, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve-tsconfig-path-to-webpack-alias.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ const { resolve } = require('path');
    * Resolve tsconfig.json paths to Webpack aliases
    * @param {string} tsconfigPath - Path to tsconfig
    * @param {string} webpackConfigBasePath - Relative base path from tsconfig to Webpack config to create absolute aliases
    * @return {object}
    * @return {object} - Webpack alias config
    */
    function resolveTsconfigPathsToAlias({
    tsconfigPath = './tsconfig.json',
  4. nerdyman revised this gist Sep 7, 2018. 3 changed files with 18 additions and 11 deletions.
    17 changes: 10 additions & 7 deletions resolve-tsconfig-path-to-webpack-alias.js
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,27 @@
    const { resolve } = require('path');

    /**
    * resolveTsConfigPaths - resolve tsconfig paths to webpack aliases
    * Resolve tsconfig.json paths to Webpack aliases
    * @param {string} tsconfigPath - Path to tsconfig
    * @param {string} webpackConfigBasePath - Relative base path from tsconfig to Webpack config to create absolute aliases
    * @return {object}
    */
    function resolveTsconfigPathsToAlias() {
    // @NOTE you may need to change this path depending on your project set up
    const { paths } = require('../tsconfig.json').compilerOptions;
    function resolveTsconfigPathsToAlias({
    tsconfigPath = './tsconfig.json',
    webpackConfigBasePath = './'} = {},
    ) {
    const { paths } = require(tsconfigPath).compilerOptions;

    const aliases = {};

    Object.keys(paths).forEach((item) => {
    const key = item.replace('/*', '');
    // @NOTE you might need to change './' depending on your project set up
    const value = resolve('./', paths[item][0].replace('/*', ''));
    const value = resolve(webpackConfigBasePath, paths[item][0].replace('/*', ''));

    aliases[key] = value;
    });

    return aliases;
    }

    module.exports = resolveTsconfigPathsToAlias;
    module.exports = resolveTsconfigPathsToAlias;
    1 change: 0 additions & 1 deletion Tsconfig paths to webpack alias → tsconfig.json
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@
    {
    ...
    "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    11 changes: 8 additions & 3 deletions webpack.config.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,14 @@
    // add paths to webpack

    const resolveTsconfigPathsToAlias = require('./resolve-tsconfig-path-to-webpack-alias');

    module.exports = {
    ...
    // ...
    resolve: {
    ...
    alias: resolveTsconfigPathsToAlias(),
    // ...
    alias: resolveTsconfigPathsToAlias({
    tsconfigPath: '../tsconfig.json', // Using custom path
    webpackConfigBasePath: '../', // Using custom path
    }),
    }
    }
  5. nerdyman revised this gist Apr 25, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion resolve-tsconfig-path-to-webpack-alias.js
    Original file line number Diff line number Diff line change
    @@ -19,4 +19,6 @@ function resolveTsconfigPathsToAlias() {
    });

    return aliases;
    }
    }

    module.exports = resolveTsconfigPathsToAlias;
  6. nerdyman renamed this gist Apr 25, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. nerdyman revised this gist Apr 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion resolve-tsconfig-path-to-webpack-alias.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import { resolve } from 'path';
    const { resolve } = require('path');

    /**
    * resolveTsConfigPaths - resolve tsconfig paths to webpack aliases
  8. nerdyman revised this gist Apr 25, 2017. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions webpack.config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    // add paths to webpack

    module.exports = {
    ...
    resolve: {
    ...
    alias: resolveTsconfigPathsToAlias(),
    }
    }
  9. nerdyman renamed this gist Apr 25, 2017. 1 changed file with 0 additions and 0 deletions.
  10. nerdyman renamed this gist Apr 25, 2017. 1 changed file with 5 additions and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,19 @@
    import { resolve } from 'path';

    /**
    * resolveTsConfigPaths - resolve tsconfig paths to webpack aliases
    * @return {object}
    */
    function resolveTsconfigPathsToAlias() {
    // @NOTE you may need to change this path depending on your project set up
    const { paths } = require('../tsconfig.json').compilerOptions;

    const aliases = {};

    Object.keys(paths).forEach((item) => {
    const key = item.replace('/*', '');
    const value = path.resolve('./', paths[item][0].replace('/*', ''));
    // @NOTE you might need to change './' depending on your project set up
    const value = resolve('./', paths[item][0].replace('/*', ''));

    aliases[key] = value;
    });
  11. nerdyman created this gist Apr 25, 2017.
    24 changes: 24 additions & 0 deletions Example ts config
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    {
    ...
    "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./dist",
    "allowJs": true,
    "target": "es6",
    "jsx": "react",
    "sourceMap": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "baseUrl": "./",
    "lib": [
    "DOM",
    "ES5",
    "ES6",
    "es2017.object"
    ],
    "paths": {
    "Components/*": ["src/components/*"],
    "Data/*": ["src/data/*"],
    }
    },
    18 changes: 18 additions & 0 deletions resolveTsConfigPaths.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    /**
    * resolveTsConfigPaths - resolve tsconfig paths to webpack aliases
    * @return {object}
    */
    function resolveTsconfigPathsToAlias() {
    const { paths } = require('../tsconfig.json').compilerOptions;

    const aliases = {};

    Object.keys(paths).forEach((item) => {
    const key = item.replace('/*', '');
    const value = path.resolve('./', paths[item][0].replace('/*', ''));

    aliases[key] = value;
    });

    return aliases;
    }
    1 change: 1 addition & 0 deletions usage.tsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    import Banner from 'Components/banner';