question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

addWebpackAlias doesn't work

See original GitHub issue

my code’s here …

const { override, fixBabelImports, addWebpackExternals, addWebpackAlias, addLessLoader } = require(‘customize-cra’); const path = require(‘path’); const UglifyJsPlugin = require(‘uglifyjs-webpack-plugin’); const { paths } = require(‘react-app-rewired’); console.log(paths.appSrc);

const myPlugin = [ new UglifyJsPlugin({ uglifyOptions: { warnings: false, compress: { drop_debugger: true, drop_console: true } } }) ];

module.exports = override( fixBabelImports(‘import’, { libraryName: ‘antd’, libraryDirectory: ‘es’, style: true }), addWebpackExternals({ echarts: ‘window.echarts’ // highcharts:“window.highcharts” }), addWebpackAlias({ ‘@’: ${paths.appSrc} // doesn’t work }), addLessLoader({ javascriptEnabled: true, modifyVars: { ‘@primary-color’: ‘#1DA57A’ } }), config => { // config.devtool = config.mode === ‘development’ ? ‘cheap-module-source-map’ : false; if (process.env.NODE_ENV === ‘production’) config.devtool = false; if (process.env.NODE_ENV !== ‘development’) config.plugins = […config.plugins, …myPlugin]; const loaders = config.module.rules.find(rule => Array.isArray(rule.oneOf)) .oneOf; loaders[5].use.push({ loader: ‘sass-resources-loader’, options: { resources: path.resolve(__dirname, ‘src/asset/base.scss’) } });

return config;

} );

use alias import { router } from '@/maps'; the wrong msg: Cannot find module ‘@/maps’

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:18

github_iconTop GitHub Comments

16reactions
gyertscommented, May 13, 2020

We need 2 steps to add aliases

  1. tell to IDE about aliases in tsconfig.json
  2. tell to webpack about aliases

First:

create separate file tsconfig.path.json and add aliases:

{
   "compilerOptions": {
      "baseUrl": "./src",
      "paths": {
         "@utils/*": ["utils/*"]
      }
   }
}

Add created tsconfig.path.json to main tsconfig.json

{
   "extends": "./tsconfig.paths.json",
   ... other staff ...
}

Second:

Add aliases to config-overrides.js

const {
   override,
   addWebpackAlias
} = require('customize-cra');

const path = require("path");

module.exports = override(
   addWebpackAlias({
      "@utils": path.resolve(__dirname, "./src/utils"),
   }),
);

It works for me, happy coding 😉

1reaction
the-simiancommented, Sep 11, 2020

Anyone is reading this and is wondering… why use the seperate file? why not just put it in the tsconfig.json? Because that file gets mostly rewritten each run, and it will delete the paths you add. You gotta keep it in an external file, unfortunately.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Webpack Alias With react-app-rewired and customize-cra
I don't know how to do this because I am using react-app-rewired and customize-cra along with babel-plugin-import , so my "config-overrides.js" ...
Read more >
How to use the customize-cra.addWebpackAlias function in ...
To help you get started, we've selected a few customize-cra.addWebpackAlias examples, based on popular ways it is used in public projects.
Read more >
Developers - addWebpackAlias doesn't work - - Bountysource
my code's here ... const { override, fixBabelImports, addWebpackExternals, addWebpackAlias, addLessLoader } = require('customize-cra');
Read more >
Resolve | webpack
If true , it will not allow extension-less files. So by default require('./foo') works if . ... foo.js') will work. webpack.config.js
Read more >
Webpack aliases not resolved when using ... - YouTrack
Dennis Ushakov Thanks for your suggestion, unfortunately it doesn't work for me. If I open the Event Log after specifying my webpack.config.babel.js in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found