Skip to content

Commit

Permalink
Update README: add usage examples, refactor build system
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Banduristov committed May 20, 2022
1 parent 786e83d commit 637f05f
Show file tree
Hide file tree
Showing 9 changed files with 1,335 additions and 191 deletions.
3 changes: 3 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
current node
last 2 versions and > 2%
ie > 10
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Nice and clean Vue component to display message about cookies

`yarn add @norvikit/vue-cookies-consent`

## 🚀 Usage
## 🚀 Usage in Vue Components

```vue
<template>
Expand All @@ -39,6 +39,31 @@ Nice and clean Vue component to display message about cookies
</script>
```

## 🚀 Usage throughout the Vue project
Add this content to main.js
```js
import Vue from "vue";
import VueCookiesConsent from "@norvikit/vue-cookies-consent";
Vue.use(VueCookiesConsent)
```

## 🚀 Usage in Nuxt.js
Create a file `plugins/vue-cookies-consent.js` with the following content:
```js
import Vue from "vue";
import VueCookiesConsent from "@norvikit/vue-cookies-consent";
Vue.use(VueCookiesConsent)
```
Then add plugin to `nuxt.config.js`

```js
plugins: [
{
src:"plugins/vue-cookies-consent.js", mode:'client'
}
]
```

## 🔧 Props

| Prop | Type | Description | Example |
Expand Down
15 changes: 14 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
const devPresets = ["@vue/babel-preset-app"];
const buildPresets = [
[
"@babel/preset-env",
// Config for @babel/preset-env
{
// Example: Always transpile optional chaining/nullish coalescing
// include: [
// /(optional-chaining|nullish-coalescing)/
// ],
},
],
];
module.exports = {
presets: ["@vue/cli-plugin-babel/preset"],
presets: process.env.NODE_ENV === "development" ? devPresets : buildPresets,
};
175 changes: 175 additions & 0 deletions build/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
// rollup.config.js
import fs from "fs";
import path from "path";
import vue from "rollup-plugin-vue";
import alias from "@rollup/plugin-alias";
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import babel from "@rollup/plugin-babel";
import { terser } from "rollup-plugin-terser";
import minimist from "minimist";

// Get browserslist config and remove ie from es build targets
const esbrowserslist = fs
.readFileSync("./.browserslistrc")
.toString()
.split("\n")
.filter((entry) => entry && entry.substring(0, 2) !== "ie");

// Extract babel preset-env config, to combine with esbrowserslist
const babelPresetEnvConfig = require("../babel.config").presets.filter(
(entry) => entry[0] === "@babel/preset-env"
)[0][1];

const argv = minimist(process.argv.slice(2));

const projectRoot = path.resolve(__dirname, "..");

const baseConfig = {
input: "src/entry.js",
plugins: {
preVue: [
alias({
entries: [
{
find: "@",
replacement: `${path.resolve(projectRoot, "src")}`,
},
],
}),
],
replace: {
"process.env.NODE_ENV": JSON.stringify("production"),
},
vue: {
css: true,
template: {
isProduction: true,
},
},
postVue: [
resolve({
extensions: [".js", ".jsx", ".ts", ".tsx", ".vue"],
}),
commonjs(),
],
babel: {
exclude: "node_modules/**",
extensions: [".js", ".jsx", ".ts", ".tsx", ".vue"],
babelHelpers: "bundled",
},
},
};

// ESM/UMD/IIFE shared settings: externals
// Refer to https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
const external = [
// list external dependencies, exactly the way it is written in the import statement.
// eg. 'jquery'
"vue",
"js-cookies",
];

// UMD/IIFE shared settings: output.globals
// Refer to https://rollupjs.org/guide/en#output-globals for details
const globals = {
// Provide global variable names to replace your external imports
// eg. jquery: '$'
vue: "Vue",
"js-cookies": "Cookies",
};

// Customize configs for individual targets
const buildFormats = [];
if (!argv.format || argv.format === "es") {
const esConfig = {
...baseConfig,
input: "src/entry.esm.js",
external,
output: {
file: "dist/vue-cookies-consent.esm.js",
format: "esm",
exports: "named",
},
plugins: [
replace(baseConfig.plugins.replace),
...baseConfig.plugins.preVue,
vue(baseConfig.plugins.vue),
...baseConfig.plugins.postVue,
babel({
...baseConfig.plugins.babel,
presets: [
[
"@babel/preset-env",
{
...babelPresetEnvConfig,
targets: esbrowserslist,
},
],
],
}),
],
};
buildFormats.push(esConfig);
}

