-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: morphing to gather changes before running
- Loading branch information
Darío Javier Cravero
committed
Aug 17, 2019
1 parent
975e91b
commit 67d4372
Showing
6 changed files
with
331 additions
and
368 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,15 +1,23 @@ | ||
export default function getPointsOfUse({ view, viewsToFiles }) { | ||
let filesView = new Set([view.file]) | ||
let filesViewLogic = new Set() | ||
let viewsId = new Set() | ||
|
||
if (view.logic) { | ||
filesViewLogic.add(view.logic) | ||
} | ||
|
||
for (let viewInView of viewsToFiles.values()) { | ||
if (viewInView.custom) continue | ||
|
||
if (viewInView.parsed.view.views.has(view.id)) { | ||
filesView.add(viewInView.file) | ||
if (viewInView.logic) { | ||
filesViewLogic.add(viewInView.logic) | ||
} | ||
viewsId.add(viewInView.id) | ||
} | ||
} | ||
|
||
return { filesView, viewsId } | ||
return { filesView, filesViewLogic, viewsId } | ||
} |
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,103 @@ | ||
import { | ||
makeGetFontImport, | ||
morphAllFonts, | ||
processCustomFonts, | ||
} from './fonts.js' | ||
import ensureFlow from './ensure-flow.js' | ||
import ensureIsBefore from './ensure-is-before.js' | ||
import ensureIsMedia from './ensure-is-media.js' | ||
import makeGetSystemImport from './make-get-system-import.js' | ||
import morphAllViews from './morph-all-views.js' | ||
import parseViews from './parse-views.js' | ||
import processViewCustomFiles from './process-view-custom-files.js' | ||
import processViewFiles from './process-view-files.js' | ||
|
||
export default function makeMorpher({ | ||
as = 'react-dom', | ||
local = 'en', | ||
src, | ||
tools = false, | ||
track = false, | ||
verbose = true, | ||
}) { | ||
let state = { | ||
as, | ||
local, | ||
src, | ||
tools, | ||
track, | ||
verbose, | ||
customFonts: new Map(), | ||
viewsById: new Map(), | ||
viewsToFiles: new Map(), | ||
} | ||
|
||
state.processFiles = async function processFiles({ | ||
filesFontCustom = new Set(), | ||
filesView = new Set(), | ||
filesViewCustom = new Set(), | ||
filesViewLogic = new Set(), | ||
}) { | ||
if (filesFontCustom.size > 0) { | ||
processCustomFonts({ | ||
customFonts: state.customFonts, | ||
filesFontCustom, | ||
}) | ||
} | ||
|
||
// detect .view files | ||
await processViewFiles({ | ||
filesView, | ||
filesViewLogic, | ||
viewsById: state.viewsById, | ||
viewsToFiles: state.viewsToFiles, | ||
}) | ||
|
||
// detect .js files meant to be custom views with "// @view" at the top | ||
processViewCustomFiles({ | ||
filesViewCustom, | ||
viewsById: state.viewsById, | ||
viewsToFiles: state.viewsToFiles, | ||
}) | ||
|
||
// TODO optimise | ||
// parse views | ||
parseViews({ | ||
customFonts: state.customFonts, | ||
filesView, | ||
src: state.src, | ||
verbose: state.verbose, | ||
viewsById: state.viewsById, | ||
viewsToFiles: state.viewsToFiles, | ||
}) | ||
|
||
await morphAllFonts({ | ||
as: state.as, | ||
customFonts: state.customFonts, | ||
filesView, | ||
src: state.src, | ||
viewsToFiles: state.viewsToFiles, | ||
}) | ||
|
||
// TODO optimise | ||
// morph views | ||
await morphAllViews({ | ||
as: state.as, | ||
filesView, | ||
getFontImport: makeGetFontImport(state.src), | ||
getSystemImport: makeGetSystemImport(state.src), | ||
local: state.local, | ||
track: state.track, | ||
viewsById: state.viewsById, | ||
viewsToFiles: state.viewsToFiles, | ||
}) | ||
|
||
await Promise.all([ | ||
ensureFlow(state), | ||
ensureIsBefore(state), | ||
ensureIsMedia(state), | ||
]) | ||
} | ||
|
||
return state | ||
} |
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,68 @@ | ||
import getFirstLine from 'firstline' | ||
import isViewCustom from './is-view-custom.js' | ||
import mm from 'micromatch' | ||
|
||
export let PATTERNS = { | ||
filesView: { | ||
match: ['**/*.view'], | ||
}, | ||
filesViewLogic: { | ||
match: ['**/*.view.logic.js'], | ||
}, | ||
filesViewCustom: { | ||
match: ['**/*.js'], | ||
ignore: ['**/*.view.logic.js'], | ||
filter: async files => { | ||
let filesFirstLine = await Promise.all( | ||
files.map(async file => [file, await getFirstLine(file)]) | ||
) | ||
|
||
return filesFirstLine | ||
.filter(([, firstLine]) => isViewCustom(firstLine)) | ||
.map(([file]) => file) | ||
}, | ||
}, | ||
filesFontCustom: { | ||
match: [ | ||
'**/Fonts/*.eot', | ||
'**/Fonts/*.otf', | ||
'**/Fonts/*.ttf', | ||
'**/Fonts/*.svg', | ||
'**/Fonts/*.woff', | ||
'**/Fonts/*.woff2', | ||
], | ||
}, | ||
} | ||
|
||
// @ts-ignore | ||
export let isViewFile = async file => mm.isMatch(file, PATTERNS.filesView.match) | ||
export let isViewLogicFile = async file => | ||
// @ts-ignore | ||
mm.isMatch(file, PATTERNS.filesViewLogic.match) | ||
export let isViewCustomFile = async file => | ||
// @ts-ignore | ||
mm.isMatch(file, PATTERNS.filesViewCustom.match) && | ||
(await PATTERNS.filesViewCustom.filter([file])).length > 0 | ||
export let isFontCustomFile = async file => | ||
// @ts-ignore | ||
mm.isMatch(file, PATTERNS.filesFontCustom.match) | ||
|
||
export let MATCH = Object.values(PATTERNS) | ||
.map(item => item.match) | ||
// @ts-ignore | ||
.flat() | ||
|
||
export async function getMatchesPerPattern(files) { | ||
// @ts-ignore | ||
return Object.fromEntries( | ||
await Promise.all( | ||
Object.entries(PATTERNS).map(async ([key, { filter, match, ignore }]) => { | ||
let matches = mm(files, match, { ignore }) | ||
if (typeof filter === 'function') { | ||
matches = await filter(matches) | ||
} | ||
return [key, new Set(matches)] | ||
}) | ||
) | ||
) | ||
} |
Oops, something went wrong.