GithubHelp home page GithubHelp logo

Comments (4)

mongkuen avatar mongkuen commented on July 19, 2024 1

I'm not sure what the issue is. I made an example with gatsby-starter-default, and using src as the root directory works by simply adding 'gatsby-plugin-root-import' to the config plugins list.

You can see the repo and the commit here: https://github.com/mongkuen/gatsby-starter-default/commit/228d01e2fb371117cf7b1100d3b6d467f338998e

options: {
    root: path.join(__dirname, 'src'),
},

This wouldn't work because as the docs say, this plugin is a shallow wrapper around webpack's alias, and the above code makes root/myFolder resolve to src/myFolder
https://webpack.js.org/configuration/resolve/#resolvealias

Furthermore, root itself has been deprecated also I believe. https://webpack.js.org/migrate/3/#resolveroot-resolvefallback-resolvemodulesdirectories

from gatsby-plugin-root-import.

Maushundb avatar Maushundb commented on July 19, 2024

@mongkuen The docs on the gatsby site, as well as in your README still have an example using the root field - https://www.gatsbyjs.org/packages/gatsby-plugin-root-import/#plugin-options-1

If this field is not longer supported, can we remove the example from the docs? Also, what do you suggest using if we want to import files like components/MyFile rather than src/component/MyFile, other than making an alias for each top level dir?

from gatsby-plugin-root-import.

andyhmltn avatar andyhmltn commented on July 19, 2024

@Maushundb Appreciate I'm probably a bit late with the solution but I found this thread via Google having had the same issue. I managed to correct it by iterating through the directories and creating an options object automatically. Two helpers need to be created:

const path = require('path');
const { readdirSync, lstatSync } = require('fs');

const isDirectory = source => lstatSync(source).isDirectory();
const getDirectories = source =>
  readdirSync(source)
    .map(name => path.join(source, name))
    .filter(isDirectory);

Then you can use getDirectories to create options:

# gatsby-config.js
{
      resolve: 'gatsby-plugin-root-import',
      // We don't want to manually add the directory to options everytime a new
      // one is created in src so this fetches all directories in src and maps them
      // to an object of options expected by `gatsby-plugin-root-import`
      options: getDirectories('src').reduce((finalOptions, directory) => {
        finalOptions[directory.replace('src/', '')] = path.resolve(
          __dirname,
          directory
        );

        return finalOptions;
      }, {}),
},

Its a shame this use-case isn't supported by gatsby-plugin-root-import without resorting to this. Having to prefix all imports with src/ seems messy and unneeded when it applies to everything

from gatsby-plugin-root-import.

mongkuen avatar mongkuen commented on July 19, 2024

Yeah you're right, having to list every top level folder isn't ideal. Plugin now resolves the src folder automatically with resolve.modules.

from gatsby-plugin-root-import.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.