first commit

This commit is contained in:
radswiat 2016-10-20 07:58:19 +01:00
commit 004c1932d9
19 changed files with 4171 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.idea
node_modules
npm-debug*

2
.npmignore Normal file
View file

@ -0,0 +1,2 @@
npm-debug*
.idea

25
README.md Normal file
View file

@ -0,0 +1,25 @@
# In development
# Installation
npm i webpack-auto-inject-version --save-dev
# Usage
Add plugin to your webpack configuration.
Require it by:
var WebpackAutoInject = require('webpack-auto-inject-version');
And add to plugins array as one of the last items ( further = better ).
plugins: [
new WebpackAutoInject(options)
]
# Options
NOT SUPPORTED YET!
autoIncrease : boolean,
injectIntoHtml : boolean,
# Auto Increase Version
Option: autoIncrease : true
- run webpack with --release major|minor|patch
DO NOT RUN IT WITH WATCH!

26
dist/components/auto-inc-version.js vendored Normal file
View file

@ -0,0 +1,26 @@
var semver = require('semver');
var config = require('../config');
var IncVersion = (function () {
function IncVersion() {
}
IncVersion.prototype.openPackageFile = function () {
return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
};
IncVersion.prototype.closePackageFile = function (content) {
fs.writeFile("/tmp/test", content, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
};
IncVersion.prototype.major = function () {
this.openPackageFile();
this.closePackageFile();
};
IncVersion.prototype.minor = function () {
};
IncVersion.prototype.patch = function () {
};
return IncVersion;
}());

45
dist/components/inject-into-any-file.js vendored Normal file
View file

@ -0,0 +1,45 @@
var chalk = require('chalk');
var path = require('path');
var endOfLine = require('os').EOL;
var config = require('./../config');
'use strict';
var InjectIntoAnyFile = (function () {
function InjectIntoAnyFile(context) {
this.context = context;
}
InjectIntoAnyFile.prototype.apply = function () {
var _this = this;
this.context.compiler.plugin('emit', function (compilation, cb) {
for (var basename in compilation.assets) {
var ext = path.extname(basename);
var asset = compilation.assets[basename];
switch (ext) {
case '.js':
_this.injectIntoJs(asset);
break;
case '.html':
_this.injectIntoHtml(asset);
break;
case '.css':
_this.injectIntoCss(asset);
break;
}
}
cb();
});
};
InjectIntoAnyFile.prototype.injectIntoCss = function (asset) {
var modAsset = "/** [" + config.SHORT + "] Build version: " + this.context.version + " **/ " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
InjectIntoAnyFile.prototype.injectIntoHtml = function (asset) {
var modAsset = "<!-- [" + config.SHORT + "] Build version: " + this.context.version + " --> " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
InjectIntoAnyFile.prototype.injectIntoJs = function (asset) {
var modAsset = "// [" + config.SHORT + "] Build version: " + this.context.version + " " + endOfLine + " " + asset.source() + " ";
asset.source = function () { return modAsset; };
};
return InjectIntoAnyFile;
}());
module.exports = InjectIntoAnyFile;

24
dist/components/inject-into-html.js vendored Normal file
View file

@ -0,0 +1,24 @@
'use strict';
var InjectIntoHtml = (function () {
function InjectIntoHtml(context) {
this.context = context;
}
InjectIntoHtml.prototype.apply = function () {
var _this = this;
this.context.compiler.plugin('emit', function (compilation, cb) {
var _loop_1 = function() {
if (/^index\.html$/.test(basename)) {
var asset = compilation.assets[basename];
var modFile_1 = asset.source().replace(/(\<\{version\}\>)/g, _this.context.version);
asset.source = function () { return modFile_1; };
}
};
for (var basename in compilation.assets) {
_loop_1();
}
cb();
});
};
return InjectIntoHtml;
}());
module.exports = InjectIntoHtml;

5
dist/config.js vendored Normal file
View file

@ -0,0 +1,5 @@
module.exports = {
NAME: 'Auto Inject Version',
SHORT: 'AIV',
PATH_PACKAGE: './package.json'
};

31
dist/main.js vendored Normal file
View file

@ -0,0 +1,31 @@
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var config = require('./config');
'use strict';
var WebpackAutoInject = (function () {
function WebpackAutoInject(options) {
this.options = WebpackAutoInject.options;
var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version;
}
WebpackAutoInject.prototype.apply = function (compiler) {
this.compiler = compiler;
if (this.options.injectIntoHtml) {
var comp_1 = new (require('./components/auto-inc-version'))(this);
comp_1.apply();
}
if (this.options.injectIntoHtml) {
var comp_2 = new (require('./components/inject-into-html'))(this);
comp_2.apply();
}
var comp = new (require('./components/inject-into-any-file'))(this);
comp.apply();
};
WebpackAutoInject.options = {
autoIncrease: true,
injectIntoHtml: true,
};
return WebpackAutoInject;
}());
module.exports = WebpackAutoInject;

18
package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "webpack-auto-inject-version",
"version": "0.0.19",
"description": "Webpack plugin for auto inject version from package.json",
"main": "dist/main.js",
"scripts": {
"build": "tsc -w"
},
"author": "Radoslaw Swiat",
"license": "ISC",
"devDependencies": {
"chalk": "^1.1.3",
"typings": "^1.4.0"
},
"dependencies": {
"semver": "^5.3.0"
}
}

View file

@ -0,0 +1,30 @@
var semver = require('semver');
var config = require('../config');
class IncVersion{
private openPackageFile() {
return JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
}
private closePackageFile(content) {
fs.writeFile("/tmp/test", content, function(err) {
if(err) {return console.log(err);}
console.log("The file was saved!");
});
}
public major() {
this.openPackageFile();
this.closePackageFile();
}
public minor() {
}
public patch() {
}
}

View file

@ -0,0 +1,55 @@
/// <reference path='../../typings/index.d.ts' />
var chalk = require('chalk');
var path = require('path');
var endOfLine = require('os').EOL;
var config = require('./../config');
'use strict';
/**
* Inject version number into HTML
* - done by parsing html file,
* > replace: <{version}>
*/
class InjectIntoAnyFile{
constructor(private context) {}
apply() {
this.context.compiler.plugin('emit', (compilation, cb) => {
for ( var basename in compilation.assets ) {
let ext = path.extname(basename);
let asset = compilation.assets[basename];
switch(ext) {
case '.js' :
this.injectIntoJs(asset);
break;
case '.html' :
this.injectIntoHtml(asset);
break;
case '.css' :
this.injectIntoCss(asset);
break;
}
}
cb();
});
}
injectIntoCss(asset) {
let modAsset = `/** [${config.SHORT}] Build version: ${this.context.version} **/ ${endOfLine} ${asset.source()} `;
asset.source = () => modAsset;
}
injectIntoHtml(asset) {
let modAsset = `<!-- [${config.SHORT}] Build version: ${this.context.version} --> ${endOfLine} ${asset.source()} `;
asset.source = () => modAsset;
}
injectIntoJs(asset) {
let modAsset = `// [${config.SHORT}] Build version: ${this.context.version} ${endOfLine} ${asset.source()} `;
asset.source = () => modAsset;
}
}
module.exports = InjectIntoAnyFile;

View file

@ -0,0 +1,29 @@
/// <reference path='../../typings/index.d.ts' />
'use strict';
/**
* Inject version number into HTML
* - done by parsing html file,
* > replace: <{version}>
*/
class InjectIntoHtml{
constructor(private context) {}
apply() {
this.context.compiler.plugin('emit', (compilation, cb) => {
for ( var basename in compilation.assets ) {
if(/^index\.html$/.test(basename)) {
let asset = compilation.assets[basename];
let modFile = asset.source().replace(/(\<\{version\}\>)/g, this.context.version);
asset.source = () => modFile;
}
}
cb();
});
}
}
module.exports = InjectIntoHtml;

5
src/config.ts Normal file
View file

@ -0,0 +1,5 @@
module.exports = {
NAME : 'Auto Inject Version',
SHORT : 'AIV',
PATH_PACKAGE : './package.json'
}

52
src/main.ts Normal file
View file

@ -0,0 +1,52 @@
/// <reference path='../typings/index.d.ts' />
var chalk = require('chalk');
var fs = require('fs');
var path = require('path');
var config = require('./config');
'use strict';
class WebpackAutoInject{
private options;
private compiler;
private version;
static options = {
autoIncrease : true,
injectIntoHtml : true,
}
constructor(options) {
this.options = WebpackAutoInject.options;
var packageFile = JSON.parse(fs.readFileSync(path.normalize(config.PATH_PACKAGE), 'utf8'));
this.version = packageFile.version;
}
protected apply(compiler) {
this.compiler = compiler;
// Component: auto-inc-version
// if: autoIncrease : true
if(this.options.injectIntoHtml) {
let comp = new (require('./components/auto-inc-version'))(this);
comp.apply();
}
// Component: Inject-into-html
// if: injectIntoHtml : true
if(this.options.injectIntoHtml) {
let comp = new (require('./components/inject-into-html'))(this);
comp.apply();
}
let comp = new (require('./components/inject-into-any-file'))(this);
comp.apply();
}
}
module.exports = WebpackAutoInject;

17
tsconfig.json Normal file
View file

@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "./dist"
},
"filesGlob": [
"./src/"
]
}

5
typings.json Normal file
View file

@ -0,0 +1,5 @@
{
"globalDependencies": {
"node": "registry:dt/node#6.0.0+20161019125345"
}
}

3790
typings/globals/node/index.d.ts vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,8 @@
{
"resolution": "main",
"tree": {
"src": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/988a48ab2cfff3243868d70d836332a118d9f060/node/node.d.ts",
"raw": "registry:dt/node#6.0.0+20161019125345",
"typings": "https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/988a48ab2cfff3243868d70d836332a118d9f060/node/node.d.ts"
}
}

1
typings/index.d.ts vendored Normal file
View file

@ -0,0 +1 @@
/// <reference path="globals/node/index.d.ts" />