Skip to content

Commit

Permalink
feat(build): added rollup, postcss
Browse files Browse the repository at this point in the history
BREAKING CHANGE: new build system (styles separated)
  • Loading branch information
iliyaZelenko committed Apr 5, 2019
1 parent 1b2dfd4 commit d901fd9
Show file tree
Hide file tree
Showing 13 changed files with 1,439 additions and 305 deletions.
14 changes: 14 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'usage',
corejs: {
version: 3,
proposals: true
}
}
]
]
}
35 changes: 35 additions & 0 deletions build/bundlesConfigs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { join } from 'path'
import { DIST_DIR } from './constants'

export default () => [
{
optimize: true,
output: {
file: join(DIST_DIR, 'bundle-umd.js'),
format: 'umd',
esModule: true
}
},
// это отдельная сборка под ES модули
{
output: {
file: join(DIST_DIR, 'bundle-esm.js'),
format: 'esm',
esModule: true
}
},
{
optimize: true,
output: {
file: join(DIST_DIR, 'bundle-cjs.js'),
format: 'cjs'
}
},
{
optimize: true,
output: {
file: join(DIST_DIR, 'bundle-iife.js'),
format: 'iife'
}
}
]
7 changes: 7 additions & 0 deletions build/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { join } from 'path'

export const ROOT_DIR = join(__dirname, '../')
export const SRC_DIR = join(ROOT_DIR, 'src')
export const STYLES_DIR = join(SRC_DIR, 'styles')
export const DIST_DIR = join(ROOT_DIR, 'dist')
export const IS_PRODUCTION = process.env.BUILD === 'production'
48 changes: 48 additions & 0 deletions build/postcss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import postcssPresetEnv from 'postcss-preset-env'
import { join } from 'path'
import { DIST_DIR, STYLES_DIR } from './constants'
import postcss from 'postcss'
import * as fs from 'fs-extra'
import cssnano from 'cssnano'

const stylus = require('stylus')

export const postcssPlugins = [
postcssPresetEnv,
cssnano
]

export async function generateThemesCSS () {
return Promise.all([
generateThemeCSS('themes/bootstrap.styl', 'themes/bootstrap.css'),
generateThemeCSS('themes/material-design.styl', 'themes/material-design.css')
])
}

async function generateThemeCSS (input, output) {
const inputAbsPath = join(STYLES_DIR, input)
const outputAbsPath = join(DIST_DIR, output)

const stylusContent = await fs.readFile(inputAbsPath, 'utf8')

// , { filename: 'nesting.css' }
const css = await new Promise((resolve, reject) => stylus.render(stylusContent, function (err, css) {
if (err) reject(err)

resolve(css)
}))

const result = await postcss(postcssPlugins).process(css, {
from: inputAbsPath,
to: outputAbsPath
// parser: sugarss
})

await fs.ensureFile(outputAbsPath)
await fs.writeFile(outputAbsPath, result.css)

// console.log(outputAbsPath)
// if (result.map) {
// fs.writeFile('dest/app.css.map', result.map, () => true)
// }
}
76 changes: 76 additions & 0 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import commonjs from 'rollup-plugin-commonjs'
import vue from 'rollup-plugin-vue'
import alias from 'rollup-plugin-alias'
import postcss from 'rollup-plugin-postcss'
import resolve from 'rollup-plugin-node-resolve'
import babel from 'rollup-plugin-babel'
import { join } from 'path'
import bundlesConfigs from './bundlesConfigs'
import { DIST_DIR, SRC_DIR, IS_PRODUCTION } from './constants'
import { postcssPlugins, generateThemesCSS } from './postcss'

export default Promise.all(
bundlesConfigs().map(getConfig)
)

generateThemesCSS()

async function getConfig ({
optimize = false,
output: {
file,
format,
name = undefined,
esModule = false
},
plugins = []
}) {
return {
input: join(SRC_DIR, 'main.js'),
output: {
esModule,
file,
format,
exports: 'named',
// используется в umd и в iife
name: 'vueCoolSelect',
globals: {
vue: 'Vue'
}
},
// можно Object.keys(globals)
external: [
'vue'
],
plugins: [
alias({
resolve: ['.ts', '.js', '.vue', '.styl'],
'~': SRC_DIR
}),
commonjs(),
resolve({
customResolveOptions: {
moduleDirectory: 'node_modules'
}
}),
// транспилирует
babel({
exclude: 'node_modules/**',
runtimeHelpers: true
}),
postcss({
// TODO для каждого конфига генерируется свой main.css (одинаковый файл), исправить
extract: join(DIST_DIR, 'main.css'),
// minimize: true, Я использую cssnano самостоятельно (под капотом тоже cssnano)
plugins: postcssPlugins
}),
vue({
// Inject CSS in JavaScript. Setting css: false would extract styles in a .css file.
css: false
}),
// оптимизация
optimize && IS_PRODUCTION && (await import('rollup-plugin-terser')).terser(),
...plugins
]
}
}
File renamed without changes.
9 changes: 5 additions & 4 deletions gh-pages-src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import 'vuetify/dist/vuetify.min.css'
import App from './App'
import router from './router'

import CoolSelect from '~/main'
import { CoolSelectPlugin } from '~/main'
import { getTheme } from './themeHelpers'

const theme = getTheme()

// require(`../dist/themes/${theme}.css`)
import(`../dist/themes/${theme}.css`)

