Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Custom build (#624)
Browse files Browse the repository at this point in the history
* v4.1.0
  • Loading branch information
surmon-china authored Mar 29, 2020
1 parent 910a131 commit 324ebf1
Show file tree
Hide file tree
Showing 17 changed files with 748 additions and 655 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
extends: './node_modules/@surmon-china/abc-factory/preset/eslintrc/vue.typescript',
rules: {
'vue/max-attributes-per-line': [0]
'vue/max-attributes-per-line': 0,
'vue/order-in-components': 0
}
}
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@
# Changelog
All notable changes to this project will be documented in this file.

### [4.1.0] (2020-03-29)

**Update**
- Upgrade abc-factory
- Update test spec
- Vue SFC component to `render`
- Move `types` to `dist`

**Features**
- Support Custom build with Swiper

**Fixed**
- [#422](https://github.com/surmon-china/vue-awesome-swiper/issues/422)
- [#527](https://github.com/surmon-china/vue-awesome-swiper/issues/527)
- [#621](https://github.com/surmon-china/vue-awesome-swiper/issues/621)

### [4.0.4](https://github.com/surmon-china/vue-awesome-swiper/compare/v4.0.3...v4.0.4) (2020-03-22)

**Fixed**
Expand Down
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# vue-awesome-swiper
![vue](https://img.shields.io/badge/MADE%20WITH-VUE-42a97a?style=for-the-badge&labelColor=35495d)
[![GitHub stars](https://img.shields.io/github/stars/surmon-china/vue-awesome-swiper.svg?style=for-the-badge)](https://github.com/surmon-china/vue-awesome-swiper/stargazers)
[![npm](https://img.shields.io/npm/v/vue-awesome-swiper?color=%23c7343a&label=npm&style=for-the-badge)](https://www.npmjs.com/package/vue-awesome-swiper)
[![npm](https://img.shields.io/npm/v/vue-awesome-swiper?color=c7343a&label=npm&style=for-the-badge)](https://www.npmjs.com/package/vue-awesome-swiper)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/surmon-china/vue-awesome-swiper/Publish?label=publish&style=for-the-badge)](https://github.com/surmon-china/vue-awesome-swiper/actions?query=workflow%3APublish)
[![GitHub issues](https://img.shields.io/github/issues-raw/surmon-china/vue-awesome-swiper.svg?style=for-the-badge)](https://github.com/surmon-china/vue-awesome-swiper/issues)
[![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=for-the-badge)](https://github.com/surmon-china/vue-awesome-swiper/blob/master/LICENSE)
Expand Down Expand Up @@ -255,12 +255,41 @@ Swiper's API and configuration can be used.

---

### [Custom Build with Swiper](https://swiperjs.com/api/#custom-build)

```ts
import Vue from 'vue'
import { Swiper as SwiperClass, Pagination, Mousewheel, Autoplay } from 'swiper/js/swiper.esm'
import getAwesomeSwiper from 'vue-awesome-swiper/dist/exporter'

// Swiper modules
SwiperClass.use([Pagination, Mousewheel, Autoplay])

// -------------------------------------------------

// Global use
Vue.use(getAwesomeSwiper(SwiperClass))

// -------------------------------------------------

// Or local component
const { Swiper, SwiperSlide } = getAwesomeSwiper(SwiperClass)
export default {
components: {
Swiper,
SwiperSlide
}
}
```

---

### Custom Swiper plugin

```javascript
import Swiper from 'swiper'
import SwiperClass from 'swiper'

Swiper.use({
SwiperClass.use({
name: 'pluginName',
params: {
pluginSwitch: false,
Expand Down
44 changes: 31 additions & 13 deletions abc.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
module.exports = {
entry: 'src/index.ts',
resolve: ['.vue', '.ts'],
targets: ['umd', 'esm'],
minimize: false,
external: [
'swiper',
'vue',
],
globals: {
swiper: 'Swiper',
vue: 'Vue',
module.exports = [
{
entry: 'src/exporter.ts',
fileName: 'exporter',
targets: ['umd', 'esm'],
minimize: false,
external: ['vue'],
globals: {
vue: 'Vue'
},
typescript: {
tsconfigOverride: {
compilerOptions: {
declaration: false
}
}
}
},
{
entry: 'src/index.ts',
targets: ['umd', 'esm'],
minimize: false,
external: [
'swiper',
'vue',
],
globals: {
swiper: 'Swiper',
vue: 'Vue',
}
}
}
]
17 changes: 7 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "vue-awesome-swiper",
"description": "Swiper component for Vue",
"version": "4.0.4",
"version": "4.1.0",
"keywords": [
"vue-awesome-swiper",
"vue awesome swiper",
"vue swiper",
"vue awesome swiper",
"vue carrousel",
"carrousel",
"swiper"
Expand Down Expand Up @@ -34,26 +33,24 @@
},
"files": [
"src",
"dist/*.js",
"types/*.d.ts"
"dist"
],
"types": "types/index.d.ts",
"types": "dist/index.d.ts",
"scripts": {
"cleanup": "rm -rf ./dist/* && rm -rf ./types/*",
"cleanup": "rm -rf ./dist/*",
"build": "npm run cleanup && cross-env NODE_ENV=production abc build",
"lint": "abc lint --ext .ts,.vue src tests",
"test": "abc test",
"test:watch": "abc test --watch -i",
"rebirth": "npm run lint && npm test && npm run build",
"ver": ". ./scripts/version.sh",
"release": ". ./scripts/release.sh"
},
"peerDependencies": {
"swiper": "^5.2.0",
"vue": "^2.6.10"
"vue": "2.x"
},
"devDependencies": {
"@surmon-china/abc-factory": "^0.3.0",
"@surmon-china/abc-factory": "^0.3.3",
"@types/swiper": "^5.2.1",
"cross-env": "^6.0.3",
"swiper": "^5.2.0",
Expand Down
7 changes: 6 additions & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ set -e

PKG_VERSION=$(jq -r '.version' package.json)

git fetch origin v"$PKG_VERSION" || git push --follow-tags origin master
git fetch origin v"$PKG_VERSION" || {
type standard-version || npm i -g standard-version
git commit -am "$PKG_VERSION"
standard-version --skip.changelog --skip.commit -a --release-as "$PKG_VERSION"
git push --follow-tags origin master
}
10 changes: 0 additions & 10 deletions scripts/version.sh

This file was deleted.

10 changes: 6 additions & 4 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

import { CommonEvent } from 'swiper'

export const SWIPER_COMPONENT_NAME = 'Swiper'
export const SWIPER_SLIDE_COMPONENT_NAME = 'SwiperSlide'
export const SWIPER_DIRECTIVE_NAME = 'swiper'
export const SWIPER_INSTANCE_NAME = '$swiper'
export enum CoreNames {
SwiperComponent = 'Swiper',
SwiperSlideComponent = 'SwiperSlide',
SwiperDirective = 'swiper',
SwiperInstance = '$swiper'
}

export const DEFAULT_CLASSES = Object.freeze({
containerClass: 'swiper-container',
Expand Down
9 changes: 4 additions & 5 deletions src/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import { DirectiveOptions, VNode } from 'vue'
import { DirectiveBinding } from 'vue/types/options'
import Swiper, { SwiperOptions } from 'swiper'
import { SWIPER_INSTANCE_NAME, DEFAULT_CLASSES, ComponentEvents, ComponentPropNames } from './constants'
import { DEFAULT_CLASSES, CoreNames, ComponentEvents, ComponentPropNames } from './constants'
import { handleClickSlideEvent, bindSwiperEvents } from './event'
import { kebabcase } from './utils'

const INSTANCE_NAME_KEY = 'instanceName'

export default getDirectiveByOptions()
export function getDirectiveByOptions (globalOptions?: SwiperOptions): DirectiveOptions {
export default function getDirective(SwiperClass: typeof Swiper, globalOptions?: SwiperOptions): DirectiveOptions {

const getStandardisedOptionByAttrs = (vnode: VNode, key: string): any => {
const value = vnode.data?.attrs?.[key]
Expand All @@ -29,7 +28,7 @@ export function getDirectiveByOptions (globalOptions?: SwiperOptions): Directive
binding.arg ||
getStandardisedOptionByAttrs(vnode, INSTANCE_NAME_KEY) ||
element.id ||
SWIPER_INSTANCE_NAME
CoreNames.SwiperInstance
)
}

Expand Down Expand Up @@ -83,7 +82,7 @@ export function getDirectiveByOptions (globalOptions?: SwiperOptions): Directive

// Swiper will destroy but not delete instance, when used <keep-alive>
if (!swiper || (swiper as any).destroyed) {
swiper = new Swiper(element, swiperOptions)
swiper = new SwiperClass(element, swiperOptions)
vueContext[instanceName] = swiper
bindSwiperEvents(swiper, emitEvent)
emitEvent(ComponentEvents.Ready, swiper)
Expand Down
43 changes: 43 additions & 0 deletions src/exporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file vue-awesome-swiper
* @module exporter
* @author Surmon <https://github.com/surmon-china>
*/

import Swiper, { SwiperOptions } from 'swiper'
import _Vue, { PluginFunction } from 'vue'
import { CoreNames } from './constants'
import getDirective from './directive'
import getSwiperComponent from './swiper'
import SwiperSlideComponent from './slide'

export interface InstallFunction extends PluginFunction<SwiperOptions> {
installed?: boolean
}

const getInstaller = (SwiperClass: typeof Swiper) => {
const install: InstallFunction = (Vue: typeof _Vue, globalOptions?: SwiperOptions) => {
if (install.installed) return

const SwiperComponent = getSwiperComponent(SwiperClass)
if (globalOptions) {
(SwiperComponent as any).options.props.defaultOptions.default = () => globalOptions
}

Vue.component(CoreNames.SwiperComponent, SwiperComponent)
Vue.component(CoreNames.SwiperSlideComponent, SwiperSlideComponent)
Vue.directive(CoreNames.SwiperDirective, getDirective(SwiperClass, globalOptions))
install.installed = true
}
return install
}

export default function exporter(SwiperClass: typeof Swiper) {
return {
version: 'PACKAGE_VERSION',
install: getInstaller(SwiperClass),
directive: getDirective(SwiperClass),
[CoreNames.SwiperComponent as const]: getSwiperComponent(SwiperClass),
[CoreNames.SwiperSlideComponent as const]: SwiperSlideComponent
}
}
58 changes: 17 additions & 41 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
/**
* @file vue-awesome-swiper
* @module entry
* @author Surmon <https://github.com/surmon-china>
*/

import { SwiperOptions } from 'swiper'
import _Vue, { PluginFunction } from 'vue'
import { SWIPER_DIRECTIVE_NAME } from './constants'
import SwiperDirective, { getDirectiveByOptions } from './directive'
import SwiperComponent from './swiper.vue'
import SwiperSlideComponent from './slide.vue'
import { SWIPER_COMPONENT_NAME, SWIPER_SLIDE_COMPONENT_NAME } from './constants'

interface InstallFunction extends PluginFunction<SwiperOptions> {
installed?: boolean
}

export const install: InstallFunction = (Vue: typeof _Vue, globalOptions?: SwiperOptions) => {
if (install.installed) return
install.installed = true

if (globalOptions) {
(SwiperComponent as any).options.props.defaultOptions.default = () => globalOptions
}

Vue.component(SWIPER_COMPONENT_NAME, SwiperComponent)
Vue.component(SWIPER_SLIDE_COMPONENT_NAME, SwiperSlideComponent)
Vue.directive(SWIPER_DIRECTIVE_NAME, getDirectiveByOptions(globalOptions))
}

export const Swiper = SwiperComponent
export const SwiperSlide = SwiperSlideComponent
export const directive = SwiperDirective

export default {
install,
directive,
Swiper: SwiperComponent,
SwiperSlide: SwiperSlideComponent
}
/**
* @file vue-awesome-swiper
* @module default-export
* @author Surmon <https://github.com/surmon-china>
*/

import SwiperClass from 'swiper'
import exporter from './exporter'

const VueAwesomeSwiper = exporter(SwiperClass)

export const version = VueAwesomeSwiper.version
export const install = VueAwesomeSwiper.install
export const directive = VueAwesomeSwiper.directive
export const Swiper = VueAwesomeSwiper.Swiper
export const SwiperSlide = VueAwesomeSwiper.SwiperSlide
export default VueAwesomeSwiper
37 changes: 37 additions & 0 deletions src/slide.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file vue-awesome-swiper
* @module SwiperSlideComponent
* @author Surmon <https://github.com/surmon-china>
*/

import Vue, { VNode, CreateElement } from 'vue'
import { CoreNames, DEFAULT_CLASSES } from './constants'

export default Vue.extend({
name: CoreNames.SwiperSlideComponent,
computed: {
slideClass(): string {
return (this.$parent as any)?.swiperOptions?.slideClass || DEFAULT_CLASSES.slideClass
}
},
methods: {
update() {
(this.$parent as any)?.swiperInstance?.update()
}
},
mounted() {
this.update()
},
updated() {
this.update()
},
render(createElement: CreateElement): VNode {
return createElement(
'div',
{
class: this.slideClass
},
this.$slots.default
)
}
})
Loading

0 comments on commit 324ebf1

Please # to comment.