Skip to content

Commit 97bbfc9

Browse files
refactor: removing insert-string option
1 parent 691eb88 commit 97bbfc9

File tree

13 files changed

+37
-600
lines changed

13 files changed

+37
-600
lines changed

README.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ module.exports = {
580580

581581
### insert
582582

583-
Type: `String|Function`
583+
Type: `Function`
584584
Default: `head`
585585

586586
By default, the `extract-css-chunks-plugin` appends styles (`<link>` elements) to `document.head` of the current `window`.
@@ -589,19 +589,7 @@ However in some circumstances it might be necessary to have finer control over t
589589

590590
If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure that the parent document has sufficient access rights to reach into the frame document and append elements to it.
591591

592-
#### `insert` as a string
593-
594-
Allows to configure a [CSS selector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) that will be used to find the element where to append the styles (`link` elements).
595-
596-
```js
597-
new MiniCssExtractPlugin({
598-
insert: '#my-container',
599-
});
600-
```
601-
602-
A new `<link>` element will be appended to the `#my-container` element.
603-
604-
#### `insert` as a function
592+
#### `insert`
605593

606594
Allows to override default behavior and insert styles at any position.
607595

src/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,7 @@ class CssModuleFactory {
101101
class MiniCssExtractPlugin {
102102
constructor(options = {}) {
103103
validateOptions(schema, options, 'Mini CSS Extract Plugin');
104-
const insert =
105-
typeof options.insert === 'undefined'
106-
? '"head"'
107-
: typeof options.insert === 'string'
108-
? JSON.stringify(options.insert)
109-
: options.insert.toString();
104+
const insert = options.insert ? options.insert.toString() : null;
110105
this.options = Object.assign(
111106
{
112107
filename: DEFAULT_FILENAME,
@@ -383,8 +378,8 @@ class MiniCssExtractPlugin {
383378
])
384379
: '',
385380
`var insert = ${insert};`,
386-
`if (typeof insert === 'function') { insert(linkTag); }`,
387-
`else { var target = document.querySelector(${insert}); target && insert === 'body' ? target && target.insertBefore(linkTag,target.firstChild) : target.appendChild(linkTag); } `,
381+
`if (insert) { insert(linkTag); }`,
382+
`else {var head = document.getElementsByTagName("head")[0]; head.appendChild(linkTag);} `,
388383
]),
389384
'}).then(function() {',
390385
Template.indent(['installedCssChunks[chunkId] = 0;']),
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* global page, document, getComputedStyle */
2+
3+
const { setup: setupDevServer } = require('jest-dev-server');
4+
5+
describe('insert-function', () => {
6+
beforeAll(async () => {
7+
await setupDevServer({
8+
command: `webpack-dev-server test/cases/insert-function/src/index.js --config test/cases/insert-function/webpack.config.e2e.js`,
9+
port: 3001,
10+
launchTimeout: 15000,
11+
});
12+
await page.goto('http://localhost:3001/');
13+
});
14+
it('stylesheet was injected into body', async () => {
15+
const bodyHTML = await page.evaluate(() => document.body.innerHTML);
16+
17+
await expect(bodyHTML.indexOf('type="text/css"') > 0).toBe(true);
18+
});
19+
20+
it('body background style set correctly', async () => {
21+
await page.waitFor(4000);
22+
const bodyStyle = await page.evaluate(() =>
23+
getComputedStyle(document.body).getPropertyValue('background-color')
24+
);
25+
26+
await expect(bodyStyle).toBe('rgb(255, 0, 0)');
27+
});
28+
afterAll(() => {
29+
const childProcess = require('child_process').exec;
30+
childProcess(`kill $(lsof -t -i:3001)`);
31+
});
32+
});

test/cases/insert-string/expected/1.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/cases/insert-string/expected/1.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/cases/insert-string/expected/main.js

Lines changed: 0 additions & 260 deletions
This file was deleted.

0 commit comments

Comments
 (0)