webpack-inject-plugin
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.registry = undefined; | ||
exports.registry = exports.ENTRY_ORDER = undefined; | ||
@@ -44,7 +44,33 @@ var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); | ||
function injectEntry(originalEntry, newEntry, entryName) { | ||
var ENTRY_ORDER = exports.ENTRY_ORDER = { | ||
First: 'first', | ||
Last: 'last', | ||
NotLast: 'notLast' | ||
}; | ||
function injectToArray(originalEntry, newEntry) { | ||
var entryOrder = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'notLast'; | ||
if (!_lodash2.default.isArray(originalEntry)) { | ||
throw new TypeError('Expected entry to be an array'); | ||
} | ||
if (entryOrder === ENTRY_ORDER.First) { | ||
return [newEntry].concat((0, _toConsumableArray3.default)(originalEntry)); | ||
} else if (entryOrder === ENTRY_ORDER.Last) { | ||
return [].concat((0, _toConsumableArray3.default)(originalEntry), [newEntry]); | ||
} | ||
return [].concat((0, _toConsumableArray3.default)(originalEntry.splice(0, originalEntry.length - 1)), [newEntry], (0, _toConsumableArray3.default)(originalEntry.splice(originalEntry.length - 1))); | ||
} | ||
function injectEntry(originalEntry, newEntry) { | ||
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var entryName = options.entryName, | ||
entryOrder = options.entryOrder; | ||
// Last module in an array gets exported, so the injected one must not be | ||
// last. https://webpack.github.io/docs/configuration.html#entry | ||
if (_lodash2.default.isArray(originalEntry)) { | ||
return [].concat((0, _toConsumableArray3.default)(originalEntry.splice(0, originalEntry.length - 1)), [newEntry], (0, _toConsumableArray3.default)(originalEntry.splice(originalEntry.length - 1))); | ||
return injectToArray(originalEntry, newEntry, entryOrder); | ||
} | ||
@@ -60,3 +86,3 @@ | ||
if (_lodash2.default.isString(originalEntry)) { | ||
return [newEntry, originalEntry]; | ||
return injectToArray([originalEntry], newEntry, entryOrder); | ||
} | ||
@@ -88,3 +114,6 @@ | ||
// Append an entry for our fake module | ||
compiler.options.entry = injectEntry(compiler.options.entry, moduleLocation, this.options.entry); | ||
compiler.options.entry = injectEntry(compiler.options.entry, moduleLocation, { | ||
entryName: this.options.entry, | ||
entryOrder: this.options.order | ||
}); | ||
@@ -91,0 +120,0 @@ // Add a loader for our fake module |
{ | ||
"name": "webpack-inject-plugin", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A webpack plugin to dynamically inject code into the bundle.", | ||
@@ -5,0 +5,0 @@ "main": "dist/main.js", |
@@ -32,6 +32,10 @@ # webpack-inject-plugin [](https://travis-ci.org/adierkens/webpack-inject-plugin) [](https://badge.fury.io/js/webpack-inject-plugin) | ||
```javascript | ||
import InjectPlugin, { ENTRY_ORDER } from 'webpack-inject-plugin'; | ||
new InjectPlugin(loader, { | ||
entry: 'entry name' // The name of the entry to inject code into, if multiple are used | ||
}) | ||
entry: 'entry name', // Limit the injected code to only the entry w/ this name | ||
order: ENTRY_ORDER.First // Make the injected code be the first entry point | ||
ENTRY_ORDER.Last // Make the injected code be the last entry point | ||
ENTRY_ORDER.NotLast // Make the injected code be second to last. (The last entry module is the API of the bundle. Useful when you don't want to override that.) This is the default. | ||
}); | ||
@@ -38,0 +42,0 @@ ``` |
@@ -20,7 +20,7 @@ import test from 'ava'; | ||
test('appends to only the specified entry', t => { | ||
t.is(injectEntry(undefined, 'foo', 'bar'), 'foo'); | ||
t.is(injectEntry(undefined, 'foo', {entryName: 'bar'}), 'foo'); | ||
t.deepEqual(injectEntry({ | ||
foo: 'bar', | ||
bar: 'baz' | ||
}, 'added', 'bar'), { | ||
}, 'added', {entryName: 'bar'}), { | ||
foo: 'bar', | ||
@@ -30,1 +30,36 @@ bar: ['added', 'baz'] | ||
}); | ||
test('respects config for order', t => { | ||
t.deepEqual(injectEntry([ | ||
'foo', | ||
'bar' | ||
], 'baz', {entryOrder: 'first'}), [ | ||
'baz', | ||
'foo', | ||
'bar' | ||
]); | ||
t.deepEqual(injectEntry([ | ||
'foo', | ||
'bar' | ||
], 'baz', {entryOrder: 'last'}), [ | ||
'foo', | ||
'bar', | ||
'baz' | ||
]); | ||
t.deepEqual(injectEntry([ | ||
'foo', | ||
'bar' | ||
], 'baz', {entryOrder: 'notLast'}), [ | ||
'foo', | ||
'baz', | ||
'bar' | ||
]); | ||
}); | ||
test('order config for strings', t => { | ||
t.deepEqual(injectEntry('original', 'new', {entryOrder: 'first'}), ['new', 'original']); | ||
t.deepEqual(injectEntry('original', 'new', {entryOrder: 'last'}), ['original', 'new']); | ||
t.deepEqual(injectEntry('original', 'new', {entryOrder: 'notLast'}), ['new', 'original']); | ||
}); |
@@ -7,7 +7,29 @@ import path from 'path'; | ||
export function injectEntry(originalEntry, newEntry, entryName) { | ||
export const ENTRY_ORDER = { | ||
First: 'first', | ||
Last: 'last', | ||
NotLast: 'notLast' | ||
}; | ||
function injectToArray(originalEntry, newEntry, entryOrder = 'notLast') { | ||
if (!_.isArray(originalEntry)) { | ||
throw new TypeError('Expected entry to be an array'); | ||
} | ||
if (entryOrder === ENTRY_ORDER.First) { | ||
return [newEntry, ...originalEntry]; | ||
} else if (entryOrder === ENTRY_ORDER.Last) { | ||
return [...originalEntry, newEntry]; | ||
} | ||
return [...originalEntry.splice(0, originalEntry.length - 1), newEntry, ...originalEntry.splice(originalEntry.length - 1)]; | ||
} | ||
export function injectEntry(originalEntry, newEntry, options = {}) { | ||
const {entryName, entryOrder} = options; | ||
// Last module in an array gets exported, so the injected one must not be | ||
// last. https://webpack.github.io/docs/configuration.html#entry | ||
if (_.isArray(originalEntry)) { | ||
return [...originalEntry.splice(0, originalEntry.length - 1), newEntry, ...originalEntry.splice(originalEntry.length - 1)]; | ||
return injectToArray(originalEntry, newEntry, entryOrder); | ||
} | ||
@@ -26,3 +48,3 @@ | ||
if (_.isString(originalEntry)) { | ||
return [newEntry, originalEntry]; | ||
return injectToArray([originalEntry], newEntry, entryOrder); | ||
} | ||
@@ -50,3 +72,6 @@ | ||
// Append an entry for our fake module | ||
compiler.options.entry = injectEntry(compiler.options.entry, moduleLocation, this.options.entry); | ||
compiler.options.entry = injectEntry(compiler.options.entry, moduleLocation, { | ||
entryName: this.options.entry, | ||
entryOrder: this.options.order | ||
}); | ||
@@ -53,0 +78,0 @@ // Add a loader for our fake module |
14402
274
67