-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt3-module): deps improvements and documentation (#200)
* feat(nuxt3-module): internal dependency resolving and README * chore: changeset * fix: allow to override frontends dependency in local project * docs: deps customization
- Loading branch information
Showing
12 changed files
with
266 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"vue-demo-store": patch | ||
"vue-blank": patch | ||
--- | ||
|
||
Remove unnecessary dependencies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shopware-pwa/nuxt3-module": patch | ||
--- | ||
|
||
Internal dependency resolving |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# shopware/frontends - nuxt3-module | ||
|
||
[](https://npmjs.com/package/@shopware-pwa/nuxt3-module) | ||
[](https://github.com/shopware/frontends/tree/main/packages/nuxt3-module) | ||
 | ||
[](https://github.com/shopware/frontends/issues?q=is%3Aopen+is%3Aissue+label%3Anuxt3-module) | ||
|
||
Nuxt [module](https://nuxt.com/docs/guide/going-further/modules) that allows you to set up a Nuxt 3 project with Shopware Frontends. It provides the composables and api-client packages. | ||
|
||
If you want to use these packages with a different Vue.js framework, see [the guide](https://frontends.shopware.com/getting-started/templates/custom-project.html) for using Shopware Frontends in a custom project or use the [vue3-plugin](https://frontends.shopware.com/framework/internal-structure.html#vue3-plugin). | ||
|
||
## Features | ||
|
||
- Business logic covered by [Composables](https://npmjs.com/package/@shopware-pwa/composables-next) package. Registering all composable functions globally. [See the reference](https://frontends.shopware.com/packages/composables.html). | ||
- Shopware context shared in Nuxt application. | ||
- Configured [API Client](https://npmjs.com/package/@shopware-pwa/api-client) package. | ||
|
||
## Setup | ||
|
||
Install npm package: | ||
|
||
```bash | ||
# Using pnpm | ||
pnpm add -D @shopware-pwa/nuxt3-module | ||
|
||
# Using yarn | ||
yarn add --dev @shopware-pwa/nuxt3-module | ||
|
||
# Using npm | ||
npm i @shopware-pwa/nuxt3-module --save-dev | ||
``` | ||
|
||
Then, register the module by editing `nuxt.config.js` or (`.ts`) file (by extending `modules` array): | ||
|
||
```js | ||
/* nuxt.config.ts */ | ||
|
||
export default defineNuxtConfig({ | ||
/* ... */ | ||
modules: [, /* ... */ "@shopware-pwa/nuxt3-module"], | ||
|
||
runtimeConfig: { | ||
public: { | ||
shopware: { | ||
// connect to your Shopware 6 API instance | ||
shopwareEndpoint: "https://demo-frontends.shopware.store", | ||
shopwareAccessToken: "SWSCBHFSNTVMAWNZDNFKSHLAYW", | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
Set up your own API instance by adding public `runtimeConfiguration` in the same file. The nuxt module (and vue plugin) will use this values. | ||
|
||
## Basic usage | ||
|
||
Now you can use any composable function you need without extra import: | ||
|
||
```html | ||
<script setup> | ||
const { login } = useUser(); | ||
const { refreshSessionContext } = useSessionContext(); | ||
refreshSessionContext(); | ||
</script> | ||
``` | ||
|
||
The information about the session is kept in a cookie (`sw-context-token`) and used in every request made by any composable or directly, invoked by `api instance`: | ||
|
||
```html | ||
<script> | ||
const { apiInstance } = useShopwareContext(); | ||
const rawApiResponse = await apiInstance.invokePost(/** params omitted */); | ||
</script> | ||
``` | ||
|
||
## TypeScript support | ||
|
||
All composable functions are fully typed with TypeScript and they are registed globally in Nuxt.js application, so the type hinting will help you to work with all of them. | ||
|
||
## 📦 Advanced packaging | ||
|
||
Internally, the module uses [API Client](https://npmjs.com/package/@shopware-pwa/api-client) and [Composables](https://npmjs.com/package/@shopware-pwa/composables-next) packages, configured together to make everything working well. If you need to check how it's working on a different version of one of them, install a package locally in your project (to be installed and available in project's `package.json` file), then the Nuxt module will use yours. Keep in mind that the different configuration may lead to unexpected behavior. | ||
|
||
## Links | ||
|
||
- [📘 Documentation](https://frontends.shopware.com) | ||
|
||
- [👥 Community](https://shopwarecommunity.slack.com) (`#shopware-frontends` & `#shopware-pwa` channel) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Nuxt } from "@nuxt/schema"; | ||
import { resolve, dirname } from "path"; | ||
import { fileURLToPath } from "node:url"; | ||
import { promises as fs, constants as FS_CONSTANTS } from "node:fs"; | ||
import packageJson from "package-json-parser"; | ||
|
||
type DEPENDENCY = "@shopware-pwa/api-client" | "@shopware-pwa/composables-next"; | ||
|
||
const distDir = dirname(fileURLToPath(import.meta.url)); | ||
const pkgDir = resolve(distDir, ".."); | ||
const pkgModulesDir = resolve(pkgDir, "./node_modules"); | ||
|
||
export async function isDependencyInstalledLocally( | ||
rootDir: string, | ||
dependency: DEPENDENCY | ||
) { | ||
try { | ||
const projectPackageJsonPath = resolve(rootDir, "package.json"); | ||
const packageJsonDefinition = packageJson.json(projectPackageJsonPath); | ||
if ( | ||
packageJsonDefinition?.dependencies?.[dependency] || | ||
packageJsonDefinition?.devDependencies?.[dependency] | ||
) { | ||
return true; | ||
} | ||
} catch (error) { | ||
console.error("nuxt3-module: unable to check local dependencies"); | ||
} | ||
return false; | ||
} | ||
|
||
export async function isExists(path: string) { | ||
try { | ||
await fs.access(path, FS_CONSTANTS.F_OK); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
export async function resolveOwnDependency(dependency: DEPENDENCY, nuxt: Nuxt) { | ||
const { rootDir, workspaceDir } = nuxt.options; | ||
const modulePath = `${dependency}/dist/index.mjs`; | ||
|
||
const targets = [ | ||
resolve(rootDir, "node_modules", modulePath), | ||
resolve(pkgModulesDir, modulePath), | ||
resolve(workspaceDir, "node_modules", modulePath), | ||
]; | ||
|
||
for (const target of targets) { | ||
if (await isExists(target)) { | ||
return target; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
{ | ||
"extends": "tsconfig/base.json", | ||
"include": ["."], | ||
"exclude": ["dist"] | ||
"exclude": [ | ||
"dist" | ||
], | ||
"compilerOptions": { | ||
"target": "esnext", | ||
"module": "esnext" | ||
} | ||
} |
Oops, something went wrong.
329b0ae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
frontends-demo – ./templates/vue-demo-store
frontends-demo-git-main-shopware-frontends.vercel.app
frontends-demo.vercel.app
frontends-demo-shopware-frontends.vercel.app
329b0ae
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
shopware-frontends-docs – ./
shopware-frontends-docs-shopware-frontends.vercel.app
shopware-frontends-docs.vercel.app
shopware-frontends-docs-git-main-shopware-frontends.vercel.app
frontends.shopware.com