forked from stenciljs/sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
185 lines (148 loc) · 5.56 KB
/
util.ts
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as d from './declarations';
import * as path from 'path';
import { Importer } from 'sass';
export function usePlugin(fileName: string) {
if (typeof fileName === 'string') {
return /(\.scss|\.sass)$/i.test(fileName);
}
return true;
}
export function getRenderOptions(opts: d.PluginOptions, sourceText: string, fileName: string, context: d.PluginCtx) {
// create a copy of the original sass config so we don't change it
const renderOpts = Object.assign({}, opts);
// always set "data" from the source text
renderOpts.data = sourceText;
// activate indented syntax if the file extension is .sass
renderOpts.indentedSyntax = /(\.sass)$/i.test(fileName);
renderOpts.includePaths = Array.isArray(opts.includePaths) ? opts.includePaths.slice() : [];
// add the directory of the source file to includePaths
renderOpts.includePaths.push(path.dirname(fileName));
renderOpts.includePaths = renderOpts.includePaths.map(includePath => {
if (path.isAbsolute(includePath)) {
return includePath;
}
// if it's a relative path then resolve it with the project's root directory
return path.resolve(context.config.rootDir, includePath);
});
const injectGlobalPaths = Array.isArray(opts.injectGlobalPaths) ? opts.injectGlobalPaths.slice() : [];
if (injectGlobalPaths.length > 0) {
const baseInjectPath = context.config.configPath? path.dirname(context.config.configPath) : context.config.rootDir;
// automatically inject each of these paths into the source text
const injectText = injectGlobalPaths.map((injectGlobalPath) => {
const includesNamespace = Array.isArray(injectGlobalPath);
let importPath = includesNamespace ? injectGlobalPath[0] as string : injectGlobalPath as string;
if (!path.isAbsolute(importPath)) {
const injectedPath = path.join(baseInjectPath, importPath);
// convert any relative paths to absolute paths relative to the project root
if (context.sys && typeof context.sys.normalizePath === 'function') {
// context.sys.normalizePath added in stencil 1.11.0
importPath = context.sys.normalizePath(injectedPath);
} else {
// TODO, eventually remove normalizePath() from @stencil/sass
importPath = normalizePath(injectedPath);
}
}
const importTerminator = renderOpts.indentedSyntax ? '\n' : ';';
return `@use "${importPath}"${includesNamespace ? ` as ${injectGlobalPath[1]}` : ''}${importTerminator}`;
}).join('');
renderOpts.data = injectText + renderOpts.data;
}
// remove non-standard sass option
delete renderOpts.injectGlobalPaths;
// the "file" config option is not valid here
delete renderOpts.file;
if (context.sys && typeof context.sys.resolveModuleId === 'function') {
const importers: Importer[] = []
if (typeof renderOpts.importer === 'function') {
importers.push(renderOpts.importer);
} else if (Array.isArray(renderOpts.importer)) {
importers.push(...renderOpts.importer);
}
const importer: Importer = (url, _prev, done) => {
if (typeof url === 'string') {
if (url.startsWith('~')) {
try {
const m = getModuleId(url);
if (m.moduleId) {
context.sys.resolveModuleId({
moduleId: m.moduleId,
containingFile: m.filePath
}).then((resolved) => {
if (resolved.pkgDirPath) {
const resolvedPath = path.join(resolved.pkgDirPath, m.filePath);
done({
file: context.sys.normalizePath(resolvedPath)
});
} else {
done(null);
}
}).catch(err => {
done(err);
});
return;
}
} catch (e) {
done(e);
}
}
}
done(null);
};
importers.push(importer);
renderOpts.importer = importers;
}
return renderOpts;
}
export function createResultsId(fileName: string) {
// create what the new path is post transform (.css)
const pathParts = fileName.split('.');
pathParts[pathParts.length - 1] = 'css';
return pathParts.join('.');
}
export function normalizePath(str: string) {
// Convert Windows backslash paths to slash paths: foo\\bar ➔ foo/bar
// https://github.com/sindresorhus/slash MIT
// By Sindre Sorhus
if (typeof str !== 'string') {
throw new Error(`invalid path to normalize`);
}
str = str.trim();
if (EXTENDED_PATH_REGEX.test(str) || NON_ASCII_REGEX.test(str)) {
return str;
}
str = str.replace(SLASH_REGEX, '/');
// always remove the trailing /
// this makes our file cache look ups consistent
if (str.charAt(str.length - 1) === '/') {
const colonIndex = str.indexOf(':');
if (colonIndex > -1) {
if (colonIndex < str.length - 2) {
str = str.substring(0, str.length - 1);
}
} else if (str.length > 1) {
str = str.substring(0, str.length - 1);
}
}
return str;
}
export function getModuleId(orgImport: string) {
if (orgImport.startsWith('~')) {
orgImport = orgImport.substring(1);
}
const splt = orgImport.split('/');
const m = {
moduleId: null as string,
filePath: null as string,
};
if (orgImport.startsWith('@') && splt.length > 1) {
m.moduleId = splt.slice(0, 2).join('/');
m.filePath = splt.slice(2).join('/');
} else {
m.moduleId = splt[0];
m.filePath = splt.slice(1).join('/');
}
return m;
}
const EXTENDED_PATH_REGEX = /^\\\\\?\\/;
const NON_ASCII_REGEX = /[^\x00-\x80]+/;
const SLASH_REGEX = /\\/g;