cloud icon indicating copy to clipboard operation
cloud copied to clipboard

Import Paths -- Always relative?

Open Gazzini opened this issue 3 years ago • 2 comments

Do all imports have to be relative?

I prefer this: import { bar } from '~/services/foo'

to this: import { bar } from '../../services/foo'

In Serverless Framework, I accomplish it like this: tsconfig.json

"compilerOptions": {
  "baseUrl": "./src",                 <or lib, or whatever, doesn't matter>
  "rootDir": ".",
  "paths": {
      "~/*": [
          "*"
      ]
  }
}

webpack.config.js

const path = require("path");

module.exports = {
    resolve: {
        extensions: [".mjs", ".js", ".jsx", ".json", ".ts", ".tsx"],
        alias: {
            '~': path.resolve(__dirname, 'src')
        }
    },

Thanks for making this framework, love the direction, and I'm excited to try it out.

Gazzini avatar Feb 04 '22 01:02 Gazzini

Hi! Serverless Cloud only performs transpiling of your sources, it doesn't actually use tsc - that said, I think we could probably accomodate ~ as a custom prefix for saying 'relative to the base of the cloud project', or maybe 'relative to the nearest package.json' ? relatively easily. There's a discussion underway here https://github.com/serverless/cloud/discussions/72 regarding monorepo support, in case you're interested.

dougmoscrop avatar Feb 04 '22 04:02 dougmoscrop

Thanks @dougmoscrop, I think that adding ~ as a custom prefix would solve my problem perfectly.

I don't have an opinion on if it's relative to the nearest package.json or the root of the cloud project, as they're the same in my case.

Gazzini avatar Feb 04 '22 18:02 Gazzini