diff --git a/js/bundler/bundler.js b/js/bundler/bundler.js index 9ee778ffa7..8f3de68d35 100644 --- a/js/bundler/bundler.js +++ b/js/bundler/bundler.js @@ -39,12 +39,12 @@ const {globSync} = require('glob'); class Bundler { /** - * @param {Object.} modulePaths + * @param {Object.} modPaths * @param {string} [basePath] * @param {string} [transpiledPath] */ - constructor(modulePaths, basePath, transpiledPath) { - this.modulePaths = modulePaths; + constructor(modPaths, basePath, transpiledPath) { + this.modPaths = modPaths; this.basePath = basePath ?? 'client'; this.transpiledPath = transpiledPath ?? 'client/lib/transpiled'; @@ -386,11 +386,11 @@ class Bundler { * @return string */ #obtainModuleName(file) { - for (let mod in this.modulePaths) { - let part = this.basePath + '/' + this.modulePaths[mod] + '/src/'; + for (let mod in this.modPaths) { + let part = this.basePath + '/' + this.modPaths[mod] + '/src/'; if (file.indexOf(part) === 0) { - return mod + ':' + file.substring(part.length, file.length - 3); + return `modules/${mod}/` + file.substring(part.length, file.length - 3); } } @@ -436,7 +436,7 @@ class Bundler { return { name: moduleName, - deps: this.#obtainModuleDeps(tsSourceFile), + deps: this.#obtainModuleDeps(tsSourceFile, moduleName), }; } @@ -451,7 +451,9 @@ class Bundler { return; } - deps.push(node.text); + let dep = this.#normalizeModModuleName(node.text); + + deps.push(dep); }); } } @@ -463,26 +465,36 @@ class Bundler { } /** - * @param sourceFile + * @param {string} sourceFile + * @param {string} mod * @return {string[]} */ - #obtainModuleDeps(sourceFile) { + #obtainModuleDeps(sourceFile, mod) { return sourceFile.statements - .filter(item => item.importClause) .filter(item => item.importClause && item.moduleSpecifier) .map(item => item.moduleSpecifier.text) - .map(item => { - if (!item.startsWith('modules/')) { - return item; - } + .map(/** string */item => { - let mod = item.split('/')[1]; - let part = item.split('/').slice(2).join('/'); + // @todo Normalize relative path. - return mod + ':' + part; + return this.#normalizeModModuleName(item); }); } + /** + * @param {string} module + * @return {string} + */ + #normalizeModModuleName(module) { + if (!module.includes(':')) { + return module; + } + + let [mod, part] = module.split(':'); + + return `modules/${mod}/` + part; + } + /** * @param {string} path * @return {boolean} @@ -494,8 +506,8 @@ class Bundler { let startParts = [this.#getSrcPath()]; - for (let mod in this.modulePaths) { - let modPath = this.basePath + '/' + this.modulePaths[mod] + '/src/'; + for (let mod in this.modPaths) { + let modPath = this.basePath + '/' + this.modPaths[mod] + '/src/'; startParts.push(modPath); } diff --git a/js/transpiler/transpiler.js b/js/transpiler/transpiler.js index d7abca88e3..e2660a6d05 100644 --- a/js/transpiler/transpiler.js +++ b/js/transpiler/transpiler.js @@ -147,6 +147,18 @@ class Transpiler { path = itemPath; } + else if (module.startsWith('modules/')) { + let items = module.split('/'); + + if (items.length < 2) { + throw new Error(`Bad module name ${module}.`); + } + + let mod = items[1]; + + part = 'modules/' + mod + '/' + part; + path = items.slice(2).join('/'); + } destDir += '/' + part + '/' + path.split('/').slice(0, -1).join('/'); @@ -185,7 +197,7 @@ class Transpiler { */ #obtainModuleName(file) { if (this.mod) { - return this.mod + ':' + file.slice(this.path.length + 1, -3); + return `modules/${this.mod}/` + file.slice(this.path.length + 1, -3); } return file.slice(this.path.length + 1, -3);