
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
gatsby-plugin-root-import
Advanced tools
Set Webpack to resolve modules and aliases, allowing you to import modules from an absolute project path rather than relative ../../
paths.
Install the gatsby-plugin-root-import
plugin:
npm install --save-dev gatsby-plugin-root-import
or
yarn add --dev gatsby-plugin-root-import
Add into gatsby-config.js
.
// gatsby-config.js
module.exports = {
plugins: ["gatsby-plugin-root-import"],
};
If no options are specified, the plugin allows you access to the src
folder and also it's contents automatically.
This means you should be able to import modules like such:
// importing gatsbyProject/src/someFolder/SomeComponent.js
import SomeComponent from "src/someFolder/SomeComponent";
// or more succinctly
import SomeComponent from "someFolder/SomeComponent";
Plugin Options allows you to:
resolve.modules
search locations with the resolveModules
key.alias
.// gatsby-config.js
const path = require("path");
module.exports = {
plugins: [
{
resolve: "gatsby-plugin-root-import",
options: {
resolveModules: [path.join(__dirname, "libs")],
utils: path.join(__dirname, "src", "components", "utilities"),
},
},
],
};
This means you should be able to import modules like such:
// gatsbyProject/libs is now a searched resolve.modules directory
// importing gatsbybProject/libs/someLib/SomeLibComponent.js
import SomeLibComponent from "someLib/SomeLibComponent";
// from the 'utils' alias
// importing gatsbyProject/src/components/utilities/UtilityComponent.js
import UtilityComponent from "utils/UtilityComponent";
The new aliased paths lets Webpack correctly compile your app. However this does not mean that Jest, or other test runners will understand where those aliases point to.
When setting up testing with Jest, see moduleNameMapper
to correctly map your your aliases to a path that Jest can understand.
For example:
// gatsby-config.js
const path = require("path");
module.exports = {
// ...other configs
plugins: [
{
resolve: "gatsby-plugin-root-import",
options: {
components: path.join(__dirname, "src", "web", "components"),
},
},
],
};
could work with a Jest map like:
// jest.config.js
module.exports = {
// ...other configs
moduleNameMapper: {
"^components/(.*)": "<rootDir>/src/web/components/$1",
},
};
Please see Jest's moduleNameMapper
documentation for specific implementation details regarding format and options.
Install the gatsby-plugin-root-import
plugin:
npm install --save-dev --save-exact gatsby-plugin-root-import@1.0.0
or
yarn add --dev gatsby-plugin-root-import@1.0.0
Add into gatsby-config.js
.
// gatsby-config.js
module.exports = {
plugins: ["gatsby-plugin-root-import"],
};
If no options are specified, the plugin defaults to your project root folder.
This means you should be able to import modules like such:
// importing gatsbyProject/src/yourFolder/YourComponent.js
import YourComponent from "src/yourFolder/YourComponent";
You can pass a root
option. You can specify your own folder, such as src
.
This option corresponds with Webpack v3's resolve.root
found here.
This may be both a directory string, or an array of directory strings.
// gatsby-config.js
const path = require("path");
module.exports = {
plugins: [
{
resolve: "gatsby-plugin-root-import",
options: {
root: path.join(__dirname, "src"),
},
},
],
};
This means you can import modules with project's src
folder as root
:
// importing gatsbyProject/src/yourFolder/YourComponent.js
import YourComponent from "yourFolder/YourComponent";
FAQs
Gatsby plugin to set Webpack to resolve import root
The npm package gatsby-plugin-root-import receives a total of 13,407 weekly downloads. As such, gatsby-plugin-root-import popularity was classified as popular.
We found that gatsby-plugin-root-import demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.