maxxton-typescript-loadable-components-plugin
v2.0.0
Published
A custom transformer of typescript that is used to add some necessary properties to loadable-components.
Maintainers
Readme
MAXXTON README
We forked this repo because of MCMS-4647 and MCMS-11957. We had to upgrade it to ts5 to make sure cms-web can run with ts5. And we had to adjust the isReady transformation to fix some empty page issue described in MCMS-4647. (see src/properties/createIsReady.ts for those changes).
To develop/debug with this package follow these steps:
npm installnpm run build-on-windows(normal build is made for linux/mac i guess)npm link- Now in your cms-web repo run:
npm link maxxton-typescript-loadable-components-plugin - Now if you run
npm run webpackClientin cms-web it will build the js files using this transformation (according to the webpack settings) - You will see this code is added in your bundle js files:
const MenuView = (0,_loadable_component__WEBPACK_IMPORTED_MODULE_29__["default"])({
chunkName() { return "plugins-menu-menu-view-Menu"; },
isReady(props) { if ( true && Boolean(__webpack_require__.m[this.resolve(props)]))
try {
this.requireSync(props);
return "true" == "true";
}
catch (_a) {
return "true" !== "true";
} return "true" !== "true"; },
requireAsync: () => (0,_utils_loadableComponents_util__WEBPACK_IMPORTED_MODULE_30__.loadableRetry)(() => __webpack_require__.e(/*! import() | plugins-menu-menu-view-Menu */ "plugins-menu-menu-view-Menu").then(__webpack_require__.bind(__webpack_require__, /*! ../plugins/menu/menu-view/Menu */ "./src/plugins/menu/menu-view/Menu.tsx"))),
requireSync(props) { return true ? __webpack_require__(this.resolve(props)) : 0; },
resolve() { if (true)
return /*require.resolve*/(/* webpackChunkName: "plugins-menu-menu-view-Menu" */ /*! ../plugins/menu/menu-view/Menu */ "./src/plugins/menu/menu-view/Menu.tsx");
else
{} }
}, {
resolveComponent: ({ MenuView }) => MenuView,
});- If you don't see those chunkName, isReady, requireAsync, etc. fields, then the transformation didn't work and loadable components will be broken.
If you need to publish a new version:
npm run build-on-windowsnpm run testnpm publish
/MAXXTON README
typescript-loadable-components-plugin
A custom transformer of typescript that is used to add some necessary properties to loadable-components.
This transformer helps you to transform code like:
import loadable from '@loadable/component';
export const LazyFoo = loadable(() => import('./input/AsyncDefaultComponent'));to the following format:
import loadable from 'loadable-components';
export const LazyFoo = loadable({
chunkName() {
return 'input-AsyncDefaultComponent';
},
isReady(props) {
return (
typeof __webpack_modules__ !== 'undefined' &&
Boolean(__webpack_modules__[this.resolve(props)])
);
},
requireAsync: () =>
import(
/* "webpackChunkName":"input-AsyncDefaultComponent" */ './input/AsyncDefaultComponent'
),
requireSync(props) {
return typeof '__webpack_require__' !== 'undefined'
? __webpack_require__(this.resolve(props))
: eval('module.require')(this.resolve(props));
},
resolve() {
if (require.resolveWeak)
return require.resolveWeak(
/* "webpackChunkName":"input-AsyncDefaultComponent" */ './input/AsyncDefaultComponent',
);
else
return eval('require.resolve')(
/* "webpackChunkName":"input-AsyncDefaultComponent" */ './input/AsyncDefaultComponent',
);
},
});Install
yarn add typescript-loadable-components-plugin -D
# or npm
npm install typescript-loadable-components-plugin -DUsage
with ttypescript
you just need add typescript--plugin to your tsconfig.json:
{
"compilerOptions": {
"plugins": [
{
"transform": "typescript-loadable-components-plugin"
}
]
}
}with webpack and ts-loader
you need to add the following options to your loader:
import { createLoadableComponentsTransformer } from 'typescript-loadable-components-plugin';
export default {
// ...
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader', // or awesome-typescript-loader
options: {
getCustomTransformers: (program) => ({
before: [createLoadableComponentsTransformer(program, {})],
}),
},
},
],
},
};Options
No options needed
Notes
Supported syntax:
- loadable default component:
loadable(() => import(...), [options]) - loadable lib component:
loadable.lib(() => import(...), [options])
LICENSE
MIT
The MIT License (MIT)
Copyright (c) 2019 acrazing
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.