Repository: webpack-contrib/style-loader (js)

Issue:
v.1.1.1 throw error when accessing the page
<!--
  Issues are so 🔥

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  👉🏽 Need support, advice, or help? Don't open an issue!
  Head to StackOverflow or https://gitter.im/webpack/webpack.
-->

- Operating System: macOS 10.14.6
- Node Version: 12.14.0
- NPM Version: 6.13.4 (yarn 1.19.1)
- webpack Version: 4.41.3
- style-loader Version: 1.1.1

### Expected Behavior
`style-loader` operating normally
<!-- Remove this section if not reporting a bug or modification request. -->

### Actual Behavior

After I upgraded from 1.0.2 to 1.1.1, webpack compiles, but I get an error (`Uncaught RangeError: Invalid array length`) when accessing the webpage.When I downgrade 1.0.2, it's fine.

<!-- Remove this section if not reporting a bug or modification request. -->

### Code

```js
// webpack.config.js
// If your code blocks are over 20 lines, please paste a link to a gist
// (https://gist.github.com).
{
        test: /\.s?css$/,
        use: [
          "style-loader",
          MiniCssExtractPlugin.loader,
          "css-loader",
          "postcss-loader",
          "sass-loader"
        ]
}
```

### How Do We Reproduce?

Stated in "Actual Behavior".

<!--
  Remove this section if not reporting a bug.

  If your webpack config is over 50 lines long, please provide a URL to a repo
  for your beefy 🍖 app that we can use to reproduce.
-->


Pull request description:
fix: checking that the list of modules is an array
<!--
  HOLY CRAP a Pull Request. We ❤️ those!

  If you remove or skip this template, you'll make the 🐼 sad and the mighty god
  of Github will appear and pile-drive the close button from a great height
  while making animal noises.

  Please place an x (no spaces!) in all [ ] that apply
-->

This PR contains a:

- [x] **bugfix**
- [ ] new **feature**
- [ ] **code refactor**
- [x] **test update** <!-- if bug or feature is checked, this should be too -->
- [ ] **typo fix**
- [ ] **metadata update**

### Motivation / Use-Case

fixes #447

### Breaking Changes

No

### Additional Info

Supporting non-array list of modules will be removed in next version, my recommendation is https://github.com/webpack-contrib/style-loader/issues/447#issuecomment-568685446

Patch:
diff --git a/src/runtime/injectStylesIntoStyleTag.js b/src/runtime/injectStylesIntoStyleTag.js
index 0c8c0e5..068827f 100644
--- a/src/runtime/injectStylesIntoStyleTag.js
+++ b/src/runtime/injectStylesIntoStyleTag.js
@@ -47,6 +47,10 @@ const getTarget = (function getTarget() {
 })();
 
 function addModulesToDom(id, list, options) {
+  if (Object.prototype.toString.call(list) !== '[object Array]') {
+    return;
+  }
+
   id = options.base ? id + options.base : id;
 
   if (!stylesInDom[id]) {


Test patch:
diff --git a/test/runtime/__snapshots__/injectStylesIntoStyleTag.test.js.snap b/test/runtime/__snapshots__/injectStylesIntoStyleTag.test.js.snap
index b42479c..16927a9 100644
--- a/test/runtime/__snapshots__/injectStylesIntoStyleTag.test.js.snap
+++ b/test/runtime/__snapshots__/injectStylesIntoStyleTag.test.js.snap
@@ -1,5 +1,7 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
+exports[`addStyle issue 447 1`] = `"<head><title>Title</title></head><body><h1>Hello world</h1></body>"`;
+
 exports[`addStyle should throw error with incorrect "insert" option 1`] = `"Couldn't find a style target. This probably means that the value for the 'insert' parameter is invalid."`;
 
 exports[`addStyle should throw error with invalid "insert" option 1`] = `"'#test><><><' is not a valid selector"`;
diff --git a/test/runtime/injectStylesIntoStyleTag.test.js b/test/runtime/injectStylesIntoStyleTag.test.js
index d5ea480..eb75b1c 100644
--- a/test/runtime/injectStylesIntoStyleTag.test.js
+++ b/test/runtime/injectStylesIntoStyleTag.test.js
@@ -706,4 +706,10 @@ describe('addStyle', () => {
 
     expect(document.documentElement.innerHTML).toMatchSnapshot();
   });
+
+  it('issue 447', () => {
+    injectStylesIntoStyleTag(getId(), {});
+
+    expect(document.documentElement.innerHTML).toMatchSnapshot();
+  });
 });