enjoythecode / strate.gg

A hobby project dedicated to the play and exploration of various abstract strategy board games.
https://strate.gg
3 stars 0 forks source link

Use src-relative imports in fe/ #29

Closed enjoythecode closed 2 years ago

enjoythecode commented 2 years ago

Current Behavior

Currently, imports use relative imports that look like this:

# fe/src/Games/Amazons/AmazonsView.js

import { useRootStore } from "../../Store/RootStore";

This is inconvenient because the ../ prefix is file specific and requires hand-modification.

Desired Behavior

Imports in any given file should not depend on the folder depth of a file.

enjoythecode commented 2 years ago

Solutions Considered

Webpack Aliases

Webpack can be configured to have aliases with a syntax that looks like this:

module.exports = {
    ...
    resolve: {
      ...
      alias: {
        ...
        'components': path.resolve(__dirname, '../src/components/'),
        'images': path.resolve(__dirname, '../src/images/'),
      },
      ...
    },
    ...
};

Pros:

NODE_PATH Environment Variable

Exporting NODE_PATH=src will make webpack look for folders inside the src folder when importing without a relative path.

Pros:

Create React App Absolute Imports

This kind of configuration is supported in Create React App See Documentation

# contents of jsconfig.json
{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

Pros:

enjoythecode commented 2 years ago

Chosen solution: Create React App Absolute Imports