Gridsome: import shared components from outside root

Created on 20 Apr 2019  ยท  3Comments  ยท  Source: gridsome/gridsome

Hello,

I have this folder structure with some components that I want to share between site-a & site-b:

.
โ””โ”€โ”€ .shared/
โ”‚   โ””โ”€โ”€ components/
โ”‚       โ””โ”€โ”€ MyComponent.vue
โ””โ”€โ”€ site-a/
โ”‚   โ””โ”€โ”€ layouts/
โ”‚   โ”‚   โ””โ”€โ”€ Default.vue
โ”‚   โ””โ”€โ”€ node_modules/
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ gridsome.config.js
โ””โ”€โ”€ site-b/
โ”‚   โ””โ”€โ”€ layouts/
โ”‚   โ”‚   โ””โ”€โ”€ Default.vue
โ”‚   โ””โ”€โ”€ node_modules/
โ”‚   โ”œโ”€โ”€ package.json
โ”‚   โ””โ”€โ”€ gridsome.config.js
โ””โ”€โ”€ package.json

So I try to import the .shared MyComponent.vue in my site-a Default.vue like this:

// site-a/layouts/Default.vue
import MyComponent from '../../.shared/components/MyComponent.vue'

But when I run gridsome develop from site-a:

c:\my-project\site-a> gridsome develop

I have this message:

These dependencies were not found:

* -!cache-loader?{"cacheDirectory":"node_modules/.cache/gridsome","cacheIdentifier":"29f0df40-vue-loader-template"}!../../../site-a/node_modules/vue-loader/lib/loaders/templateLoader.js??vue-loader-options!../../../site-a/node_modules/cache-loader/dist/cjs.js??ref--0-0!../../../site-a/node_modules/vue-loader/lib/index.js??vue-loader-options!./MyComponent.vue?vue&type=template&id=36f838bd& in ../.shared/components/MyComponent.vue?vue&type=template&id=36f838bd&
* vue in ../.shared/components/MyComponent.vue

So maybe he can't access find MyComponent.vue because the .shared folder is outside the gridsome site-a root?

So i tried to put these webpack lines in my site-a gridsome.config.js:

const path = require("path")
module.exports = {
  siteName: "site-a",
  resolve: {
      alias: {
        '~shared': path.resolve(__dirname, '../.shared/'),
      }
  }
}
// also tried this:   chainWebpack: config => { config.resolve.alias.set("~shared", path.resolve(__dirname, "../.shared")); }

To use the ~shared alias:

// site-a/layouts/Default.vue
import MyComponent from '~shared/components/MyComponent.vue'

But i have the same message...

So I dont understand how can I make this simple thing work...?

Thanks for your help!

Most helpful comment

Correct me if I'm wrong, but I think every Node.js project need to be self-contained, so no referencing outside of the root folder. What I suggest you to do is publish your shared components to npm, then import it in your site a and b.

All 3 comments

Correct me if I'm wrong, but I think every Node.js project need to be self-contained, so no referencing outside of the root folder. What I suggest you to do is publish your shared components to npm, then import it in your site a and b.

You can also look into Yarn workspaces in order to set up a multi package.json project (monorepo). Place a package.json in the .shared folder and make it a private package you import into site-a and site-b as any other npm package like:

import MyComponent from 'shared-package-name/components/MyComponent.vue'

Thank you @NahroTo & @hjvedvik, I do not know yarn wokspaces, and this fit my needs!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cyrilf picture cyrilf  ยท  3Comments

mattbrailsford picture mattbrailsford  ยท  3Comments

tanc picture tanc  ยท  3Comments

nuriaheep picture nuriaheep  ยท  3Comments

NickStees picture NickStees  ยท  3Comments