如何解决core-js 包很大使用 62kB
我正在减少我的 js 包大小并偶然发现了 core-js。它大约需要 62kB,占整个包的 24%。
我尝试使用 @babel/preset-env
,但无法进一步缩小尺寸。不确定我是否使用了“正确”的设置:
'@babel/preset-env',{
targets: {
browsers: ['>1%'],},useBuiltIns: 'usage',corejs: { version: 3,proposals: true },
完整的webpack.config.js
const path = require('path');
const webpack = require('webpack');
const dotenv = require('dotenv');
const copyWebpackPlugin = require('copy-webpack-plugin');
const bundleOutputDir = './dist';
/* eslint-disable import/no-extraneous-dependencies */
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
// const CompressionPlugin = require('compression-webpack-plugin');
module.exports = (env) => {
// get project ID (environment)
const projectID = process.env.PROJECT_ID;
if (!projectID || projectID === undefined) {
throw new Error('Need env variable PROJECT_ID');
}
const isDevEnvironment = !(projectID === 'production-project');
const isDevBuild = !(env && env.prod);
const analyzeBundle = env && env.analyze;
// call dotenv and it will return an Object with a parsed key
const dotEnv = isDevEnvironment ? dotenv.config({ path: './.env.development' }).parsed : dotenv.config().parsed;
// reduce it to a nice object,the same as before
const envKeys = Object.keys(dotEnv).reduce((prev,next) => {
const updatedPrev = prev;
updatedPrev[`process.env.${next}`] = JSON.stringify(dotEnv[next]);
return updatedPrev;
},{});
envKeys['process.env.PROJECT_ID'] = JSON.stringify(projectID);
// need to remove quotes from env
const publicURL = 'https:/mysite.com'
const plugins = [new webpack.DefinePlugin(envKeys),new ForkTsCheckerWebpackPlugin()];
if (isDevBuild) {
// eslint-disable-next-line new-cap
plugins.push(new webpack.sourceMapDevToolPlugin(),new copyWebpackPlugin([{ from: 'dev/' }]));
} else {
// Don't need to enable compressinon plugin as Firebase Hosting automatically zips the files for us
// plugins.push(new CompressionPlugin());
}
if (analyzeBundle) {
// eslint-disable-next-line global-require
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
plugins.push(new BundleAnalyzerPlugin());
}
const babelPlugins = [
// Syntax sugar found in React components
'@babel/proposal-class-properties','@babel/proposal-object-rest-spread',// transpile JSX/TSX to JS
[
'@babel/plugin-transform-react-jsx',{
// we use Preact,which has `Preact.h` instead of `React.createElement`
pragma: 'h',pragmaFrag: 'Fragment',],[
'transform-react-remove-prop-types',{
removeImport: !isDevBuild,];
if (!isDevBuild) {
babelPlugins.push(['transform-remove-console',{ exclude: ['error','warn'] }]);
}
return [
{
entry: './src/index.ts',output: {
filename: 'widget.js',path: path.resolve(bundleOutputDir),publicPath: isDevBuild ? '' : publicURL,devServer: {
host: '0.0.0.0',// your ip address
port: 8080,disableHostCheck: true,contentBase: bundleOutputDir,open: 'google chrome',plugins,optimization: {
minimize: !isDevBuild,nodeenv: 'production',mangleWasmImports: true,removeAvailableModules: true,usedExports: true,sideEffects: true,providedExports: true,concatenateModules: true,mode: isDevBuild ? 'development' : 'production',module: {
rules: [
// packs PNG's discovered in url() into bundle
{
test: /\.(jpe?g|png|webp)$/i,use: [
{
loader: 'responsive-loader',options: {
// eslint-disable-next-line global-require
adapter: require('responsive-loader/sharp'),// sizes: [160,320,640,960,1280],name: '[path][name]-[width].[ext]',sourceMap: isDevBuild,{ test: /\.svg/,use: ['@svgr/webpack'] },{
test: /\.(css)$/i,use: [
{
loader: 'style-loader',options: {
injectType: 'singletonStyleTag',{
// allows import CSS as modules
loader: 'css-loader',options: {
modules: {
// css class names format
localIdentName: '[name]-[local]-[hash:base64:5]',{
test: /\.(scss)$/i,options: { injectType: 'singletonStyleTag' },{
loader: 'sass-loader',options: {
sourceMap: isDevBuild,// use babel-loader for TS and JS modeles,// starting v7 Babel babel-loader can transpile TS into JS,// so no need for ts-loader
// note,that in dev we still use tsc for type checking
{
test: /\.(js|ts|tsx|jsx)$/,exclude: /node_modules/,use: [
{
loader: 'babel-loader',options: {
presets: [
[
'@babel/preset-env',{
targets: {
browsers: ['>1%'],[
// enable transpiling ts => js
'@babel/typescript',// tell babel to compile JSX using into Preact
{ jsxPragma: 'h' },plugins: babelPlugins,resolve: {
extensions: ['*','.js','.ts','.tsx'],plugins: [new TsconfigPathsPlugin()],alias: {
react: 'preact/compat','react-dom': 'preact/compat',images: path.join(__dirname,'images'),sharedimages: path.resolve(__dirname,'../../packages/shared/src/resources'),];
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。