微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有动态加载的 Preact 和 react-redux 错误

如何解决带有动态加载的 Preact 和 react-redux 错误

我正在使用 preact,并使用 webpack 代码拆分在我的代码中动态加载模块。 如果我使用脚本标签加载我的核心包,一切正常。如果我像下面的代码那样动态加载它,捆绑包就会中断:

const script = document.createElement('script');
script.src = 'bundle-location.js';
document.body.appendChild(script);

这是我得到的错误

enter image description here

如果我在 Context.js 文件添加断点,我可以看到问题:

enter image description here

在第一种情况下,如果使用 script 标签,那么 react__WEBPACK_IMPORTED_MODULE_0__ 实际上包含 preact。出于某种原因,如果我动态加载相同的脚本,则 preact 对象存在,但缺少其所有导出,包括认”导出。 请务必注意,如果我禁用代码拆分,则一切正常。

这是我的 webpack 配置:

const config = {
  mode: getMode(),resolve: {
    alias: {
      react: 'preact/compat','react-dom': 'preact/compat',},extensions: ['.ts','.tsx','.js'],module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,exclude: /node_modules/,use: {
          loader: 'babel-loader',{
        test: /\.svg$/,use: [
          'babel-loader',{
            loader: 'react-svg-loader',query: {
              svgo: {
                pretty: true,plugins: [
                  {
                    removeStyleElement: true,{
                    removeViewBox: false,],}],plugins: [
    new webpack.DefinePlugin({
      PLUGINS_PATH: JSON.stringify(getPluginsPath()),}),entry: './src/index.ts',output: {
    path: path.resolve(__dirname,'./src/dist/bundle/'),filename: 'bundle.js',chunkFilename: '[name].js',publicPath: getPluginsPath(),};

module.exports = config;

非常感谢任何方向。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。