【问题标题】:How to lazy load components in preact?如何预先延迟加载组件?
【发布时间】:2020-03-12 14:20:00
【问题描述】:

我的目标是在构建时获得两个包,一个用于index.tsx 文件,另一个用于lazy.tsx 文件。我很确定我只是在某处遗漏了一两个选项。

GitHub MCVE - example project link

文件src/index.tsx

import { render, h } from 'preact';

let a: any = null;
let b = 0;

const App = () => (
  <div>
    <button
      onClick={() => {
        import('./lazy').then(M => {
          console.log('LOADED COMPONENT');
          a = <M.default />;
          b++;
          render(<App></App>, document.body);
        });
      }}
    >
      test
    </button>
    {a} {b}
  </div>
);

render(<App></App>, document.body);

src/lazy.tsx

import { h, FunctionComponent } from 'preact';

console.log('LAZY');

interface LazyProps {}

const Lazy: FunctionComponent<LazyProps> = props => {
  const {} = props;

  return <div>LAZY LOADED COMPONENT</div>;
};

export default Lazy;

Webpack 配置

{
  entry: {
    index: `${__dirname}/src/index.tsx`
  },
  output: {
    path: resolve(__dirname, 'dist'),
    chunkFilename: '[name].[id].js',
    filename: '[name].bundle.js'
  },

  plugins: [new HtmlWebpackPlugin()],

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: [/node_modules/]
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  optimization: {
    minimize: false
  }
}

上面的代码工作正常,没有任何错误,但它没有像 Webpack 代码拆分那样生成多个包。

编辑: 这是working example project的链接

【问题讨论】:

  • 您遇到的错误是什么?
  • @HarshalPatil 我没有收到错误,问题是 webpack 将所有代码捆绑到 index.bundle.js 中,但我想要多个捆绑包,以便在需要时加载它们。跨度>
  • 你使用的是 Webpack 第 4 版吗?
  • @HarshalPatil 是的,我使用的是最新的 webpack 版本,我昨天开始做这个项目。
  • 好的。知道了。我在您的示例存储库中看到了。

标签: javascript typescript webpack preact


【解决方案1】:

您的配置有两个问题。

首先使用您的 TypeScript 配置:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "ESNext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "react",
    "jsxFactory": "h",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  },

  "include": [
    "src/**/*.tsx"
  ]
}

要记住的两个重要属性是:

  1. module: 'ESNext'
  2. moduleResolution: '节点'

一旦将module 设置为ESNext,就不能在TypeScript 中编写Webpack 配置,因为它期望模块为commonjs。所以改成普通JS格式的普通JS:

const Configuration = require('webpack').Configuration;
const Dev = require('webpack-dev-server').Configuration;
const resolve = require('path').resolve;
const HtmlWebpackPlugin = require('html-webpack-plugin');


const config = {
  entry: {
    index: `${__dirname}/src/index.tsx`
  },
  output: {
    path: resolve(__dirname, 'dist'),
    chunkFilename: '[name].[id].js',
    filename: '[name].bundle.js'
  },

  plugins: [new HtmlWebpackPlugin()],

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: [/node_modules/]
      }
    ]
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  optimization: {
    minimize: false
  }
};

module.exports = config;

这应该可以解决您的问题。

【讨论】:

  • 非常感谢,我仍然可以在 ts 中编写我的配置,我只需要更改 snytax。
猜你喜欢
  • 2023-02-02
  • 2021-12-10
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2019-03-11
  • 2013-03-01
相关资源
最近更新 更多