forked from umijs/dumi
-
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.
feat(preset): support to analyze multi-level local deps for demo
- Loading branch information
1 parent
a026b96
commit 6c636ad
Showing
7 changed files
with
163 additions
and
140 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
packages/preset-dumi/src/transformer/demo/dependencies.ts
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,121 @@ | ||
import path from 'path'; | ||
import slash from 'slash'; | ||
import * as babel from '@babel/core'; | ||
import * as types from '@babel/types'; | ||
import traverse from '@babel/traverse'; | ||
import { | ||
getModuleResolvePkg, | ||
getModuleResolvePath, | ||
getModuleResolveContent, | ||
} from '../../utils/moduleResolver'; | ||
import { saveFileOnDepChange } from '../../utils/watcher'; | ||
import { getBabelOptions } from './options'; | ||
|
||
interface IDepAnalyzeResult { | ||
dependencies: { [key: string]: string }; | ||
files: { [key: string]: { path: string; content: string } }; | ||
} | ||
|
||
// local dependency extensions which will be collected | ||
export const LOCAL_DEP_EXT = [ | ||
'.jsx', | ||
'.tsx', | ||
'.js', | ||
'.ts', | ||
'.json', | ||
'.less', | ||
'.css', | ||
'.scss', | ||
'.sass', | ||
'.styl', | ||
]; | ||
|
||
function analyzeDeps( | ||
raw: babel.BabelFileResult['ast'] | string, | ||
{ | ||
isTSX, | ||
fileAbsPath, | ||
entryAbsPath, | ||
}: { isTSX: boolean; fileAbsPath: string; entryAbsPath?: string }, | ||
totalFiles?: IDepAnalyzeResult['files'], | ||
): IDepAnalyzeResult { | ||
// support to pass babel transform result directly | ||
const ast = typeof raw === 'string' ? babel.transformSync(raw, getBabelOptions(isTSX)).ast : raw; | ||
const files = totalFiles || {}; | ||
const dependencies = {}; | ||
|
||
// traverse all expression | ||
traverse(ast, { | ||
CallExpression(callPath) { | ||
const callPathNode = callPath.node; | ||
|
||
// tranverse all require statement | ||
if ( | ||
types.isIdentifier(callPathNode.callee) && | ||
callPathNode.callee.name === 'require' && | ||
types.isStringLiteral(callPathNode.arguments[0]) && | ||
callPathNode.arguments[0].value !== 'react' | ||
) { | ||
const requireStr = callPathNode.arguments[0].value; | ||
const resolvePath = getModuleResolvePath({ | ||
basePath: fileAbsPath, | ||
sourcePath: requireStr, | ||
}); | ||
const resolvePathParsed = path.parse(resolvePath); | ||
|
||
if (resolvePath.includes('node_modules')) { | ||
// save external deps | ||
const pkg = getModuleResolvePkg({ | ||
basePath: fileAbsPath, | ||
sourcePath: requireStr, | ||
}); | ||
|
||
dependencies[pkg.name] = pkg.version; | ||
} else if ( | ||
// only analysis for valid local file type | ||
LOCAL_DEP_EXT.includes(resolvePathParsed.ext) && | ||
// do not collect entry file | ||
resolvePath !== entryAbsPath | ||
) { | ||
// save local deps | ||
const fileName = slash(path.relative(fileAbsPath, resolvePath)).replace( | ||
/(\.\/|\..\/)/g, | ||
'', | ||
); | ||
|
||
// to avoid circular-reference | ||
if (fileName && !files[fileName]) { | ||
files[fileName] = { | ||
path: requireStr, | ||
content: getModuleResolveContent({ | ||
basePath: fileAbsPath, | ||
sourcePath: requireStr, | ||
}), | ||
}; | ||
|
||
// continue to collect deps for dep | ||
const result = analyzeDeps( | ||
files[fileName].content, | ||
{ | ||
isTSX: /\.tsx?/.test(resolvePathParsed.ext), | ||
fileAbsPath: resolvePath, | ||
entryAbsPath: entryAbsPath || fileAbsPath, | ||
}, | ||
files, | ||
); | ||
|
||
Object.assign(files, result.files); | ||
Object.assign(dependencies, result.dependencies); | ||
|
||
// trigger parent file change to update frontmatter when dep file change | ||
saveFileOnDepChange(fileAbsPath, resolvePath); | ||
} | ||
} | ||
} | ||
}, | ||
}); | ||
|
||
return { files, dependencies }; | ||
} | ||
|
||
export default analyzeDeps; |
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,18 @@ | ||
import ctx from '../../context'; | ||
|
||
export const getBabelOptions = (isTSX: boolean) => ({ | ||
presets: [ | ||
require.resolve('@babel/preset-react'), | ||
require.resolve('@babel/preset-env'), | ||
...(ctx.umi?.config?.extraBabelPresets || []), | ||
], | ||
plugins: [ | ||
require.resolve('@babel/plugin-proposal-class-properties'), | ||
[require.resolve('@babel/plugin-transform-modules-commonjs'), { strict: true }], | ||
...(isTSX ? [[require.resolve('@babel/plugin-transform-typescript'), { isTSX: true }]] : []), | ||
...(ctx.umi?.config?.extraBabelPlugins || []), | ||
], | ||
ast: true, | ||
babelrc: false, | ||
configFile: false, | ||
}); |
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
16 changes: 0 additions & 16 deletions
16
packages/preset-dumi/src/transformer/test/demo-deps.test.ts
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