UNPKG

@omnia/tooling-vue

Version:

Used to bundle and serve manifests web component that build on Vue framework.

105 lines (104 loc) 4.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.omniaRuntime = void 0; const base_1 = require("../base"); const utils_1 = require("../../utils"); /** Inject omnia magic to runtime variable */ exports.omniaRuntime = (0, base_1.defineTransformer)("omnia-runtime", () => { const transformResourcePathRegex = new RegExp(/<omnia-transform-resource.*?path=['"](.*?)['"]/); return { test(code, id) { return utils_1.pathUtils.isJavaScriptSuperset(id); }, transform(code, id, fileGraph, ctx, fromWorker) { const transformedResourcePathResult = transformResourcePathRegex.exec(code); const webpackModulePath = transformedResourcePathResult?.length > 1 ? transformedResourcePathResult[1] : fileGraph.id; if (fromWorker == false) { code = code + "\n" + ` /** * Omnia Hot Module Reload */ import.meta.hot.accept();`; } return '\n' + injectWebpackModuleExports(code, ctx.serviceId, fileGraph, webpackModulePath); } }; }); function injectWebpackModuleExports(code, serviceId, fileGraph, webpackModulePath) { const exports = []; const wildcardExports = []; const defaultExport = fileGraph.js.metadata.exports.default; if (defaultExport.available) { if (code.includes('const __default__ ')) { // injected by vueJsx hot reload exports.push(`default: __default__`); } else if (defaultExport.nameless) { code = utils_1.moduleAnalysis.replaceNamelessDefaultExport(code); exports.push(`default: ${defaultExport.original}`); } else if (!!defaultExport.wildcard) { code = utils_1.moduleAnalysis.replaceWildcardDefaultExport(code, defaultExport.wildcard); exports.push(`default: ${defaultExport.original}`); } } if (fileGraph.js.metadata.namedExports.length > 0) { exports.push(...fileGraph.js.metadata.namedExports); } if (fileGraph.js.metadata.exports.externalNamedVariables.length > 0 || fileGraph.js.metadata.exports.wildcards.length > 0 || fileGraph.js.metadata.exports.localNamedVariables.length > 0) { if (fileGraph.js.metadata.exports.externalNamedVariables.length > 0 || fileGraph.js.metadata.exports.wildcards.length > 0) { code += `\n\n// HMR re-imports for webpack intergration`; } fileGraph.js.metadata.exports.externalNamedVariables.forEach(externalNamedVariable => { if (externalNamedVariable.default) { code += `\nimport { ${externalNamedVariable.default} } from '${externalNamedVariable.rawPath}';`; } else if (externalNamedVariable.values.length > 0) { code += `\nimport { ${externalNamedVariable.values.join(', ')} } from '${externalNamedVariable.rawPath}';`; } else if (Object.keys(externalNamedVariable.asValues).length > 0) { const asValues = externalNamedVariable.asValues; code += `\nimport { ${Object.keys(asValues).map(k => `${k} as ${asValues[k]}`).join(', ')} } from '${externalNamedVariable.rawPath}';`; } }); fileGraph.js.metadata.exports.localNamedVariables.forEach(localNamedVariable => { if (localNamedVariable.default) { return; } const asValues = localNamedVariable.asValues; Object.keys(asValues).forEach(k => { const foundIndex = exports.findIndex(i => i == asValues[k]); exports[foundIndex] = `${asValues[k]}: ${k}`; }); }); fileGraph.js.metadata.exports.wildcards.forEach((wildcard, index) => { const wildcardExportVariable = `__wildcardExport${index + 1}`; wildcardExports.push(wildcardExportVariable); code += `\nimport * as ${wildcardExportVariable} from '${wildcard.rawPath}';`; }); } let exportVariables = ''; if (exports.length > 0) { exportVariables = exports.join(`,\n `); } if (wildcardExports.length > 0) { const wildcardExportString = wildcardExports.map(variable => `...${variable}`).join(',\n '); if (exportVariables.length > 0) { exportVariables = exportVariables + `,\n ` + wildcardExportString; } else { exportVariables = wildcardExportString; } } if (exportVariables.length > 0) { code += `\n ${utils_1.transformUtils.HMR_MODULE_EXPORTS_VARIABLE}['${serviceId}']['${fileGraph.resourceId}'] = ${utils_1.transformUtils.HMR_MODULE_EXPORTS_VARIABLE}['${serviceId}']['${fileGraph.resourceId}'] || {}; ${utils_1.transformUtils.HMR_MODULE_EXPORTS_VARIABLE}['${serviceId}']['${fileGraph.resourceId}']['${webpackModulePath}'] = { ${exportVariables} };`; } return code; }