Skip to content

Instantly share code, notes, and snippets.

@phuctm97
Last active November 2, 2022 06:32
Show Gist options
  • Select an option

  • Save phuctm97/7470a3b981fd80a501b4047e1e99a9f4 to your computer and use it in GitHub Desktop.

Select an option

Save phuctm97/7470a3b981fd80a501b4047e1e99a9f4 to your computer and use it in GitHub Desktop.

Revisions

  1. phuctm97 revised this gist Apr 17, 2020. No changes.
  2. phuctm97 revised this gist Apr 17, 2020. No changes.
  3. phuctm97 revised this gist Apr 17, 2020. No changes.
  4. phuctm97 revised this gist Apr 17, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get-webpack-alias-from-tsconfig.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ const path = require('path');
    * compilerOptions.paths.
    *
    * @param {string} tsconfigPath - Path to tsconfig.json (can be either relative or absolute path).
    * @return {object} An object representing corresponding Webpack alias.
    * @return {object} An object representing analogous Webpack alias.
    */
    module.exports = (tsconfigPath = './tsconfig.json') => {
    const tsconfig = require(tsconfigPath);
  5. phuctm97 revised this gist Apr 17, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion get-webpack-alias-from-tsconfig.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ const path = require('path');
    * Helper function infers Webpack aliases from tsconfig.json compilerOptions.baseUrl and
    * compilerOptions.paths.
    *
    * @param {string} tsconfigPath - Path to tsconfig.json (Can be either relative or absolute path).
    * @param {string} tsconfigPath - Path to tsconfig.json (can be either relative or absolute path).
    * @return {object} An object representing corresponding Webpack alias.
    */
    module.exports = (tsconfigPath = './tsconfig.json') => {
  6. phuctm97 revised this gist Apr 17, 2020. No changes.
  7. phuctm97 revised this gist Apr 17, 2020. 2 changed files with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions webpack.config.js
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    const path = require('path');
    const extractAlias = require('./extract-webpack-alias-from-tsconfig');
    const getAlias = require('./get-webpack-alias-from-tsconfig');

    module.exports = {
    resolve: {
    alias: extractAlias(path.resolve(__dirname, 'tsconfig.json')),
    alias: getAlias(path.resolve(__dirname, 'tsconfig.json')),
    },
    };
  8. phuctm97 revised this gist Apr 12, 2020. 3 changed files with 6 additions and 5 deletions.
    7 changes: 4 additions & 3 deletions extract-webpack-alias-from-tsconfig.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,16 @@
    const path = require('path');

    /**
    * Helper function infers Webpack aliases from tsconfig.json compilerOptions.baseUrl and compilerOptions.paths.
    * Helper function infers Webpack aliases from tsconfig.json compilerOptions.baseUrl and
    * compilerOptions.paths.
    *
    * @param {string} tsconfigPath - Path to tsconfig.json (Can be either relative or absolute path).
    * @return {object} An object representing corresponding Webpack alias.
    */
    module.exports = (tsconfigPath = './tsconfig.json') => {
    const tsconfig = require(tsconfigPath);
    const { paths, baseUrl } = tsconfig.compilerOptions;

    return Object.fromEntries(Object.entries(paths)
    .filter(([, pathValues]) => pathValues.length > 0)
    .map(([pathKey, pathValues]) => {
    @@ -18,4 +19,4 @@ module.exports = (tsconfigPath = './tsconfig.json') => {
    baseUrl, pathValues[0].replace('/*', ''));
    return [key, value];
    }));
    }
    };
    2 changes: 1 addition & 1 deletion tsconfig.json
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,4 @@
    "@util/*": ["src/util/*"]
    }
    }
    }
    }
    2 changes: 1 addition & 1 deletion webpack.config.js
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,4 @@ module.exports = {
    resolve: {
    alias: extractAlias(path.resolve(__dirname, 'tsconfig.json')),
    },
    };
    };
  9. phuctm97 revised this gist Apr 12, 2020. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions tsconfig.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    {
    "compilerOptions": {
    "baseUrl": ".",
    "paths": {
    "@core/*": ["src/core/*"],
    "@util/*": ["src/util/*"]
    }
    }
    }
  10. phuctm97 revised this gist Apr 12, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions webpack.config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    const path = require('path');
    const extractAlias = require('./extract-webpack-alias-from-tsconfig');

    module.exports = {
    resolve: {
    alias: extractAlias(path.resolve(__dirname, 'tsconfig.json')),
    },
    };
  11. phuctm97 revised this gist Apr 12, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions extract-webpack-alias-from-tsconfig.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    const path = require('path');

    /**
    * Helper function infers Webpack aliases from tsconfig.json compilerOptions.baseUrl and compilerOptions.paths.
    *
    * @param {string} tsconfigPath - Path to tsconfig.json (Can be either relative or absolute path).
    * @return {object} An object representing corresponding Webpack alias.
    */
    module.exports = (tsconfigPath = './tsconfig.json') => {
    const tsconfig = require(tsconfigPath);
    const { paths, baseUrl } = tsconfig.compilerOptions;
  12. phuctm97 revised this gist Apr 12, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion extract-webpack-alias-from-tsconfig.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ module.exports = (tsconfigPath = './tsconfig.json') => {
    const tsconfig = require(tsconfigPath);
    const { paths, baseUrl } = tsconfig.compilerOptions;

    Object.fromEntries(Object.entries(paths)
    return Object.fromEntries(Object.entries(paths)
    .filter(([, pathValues]) => pathValues.length > 0)
    .map(([pathKey, pathValues]) => {
    const key = pathKey.replace('/*', '');
  13. phuctm97 revised this gist Apr 12, 2020. No changes.
  14. phuctm97 created this gist Apr 12, 2020.
    15 changes: 15 additions & 0 deletions extract-webpack-alias-from-tsconfig.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    const path = require('path');

    module.exports = (tsconfigPath = './tsconfig.json') => {
    const tsconfig = require(tsconfigPath);
    const { paths, baseUrl } = tsconfig.compilerOptions;

    Object.fromEntries(Object.entries(paths)
    .filter(([, pathValues]) => pathValues.length > 0)
    .map(([pathKey, pathValues]) => {
    const key = pathKey.replace('/*', '');
    const value = path.resolve(path.dirname(tsconfigPath),
    baseUrl, pathValues[0].replace('/*', ''));
    return [key, value];
    }));
    }