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 * use WebpackAutoInject in webpack plugins
* pass config as a parameter, or leave it blank as all options are "on" by default. * 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 ```js
var WebpackAutoInject = require('webpack-auto-inject-version'); 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 # 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" "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 Default: true
### components.InjectByTag ### components.InjectByTag
@ -99,7 +169,8 @@ Example:
componentsOptions: { componentsOptions: {
... ...
InjectAsComment: { 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 # 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 ## [0.5.14] - 12/04/2017
- Remove babel polyfills from webpack build as it was causing issues if babel was already used in project - Remove babel polyfills from webpack build as it was causing issues if babel was already used in project
## [0.5.13] - 12/04/2017 ## [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 /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache /******/ // The module cache
/******/ var installedModules = {}; /******/ var installedModules = {};
@ -7,9 +7,9 @@
/******/ function __webpack_require__(moduleId) { /******/ function __webpack_require__(moduleId) {
/******/ /******/
/******/ // Check if module is in cache /******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) /******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports; /******/ return installedModules[moduleId].exports;
/******/ /******/ }
/******/ // Create a new module (and put it into the cache) /******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = { /******/ var module = installedModules[moduleId] = {
/******/ i: moduleId, /******/ i: moduleId,
@ -88,7 +88,7 @@ test(myVariable);
/* 1 */ /* 1 */
/***/ (function(module, exports) { /***/ (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 */ /* 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", "name": "example-using-inject",
"version": "0.13.4", "version": "0.13.36",
"description": "This is an example how to use webpack-auto-inject-version plugin in webpack", "description": "This is an example how to use webpack-auto-inject-version plugin in webpack",
"scripts": { "scripts": {
"start": "webpack --config ./webpack.conf.js", "start": "webpack --config ./webpack.conf.js",
"patch": "webpack --config ./webpack.conf.js --env.patch", "patch": "webpack --config ./webpack.conf.js --env.patch",
"minor": "webpack --config ./webpack.conf.js --env.minor", "minor": "webpack --config ./webpack.conf.js --env.minor",
"major": "webpack --config ./webpack.conf.js --env.major" "major": "webpack --config ./webpack.conf.js --env.major"
}, },
"author": "Radoslaw Swiat", "author": "Radoslaw Swiat",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"html-loader": "^0.4.5", "html-loader": "^0.4.5",
"html-minify-loader": "^1.1.0", "html-minify-loader": "^1.1.0",
"raw-loader": "^0.5.1", "raw-loader": "^0.5.1",
"webpack": "^2.3.3", "webpack": "^2.3.3",
"webpack-auto-inject-version": "^0.5.10" "webpack-auto-inject-version": "^0.5.10"
} }
} }

View file

@ -5,6 +5,17 @@
<title>Title</title> <title>Title</title>
</head> </head>
<body> <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> </body>
</html> </html>

View file

@ -5,6 +5,7 @@ var path = require('path');
var WebpackAutoInject = require('../dist/WebpackAutoInjectVersion'); var WebpackAutoInject = require('../dist/WebpackAutoInjectVersion');
module.exports = { module.exports = {
watch: true,
entry: { entry: {
index: ['./src/main.js'] index: ['./src/main.js']
}, },
@ -39,11 +40,21 @@ module.exports = {
}, },
plugins: [ plugins: [
new WebpackAutoInject({ new WebpackAutoInject({
PACKAGE_JSON_PATH: './package.json',
components: { components: {
AutoIncreaseVersion: true, AutoIncreaseVersion: true,
InjectAsComment: true, InjectAsComment: true,
InjectByTag: 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", "name": "webpack-auto-inject-version",
"version": "0.5.14", "version": "1.0.0",
"repository": "radswiat/webpack-auto-inject-version", "repository": "radswiat/webpack-auto-inject-version",
"description": "Webpack plugin for auto inject version from package.json", "description": "Webpack plugin for auto inject version from package.json",
"main": "dist/WebpackAutoInjectVersion.js", "main": "dist/WebpackAutoInjectVersion.js",

View file

@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs'; import fs from 'fs';
import { isArgv } from 'core/utils'; import { isArgv } from 'core/utils';
import log from 'core/log'; import log from 'core/log';
import config from 'config';
export default class AutoIncreaseVersion { export default class AutoIncreaseVersion {
@ -18,6 +19,23 @@ export default class AutoIncreaseVersion {
* @returns {Promise} * @returns {Promise}
*/ */
apply() { 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) => { return new Promise((resolve, reject) => {
this.resolve = resolve; this.resolve = resolve;
this.reject = reject; this.reject = reject;
@ -31,6 +49,9 @@ export default class AutoIncreaseVersion {
*/ */
start() { start() {
this.packageFile = this.openPackageFile(); this.packageFile = this.openPackageFile();
if (!this.packageFile) {
return;
}
if (isArgv('major')) { if (isArgv('major')) {
this.major(); this.major();
} else if (isArgv('minor')) { } else if (isArgv('minor')) {
@ -38,7 +59,7 @@ export default class AutoIncreaseVersion {
} else if (isArgv('patch')) { } else if (isArgv('patch')) {
this.patch(); this.patch();
} else { } else {
this.reject(); this.resolve();
} }
} }
@ -47,7 +68,16 @@ export default class AutoIncreaseVersion {
* @returns {any} * @returns {any}
*/ */
openPackageFile() { 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 dateFormat from 'dateformat';
import config from 'config';
export default { export default {
version: (context) => { version: (context) => {
return context.version; return context.version;
}, },
date: () => { 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 log from 'core/log';
import config from 'config';
/** /**
* Inject version number into HTML * Inject version number into HTML
@ -8,6 +10,7 @@ import log from 'core/log';
export default class InjectByTag { export default class InjectByTag {
static componentName = 'InjectByTag'; static componentName = 'InjectByTag';
static AIVTagRegexp = /(\[AIV])(([a-zA-Z{} :;!()_@\-"'\\\/])+)(\[\/AIV])/g;
constructor(context) { constructor(context) {
this.context = context; this.context = context;
@ -32,11 +35,30 @@ export default class InjectByTag {
continue; 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++; replaced++;
return this.context.version;
return tag;
}); });
// let modFile = originalSource.replace(/(\[AIV\]{version}\[\/AIV\])/g, () => {
// replaced++;
// return this.context.version;
// });
asset.source = () => modFile; asset.source = () => modFile;
log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`); log.info(`InjectByTag : match : ${basename} : replaced : ${replaced}`);
} }

