Skip to content

Commit

Permalink
fix(imports): baseUrl & paths webpack tsconfig
Browse files Browse the repository at this point in the history
baseurl and paths must be configured in tsconfig.json
webpack configs will now correctly resolve paths.
  • Loading branch information
nikhilnayyar002 committed Sep 18, 2022
1 parent 5dcf86a commit 9596f78
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"compilerOptions": {
"allowJs": true,
"noEmit": true,
"baseUrl": ".",
"baseUrl": "src",
"paths": {
"@styles/*": ["src/styles/*"],
"@assets/*": ["src/assets/*"]
"@styles/*": ["styles/*"],
"@assets/*": ["assets/*"]
},
"module": "ESNext",
"target": "ESNext",
Expand Down
3 changes: 2 additions & 1 deletion wm-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require("dotenv-flow").config(); // load .env files
const tsConfig = require("./tsconfig.json");
const { getClientIPAddresses, getWebpackAliasFromTsConfig } = require("./wm-helper");
const { getClientIPAddresses, getWebpackAliasFromTsConfig, getWebpackResolveModulesFromTsConfig } = require("./wm-helper");

// *************************************************************************************************
// dont change the values of variables. These are updated by feature commands
Expand Down Expand Up @@ -68,6 +68,7 @@ const wmConfig = {
resolve: {
alias: getWebpackAliasFromTsConfig(tsConfig),
extensions,
modules: getWebpackResolveModulesFromTsConfig(tsConfig)
},
outputESModule, // ouput ECMAScript module syntax whenever possible.
// dont include "initial" vendor chunk in "initial" main chunk. generate a separate "initial" vendor chunk
Expand Down
30 changes: 21 additions & 9 deletions wm-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ const { escapeStringRegexp } = require("./wm-util");
exports.getWebpackAliasFromTsConfig = (tsConfig) => {
let webpackAlias = {};
const baseUrl = tsConfig?.compilerOptions?.baseUrl;
if (!baseUrl) throw "tsconfig.json baseUrl missing";
if (baseUrl !== ".") throw 'tsconfig.json baseUrl must have value "."';
const paths = tsConfig?.compilerOptions?.paths ?? {};
for (let p in paths) {
let a = p.substring(0, p.length - 2);
if (paths[p].length) {
let b = paths[p][0];
let c = b.substring(0, b.length - 1);
webpackAlias[a] = path.resolve(__dirname, c);
// if (!baseUrl) throw "tsconfig.json baseUrl missing";
const paths = tsConfig?.compilerOptions?.paths;

if (paths) {
// if (baseUrl !== ".") throw 'tsconfig.json baseUrl must have value "." when paths is also provided.';

for (let p in paths) {
let a = p.substring(0, p.length - 2);
if (paths[p].length) {
let b = paths[p][0];
let c = b.substring(0, b.length - 1);
webpackAlias[a] = path.resolve(__dirname, baseUrl || "", c);
}
}
}
return webpackAlias;
};

exports.getWebpackResolveModulesFromTsConfig = (tsConfig) => {
const modules = ["node_modules"];
const baseUrl = tsConfig?.compilerOptions?.baseUrl;
if (baseUrl) modules.unshift(path.resolve(__dirname, baseUrl));

return modules;
};

exports.getClientIPAddresses = (clientPort, httpsMode = false) => {
const temp = [];
for (const name of Object.keys(nets))
Expand Down

0 comments on commit 9596f78

Please sign in to comment.