UNPKG

st-bundle

Version:

CLI for watching and bundling SpringType projects.

152 lines (151 loc) 5.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const appRoot = require("app-root-path"); const fs = require("fs"); const path = require("path"); const utils_1 = require("../utils/utils"); const fileLookup_1 = require("./fileLookup"); const shared_1 = require("./shared"); const browserField_1 = require("./browserField"); const PROJECT_NODE_MODULES = path.join(appRoot.path, 'node_modules'); const NODE_MODULE_REGEX = /^(([^\.][\.a-z0-9@\-_]*)(\/)?([_a-z0-9.@-]+)?(\/)?(.*))$/i; function isNodeModule(path) { const matched = path.match(NODE_MODULE_REGEX); if (!matched) { return; } let [name, b, c] = [matched[2], matched[4], matched[6]]; const result = { name }; if (name[0] === '@') { result.name = name + '/' + b; if (c) { result.target = c; } } else { if (b) { result.target = c ? b + '/' + c : b; } } return result; } exports.isNodeModule = isNodeModule; function parseAllModulePaths(fileAbsPath) { const baseDir = path.dirname(fileAbsPath); const paths = []; const snippets = baseDir.split(/node_modules/); let current = ''; if (snippets.length > 1) { const total = snippets.length - 1; for (let i = 0; i < total; i++) { current = path.join(current, snippets[i], 'node_modules'); paths.push(current); } const last = snippets[total]; const matchedLast = last.match(/[\/|\\]([a-z-@0-9_-]+)/gi); if (matchedLast && matchedLast[0]) { paths.push(path.join(paths[paths.length - 1], matchedLast[0], 'node_modules')); } return paths; } else { return [PROJECT_NODE_MODULES]; } } exports.parseAllModulePaths = parseAllModulePaths; function findTargetFolder(props, parsed) { // handle custom modules here if (props.modules) { for (const i in props.modules) { const f = path.join(props.modules[i], parsed.name); if (fs.existsSync(f)) { return f; } } } const paths = parseAllModulePaths(props.filePath); for (let i = paths.length - 1; i >= 0; i--) { const attempted = path.join(paths[i], parsed.name); if (utils_1.fileExists(attempted)) { return attempted; } } } exports.findTargetFolder = findTargetFolder; function nodeModuleLookup(props, parsed) { const folder = findTargetFolder(props, parsed); const result = {}; const pkg = { name: parsed.name, packageRoot: folder, }; result.meta = pkg; if (!folder) { return { error: `Cannot resolve "${parsed.name}"` }; } const packageJSONFile = path.join(folder, 'package.json'); if (!utils_1.fileExists(packageJSONFile)) { return { error: `Failed to find package.json in ${folder} when resolving module ${parsed.name}` }; } const json = require(packageJSONFile); pkg.version = json.version || '0.0.0'; pkg.browser = json.browser; pkg.packageJSONLocation = packageJSONFile; pkg.packageRoot = folder; if (json['fuse-box']) { pkg.fusebox = json['fuse-box']; } const isBrowser = props.buildTarget === 'browser'; // extract entry point // extract target if required if (parsed.target) { const parsedLookup = fileLookup_1.fileLookup({ target: parsed.target, fileDir: folder }); if (!parsedLookup) { return { error: `Failed to resolve ${props.target} in ${parsed.name}` }; } result.targetAbsPath = parsedLookup.absPath; if (json.browser && typeof json.browser === 'object') { const override = browserField_1.handleBrowserField(pkg, parsedLookup.absPath); if (override) { result.targetAbsPath = override; parsedLookup.customIndex = true; } } result.isEntry = false; result.targetFuseBoxPath = utils_1.makeFuseBoxPath(folder, result.targetAbsPath); if (parsedLookup.customIndex) { result.forcedStatement = `${parsed.name}/${result.targetFuseBoxPath}`; } } else { const entryFile = shared_1.getFolderEntryPointFromPackageJSON({ json: json, isBrowserBuild: isBrowser }); const entryLookup = fileLookup_1.fileLookup({ target: entryFile, fileDir: folder }); if (!entryLookup.fileExists) { return { error: `Failed to resolve an entry point in package ${parsed.name}. File ${entryFile} cannot be resolved.`, }; } pkg.entryAbsPath = entryLookup.absPath; pkg.entryFuseBoxPath = utils_1.makeFuseBoxPath(folder, entryLookup.absPath); result.isEntry = true; result.targetAbsPath = pkg.entryAbsPath; result.targetFuseBoxPath = pkg.entryFuseBoxPath; if (shared_1.isBrowserEntry(json, isBrowser)) { result.forcedStatement = `${parsed.name}/${result.targetFuseBoxPath}`; } if (json.browser && typeof json.browser === 'object') { const override = browserField_1.handleBrowserField(pkg, entryLookup.absPath); if (override) { //result.targetFuseBoxPath = result.targetAbsPath = override; pkg.entryAbsPath = override; result.targetFuseBoxPath = utils_1.makeFuseBoxPath(folder, override); pkg.entryFuseBoxPath = result.targetFuseBoxPath; entryLookup.customIndex = true; } } } result.targetExtension = path.extname(result.targetAbsPath); return result; } exports.nodeModuleLookup = nodeModuleLookup;