Vue.use(Vuetify)
Vue.use(CoolSelect, {
theme: theme // 'bootstrap' or 'material-design'
})
Vue.use(CoolSelectPlugin)

Vue.use(VueAnalytics, {
id: 'UA-127403551-2',
Expand Down
34 changes: 26 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"scripts": {
"commit": "git-cz",
"serve": "vue-cli-service serve ./gh-pages-src/main.js",
"build": "vue-cli-service build --target lib --name vue-cool-select ./src/main.js",
"build": "rollup --c ./build/rollup.config.js --environment BUILD:production",
"build-watch": "rollup -w -c",
"lint": "vue-cli-service lint",
"lint-show": "vue-cli-service lint --no-fix",
"gp": "vue-cli-service build ./gh-pages-src/main.js --dest=gh-pages-build",
Expand All @@ -16,8 +17,11 @@
"test:unit-coverage": "vue-cli-service test:unit --coverage",
"test:e2e": "vue-cli-service test:e2e"
},
"dependencies": {},
"dependencies": {
"rollup-plugin-babel": "^4.3.2"
},
"devDependencies": {
"@babel/preset-env": "^7.4.3",
"@semantic-release/changelog": "^3.0.2",
"@semantic-release/commit-analyzer": "^6.1.0",
"@semantic-release/git": "^7.0.8",
Expand All @@ -35,12 +39,24 @@
"babel-jest": "^24.1.0",
"clone": "^2.1.2",
"commitizen": "^3.0.7",
"core-js": "3",
"cssnano": "^4.1.10",
"fs-extra": "^7.0.1",
"jest": "^24.1.0",
"postcss-preset-env": "^6.6.0",
"propdoc": "^0.9.3",
"push-dir": "^0.4.1",
"rollup": "^1.8.0",
"rollup-plugin-alias": "^1.5.1",
"rollup-plugin-commonjs": "^9.3.4",
"rollup-plugin-node-resolve": "^4.1.0",
"rollup-plugin-postcss": "^2.0.3",
"rollup-plugin-terser": "^4.0.4",
"rollup-plugin-vue": "^4.7.2",
"semantic-release": "^15.13.3",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"sugarss": "^2.0.0",
"ts-jest": "^24.0.0",
"ts-loader": "^5.3.3",
"tslint": "^5.13.0",
Expand All @@ -50,10 +66,7 @@
"vue-router": "^3.0.1",
"vue-template-compiler": "^2.5.17"
},
"browserslist": [
"> 0.3%",
"not op_mini all"
],
"browserslist": "> 0.5%, last 2 versions, Firefox ESR, not dead",
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
Expand Down Expand Up @@ -83,9 +96,14 @@
"vue": "^2.5.17"
},
"author": "Ilya Zelenko",
"main": "dist/vue-cool-select.umd.min.js",
"main": "dist/bundle-cjs.js",
"module": "dist/bundle-esm.js",
"unpkg": "dist/bundle-umd.js",
"jsdelivr": "dist/bundle-umd.js",
"typings": "types/types.d.ts",
"files": [
"dist/*",
"src/*"
"src/*",
"types/*"
]
}
67 changes: 3 additions & 64 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import '~/styles/main.styl'
// import '~/styles/themes/bootstrap.styl'
// import '~/styles/themes/material-design.styl'
import component from '~/component.vue'
import EventEmitter from '~/eventEmitter'

export default new Singelton()
export const CoolSelectPlugin = new Singelton()
export {
EventEmitter,
component,
Expand All @@ -11,72 +13,9 @@ export {
}

function Singelton () {
const self = this

self.themes = ['bootstrap', 'material-design']
self.currentTheme = null // будет определено в install
self.currentLocale = null // будет определено в install

// public object
return {
install (Vue, options = {}) {
const {
theme: optTheme = 'bootstrap'
} = options

requireTheme(optTheme, self.themes)
},

// themes: ['bootstrap', 'material-design'],

// Стилевые тема
get theme () {
return self.currentTheme
}

// set theme (val) {
// if (this.themes.includes(val)) {
// requireTheme(val)
// self.currentTheme = val
// } else {
// throw new Error('Non-existent theme')
// }
// }
}
}

function requireTheme (theme, themes) {
const errorMessage = `Theme ${theme} is not supported! Available Themes: ${themes.join(', ')}.`

if (!themes.includes(theme)) throw Error(errorMessage)

require(`./styles/themes/${theme}.styl`)
}

// export default class {
// static install (Vue, options) {
// const { optTheme = 'bootsrap' } = options
//
// Vue.prototype.$avatarUploader = this
//
// this.theme = optTheme
// }
//
// static themes = ['bootsrap']
// static currentTheme = null // будет определено в install
//
// static get theme () {
// return this.currentTheme
// }
//
// static set theme (val) {
// if (this.themes.includes(val)) {
// this.requyreTheme(val)
// this.currentTheme = val
// } else {
// throw new Error('Non-existent theme')
// }
// }
//
// requyreTheme
// }
12 changes: 6 additions & 6 deletions src/styles/main.styl
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
.IZ-select
*
box-sizing: border-box;
box-sizing: border-box

.IZ-select__input-wrap
display: flex;
align-items: center;
display: flex
align-items: center

.fade
&-leave-active
position: absolute;
position: absolute

&-enter-active, &-leave, &-leave-to
transition: opacity .2s;
transition: opacity .2s

&-enter, &-leave-to
opacity: 0;
opacity: 0

Loading

0 comments on commit d901fd9

Please sign in to comment.