acrazing / typescript-loadable-components-plugin Goto Github PK
View Code? Open in Web Editor NEWA custom transformer of typescript that is used to add some necessary properties to loadable-components.
License: MIT License
A custom transformer of typescript that is used to add some necessary properties to loadable-components.
License: MIT License
Hi @acrazing,
first of all, thank you very much for your work. It works like a charm, except for one thing.
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:
But when I remove ts-loader
and just use babel-loader
it works:
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.
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;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.