From e8d736f0fd847f39a46b5774af882c9a8313fbc4 Mon Sep 17 00:00:00 2001 From: peterhpchen Date: Wed, 20 Jan 2021 14:45:10 +0800 Subject: [PATCH] fix: transform commonjs --- src/core.js | 2 +- test/fixtures/transform-commonjs/actual.js | 7 +++++++ test/fixtures/transform-commonjs/expected.js | 19 +++++++++++++++++++ test/index-test.js | 7 +++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/transform-commonjs/actual.js create mode 100644 test/fixtures/transform-commonjs/expected.js diff --git a/src/core.js b/src/core.js index 584f7c07..7421043f 100644 --- a/src/core.js +++ b/src/core.js @@ -121,7 +121,7 @@ module.exports = function core(defaultLibraryName) { } } } - return selectedMethods[methodName]; + return Object.assign({}, selectedMethods[methodName]); } function buildExpressionHandler(node, props, path, state) { diff --git a/test/fixtures/transform-commonjs/actual.js b/test/fixtures/transform-commonjs/actual.js new file mode 100644 index 00000000..6adcdf87 --- /dev/null +++ b/test/fixtures/transform-commonjs/actual.js @@ -0,0 +1,7 @@ +import { Button } from 'element-ui'; + +export default { + components: { + [Button.name]: Button + } +} \ No newline at end of file diff --git a/test/fixtures/transform-commonjs/expected.js b/test/fixtures/transform-commonjs/expected.js new file mode 100644 index 00000000..cbc9b6bb --- /dev/null +++ b/test/fixtures/transform-commonjs/expected.js @@ -0,0 +1,19 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.default = void 0; + +require("element-ui/lib/button/style.css"); + +var _button = _interopRequireDefault(require("element-ui/lib/button")); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } + +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +var _default = { + components: _defineProperty({}, _button.default.name, _button.default) +}; +exports.default = _default; \ No newline at end of file diff --git a/test/index-test.js b/test/index-test.js index 0ed00348..21e66354 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -133,6 +133,13 @@ describe('index', () => { }]; } + if (caseName === 'transform-commonjs') { + cssPlugin = [ + [plugin], + ['@babel/plugin-transform-modules-commonjs'] + ]; + } + const actual = transformFileSync(actualFile, { presets: ['@babel/react'], plugins: cssPlugin && Array.isArray(cssPlugin[1]) ? cssPlugin : [cssPlugin || plugin],