GithubHelp home page GithubHelp logo

acrazing / typescript-loadable-components-plugin Goto Github PK

View Code? Open in Web Editor NEW
6.0 4.0 2.0 632 KB

A custom transformer of typescript that is used to add some necessary properties to loadable-components.

License: MIT License

TypeScript 52.55% JavaScript 47.45%
typescript loadable-components transformer ttypescript webpack plugin

typescript-loadable-components-plugin's People

Contributors

acrazing avatar dependabot[bot] avatar jkap avatar miniven avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

maxxtongroup jkap

typescript-loadable-components-plugin's Issues

webpackChunkNames are not applied/used, instead chunks are called "0.js", "1.js" and so on

Hi @acrazing,

first of all, thank you very much for your work. It works like a charm, except for one thing.

The Problem

When I use this plugin with babel-loader and ts-loader, webpackChunkName does not work and chunks are just called 0.js, 1.js and so on. Here is a link to the App.tsx, babel- and tsconfig.

// app.tsx
const Home = loadable(() => import('./pages/home'))
const About = loadable(() => import('./pages/about'))
// webpack.config.js
 module: {
    rules: [
      {
        test: /\.(ts)x?$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'babel-loader',
          },
          {
            loader: 'ts-loader',
            options: {
              getCustomTransformers: (program) => ({
                before: [createLoadableComponentsTransformer(program, {})],
              }),
            },
          },
        ],
      },
    ],
  },
// babel.config.js
module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-react',
    '@babel/preset-typescript',
  ],
  plugins: [
    '@loadable/babel-plugin',
  ],
}
// tsconfig.json
{
  "compilerOptions": {
    "target": "ESNext",
    "module": "esnext",
    "jsx": "react",
    "sourceMap": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "include": ["src/**/*"]
}

results in:

image

But when I remove ts-loader and just use babel-loader it works:

image

It's not even about babel-loader. It doesn't work when I just use ts-loader with your plugin too. What am I missing? Is my tsconfig wrong?

Can you please help me? Any form of support is appreciated.

Example Repo

Error: Debug Failure. Invalid cast. The supplied value [object Object] did not pass the test 'isOptionalChain'.

When I use this plugin with ts-loader and webpack, I got this error:

>> npm run startblog

> [email protected] startblog /home/hacksign/Code/blogstation
> cross-env NODE_ENV=development node -r ts-node/register ./frontend/index.ts

Example app listening on port 3000!

/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:1693
        return ts.Debug.fail("Invalid cast. The supplied value " + value + " did not pass the test '" + ts.Debug.getFunctionName(test) + "'.");
                        ^
Error: Debug Failure. Invalid cast. The supplied value [object Object] did not pass the test 'isOptionalChain'.
    at Object.cast (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:1693:25)
    at flattenChain (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:85256:28)
    at visitOptionalExpression (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:85311:22)
    at visitor (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:85236:39)
    at visitNode (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:78322:23)
    at Object.visitEachChild (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:78712:225)
    at visitor (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:85249:31)
    at visitNodes (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:78375:48)
    at Object.visitEachChild (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:78714:68)
    at visitor (/home/hacksign/Code/blogstation/node_modules/typescript/lib/typescript.js:85249:31)

webpack.config.js:

import path from 'path';
import { Program } from 'typescript';
import webpack from 'webpack';
import LoadablePlugin from '@loadable/webpack-plugin';
import { createLoadableComponentsTransformer } from 'typescript-loadable-components-plugin';

const config : webpack.Configuration = {
  mode: process.env.NODE_ENV === 'development' ? 'development' : 'production',
  target: 'web',
  devtool: 'source-map',
  entry: {
    index: path.resolve(__dirname, '../frontend/containers/default/index.tsx')
  },
  output: {
    filename: '[name].bundle.js',
    chunkFilename: '[name].chunk.js',
    path: path.resolve(__dirname, '../dist'),
    publicPath: '/',
  },
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: (module) => {
            const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
            return `npm.${packageName.replace('@', '')}`;
          },
        }
      }
    }
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        loader: 'ts-loader',
        options: {
          getCustomTransformers: (program : any) => ({
            before: [createLoadableComponentsTransformer(program as Program, {})],
          }),
        },
      },
    ],
  },
  resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
  },
  plugins: [
    new LoadablePlugin(),
  ]
}

export default config;

And the file caused the problem:

import React from 'react';
import { Route } from 'react-router-dom';
import loadable from '@loadable/component';
import Header from '../../components/header';
import Poster from '../../components/poster';
import Footer from '../../components/footer';
import Content from '../../components/content';

const HeaderAsync = loadable(
  () => import('../../components/header')
)
const Component = () => {
  return (
    <React.Fragment>
        <HeaderAsync />  // ERROR HERER: will receive above error when use loadable, remove this component will pass the compile
        <Poster />
        <Route path='/' exact component={Content} />
        <Footer />
    </React.Fragment>
  );
}

export default Component;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.