[go: up one dir, main page]

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

feat(schema,nuxt): add shared folder to alias and auto-imports #28682

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Barbapapazes
Copy link
Contributor
@Barbapapazes Barbapapazes commented Aug 23, 2024

🔗 Linked issue

fix #28675

📚 Description

Hello 👋,

This PR adds:

  • #shared alias to both tsconfig.json and tsconfig.server.json
  • Enable auto imports from #shared/types and #shared/utils in app and server directories

The second bullet point is still in progress because I'm still trying to understand the usage of unimport within the project.

So, after talking with @danielroe, this #shared folder is more complicated than expected. Here a list of expected behavior (that needs to be implemented)

  • ./app folder can import (and auto-import) from the ./shared folder
  • ./server folder can import (and auto-import) from the ./shared folder
  • Inside the ./shared folder, you should not be able to import stuff from both ./app and ./server (could be achieved by excluding the file from the tsconfig.json and a single chunk could be created)
  • Inside the./shared folder, you could (we need to decide if we want or not this behavior) import and auto-import stuff from the ./shared folder.

At the end, the shared folder is just for pure JS (or TS) stuff. Nothing have to be related to Vue or Nitro.

Copy link
stackblitz bot commented Aug 23, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@DamianGlowala DamianGlowala added 4.x and removed 3.x labels Aug 23, 2024
@Barbapapazes Barbapapazes changed the title feat(schema): add shared alias feat(schema,nuxt): add shared folder to alias and auto-imports Aug 23, 2024
@Barbapapazes Barbapapazes marked this pull request as ready for review August 23, 2024 17:30
Copy link
Member
@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

The next step is to configure import protection so that we do not use certain protected imports in that folder...

@Barbapapazes
Copy link
Contributor Author
Barbapapazes commented Aug 29, 2024

import protection

What do you mean? I never heard this term before. Do you have an example?


Edit

Oh, you mean, protecting against unwanted imports like vue / nitro related stuff within the shared folder to ensure it will works properly in both app and server.

@danielroe
Copy link
Member

You can add patterns for the shared directory here:

export const nuxtImportProtections = (nuxt: { options: NuxtOptions }, options: { isNitro?: boolean } = {}) => {
const patterns: ImportProtectionOptions['patterns'] = []
patterns.push([
/^(nuxt|nuxt3|nuxt-nightly)$/,
'`nuxt`, `nuxt3` or `nuxt-nightly` cannot be imported directly.' + (options.isNitro ? '' : ' Instead, import runtime Nuxt composables from `#app` or `#imports`.'),
])
patterns.push([
/^((~|~~|@|@@)?\/)?nuxt\.config(\.|$)/,
'Importing directly from a `nuxt.config` file is not allowed. Instead, use runtime config or a module.',
])
patterns.push([/(^|node_modules\/)@vue\/composition-api/])
for (const mod of nuxt.options.modules.filter(m => typeof m === 'string')) {
patterns.push([
new RegExp(`^${escapeRE(mod as string)}$`),
'Importing directly from module entry-points is not allowed.',
])
}
for (const i of [/(^|node_modules\/)@nuxt\/(kit|test-utils)/, /(^|node_modules\/)nuxi/, /(^|node_modules\/)nitro(?:pack)?(?:-nightly)?(?:$|\/)(?!(?:dist\/)?runtime|types)/, /(^|node_modules\/)nuxt\/(config|kit|schema)/]) {
patterns.push([i, 'This module cannot be imported' + (options.isNitro ? ' in server runtime.' : ' in the Vue part of your app.')])
}
if (options.isNitro) {
for (const i of ['#app', /^#build(\/|$)/]) {
patterns.push([i, 'Vue app aliases are not allowed in server runtime.'])
}
}
if (!options.isNitro) {
patterns.push([
new RegExp(escapeRE(relative(nuxt.options.srcDir, resolve(nuxt.options.srcDir, nuxt.options.serverDir || 'server'))) + '\\/(api|routes|middleware|plugins)\\/'),
'Importing from server is not allowed in the Vue part of your app.',
])
}
return patterns
}

Or let me know, and I'll happily implement. 🙏

@danielroe danielroe marked this pull request as draft August 29, 2024 19:52
@Barbapapazes
Copy link
Contributor Author
Barbapapazes commented Sep 17, 2024

You can add patterns for the shared directory here:

export const nuxtImportProtections = (nuxt: { options: NuxtOptions }, options: { isNitro?: boolean } = {}) => {
const patterns: ImportProtectionOptions['patterns'] = []
patterns.push([
/^(nuxt|nuxt3|nuxt-nightly)$/,
'`nuxt`, `nuxt3` or `nuxt-nightly` cannot be imported directly.' + (options.isNitro ? '' : ' Instead, import runtime Nuxt composables from `#app` or `#imports`.'),
])
patterns.push([
/^((~|~~|@|@@)?\/)?nuxt\.config(\.|$)/,
'Importing directly from a `nuxt.config` file is not allowed. Instead, use runtime config or a module.',
])
patterns.push([/(^|node_modules\/)@vue\/composition-api/])
for (const mod of nuxt.options.modules.filter(m => typeof m === 'string')) {
patterns.push([
new RegExp(`^${escapeRE(mod as string)}$`),
'Importing directly from module entry-points is not allowed.',
])
}
for (const i of [/(^|node_modules\/)@nuxt\/(kit|test-utils)/, /(^|node_modules\/)nuxi/, /(^|node_modules\/)nitro(?:pack)?(?:-nightly)?(?:$|\/)(?!(?:dist\/)?runtime|types)/, /(^|node_modules\/)nuxt\/(config|kit|schema)/]) {
patterns.push([i, 'This module cannot be imported' + (options.isNitro ? ' in server runtime.' : ' in the Vue part of your app.')])
}
if (options.isNitro) {
for (const i of ['#app', /^#build(\/|$)/]) {
patterns.push([i, 'Vue app aliases are not allowed in server runtime.'])
}
}
if (!options.isNitro) {
patterns.push([
new RegExp(escapeRE(relative(nuxt.options.srcDir, resolve(nuxt.options.srcDir, nuxt.options.serverDir || 'server'))) + '\\/(api|routes|middleware|plugins)\\/'),
'Importing from server is not allowed in the Vue part of your app.',
])
}
return patterns
}

Or let me know, and I'll happily implement. 🙏

I do not know how to know when the folder is the shared folder 🤔

Thanks to @manniL I could push a new plugin with a different cwd

@Barbapapazes
Copy link
Contributor Author

I'm facing another issue. Everything in the shared folder is available from imports folder because of this, people would want to import inside the shared folder, stuff from the shared folder but at the same time, I do not want people to import Vue stuff (from imports) inside the shared folder.

@manniL
Copy link
Member
manniL commented Sep 17, 2024

I'm facing another issue. Everything in the shared folder is available from imports folder because of this, people would want to import inside the shared folder, stuff from the shared folder but at the same time, I do not want people to import Vue stuff (from imports) inside the shared folder.

Is there anything in imports which is neither server nor client-dependent?

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

Successfully merging this pull request may close these issues.

Add #shared alias
4 participants