Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arillo/silverstripe-gulp-plate
Boilerplate / starting-point to create a Silverstripe production theme using a gulp.js workflow.
https://github.com/arillo/silverstripe-gulp-plate
boilerplate-theme css gulp javascript sass silverstripe webpack
Last synced: 2 months ago
JSON representation
Boilerplate / starting-point to create a Silverstripe production theme using a gulp.js workflow.
- Host: GitHub
- URL: https://github.com/arillo/silverstripe-gulp-plate
- Owner: arillo
- License: mit
- Created: 2015-05-28T10:32:12.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T19:00:55.000Z (about 2 years ago)
- Last Synced: 2024-04-15T00:07:38.126Z (9 months ago)
- Topics: boilerplate-theme, css, gulp, javascript, sass, silverstripe, webpack
- Language: CSS
- Homepage:
- Size: 4.04 MB
- Stars: 9
- Watchers: 12
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Silverstripe Gulp-plate
Boilerplate / starting-point to create a [SilverStripe](https://github.com/silverstripe) production theme using a [gulp.js](http://gulpjs.com/) workflow.
Includes the following tools, tasks, and work-flows:
- [Webpack](https://webpack.js.org/) as JavaScript module bundler
- [webpack-dev-middleware](https://github.com/webpack/webpack-dev-middleware) for live reloading (in memory compilation for faster rebuilds while developing)
- [ES2015](http://www.ecma-international.org/ecma-262/6.0/) syntax transpiled with [Babel](https://babeljs.io/)
- [ESLint](http://eslint.org/) for JavaScript linting
- [Prettier](https://prettier.io/) for JavaScript code formatting
- [SASS](http://sass-lang.com/) compiled with libsass, [source maps](https://github.com/sindresorhus/gulp-ruby-sass#sourcemap), [autoprefixer](https://github.com/sindresorhus/gulp-autoprefixer) and [Sass linting](https://github.com/sasstools/sass-lint)
- [BrowserSync](http://browsersync.io) for live reloading and static server
- [svgo](https://github.com/svg/svgo) for SVG compression.
- [gulp-svg-symbols](https://github.com/Hiswe/gulp-svg-symbols) to generate a SVG icon sprite with `` & `` tagsLooking for the static version? [Look here](https://github.com/arillo/gulp-plate).
## Dependencies / Installation
Gulp-plate depends on the following technologies:
- [node.js](http://nodejs.org) as local host environment for gulp (v. 7.5.0 or higher) [1]
- [gulp](http://gulpjs.com/) as task-runner
- [yarn](https://yarnpkg.com) as dependency manager[1] It is recommended to install node trough [nvm](https://github.com/creationix/nvm) (Node Version Manager).
To get started:
```bash
# In myProject `themes` folder
$ git clone https://github.com/arillo/gulp-plate source_myTheme
$ cd source_myTheme
$ rm -r .git # Remove the link to the git repo
$ yarn # Install dependencies
```**Important:**
Prefix your theme name with `source_`. For a source folder named `source_myTheme` a production theme with the name `myTheme` will be created. After compiling your folder structure should look like this:
```bash
myProject
#...
themes/
myTheme/ # Put this on the server after running the prod task. Should be ignored in `.gitignore`
source_myTheme/ # Holds your source files and gulp tasks
```## Commands
```bash
$ yarn run build
```Run the default task and generate a dev version of the site in the `dist` folder.
```bash
# Equivalent
$ yarn start
$ yarn run watch
```Run the default task once, start a server and watch for file changes. See below to learn how to customize the proxy.
```bash
$ yarn run prod
```Set `NODE_ENV='production'` and generate a production version of the site by compressing js, css & html. This is the folder that should go on the server.
If you want to run any other gulp task just append the task name to the build /gulp command:
```bash
# Equivalent
$ yarn run build sprite
$ yarn run b sprite
$ yarn run gulp sprite
$ yarn run g sprite
```**Important:**
Every time you run build / watch / prod the generated theme directory will be deleted. Don't make any changes in that directory.
## Folder structure
```bash
source_myTheme/
gulpfile.js/ # gulp tasks
src/
icons/ # SVG files to be included in he the sprite
images/ # other images
js/ # js code
sass/ # Sass code, SCSS and Sass indented syntax possible
templates/ # Silverstripe templates
```## Configuration
All paths and plugin settings have been abstracted into a centralized file: `./gulpfile.js/config.js`. Adapt the paths and settings to the structure and needs of your project.
## Setting the proxy domain for browserSync
By default browserSync will use the proxy domain `php7.test` to modify the domain you can create a file named `config-local.js`. The file should export a object containing a `proxy` key with your local domain as a value:
```js
// ./gulpfile.js/config-local.js
module.exports = {
proxy: 'local-domain.dev',
};
```## Symlink theme folders (SilverStripe 4)
Theme folders need to be symlinked in the `public/resources/themes//` folder for the theme to work, run:
```
cd public/resources/themes//
```and link each folder in the theme (replace `css` with the folder name you want to link):
```
ln -s ../../../../themes//css css
```## SVG Sprite configuration
The sprite creates an image with the name `sprite.svg` in `./dist/images/`. It also creates a Sass file named: `_sprite.scss` in `./src/sass/base/`.
The generated Sass files contains useful information about the sprite icons like the dimensions of each icon. The file will change every time an icon is added, removed or changed, do not edit it manually. You can change the file by changing the template in `./gulpfile.js/tpl/_sprite.scss`.
## Static assets
To move static assets from the source directory without transformations, e.g. font files. Add the `src` and `dest` paths to the `static` array in the `config.js`
## Sass
Sass indented syntax is used by default. The main Sass files need to have a `.sass` extension, otherwise the compiler fails. Partials can be both `.sass` and `.scss`.
### Include external vendor css files
To include third-party styles in your css use include them in the `main.sass` file:
```sass
// main.sass@import url('../../node_modules/normalize.css/normalize.css');
```A postcss plugin will then inline the files preserving the source-maps. After the Sass compilation.
Beware that Sass will move `@import url(...)` statements to the top of the generated CSS file, so independently of the place of inclusion these styles will always be included at the top of the file.
### Sass-lint errors
At the time of writing `sass-lint` fails when it encounters empty selectors. This is a [bug](https://github.com/sasstools/sass-lint/issues/456), it can be prevented by adding a indented comment `//` after the empty selector:
```sass
.mySelector
//.mySelector_child
text-align: center
```## JavaScript
The `./gulpfile.js/config.js` file contains the full webpack configuration (see the `js` variable). Feel free to alter is as needed. Keep in mind that the `babel-loader` should always be present as `eslint` will rely on it.
There configuration will be slightly altered depending on the task you are running. When using the watch task, Javascript compilation will happen in memory, so no files are written to disk (`./dist/js/` will be empty) and `webpack-hot-middleware/client` will be injected in all bundles for live reloading to work. When building for production `webpack.optimize.UglifyJsPlugin` is used for minification. Take a look at `./gulpfile.js/util/getWebpackConfig.js` to see exactly what is happening and change it as needed.
Here are some useful recipes to get you up and running:
### Declare aliases for frequently required files
```js
// gulpfile.js/config.jsconst js = {
resolve: {
extensions: ['.js'],
alias: {
// Path relative to `context`
myModule: './myModule/myModule.js',
},
},
};
``````js
// src/js/some-file.jsimport myModule from 'myModule';
myModule();
```Docs: https://webpack.js.org/configuration/resolve/#resolve-alias
### Shimming non CommonJs modules
#### jQuery plugin
```js
// gulpfile.js/config.jsconst webpack = require('webpack');
//...
const js = {
plugins: [
// Make jQuery global, expected by the plugin.
new webpack.ProvidePlugin({
'window.jQuery': 'jquery',
}),
],
//...
resolve: {
// Add extensions to prevent linting errors.
extensions: ['.js', '.json'],
// Path from `node_modules`, where `myModule` is the module name.
alias: {
myModule: 'myModule/dist/myModule.js',
},
},
};
``````js
// src/js/main.jsimport $ from 'jquery';
import 'myModule';$('.js-selector').myModule();
```#### Regular JavaScript module
```js
// gulpfile.js/config.jsconst js = {
//...
resolve: {
// Add extensions to prevent linting errors.
extensions: ['.js', '.json'],
// Path from `node_modules`, where `myModule` is the module name.
alias: {
myModule: 'myModule/dist/myModule.js',
},
},
module: {
rules: [
// ...
{
include: require.resolve('myModule/dist/myModule.js'),
loader: 'exports-loader?MyModule',
},
],
},
};
``````js
// src/js/main.jsimport $ from 'jquery';
import MyModule from 'myModule';const myInstance = new MyModule();
```Docs: https://webpack.js.org/guides/shimming/
### Multiple JavaScript bundles & vendor code sharing
To create multiple bundles add entires to `entry`
```js
// gulpfile.js/config.jsconst js = {
// ...
entry: {
main: ['./main.js'],
other: ['./someFile.js', './sotherOtherFile.js'],
},
// ...
};
```This will generate two bundles: `main.js` & `other.js`.
If you do this it is probably a good idea to generate another bundle that contains all shared vendor code:
```js
// gulpfile.js/config.jsconst webpack = require('webpack');
//...
const js = {
// ...
entry: {
main: ['./main.js'],
other: ['./someFile.js', './sotherOtherFile.js'],
// List vendor modules here:
vendor: ['jquery', 'svg4everybody'],
},
// ...
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor', // Specify the common bundle's name
}),
],
// ...
};
```Docs: https://webpack.js.org/guides/code-splitting-libraries/
## Roadmap
- [ ] Research Tree-shaking with webpack and implement if possible
## Credits
Gulp-plate is based on https://github.com/greypants/gulp-starter