Skip to content

Commit 7706927

Browse files
author
Guillaume Chau
committed
Vue component src support
1 parent 888d022 commit 7706927

File tree

1 file changed

+44
-4
lines changed

1 file changed

+44
-4
lines changed

packages/vue-component/plugin/tag-handler.js

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'path'
12
import postcss from 'postcss';
23
import autoprefixer from 'autoprefixer';
34
import postcssModules from 'postcss-modules';
@@ -57,6 +58,40 @@ VueComponentTagHandler = class VueComponentTagHandler {
5758
message: 'Expected <template>, <script>, or <style> tag in template file',
5859
});
5960
}
61+
62+
if (tag.attribs.src) {
63+
if (tag.contents.trim()) {
64+
throwCompileError({
65+
inputFile: this.inputFile,
66+
message: `Should not have any contents if using the 'src' attribute.`,
67+
tag: tag.tagName,
68+
charIndex: tag.tagStartIndex,
69+
})
70+
} else {
71+
const filePath = path.resolve(path.dirname(getFilePath(this.inputFile)), tag.attribs.src)
72+
try {
73+
tag.origin = Object.assign({}, tag)
74+
tag.basePath = filePath
75+
tag.contents = tag.fileContents = getFileContents(filePath)
76+
tag.sourceName = filePath
77+
tag.tagStartIndex = 0
78+
this.dependencyManager.addDependency(filePath)
79+
} catch (e) {
80+
if (e.message === 'file-not-found') {
81+
throwCompileError({
82+
inputFile: this.inputFile,
83+
message: `File ${filePath} not found.`,
84+
tag: tag.tagName,
85+
charIndex: tag.tagStartIndex,
86+
})
87+
} else {
88+
throw e
89+
}
90+
}
91+
}
92+
} else {
93+
tag.basePath = path.resolve(getFilePath(this.inputFile))
94+
}
6095
}
6196

6297
getResults() {
@@ -101,7 +136,9 @@ VueComponentTagHandler = class VueComponentTagHandler {
101136
//console.log(`Compiling <script> in lang ${lang}...`);
102137
let result = compile({
103138
source: script,
104-
inputFile: this.inputFile
139+
inputFile: this.inputFile,
140+
basePath: tag.basePath,
141+
dependencyManager: this.dependencyManager,
105142
});
106143
script = result.script;
107144
if (result.map) {
@@ -130,7 +167,7 @@ VueComponentTagHandler = class VueComponentTagHandler {
130167
// Babel options
131168
this.babelOptions.sourceMap = true;
132169
this.babelOptions.filename =
133-
this.babelOptions.sourceFileName = fullInputFilePath;
170+
this.babelOptions.sourceFileName = tag.basePath;
134171
this.babelOptions.sourceMapTarget = this.babelOptions.filename + '.map';
135172

136173
// Babel compilation
@@ -215,7 +252,9 @@ VueComponentTagHandler = class VueComponentTagHandler {
215252
//console.log(`Compiling <template> in lang ${lang}...`);
216253
let result = compile({
217254
source: template,
218-
inputFile: this.inputFile
255+
inputFile: this.inputFile,
256+
basePath: templateTag.basePath,
257+
dependencyManager: this.dependencyManager,
219258
});
220259
template = result.template;
221260
}
@@ -272,7 +311,8 @@ VueComponentTagHandler = class VueComponentTagHandler {
272311
let result = compile({
273312
source: css,
274313
inputFile: this.inputFile,
275-
dependencyManager: this.dependencyManager
314+
basePath: styleTag.basePath,
315+
dependencyManager: this.dependencyManager,
276316
});
277317
//console.log('Css result', result);
278318
css = result.css;

0 commit comments

Comments
 (0)