chushihua
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
generate-asset-webpack-plugin
|
||||
=============================
|
||||
|
||||
Sometimes you dynamically want to create files while having access to all the
|
||||
Webpack compilation info.
|
||||
|
||||
Install
|
||||
-------
|
||||
|
||||
```
|
||||
npm install --save-dev generate-asset-webpack-plugin
|
||||
```
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
```javascript
|
||||
var GenerateAssetPlugin = require('generate-asset-webpack-plugin');
|
||||
|
||||
var webpackConfig = {
|
||||
plugins: [
|
||||
new GenerateAssetPlugin({
|
||||
filename: 'index.html',
|
||||
fn: (compilation, cb) => {
|
||||
cb(null, createHtml(compilation));
|
||||
},
|
||||
extraFiles: ['favicon.ico']
|
||||
})
|
||||
]
|
||||
// other webpack config ...
|
||||
}
|
||||
```
|
||||
|
||||
Example function using the `compilation`:
|
||||
|
||||
```javascript
|
||||
function createHtml(compilation) {
|
||||
var chunk = compilation.chunks[0];
|
||||
var jsFile = chunk.files[0];
|
||||
var cssFile = chunk.files[1];
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Awesome!</title>
|
||||
<link rel="stylesheet" href="${cssFile}">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
<script src="${jsFile}"></script>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
};
|
||||
```
|
||||
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
'use strict';
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
||||
|
||||
var fs = require('fs');
|
||||
|
||||
var GenerateAssetWebpackPlugin = (function () {
|
||||
function GenerateAssetWebpackPlugin(options) {
|
||||
_classCallCheck(this, GenerateAssetWebpackPlugin);
|
||||
|
||||
this.filename = options.filename;
|
||||
this.fn = options.fn;
|
||||
this.files = options.extraFiles || [];
|
||||
}
|
||||
|
||||
_createClass(GenerateAssetWebpackPlugin, [{
|
||||
key: 'apply',
|
||||
value: function apply(compiler) {
|
||||
var _this = this;
|
||||
|
||||
compiler.plugin('emit', function (compilation, cb) {
|
||||
_this.fn(compilation, function (err, body) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
compilation.assets[_this.filename] = {
|
||||
source: function source() {
|
||||
return body;
|
||||
},
|
||||
size: function size() {
|
||||
return body.length;
|
||||
}
|
||||
};
|
||||
|
||||
_this.files.forEach(function (file) {
|
||||
compilation.assets[file] = {
|
||||
source: function source() {
|
||||
return fs.readFileSync(file);
|
||||
},
|
||||
size: function size() {
|
||||
return fs.statSync(file).size;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
cb();
|
||||
});
|
||||
});
|
||||
}
|
||||
}]);
|
||||
|
||||
return GenerateAssetWebpackPlugin;
|
||||
})();
|
||||
|
||||
module.exports = GenerateAssetWebpackPlugin;
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
const fs = require('fs');
|
||||
|
||||
class GenerateAssetWebpackPlugin {
|
||||
|
||||
constructor(options) {
|
||||
this.filename = options.filename;
|
||||
this.fn = options.fn;
|
||||
this.files = options.extraFiles || [];
|
||||
}
|
||||
|
||||
apply(compiler) {
|
||||
compiler.plugin('emit', (compilation, cb) => {
|
||||
this.fn(compilation, (err, body) => {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
compilation.assets[this.filename] = {
|
||||
source: () => body,
|
||||
size: () => body.length
|
||||
}
|
||||
|
||||
this.files.forEach(file => {
|
||||
compilation.assets[file] = {
|
||||
source: () => fs.readFileSync(file),
|
||||
size: () => fs.statSync(file).size
|
||||
}
|
||||
});
|
||||
|
||||
cb();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = GenerateAssetWebpackPlugin;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "generate-asset-webpack-plugin",
|
||||
"version": "0.3.0",
|
||||
"description": "A Webpack plugin for generating assets, e.g. an index.html",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "mkdir -p dist && babel < index.js > dist/index.js",
|
||||
"prepublish": "npm run build"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/kjbekkelund/generate-asset-webpack-plugin.git"
|
||||
},
|
||||
"keywords": [
|
||||
"webpack",
|
||||
"plugin",
|
||||
"asset",
|
||||
"index.html"
|
||||
],
|
||||
"author": "Kim Joar Bekkelund <kjbekkelund@gmail.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/kjbekkelund/generate-asset-webpack-plugin/issues"
|
||||
},
|
||||
"homepage": "https://github.com/kjbekkelund/generate-asset-webpack-plugin#readme",
|
||||
"devDependencies": {
|
||||
"babel": "^5.8.23"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user