Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: install dependencies in sub blocks #2432

Merged
merged 1 commit into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dev": "umi dev"
},
"dependencies": {
"antd": "^3.10.9"
"antd": "^3.10.0"
},
"devDependencies": {
"umi": "^2.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"dev": "umi dev"
},
"dependencies": {
"antd": "^3.10.9"
"antd": "^3.8.0",
"rc-select": "~2.1.0"
},
"devDependencies": {
"umi": "^2.3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,41 @@ function checkConflict(blockDeps, projectDeps) {
return [lacks, conflicts];
}

export function getAllBlockDependencies(rootDir, pkg) {
const { blockConfig = {}, dependencies = {} } = pkg;
const { dependencies: depBlocks = [] } = blockConfig;
const allDependencies = {};

function mergeDependencies(parent, sub) {
const [lacks, conflicts] = checkConflict(sub, parent);
if (conflicts.length) {
throw new Error(`
find dependencies conflict between blocks:
${conflicts
.map(info => {
return `* ${info[0]}: ${info[2]} not compatible with ${info[1]}`;
})
.join('\n')}`);
}
lacks.forEach(lack => {
const [name, version] = lack;
parent[name] = version;
});
return parent;
}

depBlocks.forEach(block => {
const rubBlockDeps = getAllBlockDependencies(
rootDir,
// eslint-disable-next-line
require(join(rootDir, block, 'package.json')),
);
mergeDependencies(allDependencies, rubBlockDeps);
});
mergeDependencies(allDependencies, dependencies);
return allDependencies;
}

export function dependenciesConflictCheck(
blockPkgDeps = {},
projectPkgDeps = {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { join } from 'path';
import {
dependenciesConflictCheck,
getNameFromPkg,
parseContentToSingular,
getSingularName,
getMockDependencies,
getAllBlockDependencies,
} from './getBlockGenerator';

describe('test block generate', () => {
Expand Down Expand Up @@ -129,4 +131,88 @@ export default {
qs: '4.0.0',
});
});

it('getAllBlockDependencies', () => {
expect(
getAllBlockDependencies(
join(__dirname, '../../../fixtures/block/test-blocks'),
{
blockConfig: {
dependencies: ['demo'],
},
dependencies: {},
},
),
).toEqual({
antd: '^3.8.0',
'rc-select': '~2.1.0',
});

expect(
getAllBlockDependencies(
join(__dirname, '../../../fixtures/block/test-blocks'),
{
dependencies: {
moment: '2.3.2',
},
},
),
).toEqual({
moment: '2.3.2',
});

expect(
getAllBlockDependencies(
join(__dirname, '../../../fixtures/block/test-blocks'),
{
blockConfig: {
dependencies: ['demo', 'demo-with-dependencies'],
},
dependencies: {
moment: '2.3.2',
},
},
),
).toEqual({
moment: '2.3.2',
antd: '^3.8.0',
'rc-select': '~2.1.0',
});

expect(
getAllBlockDependencies(
join(__dirname, '../../../fixtures/block/test-blocks'),
{
blockConfig: {
dependencies: ['demo-with-dependencies'],
},
},
),
).toEqual({
antd: '^3.8.0',
'rc-select': '~2.1.0',
});

try {
expect(
getAllBlockDependencies(
join(__dirname, '../../../fixtures/block/test-blocks'),
{
blockConfig: {
dependencies: ['demo-with-dependencies'],
},
dependencies: {
antd: '2.0.0',
},
},
),
).toEqual({
antd: '^3.10.0',
});
} catch (error) {
expect(error.message).toContain(
'* antd: ^3.8.0 not compatible with 2.0.0',
);
}
});
});
8 changes: 4 additions & 4 deletions packages/umi-build-dev/src/plugins/commands/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { existsSync, readFileSync } from 'fs';
import { dirname, join } from 'path';
import execa from 'execa';
import ora from 'ora';
import { merge } from 'lodash';
import { merge, isPlainObject } from 'lodash';
import clipboardy from 'clipboardy';
import { isPlainObject } from 'lodash';
import { getParsedData, makeSureMaterialsTempPathExist } from './download';
import writeNewRoute from '../../../utils/writeNewRoute';
import { dependenciesConflictCheck, getNameFromPkg, getMockDependencies } from './getBlockGenerator';
import { dependenciesConflictCheck, getNameFromPkg, getMockDependencies, getAllBlockDependencies } from './getBlockGenerator';
import appendBlockToContainer from './appendBlockToContainer';

export default api => {
Expand Down Expand Up @@ -225,10 +224,11 @@ export default api => {
if (existsSync(mockFilePath)) {
devDependencies = getMockDependencies(readFileSync(mockFilePath, 'utf-8'), ctx.pkg);
}
const allBlockDependencies = getAllBlockDependencies(ctx.templateTmpDirPath, ctx.pkg);
// get confilict dependencies and lack dependencies
const { conflicts, lacks, devConflicts, devLacks } = applyPlugins('_modifyBlockDependencies', {
initialValue: dependenciesConflictCheck(
ctx.pkg.dependencies,
allBlockDependencies,
projectPkg.dependencies,
devDependencies,
{
Expand Down