Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 24 additions & 20 deletions packages/build-plugin-lowcode/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 '';
Expand All @@ -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 '';
Expand Down Expand Up @@ -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;
});
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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
Expand All @@ -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');
}
Expand All @@ -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,
Expand Down Expand Up @@ -958,16 +963,15 @@ 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);
const viewJsPath = getEntry(
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);
Expand All @@ -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,
Expand Down Expand Up @@ -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`,
Expand Down