View file

@ -1,4 +1,7 @@
export default { export default {
NAME: 'Auto Inject Version',
SHORT: 'AIV',
SILENT: false,
PACKAGE_JSON_PATH: './package.json', PACKAGE_JSON_PATH: './package.json',
components: { components: {
AutoIncreaseVersion: true, AutoIncreaseVersion: true,
@ -6,11 +9,16 @@ export default {
InjectByTag: true InjectByTag: true
}, },
componentsOptions: { componentsOptions: {
AutoIncreaseVersion: {
runInWatchMode: false
},
InjectAsComment: { InjectAsComment: {
tag: 'Build version: {version} - {date}' tag: 'Build version: {version} - {date}',
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT'
}, },
InjectByTag: { InjectByTag: {
fileRegex: /\.+/ fileRegex: /\.+/,
dateFormat: 'dddd, mmmm dS, yyyy, h:MM:ss TT'
} }
}, },
LOGS_TEXT: { LOGS_TEXT: {

View file

@ -46,16 +46,19 @@ class Log {
} }
error(msg) { error(msg) {
if (config.SILENT) return;
if (this.logLevel < 3) return; if (this.logLevel < 3) return;
console.log(`${this.getHead()} ${chalk.red('error')} : ${msg}`); console.log(`${this.getHead()} ${chalk.red('error')} : ${msg}`);
} }
info(msg) { info(msg) {
if (config.SILENT) return;
if (!this.logLevel) return; if (!this.logLevel) return;
console.log(`${this.getHead()} ${chalk.blue('info')} : ${msg}`); console.log(`${this.getHead()} ${chalk.blue('info')} : ${msg}`);
} }
warn(msg) { warn(msg) {
if (config.SILENT) return;
if (!this.logLevel) return; if (!this.logLevel) return;
console.log(`${this.getHead()} ${chalk.yellow('warn')} : ${msg}`); 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 { export default class WebpackAutoInject {
/**
* Protected config
* @type {{NAME: string, SHORT: string}}
*/
static protectedConfig = {
NAME: 'Auto Inject Version',
SHORT: 'AIV'
};
/** /**
* Constructor, * Constructor,
* called on webpack config load * called on webpack config load
@ -49,8 +40,6 @@ export default class WebpackAutoInject {
this.config.components = transform(this.config.components, (result, val, key) => { this.config.components = transform(this.config.components, (result, val, key) => {
result[key.toLowerCase()] = val; 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 * and plugins is called by webpack
*/ */
async executeWebpackComponents() { async executeWebpackComponents() {
if (config.componentsOptions.AutoIncreaseVersion.runInWatchMode) {
await this.executeComponent([AutoIncreaseVersion]);
}
await this.executeComponent([InjectAsComment, InjectByTag]); await this.executeComponent([InjectAsComment, InjectByTag]);
} }

View file

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