Skip to content

Commit

Permalink
Feat: looks work fine 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
kagawagao committed Jan 26, 2018
1 parent 846a3bd commit 5a15c5c
Show file tree
Hide file tree
Showing 11 changed files with 5,683 additions and 1 deletion.
16 changes: 16 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"presets": [
[
"env",
{
"target": {
"node": 4
}
}
],
"stage-0"
],
"plugins": [
"add-module-exports"
]
}
14 changes: 14 additions & 0 deletions .bithoundrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"ignore": [
"**/node_modules/**",
"**/lib/**",
"**/es/**",
"**/dist/**"
],
"test": [
"**/test/**"
],
"critics": {
"lint": {"engine": "eslint"}
}
}
30 changes: 30 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# http://editorconfig.org

# A special property that should be specified at the top of the file outside of
# any sections. Set to true to stop .editor config file search on current file
root = true

[*]
# Indentation style
# Possible values - tab, space
indent_style = space

# Indentation size in single-spaced characters
# Possible values - an integer, tab
indent_size = 2

# Line ending file format
# Possible values - lf, crlf, cr
end_of_line = lf

# File character encoding
# Possible values - latin1, utf-8, utf-16be, utf-16le
charset = utf-8

# Denotes whether to trim whitespace at the end of lines
# Possible values - true, false
trim_trailing_whitespace = true

# Denotes whether file should end with a newline
# Possible values - true, false
insert_final_newline = true
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/

lib/
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "standard",
"parser": "babel-eslint"
}
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
sudo: false
language: node_js
node_js:
- 4.0.0
- 5.0.0
- 6.0.0
- 8.9.1
- 9.2.0

install:
- npm install

script:
- npm run build
50 changes: 49 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# html-webpack-inject-plugin
inject external source to html

inject external tag to html

## Installation

You must be running webpack on node 4.x or higher

```bash
npm install --save-dev html-webpack-inject-plugin
```

## Usage

Require the plugin in your webpack config

```javascript
import HtmlWebpackInjectPlugin from 'html-webpack-inject-plugin'
// or
var HtmlWebpackInjectPlugin = require('html-webpack-inject-plugin')
```

Add the plugin to your webpack config as follows

```javascript
plugins: [
new HtmlWebpackPlugin(),
new HtmlWebpackIncludeAssetsPlugin({
externals: [{
tag: 'script',
attrs: {
src: 'your-script.js',
type: 'text/javascript'
}
}],
parent: 'head' // default is head
})
]
```

## Options

- `externals: Array<Tag>`: external [tags](#Tag) which you want to add

- `parent`: parent element will be added into, only can be `head` and `body`, default value is `head`

## Tag

- `tag`: tag name, such as `meta` `link` `script`
- `attrs`: [html attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes)
53 changes: 53 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var HtmlWebpackInjectPlugin = function HtmlWebpackInjectPlugin(config) {
var _this = this;

_classCallCheck(this, HtmlWebpackInjectPlugin);

this.apply = function (compiler) {
compiler.plugin('compilation', function (compilation) {
compilation.plugin('html-webpack-plugin-alter-asset-tags', function (htmlPluginData, cb) {
if (_this.parent === 'head') {
htmlPluginData.head = htmlPluginData.head.concat(_this.assets);
} else {
htmlPluginData.body = _this.assets.concat(htmlPluginData.body);
}
return cb(null, htmlPluginData);
});
});
};

var _config$externals = config.externals,
externals = _config$externals === undefined ? [] : _config$externals,
_config$parent = config.parent,
parent = _config$parent === undefined ? 'head' : _config$parent;


this.assets = externals.map(function (_ref) {
var _ref$tag = _ref.tag,
tag = _ref$tag === undefined ? 'meta' : _ref$tag,
_ref$attrs = _ref.attrs,
attrs = _ref$attrs === undefined ? {} : _ref$attrs;

return {
tagName: tag,
attributes: attrs,
closeTag: true
};
});

if (parent !== 'head' && parent !== 'body') {
throw new TypeError('parent should be one of head and body');
}
this.parent = parent;
};

exports.default = HtmlWebpackInjectPlugin;
module.exports = exports['default'];
Loading

0 comments on commit 5a15c5c

Please sign in to comment.