UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

493 lines (431 loc) 19.7 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); function _bluebird() { const data = require("bluebird"); _bluebird = function () { return data; }; return data; } function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function _chai() { const data = require("chai"); _chai = function () { return data; }; return data; } function _dependencyFileLinkGenerator() { const data = _interopRequireDefault(require("./dependency-file-link-generator")); _dependencyFileLinkGenerator = function () { return data; }; return data; } function _consumerComponent() { const data = _interopRequireDefault(require("../consumer/component/consumer-component")); _consumerComponent = function () { return data; }; return data; } function _barFoo() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/plain-javascript/bar-foo.json")); _barFoo = function () { return data; }; return data; } function _utilsIsString() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/plain-javascript/utils-is-string.json")); _utilsIsString = function () { return data; }; return data; } function _barFoo2() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/es6/bar-foo.json")); _barFoo2 = function () { return data; }; return data; } function _utilsIsString2() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/es6/utils-is-string.json")); _utilsIsString2 = function () { return data; }; return data; } function _barFoo3() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/custom-resolved-modules/bar-foo.json")); _barFoo3 = function () { return data; }; return data; } function _utilsIsString3() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/custom-resolved-modules/utils-is-string.json")); _utilsIsString3 = function () { return data; }; return data; } function _barFoo4() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/sass/bar-foo.json")); _barFoo4 = function () { return data; }; return data; } function _utilsIsString4() { const data = _interopRequireDefault(require("../../fixtures/consumer-components/sass/utils-is-string.json")); _utilsIsString4 = function () { return data; }; return data; } function globalConfig() { const data = _interopRequireWildcard(require("../api/consumer/lib/global-config")); globalConfig = function () { return data; }; return data; } /* eslint-disable @typescript-eslint/no-non-null-assertion */ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! const mockBitMap = () => { return { getComponent: () => ({ rootDir: '.dependencies/utils/is-string/remote-scope/0.0.1', getRootDir() { return this.rootDir; } }) }; }; const mockConsumer = (distIsInsideTheComponent = true) => { const consumer = { bitMap: mockBitMap(), getPath: () => '/root', shouldDistsBeInsideTheComponent: () => true, toAbsolutePath: str => `/root/${str}` }; if (!distIsInsideTheComponent) { // @ts-ignore consumer.config = { workspaceSettings: { _distTarget: 'dist', _distEntry: 'src' } }; consumer.shouldDistsBeInsideTheComponent = () => false; } return consumer; }; const mockGetSync = () => { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! globalConfig().getSync = () => '@bit'; }; const getComponentMap = () => { return { rootDir: 'components/bar/foo', getRootDir() { return this.rootDir; } }; }; describe('DependencyFileLinkGenerator', () => { before(() => { mockGetSync(); }); describe('generate()', /*#__PURE__*/(0, _bluebird().coroutine)(function* () { describe('using plain javascript', () => { let dependencyFileLinkGenerator; let linkResult; before( /*#__PURE__*/(0, _bluebird().coroutine)(function* () { const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo().default)); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! component.componentMap = getComponentMap(); const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString().default)); dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! consumer: mockConsumer(), // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! bitMap: mockBitMap(), component, relativePath: component.dependencies.get()[0].relativePaths[0], dependencyComponent, createNpmLinkFiles: false, targetDir: '' }); const linkResults = dependencyFileLinkGenerator.generate(); linkResult = linkResults[0]; })); it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/utils/is-string.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should set isEs6 to false as it does not have ImportSpecifiers', () => { (0, _chai().expect)(linkResult.isEs6).to.be.false; }); }); describe('using ES6', () => { describe('when dist is inside the component dir', () => { let dependencyFileLinkGenerator; let linkResults; before( /*#__PURE__*/(0, _bluebird().coroutine)(function* () { const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo2().default)); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! component.componentMap = getComponentMap(); const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString2().default)); dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! consumer: mockConsumer(), // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! bitMap: mockBitMap(), component, relativePath: component.dependencies.get()[0].relativePaths[0], dependencyComponent, createNpmLinkFiles: false, targetDir: '' }); linkResults = dependencyFileLinkGenerator.generate(); })); it('should generate two link files, one for the source and one for the dist', () => { (0, _chai().expect)(linkResults).to.have.lengthOf(2); }); describe('link file of the source', () => { let linkResult; before(() => { linkResult = linkResults[0]; }); it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/utils/is-string.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should set isEs6 to true as it has ImportSpecifiers', () => { (0, _chai().expect)(linkResult.isEs6).to.be.true; }); }); describe('link file of the dist', () => { let linkResult; before(() => { linkResult = linkResults[1]; }); it('should generate linkPath that consist of component rootDir + dist + sourceRelativePath', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/dist/utils/is-string.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should set isEs6 to true as it has ImportSpecifiers', () => { (0, _chai().expect)(linkResult.isEs6).to.be.true; }); }); }); describe('when dist is outside the component dir', () => { let dependencyFileLinkGenerator; let linkResults; before( /*#__PURE__*/(0, _bluebird().coroutine)(function* () { const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo2().default)); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! component.componentMap = getComponentMap(); const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString2().default)); dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! consumer: mockConsumer(false), // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! bitMap: mockBitMap(), component, relativePath: component.dependencies.get()[0].relativePaths[0], dependencyComponent, createNpmLinkFiles: false, targetDir: '' }); linkResults = dependencyFileLinkGenerator.generate(); })); it('should generate two link files, one for the source and one for the dist', () => { (0, _chai().expect)(linkResults).to.have.lengthOf(2); }); describe('link file of the source', () => { let linkResult; before(() => { linkResult = linkResults[0]; }); it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/utils/is-string.js')); }); it('should generate linkContent that is relative to the linkPath', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should set isEs6 to true as it has ImportSpecifiers', () => { (0, _chai().expect)(linkResult.isEs6).to.be.true; }); }); describe('link file of the dist', () => { let linkResult; before(() => { linkResult = linkResults[1]; }); it('should generate linkPath that consist of dist + component rootDir + sourceRelativePath', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('dist/components/bar/foo/utils/is-string.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should set isEs6 to true as it has ImportSpecifiers', () => { (0, _chai().expect)(linkResult.isEs6).to.be.true; }); }); }); }); describe('using custom resolved modules with ES6', () => { describe('when dist is inside the component dir', () => { let dependencyFileLinkGenerator; let linkResults; let linkResult; before( /*#__PURE__*/(0, _bluebird().coroutine)(function* () { const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo3().default)); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! component.componentMap = getComponentMap(); const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString3().default)); dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! consumer: mockConsumer(), // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! bitMap: mockBitMap(), component, relativePath: component.dependencies.get()[0].relativePaths[0], dependencyComponent, createNpmLinkFiles: false, targetDir: '' }); linkResults = dependencyFileLinkGenerator.generate(); linkResult = linkResults[0]; })); it('should generate one link file because it is stored in node_modules which is shared between the src and the dist', () => { (0, _chai().expect)(linkResults).to.have.lengthOf(1); }); it('should generate linkPath that consist of component rootDir + node_modules + importSource (stored in relativePaths) + index.js', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/node_modules/utils/is-string/index.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); describe('when createNpmLinkFiles is set to true', () => { before(() => { dependencyFileLinkGenerator.createNpmLinkFiles = true; linkResults = dependencyFileLinkGenerator.generate(); linkResult = linkResults[0]; }); it('should generate one link file because it is stored in node_modules which is shared between the src and the dist', () => { (0, _chai().expect)(linkResults).to.have.lengthOf(1); }); it('should generate the link as postInstallLink (so then npm install will rewrite the links that are written in node_modules)', () => { (0, _chai().expect)(linkResult.postInstallLink).to.be.true; }); it('should generate linkContent that points to the package name instead of a file', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should generate linkPath that consist of node_modules + importSource + index.js (it must be relative because it is written by postinstall script)', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('node_modules/utils/is-string/index.js')); }); }); }); describe('when dist is outside the component dir', () => { let dependencyFileLinkGenerator; let linkResults; before( /*#__PURE__*/(0, _bluebird().coroutine)(function* () { const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo3().default)); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! component.componentMap = getComponentMap(); const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString3().default)); dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! consumer: mockConsumer(false), // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! bitMap: mockBitMap(), component, relativePath: component.dependencies.get()[0].relativePaths[0], dependencyComponent, createNpmLinkFiles: false, targetDir: '' }); linkResults = dependencyFileLinkGenerator.generate(); })); it('should generate two link files, one for the source and one for the dist', () => { (0, _chai().expect)(linkResults).to.have.lengthOf(2); }); describe('link file of the source', () => { let linkResult; before(() => { linkResult = linkResults[0]; }); it('should generate linkPath that consist of component rootDir + importSource + index.js', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/node_modules/utils/is-string/index.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); it('should set isEs6 to true as it has ImportSpecifiers', () => { (0, _chai().expect)(linkResult.isEs6).to.be.true; }); }); describe('link file of the dist', () => { let linkResult; before(() => { linkResult = linkResults[1]; }); it('should generate linkPath that consist of dist + component rootDir + importSource + index.js', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('dist/components/bar/foo/node_modules/utils/is-string/index.js')); }); it('should generate linkContent that points to the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');"); }); }); }); }); describe('using sass files', () => { let dependencyFileLinkGenerator; let linkResult; before( /*#__PURE__*/(0, _bluebird().coroutine)(function* () { const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo4().default)); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! component.componentMap = getComponentMap(); const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString4().default)); dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! consumer: mockConsumer(), // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! bitMap: mockBitMap(), component, relativePath: component.dependencies.get()[0].relativePaths[0], dependencyComponent, createNpmLinkFiles: false, targetDir: '' }); const linkResults = dependencyFileLinkGenerator.generate(); linkResult = linkResults[0]; })); it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => { (0, _chai().expect)(linkResult.linkPath).to.equal(path().normalize('components/bar/foo/utils/is-string.scss')); }); it('should generate linkContent that points to the main file inside the package', () => { (0, _chai().expect)(linkResult.linkContent).to.equal("@import '~@bit/remote-scope.utils.is-string/utils/is-string.scss';"); }); }); })); });