Skip to content

Commit 7384527

Browse files
CopilotIsengo1989
andauthored
Update "Adding NPM dependencies" guide for Shopware 6.7 Vite migration (#2246)
* Update npm dependencies guide for Shopware 6.7 Vite migration - Remove outdated webpack-based approach for the Administration - Add new Administration section explaining Vite handles npm package resolution automatically - Add optional vite.config.mts example for custom configuration - Keep Webpack approach for the Storefront (still webpack-based) - Update build commands (composer build:js:admin for admin, ./bin/build-storefront.sh for storefront) - Remove outdated YouTube video reference showing webpack approach - Add link to Webpack to Vite migration guide Agent-Logs-Url: https://github.com/shopware/docs/sessions/07bcf0eb-b3cc-4003-919c-f691029a5fd0 Co-authored-by: Isengo1989 <8600299+Isengo1989@users.noreply.github.com> * Clarify vite.config.mts location and fix example alias path - Add note clarifying that vite.config.mts is placed in src/ (next to entry file) while package.json stays in administration/ - Replace placeholder absolute path with realistic relative path in alias example Agent-Logs-Url: https://github.com/shopware/docs/sessions/07bcf0eb-b3cc-4003-919c-f691029a5fd0 Co-authored-by: Isengo1989 <8600299+Isengo1989@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Isengo1989 <8600299+Isengo1989@users.noreply.github.com>
1 parent ba83883 commit 7384527

1 file changed

Lines changed: 54 additions & 28 deletions

File tree

guides/plugins/plugins/dependencies/using-npm-dependencies.md

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,83 @@ nav:
77

88
# Adding NPM Dependencies
99

10-
In this guide, you'll learn how to add NPM dependencies to your project.
10+
In this guide, you'll learn how to add NPM dependencies to your plugin.
1111

1212
## Prerequisites
1313

1414
All you need for this guide is a running Shopware 6 instance and full access to both the files and a running plugin. Of course, you'll have to understand JavaScript, but that's a prerequisite for Shopware as a whole and will not be taught as part of this documentation. Further, a basic understanding of Node and NPM is required.
1515

16-
## Video
16+
## Installing an npm package
1717

18-
This guide is also available as a video:
18+
Presuming you have `npm` installed, run `npm init -y` in the `<plugin root>/src/Resources/app/administration/` folder or the `<plugin root>/src/Resources/app/storefront/` folder. This command creates a `package.json` file in the respective folder, depending on the environment you're working in.
1919

20-
<YoutubeRef video="wfBuWdff35c" title="Shopware 6: Your custom NPM dependencies (Developer Tutorial) - YouTube" target="_blank" />
20+
To add a package to the `package.json` file, run the `npm install` command. In this example we will be installing [`missionlog`](https://www.npmjs.com/package/missionlog):
2121

22-
::: warning
23-
This video shows how to resolve the NPM package name as an alias. We recommend resolving all node_modules instead like shown in the code example below.
24-
:::
22+
```bash
23+
npm install missionlog
24+
```
25+
26+
## Administration (Shopware 6.7+ with Vite)
27+
28+
Since Shopware 6.7, the Administration build system has been migrated from Webpack to [Vite](https://vite.dev/). With Vite, you no longer need a custom `webpack.config.js` file to use npm packages. Vite resolves npm packages from your plugin's `node_modules` directory automatically using standard Node.js module resolution.
29+
30+
You can import npm packages directly in your code without any additional build configuration:
31+
32+
```javascript
33+
// <plugin root>/src/Resources/app/administration/src/example-component.js
34+
import { log } from 'missionlog';
35+
36+
// Initializing the logger
37+
log.init({ initializer: 'INFO' }, (level, tag, msg, params) => {
38+
console.log(`${level}: [${tag}] `, msg, ...params);
39+
});
40+
```
2541

26-
## Adding a npm package to the Administration or the Storefront
42+
If you need custom Vite configuration (for example, path aliases), create a `vite.config.mts` file in the `<plugin root>/src/Resources/app/administration/src/` directory (alongside your entry file, e.g., `main.js`). Note that `package.json` stays in `<plugin root>/src/Resources/app/administration/`:
2743

28-
Presuming you have `npm` installed, run `npm init -y` in the `<plugin root>src/Resources/app/administration/` folder or the `<plugin root>src/Resources/app/storefront/` folder. This command creates a `package.json` file in the respective folder, depending on the environment you're working in. To add a package to the `package.json` file simply run the `npm install` command. In this example we will be installing [`missionlog`](https://www.npmjs.com/package/missionlog).
44+
```typescript
45+
// <plugin root>/src/Resources/app/administration/src/vite.config.mts
46+
import { defineConfig } from 'vite';
2947

30-
So in order to install `missionlog`, run `npm install missionlog` in the folder you have created your `package.json` file in.
48+
export default defineConfig({
49+
resolve: {
50+
alias: {
51+
'@my-module': 'src/my-module',
52+
},
53+
},
54+
});
55+
```
3156

32-
## Registering a package in the build system
57+
Build the Administration using:
3358

34-
Shopware's storefront as well as administration is based on the build system [Webpack](https://webpack.js.org/). Webpack is a source file bundler: In essence it bundles all the source files into a single `bundle.js` to be shipped to a browser. So in order to make Webpack aware of the new dependency, we have to register it and give it an alias/pseudonym so that the package can be bundled correctly.
59+
```bash
60+
composer build:js:admin
61+
```
62+
63+
For more information on migrating from Webpack to Vite, see the [Webpack to Vite migration guide](/guides/upgrades-migrations/administration/vite).
64+
65+
## Storefront (Webpack)
3566

36-
To do this, we create a new folder called "build" under either `Resources/app/storefront` or `Resources/app/administration`. In this build folder we create a new file with the name `webpack.config.js`. We thereby make it possible to extend the Webpack configuration of Shopware.
67+
The Storefront build system continues to use [Webpack](https://webpack.js.org/). To make Webpack aware of the npm packages installed in your plugin, create a `webpack.config.js` file in the `<plugin root>/src/Resources/app/storefront/build/` directory:
3768

3869
```javascript
70+
// <plugin root>/src/Resources/app/storefront/build/webpack.config.js
3971
module.exports = (params) => {
40-
return {
41-
resolve: {
72+
return {
73+
resolve: {
4274
modules: [
4375
`${params.basePath}/Resources/app/storefront/node_modules`,
4476
],
45-
}
46-
};
77+
}
78+
};
4779
}
4880
```
4981

50-
Let us take a closer look at the code. In the first line, we export a so-called arrow function. The build system from Shopware calls this function when either the Administration or Storefront is being built.
82+
This tells Webpack to also search for modules in your plugin's `node_modules` folder, in addition to Shopware's own `node_modules`.
5183

52-
Now we add the `node_modules` folder from our extension. `resolve.modules` tells webpack what directories should be searched when resolving modules. By default, the shopware webpack config only considers the `node_modules` folder of the platform. By accessing `params.basePath` we get the absolute path to our extension. We then add the rest of the path to our extensions `node_modules`. Now webpack will also search for modules in our `node_modules` folder.
84+
### Using the dependency in the Storefront
5385

54-
## Using the dependency
55-
56-
Once we have installed all the dependencies and registered the package in the build system, we can use the package in our own code.
86+
Once you have installed all the dependencies and registered the plugin's `node_modules` path in the build system, you can import and use the package in your code:
5787

5888
```javascript
5989
// <plugin root>/src/Resources/app/storefront/src/example.plugin.js
@@ -78,7 +108,7 @@ export default class ExamplePlugin extends PluginBaseClass {
78108
}
79109
```
80110

81-
We import the function log as well as the constants tag via `destructuring` in the specified code and register our above plugin in our main.js file, so it can be loaded by the plugin system.
111+
Register the plugin in your `main.js` file so it can be loaded by the plugin system:
82112

83113
```javascript
84114
// <plugin root>/src/Resources/app/storefront/src/main.js
@@ -90,14 +120,10 @@ PluginManager.register(
90120
);
91121
```
92122

93-
The final step in this process is to build your Storefront or Administration so that your changes are processed by Webpack.
123+
Build the Storefront using:
94124

95125
```bash
96-
# Build the Storefront
97126
./bin/build-storefront.sh
98-
99-
# Build the Administration
100-
./bin/build-administration.sh
101127
```
102128

103129
## Next steps

0 commit comments

Comments
 (0)