MCPcopy
hub / github.com/zh-lx/code-inspector / transformWebpackCodeInspectorContent

Function transformWebpackCodeInspectorContent

packages/webpack/src/loader.ts:12–110  ·  view source on GitHub ↗
(
  loaderContext: any,
  content: string,
)

Source from the content-addressed store, hash-verified

10const jsxParamList = ['isJsx', 'isTsx', 'lang.jsx', 'lang.tsx'];
11
12async function transformWebpackCodeInspectorContent(
13 loaderContext: any,
14 content: string,
15) {
16 let filePath = normalizePath(loaderContext.resourcePath); // 当前文件的绝对路径
17 let params = new URLSearchParams(loaderContext.resource.split('?')?.[1] || '');
18 const options = loaderContext.query || {};
19 let { escapeTags = [], mappings } = options;
20
21 if (isExcludedFile(filePath, options)) {
22 return content;
23 }
24
25 filePath = getMappingFilePath(filePath, mappings);
26
27 // jsx 语法
28 const isJSX =
29 isJsTypeFile(filePath) ||
30 (filePath.endsWith('.vue') &&
31 jsxParamList.some((param) => params.get(param) !== null));
32 if (isJSX) {
33 return await transformCode({
34 content,
35 filePath,
36 fileType: 'jsx',
37 escapeTags,
38 pathType: options.pathType,
39 mdx: options.mdx,
40 });
41 }
42
43 // vue jsx
44 const isJsxWithScript =
45 filePath.endsWith('.vue') &&
46 (params.get('lang') === 'tsx' || params.get('lang') === 'jsx');
47 if (isJsxWithScript) {
48 const { descriptor } = parseSFC(content, {
49 sourceMap: false,
50 });
51 // 处理 <script> 标签内容
52 // 注意:.vue 允许同时存在 <script> 和 <script setup>
53 const scripts = [
54 descriptor.script?.content,
55 descriptor.scriptSetup?.content,
56 ];
57 for (const script of scripts) {
58 if (!script) continue;
59 const newScript = await transformCode({
60 content: script,
61 filePath,
62 fileType: 'jsx',
63 escapeTags,
64 pathType: options.pathType,
65 mdx: options.mdx,
66 });
67 content = content.replace(script, () => newScript);
68 }
69 return content;

Callers 1

Calls 6

normalizePathFunction · 0.90
isExcludedFileFunction · 0.90
getMappingFilePathFunction · 0.90
isJsTypeFileFunction · 0.90
transformCodeFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected