pasoul / jtools Goto Github PK
View Code? Open in Web Editor NEW🖇 js工具库
🖇 js工具库
本文列出常见的几种加载方式,旨在探索最佳加载实践。
jlb-tools
版本:v0.0.4
webpack
版本: v4.25.1
import jtools from "jlb-tools";
打包后体积查看
import { handleEmoji } from "jlb-tools";
打包后体积查看
发现体积依然是22.2Kb,部分加载没有生效,依然引入所有的js文件。按需加载没有生效?为什么element-ui 可以通过 import { Button } from 'element-ui';
方式按需加载?
实际上 element-ui
想要实现按需加载,必须要借助babel-plugin-component插件完成,此插件将import { Button } from 'components'
引入方法编译成var button = require('components/lib/button')
,即最终引入的组件都放在lib目录下
比如lodash,可以通过如下方式引入需要的模块:
npm i --save lodash.debounce
var debounce = require('lodash.debounce');
通过如下方式引入
var isMobile = require("jlb-tools/lib/isMobile");
打包体积确实减小了,但是写法过于冗长,借助babel实现import {isMobile} from 'jlb-tools'
方式引入。
此方案是在babel插件基础上,对方案二的改进
写法如下:
import {isMobile} from 'jlb-tools'
在.babelrc
中配置
// .babelrc
{
"plugins": [
["import-load", { library: "jlb-tools" }]
]
}
查看打包后的体积:
实际上此插件将import {isMobile} from 'jlb-tools'
最终编译成import isMobile from "jlb-tools/lib/isMobile";
如果遇到此问题,应该在babel-loader配置中移除对node_modules校验
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
exclude: /node_modules/
}
]
}
babel6只会对.babelrc进行处理,为了避免此问题,jlb-tools
将 .babelrc
替换成 .babelrc.js
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.