@omnia/tooling-vue
Version:
Used to bundle and serve manifests web component that build on Vue framework.
97 lines (96 loc) • 5.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.importPath = void 0;
const utils_1 = require("../utils");
const base_1 = require("./base");
/** Transform es6 import paths to browser runtime variables */
exports.importPath = (0, base_1.defineTransformer)("omnia-import-path", (replacers) => {
validate(replacers);
const froms = replacers.reduce((prev, current) => {
if (Array.isArray(current.from)) {
return prev.concat(current.from);
}
return prev.concat([current.from]);
}, []);
const regex = new RegExp(`(import)(\\s+|(\\s+.[\n\r]*))[^\'\"]*[\'\"](${froms.map(f => f.replace('.', '\.').replace('/', '\/')).join('|')})[\'\"]`, 'g');
return {
test(code, id) {
return utils_1.pathUtils.isJavaScriptSuperset(id) && regex.test(code);
},
transform(code, id, fileGraph, ctx, fromWorker) {
let hasDefaultImports = false;
code = code.replace(regex, original => {
const startIndex = original.indexOf('\'') > 0 ? original.indexOf('\'') : original.indexOf('\"');
const endIndex = original.lastIndexOf('\'') > 0 ? original.lastIndexOf('\'') : original.lastIndexOf('\"');
const moduleName = original.substr(startIndex + 1, endIndex - startIndex - 1);
const replacer = replacers.find(r => {
if (Array.isArray(r.from)) {
return r.from.indexOf(moduleName) > -1;
}
return r.from === moduleName;
});
if (!replacer) {
throw new utils_1.ErrorHMR('Unexpected: replacer not found during code transform');
}
if (replacer.ignore) {
// allow ignored vendor to register bareImportVariable so dynamicBundle will skip this
const fileGraph = ctx.getFileGraph(replacer.from);
if (fileGraph && fileGraph.bareImport && fileGraph.js && !fileGraph.js.bareImportVariable) {
fileGraph.js.bareImportVariable = 'ignored';
}
return '';
}
let importedVariablesStatement = original.substr(0, startIndex + 1);
const selfImport = importedVariablesStatement.includes('from') == false;
if (selfImport) {
importedVariablesStatement = importedVariablesStatement
.replace('import', '');
}
else {
importedVariablesStatement = importedVariablesStatement
.replace('import', 'const')
.replace('from', '=');
}
importedVariablesStatement = importedVariablesStatement
.replace(/\*\s+as/g, '')
.replace(/\s+as/g, ':')
.replace('\'', '')
.replace('\"', '');
const semicolonIndexOfDefaultAndVariableImports = importedVariablesStatement.substr(importedVariablesStatement.indexOf('const'), importedVariablesStatement.indexOf('{')).indexOf(',');
let transformedStatement = '';
if (semicolonIndexOfDefaultAndVariableImports > -1) {
hasDefaultImports = true;
const defaultVariableStr = importedVariablesStatement.substr(0, semicolonIndexOfDefaultAndVariableImports).trim();
transformedStatement += `${defaultVariableStr} = ${utils_1.transformUtils.wrapDefaultImportVariable(replacer.to(true, ctx))};\n`;
const variablesStr = importedVariablesStatement.substr(semicolonIndexOfDefaultAndVariableImports + 1, importedVariablesStatement.indexOf('}') - semicolonIndexOfDefaultAndVariableImports + 1).trim();
transformedStatement += `const ${variablesStr} = ${replacer.to(false, ctx)}`;
}
else {
const defaultImport = importedVariablesStatement.includes('{') == false;
transformedStatement = `${importedVariablesStatement}${defaultImport ?
utils_1.transformUtils.wrapDefaultImportVariable(replacer.to(defaultImport, ctx)) :
replacer.to(defaultImport, ctx)}`;
if (!hasDefaultImports && defaultImport) {
hasDefaultImports = true;
}
}
// console.log(original, transformedStatement);
return transformedStatement.trim();
});
if (hasDefaultImports) {
code = utils_1.transformUtils.injectDefaultImportVariableDecleration(code);
}
return code;
}
};
});
function validate(replacers) {
replacers.forEach(r => {
if (r.from.length == 0) {
throw new utils_1.ErrorHMR('replacer.from is empty or undefined');
}
if (typeof r.to != 'function') {
throw new utils_1.ErrorHMR('replacer.to needs to be a function');
}
});
}