Spryker Suite Frontend Builder
This tool is needed to build the frontend for Spryker Suite projects.
It's based on webpack
4, sass
and typescript
.
Requirements
Setup
Add this as a dev dependency in your package.json
:
npm install --save-dev @spryker/suite-frontend-builder
Usage in Spryker Suite
1. Add project folder
Create/update the folder ./src/Pyz/Yves/ShopUi/Theme/default
by adding these files:
import { bootstrap } from 'ShopUi/app';
bootstrap();
import '@webcomponents/webcomponentsjs/webcomponents-bundle';
import 'core-js/fn/promise';
import 'core-js/fn/array';
import 'core-js/fn/set';
import 'core-js/fn/map';
const hasNativeCustomElements = !!window.customElements;
if (hasNativeCustomElements) {
import('@webcomponents/webcomponentsjs/custom-elements-es5-adapter');
}
Create/update the folder ./src/Pyz/Yves/ShopUi/Theme/default/styles
by adding these files:
@import '~ShopUi/styles/basic';
@include basic-reset;
@include basic-typography;
@include basic-grid;
@include basic-animation;
@import '~ShopUi/styles/shared';
@import '~ShopUi/styles/util';
@include util-spacing;
@include util-text;
@include util-float;
@include util-visibility;
2. Add build folder
Create a folder ./frontend
and add these files:
const path = require('path');
const name = 'yves_default';
const theme = 'default';
const context = process.cwd();
const paths = {
public: './public/Yves/assets',
core: {
modules: './vendor/spryker-shop',
shopUiModule: `./vendor/spryker-shop/shop-ui/src/SprykerShop/Yves/ShopUi/Theme/${theme}`
},
project: {
modules: './src/Pyz/Yves',
shopUiModule: `./src/Pyz/Yves/ShopUi/Theme/${theme}`
}
};
module.exports = {
name,
theme,
context,
paths,
find: {
componentEntryPoints: {
dirs: [
path.join(context, paths.core.modules),
path.join(context, paths.project.modules)
],
patterns: [
`**/Theme/${theme}/components/atoms/*/index.ts`,
`**/Theme/${theme}/components/molecules/*/index.ts`,
`**/Theme/${theme}/components/organisms/*/index.ts`,
`**/Theme/${theme}/templates/*/index.ts`,
`**/Theme/${theme}/views/*/index.ts`,
'!config',
'!data',
'!deploy',
'!node_modules',
'!public',
'!test'
]
},
componentStyles: {
dirs: [
path.join(context, paths.core.modules)
],
patterns: [
`**/Theme/${theme}/components/atoms/*/*.scss`,
`**/Theme/${theme}/components/molecules/*/*.scss`,
`**/Theme/${theme}/components/organisms/*/*.scss`,
`**/Theme/${theme}/templates/*/*.scss`,
`**/Theme/${theme}/views/*/*.scss`,
`!**/Theme/${theme}/**/style.scss`,
'!config',
'!data',
'!deploy',
'!node_modules',
'!public',
'!test'
]
}
}
}
const builder = require('@spryker/suite-frontend-builder');
const settings = require('./settings');
const [mode] = process.argv.slice(2);
require('./config/development');
builder.build(settings, mode);
3. Update package.json
Update your package.json
script section adding the following:
"scripts": {
"yves": "node ./frontend/build development",
"yves:watch": "node ./frontend/build development-watch",
"yves:production": "node ./frontend/build production",
...
}
4. Update tsconfig.json
Update your tsconfig.json
file as following:
{
"extends": "./node_modules/@spryker/suite-frontend-builder/tsconfig.suite",
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [
"*"
],
"ShopUi/*": [
"./vendor/spryker-shop/shop-ui/src/SprykerShop/Yves/ShopUi/Theme/default/*"
]
}
},
"files": [
"./src/Pyz/Yves/ShopUi/Theme/default/app.ts",
"./src/Pyz/Yves/ShopUi/Theme/default/vendor.ts",
"./src/Pyz/Yves/ShopUi/Theme/default/es6-polyfill.ts"
],
"include": [
"./vendor/spryker-shop/**/*",
"./src/Pyz/Yves/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
tsconfig.json
is now extending the basic configuration provided by @spryker/suite-frontend-builder
and pointing to project src
folder for entry points.
Extend webpack config
This tool offers 4 configurations:
- base: empty
- development: extends base;
- development-watch extends development and add watchers;
- production: extends development and change setting to optimise the build/output.
Might happen that you need more from webpack.
Here an example on how to extend one of the configurations.
Adding copy-webpack-plugin
to the development configuration
Add your dep to package.json
:
"devDependencies": {
"@spryker/suite-frontend-builder": "^0.3.0",
"copy-webpack-plugin": "~4.5.1",
...
}
Create the folder ./frontend/config
and add this file:
const path = require('path');
const builder = require('@spryker/suite-frontend-builder');
const CopyWebpackPlugin = require('copy-webpack-plugin');
builder.factory.register('development', () => class extends builder.factory.default.Development {
getCleanWebpackPluginPaths() {
const paths = super.getCleanWebpackPluginPaths();
return [
...paths,
'images',
'fonts'
]
}
getCopyWebpackPluginPatterns() {
return [
{
from: './frontend/assets/images/*',
to: './images/[name].[ext]',
ignore: ['*.gitkeep']
}, {
from: './frontend/assets/fonts/*',
to: './fonts/[name].[ext]',
ignore: ['*.gitkeep']
}
]
}
getCopyWebpackPluginOptions() {
return {
context: this.settings.context
}
}
create() {
const config = super.create();
return {
...config,
plugins: [
...config.plugins,
new CopyWebpackPlugin(this.getCopyWebpackPluginPatterns(), this.getCopyWebpackPluginOptions())
]
}
}
})