From a990619bc656a1b51b8d99635e1536a43917dbe5 Mon Sep 17 00:00:00 2001 From: feirlau Date: Fri, 17 May 2024 14:27:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20windows=E4=B8=8B=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 支持windows --- packages/build-plugin-lowcode/src/index.js | 44 ++++++++++++---------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/packages/build-plugin-lowcode/src/index.js b/packages/build-plugin-lowcode/src/index.js index 87bf4bc..c28e4ab 100644 --- a/packages/build-plugin-lowcode/src/index.js +++ b/packages/build-plugin-lowcode/src/index.js @@ -11,6 +11,7 @@ const babelCompile = require('./compile/babel'); const metaCompile = require('./compile/meta'); const getDemoDir = require('./utils/getDemoDir'); const isWsl = require('is-wsl'); +const os = require('os'); let INIT_STATE = false; let PARSED_NPM_NAME; @@ -65,15 +66,21 @@ const defaultScssEntryPaths = [ `./components/index.scss`, ]; +function normalizePathTextForWindows(pathText) { + if (os.platform() === 'win32' && pathText && !pathText.includes('\\\\')) { + return pathText.replace(/\\/g, '\\\\'); + } + return pathText; +} + function getEntry(rootDir, entryPath) { if (entryPath && fse.existsSync(path.resolve(rootDir, entryPath))) { - return path.resolve(rootDir, entryPath).replace(/\\/g, '\\\\'); + return path.resolve(rootDir, entryPath); } for (let i = 0; i < defaultEntryPaths.length; i++) { const p = path.resolve(rootDir, defaultEntryPaths[i]); if (fse.existsSync(p)) { - return p.replace(/\\/g, '\\\\'); - // return p; + return p; } } return ''; @@ -83,8 +90,7 @@ function getScssEntry(rootDir) { for (let i = 0; i < defaultScssEntryPaths.length; i++) { const p = path.resolve(rootDir, defaultScssEntryPaths[i]); if (fse.existsSync(p)) { - return p.replace(/\\/g, '\\\\'); - // return p; + return p; } } return ''; @@ -292,7 +298,7 @@ async function build(options, pluginOptions, execCompile) { }), ); const metaPathMap = {}; - metaPaths.forEach((item) => { + metaPaths.forEach((item) => { metaPathMap[path.basename(item).replace(path.extname(item), '')] = item; // metaPathMap[item.slice(item.lastIndexOf('/') + 1, item.lastIndexOf('.'))] = item; }); @@ -673,10 +679,9 @@ async function bundleMetaV2(options, pluginOptions, execCompile, metaType) { ); usedComponents.push(component); } - metaJsPath = metaJsPath.replace(/\\/g, '\\\\') return `import ${ component.includes('.') ? component.replace(/\./g, '') : component - }Meta from '${metaJsPath}'`; + }Meta from '${normalizePathTextForWindows(metaJsPath)}'`; }) .join('\n'); const metaPath = generateEntry({ @@ -823,12 +828,12 @@ async function bundleEditorView( let componentViewsImportStr; const lowcodeViewPath = path.resolve(rootDir, `${lowcodeDir}/view.tsx`); if (singleComponent && fse.existsSync(lowcodeViewPath)) { - componentViewsImportStr = `import * as SingleComponentData from '${lowcodeViewPath}'`; + componentViewsImportStr = `import * as SingleComponentData from '${normalizePathTextForWindows(lowcodeViewPath)}'`; componentViews = `{ ...SingleComponentData }`; // default 不一定存在,export { default } 不安全可能会报错 - componentViewsExportStr = `\nconst entryDefault = componentInstances.default;\nexport { entryDefault as default };\nexport * from '${lowcodeViewPath}';`; + componentViewsExportStr = `\nconst entryDefault = componentInstances.default;\nexport { entryDefault as default };\nexport * from '${normalizePathTextForWindows(lowcodeViewPath)}';`; } else { const _componentViews = getUsedComponentViews(rootDir, lowcodeDir, components) || []; componentViews = `{${_componentViews @@ -846,8 +851,8 @@ async function bundleEditorView( componentViewsImportStr = _componentViews .map((component) => { const componentNameFolder = camel2KebabComponentName(component); - const viewJsPath = path.resolve(rootDir, `${lowcodeDir}/${componentNameFolder}/view`).replace(/\\/g, '\\\\'); - return `import * as ${component}Data from '${viewJsPath}';`; + const viewJsPath = path.resolve(rootDir, `${lowcodeDir}/${componentNameFolder}/view`); + return `import * as ${component}Data from '${normalizePathTextForWindows(viewJsPath)}'`; }) .join('\n'); } @@ -857,8 +862,8 @@ async function bundleEditorView( filename: 'view.js', rootDir, params: { - entryPath: getEntry(rootDir, entryPath), - scssImport: scssEntry ? `import '${scssEntry}'` : '', + entryPath: normalizePathTextForWindows(getEntry(rootDir, entryPath)), + scssImport: scssEntry ? `import '${normalizePathTextForWindows(scssEntry)}'` : '', componentViews, componentViewsExportStr, componentViewsImportStr, @@ -958,8 +963,7 @@ async function bundleRenderView(options, pluginOptions, platform, execCompile) { return `const ${component} = getRealComponent(${component}Data, '${component}');\nexport { ${component} };`; }) .join('\n'); - const exportPath = `\nexport { default } from '${getEntry(rootDir, entryPath)}';`; - componentViewsExportStr += exportPath.includes('\\\\') ? exportPath : exportPath.replace(/\\/g, '\\\\'); + const exportPath = `\nexport { default } from '${normalizePathTextForWindows(getEntry(rootDir, entryPath))}';`; componentViewsImportStr = _componentViews .map((component) => { const componentNameFolder = camel2KebabComponentName(component); @@ -967,7 +971,7 @@ async function bundleRenderView(options, pluginOptions, platform, execCompile) { rootDir, `src/${platform}/components/${componentNameFolder}/view`, ); - return `import * as ${component}Data from '${viewJsPath}'`; + return `import * as ${component}Data from '${normalizePathTextForWindows(viewJsPath)}'`; }) .join('\n'); const scssEntry = getScssEntry(rootDir); @@ -976,8 +980,8 @@ async function bundleRenderView(options, pluginOptions, platform, execCompile) { filename: `${platform}.view.js`, rootDir, params: { - entryPath: getEntry(rootDir, entryPath), - scssImport: scssEntry ? `import '${scssEntry}'` : '', + entryPath: normalizePathTextForWindows(getEntry(rootDir, entryPath)), + scssImport: scssEntry ? `import '${normalizePathTextForWindows(scssEntry)}'` : '', componentViews, componentViewsExportStr, componentViewsImportStr, @@ -1254,7 +1258,7 @@ async function bundleComponentMeta(webPackConfig, options, pluginOptions, execCo const componentJsPath = `${lowcodeDir}/${componentNameFolder}/${metaFilename}`; const metaJsPath = path.resolve(rootDir, componentJsPath); const componentMetaName = `${component}Meta`; - const componentImportStr = `import ${componentMetaName} from '${metaJsPath}';`; + const componentImportStr = `import ${componentMetaName} from '${normalizePathTextForWindows(metaJsPath)}';`; const componentMetaPath = generateEntry({ template: 'component-meta.js', filename: `${componentJsPath}.js`,