Date tag, watch mode, silent option, date formats, v 1.0.0

This commit is contained in:
radswiat 2017-08-25 14:00:19 +01:00
parent 90e0587ac8
commit ee443b5db9
15 changed files with 5705 additions and 2116 deletions

View File

@ -31,7 +31,7 @@ It's easy to set it up, all you need is:
* use WebpackAutoInject in webpack plugins
* pass config as a parameter, or leave it blank as all options are "on" by default.
### Example ( in webpack.conf.js )
### Simple config example ( in webpack.conf.js )
```js
var WebpackAutoInject = require('webpack-auto-inject-version');
...
@ -49,7 +49,59 @@ module.exports = {
}
```
### Full config example ( in webpack.conf.js )
```
module.exports = {
...
plugins: [
new WebpackAutoInject({
NAME: 'AIV custom name',
SHORT: 'CUSTOM',
SILENT: false,
PACKAGE_JSON_PATH: './package.json',
components: {
AutoIncreaseVersion: true,
InjectAsComment: true,
InjectByTag: true
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false // it will increase version with every single build!
},
InjectAsComment: {
tag: 'Version: {version} - {date}',
dateFormat: 'h:MM:ss TT'
},
InjectByTag: {
fileRegex: /\.+/,
dateFormat: 'h:MM:ss TT'
}
},
LOGS_TEXT: {
AIS_START: 'DEMO AIV started'
}
})
]
}
```
### Inject by tag example
```
<body>
<span>
[AIV]{version}[/AIV]
</span>
<span>
[AIV]{date}[/AIV]
</span>
<span>
[AIV]{version}_{date}[/AIV]
</span>
<span>
[AIV]V:{version} Date:{date}[/AIV]
</span>
</body>
```
# Available options
@ -66,6 +118,24 @@ Example for package.json run type, npm run start => ( 1.2.10 to 2.0.0 )
"start": "webpack --env.major"
}
```
To enable watch mode:
```
plugins: [
new WebpackAutoInject({
...
components: {
AutoIncreaseVersion: true,
...
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false // it will increase version with every single build!
}
}
})
]
```
Default: true
### components.InjectByTag
@ -99,7 +169,8 @@ Example:
componentsOptions: {
...
InjectAsComment: {
tag: 'Build version: {version} - {date}' // default
tag: 'Build version: {version} - {date}', // default
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT' // default
}
})
]
@ -127,6 +198,12 @@ Example html:
# Change log
## [1.0.0] - 25/08/2017
- Date format can now be specified for InjectAsComment
- Date format can now be specified for InjectByTag
- Webpack WATCH support added
- Root SILENT option added
- Minor fixes
## [0.5.14] - 12/04/2017
- Remove babel polyfills from webpack build as it was causing issues if babel was already used in project
## [0.5.13] - 12/04/2017

View File

@ -1,4 +1,4 @@
// [AIV] Build version: 0.13.4 - Wednesday, April 12th, 2017, 11:34:31 PM
// [AIV] Version: 0.13.36 - Friday, August 25th, 2017, 1:56:17 PM
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
@ -7,9 +7,9 @@
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
@ -88,7 +88,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>My awesome project | 0.13.4></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>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>"
/***/ }),
/* 2 */

3346
demo/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,20 +1,20 @@
{
"name": "example-using-inject",
"version": "0.13.4",
"description": "This is an example how to use webpack-auto-inject-version plugin in webpack",
"scripts": {
"start": "webpack --config ./webpack.conf.js",
"patch": "webpack --config ./webpack.conf.js --env.patch",
"minor": "webpack --config ./webpack.conf.js --env.minor",
"major": "webpack --config ./webpack.conf.js --env.major"
},
"author": "Radoslaw Swiat",
"license": "ISC",
"devDependencies": {
"html-loader": "^0.4.5",
"html-minify-loader": "^1.1.0",
"raw-loader": "^0.5.1",
"webpack": "^2.3.3",
"webpack-auto-inject-version": "^0.5.10"
}
}
"name": "example-using-inject",
"version": "0.13.36",
"description": "This is an example how to use webpack-auto-inject-version plugin in webpack",
"scripts": {
"start": "webpack --config ./webpack.conf.js",
"patch": "webpack --config ./webpack.conf.js --env.patch",
"minor": "webpack --config ./webpack.conf.js --env.minor",
"major": "webpack --config ./webpack.conf.js --env.major"
},
"author": "Radoslaw Swiat",
"license": "ISC",
"devDependencies": {
"html-loader": "^0.4.5",
"html-minify-loader": "^1.1.0",
"raw-loader": "^0.5.1",
"webpack": "^2.3.3",
"webpack-auto-inject-version": "^0.5.10"
}
}

View File

@ -5,6 +5,17 @@
<title>Title</title>
</head>
<body>
<span>My awesome project | [AIV]{version}[/AIV]></span>
<span>
[AIV]{version}[/AIV]
</span>
<span>
[AIV]{date}[/AIV]
</span>
<span>
[AIV]{version}_{date}[/AIV]
</span>
<span>
[AIV]V:{version} Date:{date}[/AIV]
</span>
</body>
</html>
</html>

View File

@ -5,6 +5,7 @@ var path = require('path');
var WebpackAutoInject = require('../dist/WebpackAutoInjectVersion');
module.exports = {
watch: true,
entry: {
index: ['./src/main.js']
},
@ -39,11 +40,21 @@ module.exports = {
},
plugins: [
new WebpackAutoInject({
PACKAGE_JSON_PATH: './package.json',
components: {
AutoIncreaseVersion: true,
InjectAsComment: true,
InjectByTag: true
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false // it will increase version with every single build!
},
InjectAsComment: {
tag: 'Version: {version} - {date}'
},
InjectByTag: {
fileRegex: /\.+/
}
}
})
]

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "webpack-auto-inject-version",
"version": "0.5.14",
"version": "1.0.0",
"repository": "radswiat/webpack-auto-inject-version",
"description": "Webpack plugin for auto inject version from package.json",
"main": "dist/WebpackAutoInjectVersion.js",

View File

@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs';
import { isArgv } from 'core/utils';
import log from 'core/log';
import config from 'config';
export default class AutoIncreaseVersion {
@ -18,6 +19,23 @@ export default class AutoIncreaseVersion {
* @returns {Promise}
*/
apply() {
// when runInWatchMode
// we have to register AutoIncreaseVersion instead of firing it straight away
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
if (this.context.compiler) {
this.context.compiler.plugin('emit', async (compilation, cb) => {
await new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.start();
});
cb();
});
}
return null;
}
// when runInWatchMode is off
return new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
@ -31,6 +49,9 @@ export default class AutoIncreaseVersion {
*/
start() {
this.packageFile = this.openPackageFile();
if (!this.packageFile) {
return;
}
if (isArgv('major')) {
this.major();
} else if (isArgv('minor')) {
@ -38,7 +59,7 @@ export default class AutoIncreaseVersion {
} else if (isArgv('patch')) {
this.patch();
} else {
this.reject();
this.resolve();
}
}
@ -47,7 +68,16 @@ export default class AutoIncreaseVersion {
* @returns {any}
*/
openPackageFile() {
return JSON.parse(fs.readFileSync(path.resolve(this.context.config.PACKAGE_JSON_PATH), 'utf8'));
try {
return JSON.parse(
fs.readFileSync(
path.resolve(this.context.config.PACKAGE_JSON_PATH),
'utf8'
)
);
} catch (err) {
return null;
}
}
/**

View File

@ -1,10 +1,11 @@
import dateFormat from 'dateformat';
import config from 'config';
export default {
version: (context) => {
return context.version;
},
date: () => {
return dateFormat(new Date(), 'dddd, mmmm dS, yyyy, h:MM:ss TT');
return dateFormat(new Date(), config.componentsOptions.InjectAsComment.dateFormat);
}
};

View File

@ -1,4 +1,6 @@
import dateFormat from 'dateformat';
import log from 'core/log';
import config from 'config';
/**
* Inject version number into HTML
@ -8,6 +10,7 @@ import log from 'core/log';
export default class InjectByTag {
static componentName = 'InjectByTag';
static AIVTagRegexp = /(\[AIV])(([a-zA-Z{} :;!()_@\-"'\\\/])+)(\[\/AIV])/g;
constructor(context) {
this.context = context;
@ -32,11 +35,30 @@ export default class InjectByTag {
continue;
}
let modFile = originalSource.replace(/(\[AIV\]{version}\[\/AIV\])/g, () => {
let modFile = originalSource.replace(InjectByTag.AIVTagRegexp, (tag) => {
// handle version
tag = tag.replace(/(\{)(version)(\})/g, () => {
return this.context.version;
});
// handle date
tag = tag.replace(/(\{)(date)(\})/g, () => {
return dateFormat(new Date(), config.componentsOptions.InjectByTag.dateFormat);
});
// remove [AIV] and [/AIV]
tag = tag.replace(/(\[AIV])|(\[\/AIV])/g, '');
replaced++;
return this.context.version;
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,4 +1,7 @@
export default {
NAME: 'Auto Inject Version',
SHORT: 'AIV',
SILENT: false,
PACKAGE_JSON_PATH: './package.json',
components: {
AutoIncreaseVersion: true,
@ -6,11 +9,16 @@ export default {
InjectByTag: true
},
componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false
},
InjectAsComment: {
tag: 'Build version: {version} - {date}'
tag: 'Build version: {version} - {date}',
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT'
},
InjectByTag: {
fileRegex: /\.+/
fileRegex: /\.+/,
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT'
}
},
LOGS_TEXT: {

View File

@ -46,16 +46,19 @@ class Log {
}
error(msg) {
if (config.SILENT) return;
if (this.logLevel < 3) return;
console.log(`${this.getHead()} ${chalk.red('error')} : ${msg}`);
}
info(msg) {
if (config.SILENT) return;
if (!this.logLevel) return;
console.log(`${this.getHead()} ${chalk.blue('info')} : ${msg}`);
}
warn(msg) {
if (config.SILENT) return;
if (!this.logLevel) return;
console.log(`${this.getHead()} ${chalk.yellow('warn')} : ${msg}`);
}

View File

@ -12,15 +12,6 @@ import InjectByTag from 'components/inject-by-tag/inject-by-tag';
export default class WebpackAutoInject {
/**
* Protected config
* @type {{NAME: string, SHORT: string}}
*/
static protectedConfig = {
NAME: 'Auto Inject Version',
SHORT: 'AIV'
};
/**
* Constructor,
* called on webpack config load
@ -49,8 +40,6 @@ export default class WebpackAutoInject {
this.config.components = transform(this.config.components, (result, val, key) => {
result[key.toLowerCase()] = val;
});
this.config = merge(this.config, WebpackAutoInject.protectedConfig);
}
/**
@ -80,6 +69,9 @@ export default class WebpackAutoInject {
* and plugins is called by webpack
*/
async executeWebpackComponents() {
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
await this.executeComponent([AutoIncreaseVersion]);
}
await this.executeComponent([InjectAsComment, InjectByTag]);
}

View File

@ -12,14 +12,6 @@ export default {
libraryTarget: 'umd'
},
module: {
// rules: [
// { // eslint feature
// enforce: 'pre',
// test: /\.js$/,
// loader: 'eslint-loader',
// exclude: /node_modules/
// }
// ],
loaders: [
{
test: /\.js$/,