簡體   English   中英

date-fns 2 - 無法進行 tree-shaking 工作

[英]date-fns 2 - can't get tree-shaking to work

我無法弄清楚date-fns v.2 tree-shaking 功能是如何工作的......

為了幫助我,我使用以下方法制作了一個非常簡單的項目:

  • date-fns 2.1.0
  • webpack 4.39.3
  • typescript 3.6.2

測試包含 2 個文件,一個充當“庫”,另一個充當要運行的代碼,它就像......

import ParseDate from './parse-date'

const parse = new ParseDate();
const result = parse.getExpirationDate({ months: 3 });

console.log(result);

但問題是,雖然我只要求使用 tree-shaking 使用 6 個庫

import { addYears, addMonths, addWeeks, addDays, addHours, addMinutes } from 'date-fns';

從他們的 文檔

// Without tree-shaking:
import format from 'date-fns/format'
import parse from 'date-fns/parse'

// With tree-shaking:
import { format, parse } from 'date-fns'

webpack 捆綁了整個 date-fns 庫,是 2 個文件的結果, 726Kb !!

> npm run build

> date_fns_test@1.0.0 build C:\Users\balexandre\date_fns_test
> tsc && webpack

Hash: 419a712549fc2309f21e
Version: webpack 4.39.3
Time: 820ms
Built at: 2019-09-09 17:27:36
    Asset     Size  Chunks             Chunk Names
bundle.js  726 KiB    main  [emitted]  main
Entrypoint main = bundle.js
chunk {main} bundle.js (main) 441 KiB [entry] [rendered]
    > ./src/index.js main
 [./src/index.js] 410 bytes {main} [depth 0] [built]
     single entry ./src/index.js  main
 [./src/parse-date.js] 1.27 KiB {main} [depth 1] [built]
     cjs require ./parse-date [./src/index.js] 6:35-58
     + 212 hidden modules

我錯過了什么? ...一定是一件簡單的事情,但我的想法已經用完了:(

該項目可以在 GitHub上找到,以便於審查(和 git 拉):)

要使 tree-shaking 工作,您必須配置 TypeScript 以編譯為 ES 模塊而不是 CommonJS,並在 webpack 中啟用生產模式:

  • tsconfig.json設置module: 'es2015'
  • webpack.config.js設置mode: 'production'

使用此設置,您將只有 1Kb 構建:

$ size-limit dist/bundle.js 

  Package size: 724 B
  With all dependencies, minified and gzipped

這是您的回購的補丁:

diff --git a/tsconfig.json b/tsconfig.json
index 42d6d90..b64255d 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,7 +1,7 @@
 {
     "compilerOptions": {
       /* Basic Options */
-      "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
+      "target": "es2015",                       /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
       "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
       // "lib": [],                             /* Specify library files to be included in the compilation. */
        "allowJs": false,                        /* Allow javascript files to be compiled. */
diff --git a/webpack.config.js b/webpack.config.js
index 8ccbc94..1419137 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -3,7 +3,7 @@ const path = require('path');

 const config = {
   entry: './src/index.js',
-  mode: 'development',
+  mode: 'production',
   stats: 'verbose',
   output: {
       path: path.resolve(__dirname, 'dist'),

這是對我有用的 date-fns 函數的 tree-shaking 修復,使用 Webpack 別名(最初在打開的 Github 問題中提到)

我從locales 的 tree-shaking work的解決方案中導出了解決方案。

  1. 在您的 webpack 配置旁邊創建一個名為date-fns-functions.js的文件,您將在其中重新導出您在項目中使用的函數,如下所示:
export { default as parseISO } from 'date-fns/parseISO';
export { default as formatDistanceToNow } from 'date-fns/formatDistanceToNow';
export { default as parse } from 'date-fns/parse';
...
  1. 然后,為您的 Webpack 配置添加一個別名:
    resolve: {
        alias: {
            'date-fns$': require.resolve('./date-fns-functions.js')
        }
    },

在我的特殊情況下,使用 Nuxt.js 進行 Webpack 捆綁,我在 nuxt.config.js 中添加了它:

extend (config, ctx) {
   config.resolve.alias['date-fns$'] = require.resolve('./date-fns-functions.js');
   ...
}

每次從date-fns導入內容時,它都會使用別名查找導入,避免完全導入整個庫。

這解決了我的問題並大大減少了包的大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM