Skip to content

Commit

Permalink
Update/modernize npm packages and fix small errors in build script
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Barfurth committed Nov 13, 2020
1 parent 60cb862 commit 47f9ea3
Show file tree
Hide file tree
Showing 8 changed files with 5,575 additions and 5,235 deletions.
2 changes: 1 addition & 1 deletion .babelrc
@@ -1,3 +1,3 @@
{
"presets": ["env"]
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]
}
6 changes: 5 additions & 1 deletion .eslintrc.json
@@ -1,9 +1,13 @@
{
"root": true,
"env": {
"browser": true,
"es6": true,
"node": true
},
"extends": ["prettier"],
"parser": "babel-eslint"
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020
}
}
29 changes: 16 additions & 13 deletions package.json
Expand Up @@ -17,7 +17,8 @@
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs",
"build": "rimraf -r dist && rollup -c",
"deploy": "yarn build && npm publish"
"deploy": "yarn build && npm publish",
"format": "prettier --write ./src/**/*.js"
},
"keywords": [
"javascript",
Expand All @@ -35,29 +36,31 @@
"homepage": "https://github.com/imbrn/v8n#readme",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.5",
"babel-plugin-external-helpers": "^6.22.0",
"babel-preset-env": "^1.7.0",
"eslint": "^5.16.0",
"eslint-config-prettier": "^2.9.0",
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@rollup/plugin-buble": "^0.21.3",
"babel-jest": "^26.6.3",
"eslint": "^7.13.0",
"eslint-config-prettier": "^6.15.0",
"husky": ">=4",
"jest": "^23.1.0",
"jest": "^26.6.3",
"lint-staged": ">=10",
"prettier": "^1.19.1",
"rimraf": "^2.7.1",
"rollup": "^0.62.0",
"rollup-plugin-babel": "^3.0.5",
"rollup-plugin-uglify": "^4.0.0",
"vuepress": "^0.12.0"
"rollup": "^2.33.1",
"rollup-plugin-terser": "^7.0.2",
"vuepress": "^1.7.1"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.js": [
"eslint --cache --fix",
"jest --bail --findRelatedTests"
],
"*.{js,css,md}": "prettier --write"
}
}
103 changes: 34 additions & 69 deletions rollup.config.js
@@ -1,89 +1,54 @@
import project from './package.json';
import babel from 'rollup-plugin-babel';
import { uglify } from 'rollup-plugin-uglify';
import buble from '@rollup/plugin-buble';
import { terser } from 'rollup-plugin-terser';

