-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support mpx.mixin with stage & fix unit env
- Loading branch information
Showing
13 changed files
with
261 additions
and
42 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"modules": false, | ||
"shippedProposals": true | ||
} | ||
] | ||
], | ||
"sourceType": "unambiguous", | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"shippedProposals": true | ||
} | ||
] | ||
] | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(js|jsx)?$": "babel-jest" | ||
}, | ||
"moduleNameMapper": { | ||
"\\.(css|styl)$": "identity-obj-proxy" | ||
}, | ||
"testEnvironment": "jsdom" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"modules": false, | ||
"shippedProposals": true | ||
} | ||
] | ||
], | ||
"sourceType": "unambiguous", | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"shippedProposals": true | ||
} | ||
] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(js|jsx)?$": "babel-jest" | ||
}, | ||
"testEnvironment": "jsdom", | ||
"moduleNameMapper": { | ||
"\\.(css|styl)$": "identity-obj-proxy" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { injectMixins, mergeInjectedMixins, clearInjectMixins } from '../../src/core/injectMixins' | ||
|
||
describe('InjectMixins should work right', () => { | ||
beforeEach(() => { | ||
clearInjectMixins() | ||
}) | ||
|
||
it('Default inject should be inserted before', () => { | ||
injectMixins({ | ||
index: 0 | ||
}, 'page') | ||
injectMixins({ | ||
index: 1 | ||
}) | ||
|
||
const options = { | ||
mixins: [{ | ||
index: 2 | ||
}] | ||
} | ||
mergeInjectedMixins(options, 'page') | ||
expect(options.mixins.length).toEqual(3) | ||
options.mixins.forEach((mixin, i) => { | ||
expect(mixin.index).toEqual(i) | ||
}) | ||
|
||
const options2 = { | ||
mixins: [{ | ||
index: 0 | ||
}] | ||
} | ||
mergeInjectedMixins(options2, 'component') | ||
expect(options2.mixins.length).toEqual(2) | ||
}) | ||
|
||
it('Inject with plus stage should be inserted after', () => { | ||
injectMixins({ | ||
index: 1 | ||
}, { | ||
stage: 1, | ||
types: 'page' | ||
}) | ||
const options = { | ||
mixins: [{ | ||
index: 0 | ||
}] | ||
} | ||
mergeInjectedMixins(options, 'page') | ||
expect(options.mixins.length).toEqual(2) | ||
options.mixins.forEach((mixin, i) => { | ||
expect(mixin.index).toEqual(i) | ||
}) | ||
|
||
const options2 = { | ||
mixins: [{ | ||
index: 0 | ||
}] | ||
} | ||
mergeInjectedMixins(options2, 'component') | ||
expect(options2.mixins.length).toEqual(1) | ||
}) | ||
|
||
it('Inject with stage multiply should be inserted with right order', () => { | ||
injectMixins({ | ||
index: 5 | ||
}, { | ||
stage: 100, | ||
types: ['page'] | ||
}) | ||
injectMixins({ | ||
index: 4 | ||
}, { | ||
stage: 50, | ||
types: 'page' | ||
}) | ||
|
||
injectMixins([ | ||
{ | ||
index: 1 | ||
}, { | ||
index: 2 | ||
} | ||
], { | ||
stage: -100, | ||
types: ['page'] | ||
}) | ||
|
||
injectMixins([ | ||
{ | ||
index: 0 | ||
} | ||
], { | ||
stage: -1000, | ||
types: ['page'] | ||
}) | ||
|
||
const options = { | ||
mixins: [{ | ||
index: 3 | ||
}] | ||
} | ||
|
||
mergeInjectedMixins(options, 'page') | ||
expect(options.mixins.length).toEqual(6) | ||
options.mixins.forEach((mixin, i) => { | ||
expect(mixin.index).toEqual(i) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"modules": false, | ||
"shippedProposals": true | ||
} | ||
] | ||
], | ||
"sourceType": "unambiguous", | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
[ | ||
"@babel/env", | ||
{ | ||
"shippedProposals": true | ||
} | ||
] | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"transform": { | ||
"^.+\\.(js|jsx)?$": "babel-jest" | ||
}, | ||
"testEnvironment": "jsdom" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,63 @@ | ||
const MIXINS_MAPS = { | ||
app: [], | ||
page: [], | ||
component: [] | ||
import flatten from 'lodash/flatten.js' | ||
|
||
const mixinsQueueMap = { | ||
app: [[], []], | ||
page: [[], []], | ||
component: [[], []] | ||
} | ||
|
||
export function clearInjectMixins () { | ||
mixinsQueueMap.app = [[], []] | ||
mixinsQueueMap.page = [[], []] | ||
mixinsQueueMap.component = [[], []] | ||
} | ||
|
||
export function injectMixins (mixins, type) { | ||
if (typeof type === 'string') { | ||
type = [type] | ||
} else { | ||
type = ['app', 'page', 'component'] | ||
export function injectMixins (mixins, options = {}) { | ||
if (typeof options === 'string' || Array.isArray(options)) { | ||
options = { | ||
types: options | ||
} | ||
} | ||
|
||
let types = options.types || ['app', 'page', 'component'] | ||
let stage = options.stage || -1 | ||
|
||
if (typeof types === 'string') { | ||
types = [types] | ||
} | ||
|
||
if (!Array.isArray(mixins)) { | ||
mixins = [mixins] | ||
} | ||
type.forEach(key => { | ||
const curMixins = MIXINS_MAPS[key] | ||
if (curMixins) { | ||
for (const mixin of mixins) { | ||
curMixins.indexOf(mixin) === -1 && curMixins.push(mixin) | ||
|
||
mixins.stage = stage | ||
|
||
types.forEach(type => { | ||
const mixinsQueue = stage < 0 ? mixinsQueueMap[type][0] : mixinsQueueMap[type][1] | ||
for (let i = 0; i <= mixinsQueue.length; i++) { | ||
if (i === mixinsQueue.length) { | ||
mixinsQueue.push(mixins) | ||
break | ||
} | ||
const item = mixinsQueue[i] | ||
if (mixins === item) break | ||
if (stage < item.stage) { | ||
mixinsQueue.splice(i, 0, mixins) | ||
break | ||
} | ||
} | ||
}) | ||
return this | ||
} | ||
|
||
export function getInjectedMixins (type) { | ||
return MIXINS_MAPS[type].slice(0) | ||
return this | ||
} | ||
|
||
export function mergeInjectedMixins (options, type) { | ||
const injectedMixins = getInjectedMixins(type) | ||
if (injectedMixins.length) { | ||
options.mixins = options.mixins ? injectedMixins.concat(options.mixins) : injectedMixins | ||
const before = flatten(mixinsQueueMap[type][0]) | ||
const middle = options.mixins || [] | ||
const after = flatten(mixinsQueueMap[type][1]) | ||
const mixins = before.concat(middle).concat(after) | ||
if (mixins.length) { | ||
options.mixins = mixins | ||
} | ||
return options | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters