Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 34641cc

Browse files
committed
initial commit
0 parents  commit 34641cc

File tree

20 files changed

+657
-0
lines changed

20 files changed

+657
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules
2+
.eslintrc
3+
.DS_Store
4+
*.log
5+
.idea

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test
2+
*.yml
3+
src
4+
.eslintrc
5+
.gitignore

.travis.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- stable
5+
cache:
6+
directories:
7+
- node_modules
8+
branches:
9+
only:
10+
- master
11+
notifications:
12+
email: false
13+
script:
14+
- npm test

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2016 Donskov Dmitry
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
Static Render HTML Webpack Plugin
2+
=================================
3+
[![npm version](https://badge.fury.io/js/static-render-html-webpack-plugin.svg)](https://badge.fury.io/js/static-render-html-webpack-plugin) [![Build status](https://travis-ci.org/donskov/static-render-html-webpack-plugin.svg)](https://travis-ci.org/donskov/static-render-html-webpack-plugin) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/donskov/static-render-html-webpack-plugin/blob/master/LICENSE)
4+
[![NPM](https://nodei.co/npm/static-render-html-webpack-plugin.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/static-render-html-webpack-plugin/)
5+
6+
This is a webpack plugin that simplifies creation of HTML static files using webpack. It will be useful if you are creating a PWA and you need as quickly as possible to show the user first paint.
7+
8+
Installation
9+
------------
10+
Install the plugin with npm:
11+
```shell
12+
$ npm install static-render-html-webpack-plugin --save-dev
13+
```
14+
15+
Basic Usage
16+
-----------
17+
18+
```javascript
19+
var StaticRenderHtmlWebpackPlugin = require('static-render-html-webpack-plugin');
20+
var webpackConfig = {
21+
entry: 'index.js',
22+
output: {
23+
path: 'dist',
24+
filename: 'main.js'
25+
},
26+
plugins: [
27+
new StaticRenderHtmlWebpackPlugin({
28+
entry: path.join(__dirname, './shells/index.jsx');
29+
})
30+
]
31+
};
32+
```
33+
34+
```javascript
35+
// index.jsx
36+
import React from 'react';
37+
import { renderToStaticMarkup } from 'react-dom/server';
38+
39+
const IndexPage = (props) => (
40+
<html lang="en">
41+
<head>
42+
<meta charSet="utf-8" />
43+
<title>
44+
Website title
45+
</title>
46+
</head>
47+
<body>
48+
<div id=""root>
49+
<span>
50+
Index page
51+
</span>
52+
</div>
53+
</body>
54+
</html>
55+
);
56+
57+
export default {
58+
index: renderToStaticMarkup(IndexPage),
59+
};
60+
```
61+
62+
This will generate a file `dist/index.html` containing the following:
63+
```html
64+
<html lang="en">
65+
<head>
66+
<meta charSet="utf-8" />
67+
<title>
68+
Website title
69+
</title>
70+
</head>
71+
<body>
72+
<div id=""root>
73+
<span>
74+
Main page
75+
</span>
76+
</div>
77+
</body>
78+
</html>
79+
```
80+
81+
# License
82+
83+
This project is licensed under [MIT](https://github.com/donskov/static-render-html-webpack-plugin/blob/master/LICENSE).

dist/index.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
'use strict';
2+
3+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
4+
5+
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; }; }();
6+
7+
var _pretty = require('pretty');
8+
9+
var _pretty2 = _interopRequireDefault(_pretty);
10+
11+
var _requirefresh = require('requirefresh');
12+
13+
var _requirefresh2 = _interopRequireDefault(_requirefresh);
14+
15+
var _errors = require('./utils/errors');
16+
17+
var _errors2 = _interopRequireDefault(_errors);
18+
19+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20+
21+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22+
23+
require('babel-register')({
24+
extensions: ['.js', '.jsx']
25+
});
26+
27+
var StaticRenderHtmlWebpackPlugin = function () {
28+
function StaticRenderHtmlWebpackPlugin(options) {
29+
_classCallCheck(this, StaticRenderHtmlWebpackPlugin);
30+
31+
this.options = Object.assign({}, {
32+
entry: '',
33+
pretty: false
34+
}, options);
35+
}
36+
37+
_createClass(StaticRenderHtmlWebpackPlugin, [{
38+
key: 'apply',
39+
value: function apply(compiler) {
40+
var _this = this;
41+
42+
var entry = this.options.entry;
43+
44+
compiler.plugin('emit', function (compilation, callback) {
45+
var result = '';
46+
47+
if (!entry) {
48+
compilation.errors.push(_errors2.default.emptyEntry(compiler.context));
49+
return callback();
50+
}
51+
52+
var FILE_SUPPORT_REGEXP = /.(js|jsx)$/g;
53+
var fileExtension = entry.split('.');
54+
fileExtension = '.' + fileExtension[fileExtension.length - 1];
55+
56+
if (!FILE_SUPPORT_REGEXP.test(fileExtension)) {
57+
compilation.errors.push(_errors2.default.fileExtension(entry, compiler.context));
58+
return callback();
59+
}
60+
61+
try {
62+
result = (0, _requirefresh2.default)(entry);
63+
} catch (error) {
64+
compilation.errors.push(_errors2.default.errorWrapper(error));
65+
return callback();
66+
}
67+
68+
compilation.fileDependencies.push(entry);
69+
70+
if (result.default && _typeof(result.default) === 'object') {
71+
result = result.default;
72+
}
73+
74+
Object.keys(result).map(function (key) {
75+
var file = {
76+
name: key + '.html',
77+
source: result[key],
78+
size: result[key].length
79+
};
80+
81+
var html = file.source;
82+
83+
if (_this.options.pretty) {
84+
try {
85+
html = (0, _pretty2.default)(html);
86+
} catch (error) {
87+
html = 'Error: \'' + error + '\'\nFile: \'' + entry + '\'\nProperty: \'' + key + '\'';
88+
compilation.errors.push(_errors2.default.errorWrapper(error));
89+
}
90+
}
91+
92+
compilation.assets[file.name] = {
93+
source: function source() {
94+
return html;
95+
},
96+
size: function size() {
97+
return file.size;
98+
}
99+
};
100+
});
101+
callback();
102+
});
103+
}
104+
}]);
105+
106+
return StaticRenderHtmlWebpackPlugin;
107+
}();
108+
109+
module.exports = StaticRenderHtmlWebpackPlugin;

dist/utils/errors.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _chalk = require('chalk');
8+
9+
var _chalk2 = _interopRequireDefault(_chalk);
10+
11+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12+
13+
var _errorWrapper = function _errorWrapper(error) {
14+
return _chalk2.default.red('Static Render Html Webpack Plugin: ' + error);
15+
};
16+
17+
exports.default = {
18+
emptyEntry: function emptyEntry(context) {
19+
return _errorWrapper('Could not find entry property in options.\n' + context);
20+
},
21+
fileExtension: function fileExtension(file, context) {
22+
return _errorWrapper('Entry file extension ' + file + ' don\'t correct. Expect \'.js\' or \'.jsx\' file.\n' + context);
23+
},
24+
errorWrapper: function errorWrapper(error) {
25+
return _errorWrapper(error);
26+
}
27+
};

package.json

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "static-render-html-webpack-plugin",
3+
"version": "1.0.4",
4+
"description": "Simplifies creation of HTML static files using webpack",
5+
"main": "dist/index.js",
6+
"scripts": {
7+
"build": "babel ./src --out-dir ./dist",
8+
"test": "mocha test/index.js --compilers js:babel-register",
9+
"prebuild": "rimraf dist",
10+
"test:watch": "npm t -- --watch"
11+
},
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/donskov/static-render-html-webpack-plugin.git"
15+
},
16+
"keywords": [
17+
"webpack",
18+
"react",
19+
"static",
20+
"html",
21+
"render"
22+
],
23+
"author": "Donskov D. <donskovdmitriyv@gmail.com>",
24+
"license": "MIT",
25+
"bugs": {
26+
"url": "https://github.com/donskov/static-render-html-webpack-plugin/issues"
27+
},
28+
"homepage": "https://github.com/donskov/static-render-html-webpack-plugin",
29+
"dependencies": {
30+
"babel-register": "^6.22.0",
31+
"chai-spies": "^0.7.1",
32+
"chalk": "^1.1.3",
33+
"fs-extra": "^2.0.0",
34+
"pretty": "^1.0.0",
35+
"react": "^15.4.2",
36+
"react-dom": "^15.4.2",
37+
"requirefresh": "^2.1.0",
38+
"webpack": "^1.14.0"
39+
},
40+
"devDependencies": {
41+
"babel-cli": "^6.22.2",
42+
"babel-preset-es2015": "^6.22.0",
43+
"babel-preset-react": "^6.22.0",
44+
"babel-preset-stage-2": "^6.22.0",
45+
"chai": "^3.5.0",
46+
"mocha": "^3.2.0",
47+
"rimraf": "^2.5.4"
48+
},
49+
"babel": {
50+
"presets": [
51+
"es2015",
52+
"stage-2",
53+
"react"
54+
]
55+
}
56+
}

0 commit comments

Comments
 (0)