Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ./ import by @lib/ using Babel resolver #50

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

tschaffter
Copy link
Collaborator

@tschaffter tschaffter commented Sep 2, 2020

This PR aims to fix #27.

This PR replaces all relative import by absolute imports using @lib/.

npm run build and npm run test completes successfully.

@jb-adams
Copy link
Member

i'm happy to do a review of this if you'd like

@tschaffter
Copy link
Collaborator Author

tschaffter commented Nov 1, 2020

In #50, @jaeddy says:

The @lib/environment import apparently gets interpreted (when trying to use babel-node to compile and execute an ES6+ formatted script) as a module/dependency:

Currently we have in webpack.config.js:

    resolve: {
        alias: {
            '@lib': path.resolve(__dirname, 'src/lib')
        },
        extensions: ['.js'],
        modules: ['node_modules']
    },

First, I think that it is confusing to use the prefix "@" for aliases since this is also the prefix for npmjs organization like "@sentinel-one/ui-compodoc".

Second, we are currently using webpack to define the alias. In #50, @jaeddy proposes to use the Babel plugin "babel-plugin-module-resolver". At the same time, I think that @jb-adams implemented the current solution that uses webpack aliases. The article shared by @jaeddy in #50 comment on this:

Aside: Alternative by using webpack
For completeness, you can probably also use webpack to achieve this; it has a resolve.alias configuration option which allows for similar behaviour and has a corresponding eslint resolver plugin available. Generally I shy away from a webpack solution when a babel solution is viable, since many more projects and tools use babel (either standalone or with webpack). The part where the webpack version shines is when you need to hijack entire modules in your build (e.g. mapping third party dependency react to inferno). Things like this are typically not done with babel since babel is rarely configured to run on things inside the node_modules folder.

We currently do not depend on the advantage mentioned by the author for the webpack solution. However, I think that there is an advantage to use the babel plugin approach: enabling developers to import gh-openapi-docs into their projects.

Proposal:

@jb-adams
Copy link
Member

jb-adams commented Nov 2, 2020

  • Use another prefix character for aliases, for example "#" as used in Why You Should Use Babel Resolvers. We could also decide to drop the prefix character as illustrated in the README of the plugin babel-plugin-module-resolver.
  • If at least one person agrees and there is no objection, I'll update the code to set the alias with babel instead of webpack.

Sounds good to me. I wasn't aware that babel resolvers could do this (was only familiar with webpack resolvers).

+1 for using babel instead of webpack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use babel resolvers instead of 'absolute' imports
2 participants