From 75f3fb2df04f48da202dd7927ad43d599f25a140 Mon Sep 17 00:00:00 2001 From: endiliey Date: Sat, 23 Nov 2019 14:27:09 +0700 Subject: [PATCH] perf(v2): use @babel/runtime plugin to reduce codesize --- packages/docusaurus/package.json | 2 ++ packages/docusaurus/src/webpack/utils.ts | 20 ++++++++++++ yarn.lock | 40 ++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index 18a899ca2041..369853f084c1 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -34,8 +34,10 @@ "dependencies": { "@babel/core": "^7.7.2", "@babel/plugin-syntax-dynamic-import": "^7.2.0", + "@babel/plugin-transform-runtime": "^7.7.4", "@babel/preset-env": "^7.7.1", "@babel/preset-react": "^7.7.0", + "@babel/runtime": "^7.7.4", "@docusaurus/utils": "^2.0.0-alpha.36", "@endiliey/static-site-generator-webpack-plugin": "^4.0.0", "babel-loader": "^8.0.6", diff --git a/packages/docusaurus/src/webpack/utils.ts b/packages/docusaurus/src/webpack/utils.ts index a392ec800d4f..78aa0dfeb5d1 100644 --- a/packages/docusaurus/src/webpack/utils.ts +++ b/packages/docusaurus/src/webpack/utils.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import path from 'path'; import MiniCssExtractPlugin from 'mini-css-extract-plugin'; import env from 'std-env'; import merge from 'webpack-merge'; @@ -83,6 +84,9 @@ export function getCacheLoader( } export function getBabelLoader(isServer: boolean, babelOptions?: {}): Loader { + const absoluteRuntimePath = path.dirname( + require.resolve(`@babel/runtime/package.json`), + ); return { loader: require.resolve('babel-loader'), options: Object.assign( @@ -116,6 +120,22 @@ export function getBabelLoader(isServer: boolean, babelOptions?: {}): Loader { '@babel/react', ], plugins: [ + // Polyfills the runtime needed for async/await, generators, and friends + // https://babeljs.io/docs/en/babel-plugin-transform-runtime + [ + '@babel/plugin-transform-runtime', + { + corejs: false, + helpers: true, + regenerator: true, + useESModules: true, + // Undocumented option that lets us encapsulate our runtime, ensuring + // the correct version is used + // https://github.com/babel/babel/blob/090c364a90fe73d36a30707fc612ce037bdbbb24/packages/babel-plugin-transform-runtime/src/index.js#L35-L42 + absoluteRuntime: absoluteRuntimePath, + }, + ], + // Adds syntax support for import() isServer ? 'dynamic-import-node' : '@babel/syntax-dynamic-import', ], }, diff --git a/yarn.lock b/yarn.lock index 60a23f6cdc7c..4c7aa68070e1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -261,6 +261,13 @@ dependencies: "@babel/types" "^7.7.0" +"@babel/helper-module-imports@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.7.4.tgz#e5a92529f8888bf319a6376abfbd1cebc491ad91" + integrity sha512-dGcrX6K9l8258WFjyDLJwuVKxR4XZfU0/vTUgOQYWEnRD8mgr+p4d6fCUMq/ys0h4CCt/S5JhbvtyErjWouAUQ== + dependencies: + "@babel/types" "^7.7.4" + "@babel/helper-module-transforms@^7.1.0": version "7.5.5" resolved "https://registry.yarnpkg.com/@babel/helper-module-transforms/-/helper-module-transforms-7.5.5.tgz#f84ff8a09038dcbca1fd4355661a500937165b4a" @@ -817,6 +824,16 @@ dependencies: "@babel/helper-plugin-utils" "^7.0.0" +"@babel/plugin-transform-runtime@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.7.4.tgz#51fe458c1c1fa98a8b07934f4ed38b6cd62177a6" + integrity sha512-O8kSkS5fP74Ad/8pfsCMGa8sBRdLxYoSReaARRNSz3FbFQj3z/QUvoUmJ28gn9BO93YfnXc3j+Xyaqe8cKDNBQ== + dependencies: + "@babel/helper-module-imports" "^7.7.4" + "@babel/helper-plugin-utils" "^7.0.0" + resolve "^1.8.1" + semver "^5.5.1" + "@babel/plugin-transform-shorthand-properties@^7.2.0": version "7.2.0" resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.2.0.tgz#6333aee2f8d6ee7e28615457298934a3b46198f0" @@ -988,6 +1005,13 @@ dependencies: regenerator-runtime "^0.13.2" +"@babel/runtime@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.7.4.tgz#b23a856751e4bf099262f867767889c0e3fe175b" + integrity sha512-r24eVUUr0QqNZa+qrImUk8fn5SPhHq+IfYvIoIMg0do3GdK9sMdiLKP3GYVVaxpPKORgm8KRKaNTEhAjgIpLMw== + dependencies: + regenerator-runtime "^0.13.2" + "@babel/template@^7.1.0", "@babel/template@^7.4.4", "@babel/template@^7.6.0": version "7.6.0" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.6.0.tgz#7f0159c7f5012230dad64cca42ec9bdb5c9536e6" @@ -1102,6 +1126,15 @@ lodash "^4.17.13" to-fast-properties "^2.0.0" +"@babel/types@^7.7.4": + version "7.7.4" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.7.4.tgz#516570d539e44ddf308c07569c258ff94fde9193" + integrity sha512-cz5Ji23KCi4T+YIE/BolWosrJuSmoZeN1EFnRtBwF+KKLi8GG/Z2c2hOJJeCXPk4mwk4QFvTmwIodJowXgttRA== + dependencies: + esutils "^2.0.2" + lodash "^4.17.13" + to-fast-properties "^2.0.0" + "@cnakazawa/watch@^1.0.3": version "1.0.3" resolved "https://registry.yarnpkg.com/@cnakazawa/watch/-/watch-1.0.3.tgz#099139eaec7ebf07a27c1786a3ff64f39464d2ef" @@ -14075,6 +14108,13 @@ resolve@^1.11.0, resolve@^1.12.0, resolve@^1.3.2: dependencies: path-parse "^1.0.6" +resolve@^1.8.1: + version "1.12.2" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.12.2.tgz#08b12496d9aa8659c75f534a8f05f0d892fff594" + integrity sha512-cAVTI2VLHWYsGOirfeYVVQ7ZDejtQ9fp4YhYckWDEkFfqbVjaT11iM8k6xSAfGFMM+gDpZjMnFssPu8we+mqFw== + dependencies: + path-parse "^1.0.6" + responselike@1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/responselike/-/responselike-1.0.2.tgz#918720ef3b631c5642be068f15ade5a46f4ba1e7"