-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathvue-sass.js
151 lines (135 loc) · 4.01 KB
/
vue-sass.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import path from 'path'
import fs from 'fs'
import sass from 'node-sass'
import { Meteor } from 'meteor/meteor'
global.vue = global.vue || {}
global.vue.lang = global.vue.lang || {}
function resolveImport (dependencyManager) {
return function (url, prev, done) {
let resolvedFilename
url = url.replace(/^["']?(.*?)["']?$/, '$1')
if (url.indexOf('~') === 0 || url.indexOf('/') === 0) {
resolvedFilename = url.substr(1)
/* } else if (url.indexOf('{') === 0) {
resolvedFilename = decodeFilePath(url) */
} else {
let currentDirectory = path.dirname(prev === 'stdin' ? this.options.outFile : prev)
resolvedFilename = path.resolve(currentDirectory, url)
}
const importPaths = [resolvedFilename]
const pkg = require('package.json') // can not be moved outside. Reqired here to get the package.json of the project that is being run
try {
// get the package.json config option and create paths for the requested file.
pkg.vue.css.sass.includePaths.forEach((str) => {
importPaths.push(path.resolve(str, url))
})
} catch (e) {
// Ignore error. package.json option is not set.
}
const resolvedNames = importPaths.map(discoverImportPath).filter(
fileName => fileName !== null && typeof fileName !== 'undefined'
)
if (resolvedNames.length < 1) {
done(new Error('Unknown import (file not found): ' + url))
} else {
dependencyManager.addDependency(resolvedNames[0])
done({
file: resolvedNames[0],
})
}
}
}
function discoverImportPath (importPath) {
const potentialPaths = [importPath]
const potentialFileExtensions = ['scss', 'sass']
if (!path.extname(importPath)) {
potentialFileExtensions.forEach(extension => potentialPaths.push(`${importPath}.${extension}`))
}
if (path.basename(importPath)[0] !== '_') {
[].concat(potentialPaths).forEach(potentialPath => potentialPaths.push(`${path.dirname(potentialPath)}/_${path.basename(potentialPath)}`))
}
for (let i = 0, potentialPath = potentialPaths[i]; i < potentialPaths.length; i++, potentialPath = potentialPaths[i]) {
if(fs.existsSync(potentialPath)) {
const stat = fs.lstatSync(potentialPath);
// if path is an symlink, check if the symlink points to a file
if(stat.isSymbolicLink()) {
try {
const realPath = fs.realpathSync(potentialPath);
if(fs.lstatSync(realPath).isFile()) {
return potentialPath;
}
} catch (e) {
// ignore errors
}
} else if(stat.isFile()) {
return potentialPath;
}
}
}
return null
}
// function decodeFilePath (filePath) {
// const match = filePath.match(/^{(.*)}\/(.*)$/)
// if (!match)
// {throw new Error('Failed to decode Sass path: ' + filePath)}
// if (match[1] === '') {
// // app
// return match[2]
// }
// return 'packages/' + match[1] + '/' + match[2]
// }
global.vue.lang.scss = Meteor.wrapAsync(function ({
source,
basePath,
inputFile,
dependencyManager,
}, cb) {
if (!source.trim()) {
cb(null, { css: '' })
return
}
sass.render({
data: source,
importer: resolveImport(dependencyManager),
outFile: inputFile.getPathInPackage() + '.css',
sourceMap: true,
sourceMapContents: true,
}, function (error, result) {
if (error) {
cb(error, null)
} else {
cb(null, {
css: result.css.toString(),
map: result.map.toString(),
})
}
})
})
global.vue.lang.sass = Meteor.wrapAsync(function ({
source,
basePath,
inputFile,
dependencyManager,
}, cb) {
if (!source.trim()) {
cb(null, { css: '' })
return
}
sass.render({
data: source,
importer: resolveImport(dependencyManager),
outFile: basePath + '.css',
sourceMap: true,
sourceMapContents: true,
indentedSyntax: true,
}, function (error, result) {
if (error) {
cb(error, null)
} else {
cb(null, {
css: result.css.toString(),
map: result.map.toString(),
})
}
})
})