Browse Source

feature: update analyze dependences regex

master
hanchai.chc 4 years ago
parent
commit
e9981c5949
  1. 5
      packages/compile-code/src/compileForOnline.ts
  2. 13
      packages/core/src/utils/analyzeDeps.ts

5
packages/compile-code/src/compileForOnline.ts

@ -23,7 +23,7 @@ interface DSL {
const INSERT_DSL_COMMENT = '// define dsl here'; const INSERT_DSL_COMMENT = '// define dsl here';
const INSERT_NODE_FNS_COMMENT = '// define nodeFns here'; const INSERT_NODE_FNS_COMMENT = '// define nodeFns here';
const importRegex = /import\s+([\s\S]*?)\s+from\s+(?:('[@\.\/\-\w]+')|("[@\.\/\-\w]+"))\s*;?/gm; const importRegex = /import\s([\s\S]*?)\sfrom\s('|")((@\w[\w\.\-]+\/)?(\w[\w\.\-\/]+))\2/gm;
const virtualSourceNode = { const virtualSourceNode = {
id: 'virtual-imove-start', id: 'virtual-imove-start',
shape: 'imove-start', shape: 'imove-start',
@ -92,8 +92,7 @@ const compileNodeFn = (node: Cell.Properties): string => {
.replace( .replace(
importRegex, importRegex,
(match: string, p1: string, p2: string, p3: string) => { (match: string, p1: string, p2: string, p3: string) => {
const pkgName = (p2 || p3).replace(/('|")/g, ''); return `const ${p1} = (await import('https://jspm.dev/${p3}')).default;`;
return `const ${p1} = (await import('https://jspm.dev/${pkgName}')).default;`;
}, },
) )
.replace(/export\s+default/, 'return'); .replace(/export\s+default/, 'return');

13
packages/core/src/utils/analyzeDeps.ts

@ -2,20 +2,15 @@ import axios from 'axios';
import { safeGet } from './index'; import { safeGet } from './index';
import { getLocalConfig } from '../api'; import { getLocalConfig } from '../api';
const regex = /import\s([\s\S]*?)\sfrom\s(?:('[@\.\/\-\w]+')|("[@\.\/\-\w]+"))/gm; const regex = /import\s([\s\S]*?)\sfrom\s('|")((@\w[\w\.\-]+\/)?(\w[\w\.\-]+))(\/[\w\.\-]+)*\2/gm;
// (1 ) (2 )(3(4 ) (5 ))(6 )
const extractPkgs = (code: string, excludePkgs?: string[]): string[] => { const extractPkgs = (code: string, excludePkgs?: string[]): string[] => {
let matchRet = null; let matchRet = null;
const pkgNames: string[] = []; const pkgNames: string[] = [];
while ((matchRet = regex.exec(code)) != null) { while ((matchRet = regex.exec(code)) != null) {
let pkgName = matchRet[2] || matchRet[3]; const pkgName = matchRet[3];
pkgName = pkgName.slice(1, pkgName.length - 1); if (excludePkgs?.indexOf(pkgName) === -1) {
// NOTE: ignore relative path (./ and ../) and excludePkgs
if (
pkgName.indexOf('./') === -1 &&
pkgName.indexOf('../') === -1 &&
excludePkgs?.indexOf(pkgName) === -1
) {
pkgNames.push(pkgName); pkgNames.push(pkgName);
} }
} }

Loading…
Cancel
Save