Skip to content

Commit

Permalink
Support webpack aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodal committed Aug 26, 2019
1 parent a6b5db8 commit ab4b43a
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -76,15 +77,25 @@ 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
if (err) reject(err);

// 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);
Expand Down Expand Up @@ -129,7 +140,7 @@ function resolveComponent(currentSource, currPath) {
});
});
} else {
resolve({source:currentSource, ancestorsPaths:[]});
resolve({source: currentSource, ancestorsPaths: []});
}
} catch (e) {
reject(e)
Expand Down

0 comments on commit ab4b43a

Please sign in to comment.