if (!argv.format || argv.format === "cjs") {
const umdConfig = {
...baseConfig,
external,
output: {
compact: true,
file: "dist/vue-cookies-consent.ssr.js",
format: "cjs",
name: "VueCookiesConsent",
exports: "auto",
globals,
},
plugins: [
replace(baseConfig.plugins.replace),
...baseConfig.plugins.preVue,
vue({
...baseConfig.plugins.vue,
template: {
...baseConfig.plugins.vue.template,
optimizeSSR: true,
},
}),
...baseConfig.plugins.postVue,
babel(baseConfig.plugins.babel),
],
};
buildFormats.push(umdConfig);
}

if (!argv.format || argv.format === "iife") {
const unpkgConfig = {
...baseConfig,
external,
output: {
compact: true,
file: "dist/vue-cookies-consent.min.js",
format: "iife",
name: "VueCookiesConsent",
exports: "auto",
globals,
},
plugins: [
replace(baseConfig.plugins.replace),
...baseConfig.plugins.preVue,
vue(baseConfig.plugins.vue),
...baseConfig.plugins.postVue,
babel(baseConfig.plugins.babel),
terser({
output: {
ecma: 5,
},
}),
],
};
buildFormats.push(unpkgConfig);
}

// Export config
export default buildFormats;
61 changes: 41 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
{
"name": "@norvikit/vue-cookies-consent",
"description": "Nice and clean Vue component to display message about cookies",
"version": "1.0.2",
"version": "1.0.10",
"private": false,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"build:lib": "cross-env NODE_ENV=production rollup --config build/rollup.config.js",
"build:ssr": "cross-env NODE_ENV=production rollup --config build/rollup.config.js --format cjs",
"build:es": "cross-env NODE_ENV=production rollup --config build/rollup.config.js --format es",
"build:unpkg": "cross-env NODE_ENV=production rollup --config build/rollup.config.js --format iife",
"lint": "vue-cli-service lint"
},
"license": "MIT",
"main": "dist/vueCookiesConsent.umd.min.js",
"main": "dist/vue-cookies-consent.ssr.js",
"browser": "dist/vue-cookies-consent.esm.js",
"module": "dist/vue-cookies-consent.esm.js",
"unpkg": "dist/vue-cookies-consent.min.js",
"files": [
"dist/*",
"src/**/*.vue"
],
"repository": {
"type": "git",
"url": "https://github.com/NorvikIT/vue-cookies-consent.git"
Expand All @@ -19,36 +35,46 @@
"vue component",
"cookies"
],
"author": "norvikit",
"files": [
"dist"
],
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"build:lib": "vue-cli-service build --target lib --name vueCookiesConsent ./src/components/VueCookiesConsent.vue",
"lint": "vue-cli-service lint"
},
"author": "Vladimir Banduristov",
"dependencies": {
"core-js": "^3.8.3",
"js-cookie": "^3.0.1",
"vue": "^2.6.14"
},
"devDependencies": {
"@babel/core": "^7.12.16",
"@babel/core": "^7.14.6",
"@babel/eslint-parser": "^7.12.16",
"@vue/cli-plugin-babel": "~5.0.0",
"@babel/preset-env": "^7.14.7",
"@rollup/plugin-alias": "^3.1.9",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-replace": "^2.4.2",
"@vue/cli-plugin-babel": "^5.0.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-service": "~5.0.0",
"@vue/compiler-sfc": "^3.2.34",
"autoprefixer": "^10.4.7",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^8.0.3",
"lint-staged": "^11.1.2",
"minimist": "^1.2.5",
"prettier": "^2.4.1",
"rollup": "^2.52.8",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-vue": "^5.1.9",
"sass": "^1.32.7",
"sass-loader": "^12.0.0",
"vue-template-compiler": "^2.6.14"
"vue-template-compiler": "^2.6.14",
"cross-env": "^7.0.3"
},
"peerDependencies": {
"vue": "^2.6.14"
},
"engines": {
"node": ">=12"
},
"eslintConfig": {
"root": true,
Expand All @@ -65,11 +91,6 @@
},
"rules": {}
},
"browserslist": [
"> 1%",
"last 2 versions",
"not dead"
],
"gitHooks": {
"pre-commit": "lint-staged"
},
Expand Down
Loading

0 comments on commit 637f05f

Please sign in to comment.