使用样式化组件进行预处理会导致浏览器控制台出错

Preact with styled-components causes error in browser console

简单地导入 styled-components 会在浏览器控制台中导致此错误:

styled-components.browser.esm.js?face:1670 Uncaught TypeError: (0 , _react.createContext) is not a function
    at Object.eval (styled-components.browser.esm.js?face:1670)
    at eval (styled-components.browser.esm.js:2490)
    at Object../node_modules/styled-components/dist/styled-components.browser.esm.js (vendors~index.js:167)
    at __webpack_require__ (index.js:79)
    at eval (index.js?12d5:2)
    at Object../src/index.js (index.js:165)
    at __webpack_require__ (index.js:79)
    at checkDeferredModules (index.js:46)
    at index.js:152
    at index.js:155

我正在使用 webpack、preact、Babel。

要重现的代码实际上就是这样,在一个新的空构建中:

src/index.js:

import { h, Component, render } from "preact"
import styled from "styled-components"

package.json:

{
  "name": "App",
  "version": "0.0.1",
  "description": "Web app.",
  "main": "index.js",
  "scripts": {
    "build": "rm -rf dist/* && NODE_ENV=development webpack -d --config webpack.conf.js --env development"
  },
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-react-jsx": "^6.24.1",
    "babel-preset-env": "^1.7.0",
    "babel-preset-react": "^6.24.1",
    "html-webpack-plugin": "^3.2.0",
    "preact": "^8.4.2",
    "preact-compat": "^3.18.4",
    "styled-components": "^4.1.3",
    "webpack": "^4.28.4",
    "webpack-cli": "^3.2.1"
  },
  "dependencies": {}
}

webpack.conf.js:

const path = require("path")
const webpack = require("webpack")
const htmlWebpackPlugin = require("html-webpack-plugin")

module.exports = {
  entry: {
    index: "./src/index.js"
  },
  module: {
    rules: [{
      test: /\.(js|jsx)$/i,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            'babel-preset-env',
            'babel-preset-react'
          ],
          plugins: [
            [ 'babel-plugin-transform-react-jsx', {
              pragma: 'h'
            }],
          ]
        }
      }
    }]
  },
  output: {
    publicPath: ("http://your-hostname.com/"),
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js',
    chunkFilename: '[name].js'
  },
  optimization: {
    splitChunks: {
      chunks: 'all'
    },
    occurrenceOrder: true
  },
  plugins: [
    new htmlWebpackPlugin({
      template: './public/index.html',
      filename: 'index.html'
})
  ],
  resolve: {
    alias: {
      'react': 'preact-compat',
      'react-dom': 'preact-compat'
    }
  }
}

使用 npm run build 构建工作正常,但在浏览器中访问生成的页面会产生上述错误。

如有指点,将不胜感激。

编辑:

根据 this issue logged against Preact-CLI,preact 没有 React 的 contextApi 所以我应该将 styled-components 降级到 v3 而不是 v4。降级似乎确实可以解决问题。

令人困惑的是,preact 的作者 Jason Miller 说 in a tweet "preact supports context just fine"。我可能误解了这一点。

请参阅此处 https://github.com/developit/preact-cli/issues/672#issuecomment-440985092 和我上面的编辑。

正在将 styled-components 从 v4 降级到 v3 "fixes" 问题。

免责声明:我从事 preact。

您看到此错误是因为 Preact 8.x 不支持 createContext-API。它将成为我们即将发布的下一个主要版本的一部分。我们目前处于测试阶段,并希望尽快进入候选版本。

如果您绝对需要 styled-components 来使用 Preact 8.x,那么您唯一的选择就是像@ukosteopath 建议的那样降级到 styled-components V3。