From ab4b43aa10306863d3cc1d4e131ef1cadf628988 Mon Sep 17 00:00:00 2001 From: mrodal Date: Mon, 26 Aug 2019 16:43:13 -0300 Subject: [PATCH] Support webpack aliases --- src/index.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 3a02b06..35aaa96 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,8 @@ const loader = function (source, map) { validateOptions(schema, options, 'vue-inheritance-loader'); let callback = this.async(); - getMergedCode(source, this.context).then(result => { + let aliases = this._compiler.options.resolve.alias; + getMergedCode(source, this.context, aliases).then(result => { // To make HMR aware of the base file and reload it when it changes result.ancestorsPaths.forEach(ancestor => { this.addDependency(ancestor); @@ -37,9 +38,9 @@ const loader = function (source, map) { }); }; -const getMergedCode = function (source, currPath) { +const getMergedCode = function (source, currPath, aliases) { return new Promise((resolve, reject) => { - resolveComponent(source, currPath).then(({source, ancestorsPaths}) => { + resolveComponent(source, currPath, aliases).then(({source, ancestorsPaths}) => { // Remove comment lines at beginning of script block that were generated by the SFC parser let finalDescriptor = toDescriptor(source); @@ -67,7 +68,7 @@ const getMergedCode = function (source, currPath) { }) }; -function resolveComponent(currentSource, currPath) { +function resolveComponent(currentSource, currPath, aliases) { return new Promise((resolve, reject) => { try { let currentDesc = toDescriptor(currentSource); @@ -76,7 +77,17 @@ function resolveComponent(currentSource, currPath) { // else return code as is if (currentDesc.template && currentDesc.template.attrs[options.EXTENDS_ATTR]) { let baseRelPath = currentDesc.template.attrs[options.EXTENDS_ATTR]; - let baseAbsPath = path.join(currPath, baseRelPath); + let baseRelPathParts = baseRelPath.split(/[\/\\]/); + + // If there's a matching alias, use it. If not, use the context path + let matchingAlias = Object.keys(aliases).find(k => baseRelPathParts[0] === k); + if (matchingAlias) { + baseRelPathParts.shift(); + baseRelPath = baseRelPathParts.join('/'); + var baseAbsPath = path.join(aliases[matchingAlias], baseRelPath); + } else { + var baseAbsPath = path.join(currPath, baseRelPath); + } fs.readFile(baseAbsPath, 'utf8', (err, contents) => { // File read error, reject @@ -84,7 +95,7 @@ function resolveComponent(currentSource, currPath) { // Resolve the base component recursively to support inheritance in N levels let basePath = path.dirname(baseAbsPath); - resolveComponent(contents, basePath).then(({source, ancestorsPaths}) => { + resolveComponent(contents, basePath, aliases).then(({source, ancestorsPaths}) => { try { // Add this ancestor to the ancestors return array ancestorsPaths.push(baseAbsPath); @@ -129,7 +140,7 @@ function resolveComponent(currentSource, currPath) { }); }); } else { - resolve({source:currentSource, ancestorsPaths:[]}); + resolve({source: currentSource, ancestorsPaths: []}); } } catch (e) { reject(e)