From 260e8ab19e721b510f31bd0cd8be7b7fdcc47bcd Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 24 May 2022 20:47:17 +0800 Subject: [PATCH 1/5] fix(father-build): babel runtime resolve logic --- packages/father-build/src/getBabelConfig.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/father-build/src/getBabelConfig.ts b/packages/father-build/src/getBabelConfig.ts index 2a2dba49..686137dc 100644 --- a/packages/father-build/src/getBabelConfig.ts +++ b/packages/father-build/src/getBabelConfig.ts @@ -35,6 +35,14 @@ function transformImportLess2Css() { } } +function getBabelRuntimeVersion(cwd: string) { + try { + return require(require.resolve('@babel/runtime/package.json', { paths: [cwd] })).version; + } catch { + return require('@babel/runtime/package.json').version; + } +} + export default function(opts: IGetBabelConfigOpts) { const { target, typescript, type, runtimeHelpers, filePath, browserFiles, nodeFiles, nodeVersion, lazy, lessInBabelMode } = opts; let isBrowser = target === 'browser'; @@ -81,7 +89,7 @@ export default function(opts: IGetBabelConfigOpts) { ...(runtimeHelpers ? [[require.resolve('@babel/plugin-transform-runtime'), { useESModules: isBrowser && (type === 'esm'), - version: require('@babel/runtime/package.json').version, + version: getBabelRuntimeVersion(filePath), }]] : []), ...(process.env.COVERAGE From fb2430ef79e5989cbd323236ed33b21c3ff31389 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 24 May 2022 21:21:55 +0800 Subject: [PATCH 2/5] test(father-build): add case for local babel runtime --- .../build/babel-runtime-local/.fatherrc.js | 6 ++++ .../babel-runtime-local/expected/es/index.js | 25 ++++++++++++++++ .../babel-runtime-local/expected/lib/index.js | 30 +++++++++++++++++++ .../node_modules/@babel/runtime/package.json | 3 ++ .../build/babel-runtime-local/package.json | 5 ++++ .../build/babel-runtime-local/src/index.js | 9 ++++++ 6 files changed, 78 insertions(+) create mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/.fatherrc.js create mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/expected/es/index.js create mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/expected/lib/index.js create mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json create mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/package.json create mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/src/index.js diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/.fatherrc.js b/packages/father-build/src/fixtures/build/babel-runtime-local/.fatherrc.js new file mode 100644 index 00000000..6d1ce848 --- /dev/null +++ b/packages/father-build/src/fixtures/build/babel-runtime-local/.fatherrc.js @@ -0,0 +1,6 @@ + +export default { + runtimeHelpers: true, + esm: { type: 'babel' }, + cjs: { type: 'babel' }, +} diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/expected/es/index.js b/packages/father-build/src/fixtures/build/babel-runtime-local/expected/es/index.js new file mode 100644 index 00000000..d149bf66 --- /dev/null +++ b/packages/father-build/src/fixtures/build/babel-runtime-local/expected/es/index.js @@ -0,0 +1,25 @@ +import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; +import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; +import _createClass from "@babel/runtime/helpers/esm/createClass"; + +function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } + +var A = /*#__PURE__*/function () { + function A() { + _classCallCheck(this, A); + } + + _createClass(A, [{ + key: "foo", + value: function foo() {} + }]); + + return A; +}(); + +new A().foo(); +var a = {}; + +var b = _objectSpread({}, a); \ No newline at end of file diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/expected/lib/index.js b/packages/father-build/src/fixtures/build/babel-runtime-local/expected/lib/index.js new file mode 100644 index 00000000..905666f9 --- /dev/null +++ b/packages/father-build/src/fixtures/build/babel-runtime-local/expected/lib/index.js @@ -0,0 +1,30 @@ +"use strict"; + +var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); + +var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); + +var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); + +var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); + +function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } + +function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } + +var A = /*#__PURE__*/function () { + function A() { + (0, _classCallCheck2.default)(this, A); + } + + (0, _createClass2.default)(A, [{ + key: "foo", + value: function foo() {} + }]); + return A; +}(); + +new A().foo(); +var a = {}; + +var b = _objectSpread({}, a); \ No newline at end of file diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json b/packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json new file mode 100644 index 00000000..539cf748 --- /dev/null +++ b/packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json @@ -0,0 +1,3 @@ +{ + "version": "7.0.0" +} \ No newline at end of file diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/package.json b/packages/father-build/src/fixtures/build/babel-runtime-local/package.json new file mode 100644 index 00000000..b0d2efd8 --- /dev/null +++ b/packages/father-build/src/fixtures/build/babel-runtime-local/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "@babel/runtime": "7.0.0" + } +} diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/src/index.js b/packages/father-build/src/fixtures/build/babel-runtime-local/src/index.js new file mode 100644 index 00000000..5f0d5578 --- /dev/null +++ b/packages/father-build/src/fixtures/build/babel-runtime-local/src/index.js @@ -0,0 +1,9 @@ + +class A { + foo() {} +} + +(new A()).foo(); + +const a = {}; +const b = {...a}; \ No newline at end of file From 63c7e443f75e768a13f1cff940352766034ecc83 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 24 May 2022 21:40:51 +0800 Subject: [PATCH 3/5] refactor(father-build): use real cwd for babel runtime --- packages/father-build/src/babel.ts | 1 + packages/father-build/src/getBabelConfig.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/father-build/src/babel.ts b/packages/father-build/src/babel.ts index 5a16537c..73da2a8f 100644 --- a/packages/father-build/src/babel.ts +++ b/packages/father-build/src/babel.ts @@ -79,6 +79,7 @@ export default async function (opts: IBabelOpts) { nodeVersion, lazy: cjs && cjs.lazy, lessInBabelMode, + cwd, }); if (importLibToEs && type === "esm") { babelOpts.plugins.push(require.resolve("../lib/importLibToEs")); diff --git a/packages/father-build/src/getBabelConfig.ts b/packages/father-build/src/getBabelConfig.ts index 686137dc..cac44036 100644 --- a/packages/father-build/src/getBabelConfig.ts +++ b/packages/father-build/src/getBabelConfig.ts @@ -19,6 +19,7 @@ interface IGetBabelConfigOpts { paths?: any[]; plugins?: any[]; }; + cwd?: string; } function transformImportLess2Css() { @@ -89,7 +90,7 @@ export default function(opts: IGetBabelConfigOpts) { ...(runtimeHelpers ? [[require.resolve('@babel/plugin-transform-runtime'), { useESModules: isBrowser && (type === 'esm'), - version: getBabelRuntimeVersion(filePath), + version: getBabelRuntimeVersion(opts.cwd), }]] : []), ...(process.env.COVERAGE From 532e7ddb752e23217049c4d99176ea6209eaba60 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Wed, 25 May 2022 00:56:49 +0800 Subject: [PATCH 4/5] refactor(father-build): use babel runtime version from project deps --- .../node_modules/@babel/runtime/package.json | 3 --- packages/father-build/src/getBabelConfig.ts | 11 ++--------- 2 files changed, 2 insertions(+), 12 deletions(-) delete mode 100644 packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json diff --git a/packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json b/packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json deleted file mode 100644 index 539cf748..00000000 --- a/packages/father-build/src/fixtures/build/babel-runtime-local/node_modules/@babel/runtime/package.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "version": "7.0.0" -} \ No newline at end of file diff --git a/packages/father-build/src/getBabelConfig.ts b/packages/father-build/src/getBabelConfig.ts index cac44036..2564fd19 100644 --- a/packages/father-build/src/getBabelConfig.ts +++ b/packages/father-build/src/getBabelConfig.ts @@ -36,14 +36,6 @@ function transformImportLess2Css() { } } -function getBabelRuntimeVersion(cwd: string) { - try { - return require(require.resolve('@babel/runtime/package.json', { paths: [cwd] })).version; - } catch { - return require('@babel/runtime/package.json').version; - } -} - export default function(opts: IGetBabelConfigOpts) { const { target, typescript, type, runtimeHelpers, filePath, browserFiles, nodeFiles, nodeVersion, lazy, lessInBabelMode } = opts; let isBrowser = target === 'browser'; @@ -90,7 +82,8 @@ export default function(opts: IGetBabelConfigOpts) { ...(runtimeHelpers ? [[require.resolve('@babel/plugin-transform-runtime'), { useESModules: isBrowser && (type === 'esm'), - version: getBabelRuntimeVersion(opts.cwd), + // use @babel/runtime version from project dependencies + version: require(`${opts.cwd}/package.json`).dependencies['@babel/runtime'], }]] : []), ...(process.env.COVERAGE From 5d616a4c736d76e1ab8007c5d21759e28e435fe2 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Wed, 25 May 2022 01:11:47 +0800 Subject: [PATCH 5/5] refactor(father-build): correct babel runtime for rollup mode --- .../src/fixtures/build/rollup-runtimeHelpers/package.json | 2 +- packages/father-build/src/getRollupConfig.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/father-build/src/fixtures/build/rollup-runtimeHelpers/package.json b/packages/father-build/src/fixtures/build/rollup-runtimeHelpers/package.json index 689bf9cf..9c4a0984 100644 --- a/packages/father-build/src/fixtures/build/rollup-runtimeHelpers/package.json +++ b/packages/father-build/src/fixtures/build/rollup-runtimeHelpers/package.json @@ -1,5 +1,5 @@ { "dependencies": { - "@babel/runtime": "0.1.0" + "@babel/runtime": "7.10.4" } } diff --git a/packages/father-build/src/getRollupConfig.ts b/packages/father-build/src/getRollupConfig.ts index c7513bec..adae9818 100644 --- a/packages/father-build/src/getRollupConfig.ts +++ b/packages/father-build/src/getRollupConfig.ts @@ -86,6 +86,7 @@ export default function(opts: IGetRollupConfigOpts): RollupOptions[] { typescript: true, runtimeHelpers, nodeVersion, + cwd, }).opts), // ref: https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers babelHelpers: (runtimeHelpers ? 'runtime' : 'bundled') as RollupBabelInputPluginOptions['babelHelpers'],