Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.

fix: Resolve src values on style blocks with require.resolve #70

Merged
merged 1 commit into from
May 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ export class SFCCompiler {
script: ScriptOptions
style: StyleOptions
template: TemplateOptions
resolve: RequireResolve

constructor(
script: ScriptOptions,
style: StyleOptions,
template: TemplateOptions
template: TemplateOptions,
resolve: RequireResolve = require.resolve
) {
this.template = template
this.style = style
this.script = script
this.resolve = resolve
}

compileToDescriptor(
Expand Down Expand Up @@ -193,8 +196,20 @@ export class SFCCompiler {
}

private read(filename: string, context: string): string {
return fs
.readFileSync(path.resolve(path.dirname(context), filename))
.toString()
try {
return fs
.readFileSync(
filename.startsWith('.')
? path.resolve(path.dirname(context), filename)
: this.resolve(filename, { paths: [path.dirname(context)] })
)
.toString()
} catch (e) {
if (/cannot find module/i.test(e.message)) {
throw Error(`Cannot find '${filename}' in '${context}'`)
}

throw e
}
}
}