InjectByTag not working with interior comma, fix + some eslint fixes

This commit is contained in:
rswiat 2018-03-15 07:55:41 +00:00
parent ee443b5db9
commit 9e753447d3
12 changed files with 6440 additions and 18254 deletions

130
.eslintrc
View file

@ -1,27 +1,24 @@
{
"parser": "babel-eslint",
"extends": "airbnb",
"plugins": [
"jsdoc",
"import",
"filenames"
],
"env": {
"browser": true,
"node": true,
"es6": true,
"es6": true
},
"parserOptions": {
"ecmaFeatures": {
"arrowFunctions": true,
"blockBindings": true,
"classes": true,
"defaultParams": true,
"destructuring": true,
"jsx": true,
"modules": true,
"objectLiteralComputedProperties": true,
"objectLiteralShorthandMethods": true,
"objectLiteralShorthandProperties": true,
"spread": true,
"templateStrings": true
"jsx": true
}
},
"globals": {
"__DEV__": true,
"test": true,
"describe": true,
"it": true,
"beforeEach": true,
@ -30,44 +27,81 @@
"browser": true,
"by": true,
"element": true,
"jasmine": true
"jasmine": true,
"If": true,
"For": true,
"When": true,
"Choose": true,
"cy": true
},
"settings": {
"jsdoc": {
"tagNamePreference": {
"param": "param",
"returns": "return"
}
}
},
"plugins": [
"react"
],
"rules": {
"no-unneeded-ternary" : "off",
"spaced-comment": "off",
"no-unused-expressions": "off",
"func-names": "off",
"new-parens": "off",
"no-continue": "off",
"one-var": ["error", "never"],
"class-methods-use-this": "off",
"no-restricted-syntax": "off",
"no-underscore-dangle": "off",
"consistent-return": "off",
"no-console": "off",
"object-curly-spacing": ["error", "always", {
"objectsInObjects": false,
"arraysInObjects": false
"guard-for-in": "off",
"no-param-reassign": "off",
"arrow-body-style": "off",
"prefer-destructuring": "off",
"max-len": ["error", 140],
"no-unused-vars": ["error", { "vars": "all", "args": "none" }],
"arrow-parens": ["error", "always"],
"padded-blocks": "off",
"indent": ["error", 2, { "SwitchCase": 1 }],
"no-new": "off",
"new-cap": "off",
"function-paren-newline": "off",
"no-mixed-operators": "off",
"no-await-in-loop": "off",
"no-plusplus": "off",
"newline-per-chained-call": "off",
"no-restricted-globals": "off",
"object-curly-newline":"off",
"array-callback-return": "off",
"import/no-dynamic-require": "off",
"import/prefer-default-export": "off",
"import/first": "off",
"import/no-extraneous-dependencies": "off",
"import/extensions": "off",
"import/no-unresolved": "off",
"import/no-mutable-exports": "off",
"import/order": ["error", {
"groups": ["builtin", "external", "internal", "sibling", "parent", "index"],
"newlines-between": "always-and-inside-groups"
}],
"comma-dangle": ["error", "never"],
"max-len": ["error", 100, 4, {"ignoreUrls": true}],
"space-before-function-paren": ["error", "never"],
"no-multiple-empty-lines": ["error", {max: 1}],
"no-use-before-define": 0,
"global-require": 0,
"no-else-return": "error",
"no-param-reassign": 0,
"prefer-const": 0,
"func-names": 0,
"arrow-body-style": 0,
"react/jsx-no-bind": [2, {
"ignoreRefs": false,
"allowArrowFunctions": true,
"allowBind": true
}],
"guard-for-in": 0,
"react/jsx-key": 2,
"react/jsx-no-undef": 0,
"react/prefer-stateless-function": 0,
"react/jsx-closing-bracket-location": 0,
"no-undef": "error",
"one-var": 0,
"no-extra-semi": "error",
"semi": "error",
"prefer-arrow-callback": "error",
"new-cap": "off"
"jsdoc/check-param-names": 2,
"jsdoc/check-tag-names": 2,
"jsdoc/check-types": 2,
"jsdoc/newline-after-description": 0,
"jsdoc/require-description-complete-sentence": 0,
"jsdoc/require-example": 0,
"jsdoc/require-hyphen-before-param-description": 2,
"jsdoc/require-param": 2,
"jsdoc/require-param-description": 0,
"jsdoc/require-param-name": 2,
"jsdoc/require-param-type": 0,
"jsdoc/require-returns-description": 0,
"jsdoc/require-returns-type": 2,
"filenames/match-regex": [2, "^(_){0,1}([a-zA-Z0-9])[0-9a-z-.]+$"],
"filenames/match-exported": [2, "kebab", "_" ],
"filenames/no-index": "off"
}
}

View file

@ -24,8 +24,6 @@ Auto Inject Version (AIV) can:
- auto increase package.json version by --env.major, --env.minor, --env.patch passed into webpack
# How to use
It's easy to set it up, all you need is:
* use WebpackAutoInject in webpack plugins
@ -56,6 +54,8 @@ module.exports = {
plugins: [
new WebpackAutoInject({
NAME: 'AIV custom name',
// specify the name of the tag in the outputed files eg
// bundle.js: [SHORT] Version: 0.13.36 ...
SHORT: 'CUSTOM',
SILENT: false,
PACKAGE_JSON_PATH: './package.json',
@ -74,6 +74,9 @@ module.exports = {
},
InjectByTag: {
fileRegex: /\.+/,
// regexp to find [AIV] tag inside html, if you tag contains unallowed characters you can adjust the regex
// but also you can change [AIV] tag to anything you want
AIVTagRegexp: /(\[AIV])(([a-zA-Z{} ,:;!()_@\-"'\\\/])+)(\[\/AIV])/g,
dateFormat: 'h:MM:ss TT'
}
},
@ -100,6 +103,9 @@ module.exports = {
<span>
[AIV]V:{version} Date:{date}[/AIV]
</span>
<span>
[AIV]Version {version} , {date}[/AIV]
</span>
</body>
```

View file

@ -1,4 +1,3 @@
// [AIV] Version: 0.13.36 - Friday, August 25th, 2017, 1:56:17 PM
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
@ -88,7 +87,7 @@ test(myVariable);
/* 1 */
/***/ (function(module, exports) {
module.exports = "<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><title>Title</title></head><body><span>0.13.36</span> <span>Friday, August 25th, 2017, 1:56:17 PM</span> <span>0.13.36_Friday, August 25th, 2017, 1:56:17 PM</span> <span>V:0.13.36 Date:Friday, August 25th, 2017, 1:56:17 PM</span></body></html>"
module.exports = "<!DOCTYPE html><html lang=en><head><meta charset=UTF-8><title>Title</title></head><body><span>0.13.36</span> <span>7:54:43</span> <span>0.13.36_7:54:43</span> <span>V:0.13.36 Date:7:54:43</span> <span>Version 0.13.36 , 7:54:43</span></body></html>"
/***/ }),
/* 2 */

View file

@ -13,6 +13,7 @@
"devDependencies": {
"html-loader": "^0.4.5",
"html-minify-loader": "^1.1.0",
"nodemon": "^1.17.2",
"raw-loader": "^0.5.1",
"webpack": "^2.3.3",
"webpack-auto-inject-version": "^0.5.10"

View file

@ -17,5 +17,8 @@
<span>
[AIV]V:{version} Date:{date}[/AIV]
</span>
<span>
[AIV]Version {version} , {date}[/AIV]
</span>
</body>
</html>

View file

@ -1,61 +1,63 @@
var path = require('path');
const path = require('path');
// Require WebpackAutoInject from npm installed modules ( preferred )
// var WebpackAutoInject = require('webpack-auto-inject-version');
// Require WebpackAutoInject from dist - dev purpose only ( do not use the below line )
var WebpackAutoInject = require('../dist/WebpackAutoInjectVersion');
const WebpackAutoInject = require('../dist/WebpackAutoInjectVersion');
module.exports = {
watch: true,
entry: {
index: ['./src/main.js']
index: ['./src/main.js'],
},
resolve: {
extensions: ['.js', '.html']
extensions: ['.js', '.html'],
},
output: {
filename: '[name]-bundle.js',
path: path.resolve(process.cwd(), 'dist')
path: path.resolve(process.cwd(), 'dist'),
},
module: {
loaders: [
{
test: /\.js$/,
include: [
path.resolve('src')
]
path.resolve('src'),
],
},
{
test: /\.json$/,
loader: 'json-loader'
loader: 'json-loader',
},
{
test: /\.txt$/,
loader: 'raw-loader'
loader: 'raw-loader',
},
{
test: /\.html$/,
loader: 'raw-loader!html-minify-loader'
}
]
loader: 'raw-loader!html-minify-loader',
},
],
},
plugins: [
new WebpackAutoInject({
components: {
AutoIncreaseVersion: true,
InjectAsComment: true,
InjectByTag: true
AutoIncreaseVersion: false,
InjectAsComment: false,
InjectByTag: true,
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false // it will increase version with every single build!
runInWatchMode: false, // it will increase version with every single build!
},
InjectAsComment: {
tag: 'Version: {version} - {date}'
tag: 'Version: {version}, {date}',
},
InjectByTag: {
fileRegex: /\.+/
}
}
})
]
fileRegex: /\.+/,
dateFormat: 'h:MM:ss',
},
},
}),
],
};

File diff suppressed because it is too large Load diff

View file

@ -5,7 +5,8 @@
"description": "Webpack plugin for auto inject version from package.json",
"main": "dist/WebpackAutoInjectVersion.js",
"scripts": {
"start": "babel-node tools/compile.js"
"start": "babel-node tools/compile.js",
"eslint": "eslint ./src/**"
},
"author": "Radoslaw Swiat",
"license": "ISC",
@ -26,6 +27,9 @@
"eslint-config-airbnb": "^6.2.0",
"eslint-loader": "^1.5.0",
"eslint-plugin-babel": "^3.2.0",
"eslint-plugin-filenames": "^1.2.0",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jsdoc": "^3.5.0",
"eslint-plugin-jsx-control-statements": "^2.1.1",
"eslint-plugin-react": "^4.2.3",
"gutil": "^1.6.4",

View file

@ -10,7 +10,6 @@ import config from 'config';
export default class InjectByTag {
static componentName = 'InjectByTag';
static AIVTagRegexp = /(\[AIV])(([a-zA-Z{} :;!()_@\-"'\\\/])+)(\[\/AIV])/g;
constructor(context) {
this.context = context;
@ -19,23 +18,23 @@ export default class InjectByTag {
/**
* Apply will be called from main class
* @protected
* @returns {Promise}
* @return {Promise}
*/
apply() {
this.context.compiler.plugin('emit', (compilation, cb) => {
// for every output file
for (let basename in compilation.assets) {
for (const basename in compilation.assets) {
// only if match regex
if (this.context.config.componentsOptions.InjectByTag.fileRegex.test(basename)) {
let replaced = 0;
let asset = compilation.assets[basename];
const asset = compilation.assets[basename];
const originalSource = asset.source();
if (!originalSource || typeof originalSource.replace !== 'function') {
continue;
}
let modFile = originalSource.replace(InjectByTag.AIVTagRegexp, (tag) => {
const modFile = originalSource.replace(this.context.config.componentsOptions.InjectByTag.AIVTagRegexp, (tag) => {
// handle version
tag = tag.replace(/(\{)(version)(\})/g, () => {
return this.context.version;
@ -54,11 +53,6 @@ export default class InjectByTag {
return tag;
});
// let modFile = originalSource.replace(/(\[AIV\]{version}\[\/AIV\])/g, () => {
// replaced++;
// return this.context.version;
// });
asset.source = () => modFile;
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
}

View file

@ -1,27 +1,28 @@
export default {
NAME: 'Auto Inject Version',
SHORT: 'AIV',
NAME: 'Auto Inject Version CONFIG',
SHORT: 'AIV_SHORT',
SILENT: false,
PACKAGE_JSON_PATH: './package.json',
components: {
AutoIncreaseVersion: true,
InjectAsComment: true,
InjectByTag: true
InjectByTag: true,
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false
runInWatchMode: false,
},
InjectAsComment: {
tag: 'Build version: {version} - {date}',
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT'
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT',
},
InjectByTag: {
fileRegex: /\.+/,
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT'
}
AIVTagRegexp: /(\[AIV])(([a-zA-Z{} ,:;!()_@\-"'\\\/])+)(\[\/AIV])/g,
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT',
},
},
LOGS_TEXT: {
AIS_START: 'Auto inject version started'
}
AIS_START: 'Auto inject version started',
},
};

View file

@ -1,9 +1,10 @@
/* global define */
import fs from 'fs';
import path from 'path';
import config from 'config';
import log from 'core/log';
import { merge, transform } from 'lodash';
import merge from 'lodash/merge';
import transform from 'lodash/transform';
// import sub components
import AutoIncreaseVersion from 'components/auto-increase-version/auto-increase-version';
@ -19,7 +20,7 @@ export default class WebpackAutoInject {
*/
constructor(userConfig) {
this.setConfig(userConfig);
let packageFile = JSON.parse(
const packageFile = JSON.parse(
fs.readFileSync(path.resolve(this.config.PACKAGE_JSON_PATH), 'utf8')
);
this.version = packageFile.version;
@ -35,7 +36,7 @@ export default class WebpackAutoInject {
*/
setConfig(userConfig) {
this.config = merge(config, userConfig);
console.log(this.config);
// lets convert all components names to lowercase - to prevent issues
this.config.components = transform(this.config.components, (result, val, key) => {
result[key.toLowerCase()] = val;

View file

@ -4,12 +4,12 @@ export default {
target: 'node',
entry: ['./src/main.js'],
resolve: {
extensions: ['.js']
extensions: ['.js'],
},
output: {
filename: 'WebpackAutoInjectVersion.js',
path: path.resolve(process.cwd(), 'dist'),
libraryTarget: 'umd'
libraryTarget: 'umd',
},
module: {
loaders: [
@ -17,23 +17,22 @@ export default {
test: /\.js$/,
use: [
'babel-loader',
'eslint-loader'
],
include: [
path.resolve('src')
]
path.resolve('src'),
],
},
{
test: /\.json$/,
loader: 'json-loader'
loader: 'json-loader',
},
{
test: /\.txt$/,
loader: 'raw-loader'
}
]
loader: 'raw-loader',
},
],
},
plugins: [
]
],
};