function buildBabelConfig() {
return {
babelrc: false,
presets: [
[
'env',
{
modules: false,
exclude: ['transform-es2015-typeof-symbol'],
},
],
],
plugins: ['external-helpers'],
};
}
function buildConfig({ dest, format, transpile = true }) {
// Enforces the '.min.js' naming standard
const minify = dest.endsWith('.min.js');

function buildConfigBuilder({ name, input, dist = 'dist' }) {
return ({
filename,
format,
transpiled = true,
minified = false,
sourceMap = false,
}) => {
function buildPlugins() {
const plugins = [];
if (transpiled) plugins.push(babel(buildBabelConfig()));
if (minified) plugins.push(uglify());
return plugins;
}
const plugins = [];
if (transpile) plugins.push(buble());
if (minify) plugins.push(terser());

return {
input: input,
output: {
name,
format,
file: filename,
dir: dist,
sourcemap: sourceMap,
},
plugins: buildPlugins(),
};
return {
input: './src/v8n.js',
output: {
name: project.name,
format,
file: './dist/' + dest,
sourcemap: minify,
exports: 'default',
},
plugins,
};
}

const buildConfig = buildConfigBuilder({
name: project.name,
input: './src/v8n.js',
});

const configs = [
// AMD″
buildConfig({ filename: 'v8n.amd.js', format: 'amd' }),
buildConfig({ dest: 'v8n.amd.js', format: 'amd' }),

// CJS
buildConfig({ filename: 'v8n.cjs.js', format: 'cjs' }),
buildConfig({ dest: 'v8n.cjs.js', format: 'cjs' }),

// UMD
buildConfig({ filename: 'v8n.umd.js', format: 'umd' }),
buildConfig({
filename: 'v8n.min.js',
format: 'umd',
minified: true,
sourceMap: true,
}),
buildConfig({ dest: 'v8n.umd.js', format: 'umd' }),
buildConfig({ dest: 'v8n.min.js', format: 'umd' }),

// IIFE
buildConfig({ filename: 'v8n.browser.js', format: 'iife' }),
buildConfig({
filename: 'v8n.browser.min.js',
format: 'iife',
extension: 'browser',
minified: true,
sourceMap: true,
}),
buildConfig({ dest: 'v8n.browser.js', format: 'iife' }),
buildConfig({ dest: 'v8n.browser.min.js', format: 'iife' }),

// ESM
buildConfig({ filename: 'v8n.esm.js', format: 'es' }),
buildConfig({ dest: 'v8n.esm.js', format: 'es' }),
buildConfig({ dest: 'v8n.esm.browser.js', format: 'es', transpile: false }),
buildConfig({
filename: 'v8n.esm.browser.js',
dest: 'v8n.esm.browser.min.js',
format: 'es',
extension: 'browser',
transpiled: false,
transpile: false,
}),

// System
buildConfig({ filename: 'v8n.system.js', format: 'system' }),
buildConfig({ dest: 'v8n.system.js', format: 'system' }),
];

export default configs;
12 changes: 6 additions & 6 deletions src/Context.js
@@ -1,6 +1,6 @@
import Rule from "./Rule";
import Modifier from "./Modifier";
import ValidationError from "./ValidationError";
import Rule from './Rule';
import Modifier from './Modifier';
import ValidationError from './ValidationError';

class Context {
constructor(chain = [], nextRuleModifiers = []) {
Expand All @@ -11,7 +11,7 @@ class Context {
_applyRule(ruleFn, name) {
return (...args) => {
this.chain.push(
new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers)
new Rule(name, ruleFn.apply(this, args), args, this.nextRuleModifiers),
);
this.nextRuleModifiers = [];
return this;
Expand All @@ -20,7 +20,7 @@ class Context {

_applyModifier(modifier, name) {
this.nextRuleModifiers.push(
new Modifier(name, modifier.simple, modifier.async)
new Modifier(name, modifier.simple, modifier.async),
);
return this;
}
Expand Down Expand Up @@ -71,7 +71,7 @@ function executeAsyncRules(value, rules, resolve, reject) {
},
cause => {
reject(new ValidationError(rule, value, cause));
}
},
);
} else {
resolve(value);
Expand Down
8 changes: 4 additions & 4 deletions src/Rule.js
Expand Up @@ -40,7 +40,7 @@ class Rule {
return new Promise((resolve, reject) => {
testAsyncAux(
this.modifiers.slice(),
this.fn
this.fn,
)(value)
.then(valid => {
if (valid) {
Expand All @@ -54,8 +54,8 @@ class Rule {
}
}

function pickFn(fn, variant = "simple") {
return typeof fn === "object" ? fn[variant] : fn;
function pickFn(fn, variant = 'simple') {
return typeof fn === 'object' ? fn[variant] : fn;
}

function testAux(modifiers, fn) {
Expand All @@ -74,7 +74,7 @@ function testAsyncAux(modifiers, fn) {
const nextFn = testAsyncAux(modifiers, fn);
return modifier.performAsync(nextFn);
} else {
return value => Promise.resolve(pickFn(fn, "async")(value));
return value => Promise.resolve(pickFn(fn, 'async')(value));
}
}

Expand Down

0 comments on commit 47f9ea3

Please sign in to comment.