@parcel/transformer-sass 2.12.0 → 2.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/SassTransformer.js +61 -160
- package/lib/legacy.js +172 -0
- package/lib/modern.js +188 -0
- package/package.json +5 -5
- package/src/SassTransformer.js +83 -152
- package/src/legacy.js +159 -0
- package/src/modern.js +190 -0
package/lib/SassTransformer.js
CHANGED
|
@@ -18,20 +18,6 @@ function _path() {
|
|
|
18
18
|
};
|
|
19
19
|
return data;
|
|
20
20
|
}
|
|
21
|
-
function _os() {
|
|
22
|
-
const data = require("os");
|
|
23
|
-
_os = function () {
|
|
24
|
-
return data;
|
|
25
|
-
};
|
|
26
|
-
return data;
|
|
27
|
-
}
|
|
28
|
-
function _sourceMap() {
|
|
29
|
-
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
30
|
-
_sourceMap = function () {
|
|
31
|
-
return data;
|
|
32
|
-
};
|
|
33
|
-
return data;
|
|
34
|
-
}
|
|
35
21
|
function _sass() {
|
|
36
22
|
const data = _interopRequireDefault(require("sass"));
|
|
37
23
|
_sass = function () {
|
|
@@ -39,16 +25,9 @@ function _sass() {
|
|
|
39
25
|
};
|
|
40
26
|
return data;
|
|
41
27
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return data;
|
|
46
|
-
};
|
|
47
|
-
return data;
|
|
48
|
-
}
|
|
49
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
50
|
-
// E.g: ~library/file.sass
|
|
51
|
-
const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
|
|
28
|
+
var _legacy = require("./legacy");
|
|
29
|
+
var _modern = require("./modern");
|
|
30
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
52
31
|
var _default = exports.default = new (_plugin().Transformer)({
|
|
53
32
|
async loadConfig({
|
|
54
33
|
config,
|
|
@@ -65,157 +44,79 @@ var _default = exports.default = new (_plugin().Transformer)({
|
|
|
65
44
|
if (typeof configResult === 'string') {
|
|
66
45
|
configResult = {};
|
|
67
46
|
}
|
|
47
|
+
let version = detectVersion(configResult);
|
|
48
|
+
if (version === 'legacy') {
|
|
49
|
+
// Resolve relative paths from config file
|
|
50
|
+
if (configFile && configResult.includePaths) {
|
|
51
|
+
configResult.includePaths = configResult.includePaths.map(p => _path().default.resolve(_path().default.dirname(configFile.filePath), p));
|
|
52
|
+
}
|
|
53
|
+
if (configResult.importer === undefined) {
|
|
54
|
+
configResult.importer = [];
|
|
55
|
+
} else if (!Array.isArray(configResult.importer)) {
|
|
56
|
+
configResult.importer = [configResult.importer];
|
|
57
|
+
}
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
configResult.
|
|
75
|
-
} else if (
|
|
76
|
-
|
|
77
|
-
|
|
59
|
+
// Always emit sourcemap
|
|
60
|
+
configResult.sourceMap = true;
|
|
61
|
+
// sources are created relative to the directory of outFile
|
|
62
|
+
configResult.outFile = _path().default.join(options.projectRoot, 'style.css.map');
|
|
63
|
+
configResult.omitSourceMapUrl = true;
|
|
64
|
+
configResult.sourceMapContents = false;
|
|
65
|
+
} else if (version === 'modern') {
|
|
66
|
+
// Resolve relative paths from config file
|
|
67
|
+
if (configFile && configResult.loadPaths) {
|
|
68
|
+
configResult.loadPaths = configResult.loadPaths.map(p => _path().default.resolve(_path().default.dirname(configFile.filePath), p));
|
|
69
|
+
}
|
|
78
70
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
71
|
+
// Always emit sourcemap
|
|
72
|
+
configResult.sourceMap = true;
|
|
73
|
+
}
|
|
74
|
+
return {
|
|
75
|
+
version,
|
|
76
|
+
config: configResult
|
|
77
|
+
};
|
|
86
78
|
},
|
|
87
79
|
async transform({
|
|
88
80
|
asset,
|
|
89
81
|
options,
|
|
90
|
-
config
|
|
82
|
+
config: {
|
|
83
|
+
version,
|
|
84
|
+
config
|
|
85
|
+
},
|
|
91
86
|
resolve
|
|
92
87
|
}) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
let code = await asset.getCode();
|
|
98
|
-
let result = await sassRender({
|
|
99
|
-
...rawConfig,
|
|
100
|
-
file: asset.filePath,
|
|
101
|
-
data: rawConfig.data ? rawConfig.data + _os().EOL + code : code,
|
|
102
|
-
importer: [...rawConfig.importer, resolvePathImporter({
|
|
103
|
-
asset,
|
|
104
|
-
resolve,
|
|
105
|
-
includePaths: rawConfig.includePaths,
|
|
106
|
-
options
|
|
107
|
-
})],
|
|
108
|
-
indentedSyntax: typeof rawConfig.indentedSyntax === 'boolean' ? rawConfig.indentedSyntax : asset.type === 'sass'
|
|
109
|
-
});
|
|
110
|
-
css = result.css;
|
|
111
|
-
for (let included of result.stats.includedFiles) {
|
|
112
|
-
if (included !== asset.filePath) {
|
|
113
|
-
asset.invalidateOnFileChange(included);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (result.map != null) {
|
|
117
|
-
let map = new (_sourceMap().default)(options.projectRoot);
|
|
118
|
-
map.addVLQMap(JSON.parse(result.map));
|
|
119
|
-
asset.setMap(map);
|
|
120
|
-
}
|
|
121
|
-
} catch (err) {
|
|
122
|
-
// Adapt the Error object for the reporter.
|
|
123
|
-
err.fileName = err.file;
|
|
124
|
-
err.loc = {
|
|
125
|
-
line: err.line,
|
|
126
|
-
column: err.column
|
|
127
|
-
};
|
|
128
|
-
throw err;
|
|
88
|
+
if (version === 'legacy') {
|
|
89
|
+
await (0, _legacy.transformLegacy)(asset, config, resolve, options);
|
|
90
|
+
} else {
|
|
91
|
+
await (0, _modern.transformModern)(asset, config, resolve, options);
|
|
129
92
|
}
|
|
130
|
-
asset.type = 'css';
|
|
131
|
-
asset.setCode(css);
|
|
132
93
|
return [asset];
|
|
133
94
|
}
|
|
134
95
|
});
|
|
135
|
-
function
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
// FS and tracks all tried files so they are watched for creation.
|
|
143
|
-
async function resolvePath(url, prev) {
|
|
144
|
-
/*
|
|
145
|
-
Imports are resolved by trying, in order:
|
|
146
|
-
* Loading a file relative to the file in which the `@import` appeared.
|
|
147
|
-
* Each custom importer.
|
|
148
|
-
* Loading a file relative to the current working directory (This rule doesn't really make sense for Parcel).
|
|
149
|
-
* Each load path in `includePaths`
|
|
150
|
-
* Each load path specified in the `SASS_PATH` environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.
|
|
151
|
-
See: https://sass-lang.com/documentation/js-api#importer
|
|
152
|
-
See also: https://github.com/sass/dart-sass/blob/006e6aa62f2417b5267ad5cdb5ba050226fab511/lib/src/importer/node/implementation.dart
|
|
153
|
-
*/
|
|
154
|
-
|
|
155
|
-
let paths = [_path().default.dirname(prev)];
|
|
156
|
-
if (includePaths) {
|
|
157
|
-
paths.push(...includePaths);
|
|
158
|
-
}
|
|
159
|
-
asset.invalidateOnEnvChange('SASS_PATH');
|
|
160
|
-
if (options.env.SASS_PATH) {
|
|
161
|
-
paths.push(...options.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':').map(p => _path().default.resolve(options.projectRoot, p)));
|
|
162
|
-
}
|
|
163
|
-
const urls = [url];
|
|
164
|
-
const urlFileName = _path().default.basename(url);
|
|
165
|
-
if (urlFileName[0] !== '_') {
|
|
166
|
-
urls.push(_path().default.join(_path().default.dirname(url), `_${urlFileName}`));
|
|
96
|
+
function detectVersion(config) {
|
|
97
|
+
if (!_sass().default.compileStringAsync) {
|
|
98
|
+
return 'legacy';
|
|
99
|
+
}
|
|
100
|
+
for (let legacyOption of ['data', 'indentType', 'indentWidth', 'linefeed', 'outputStyle', 'importer', 'pkgImporter', 'includePaths', 'omitSourceMapUrl', 'outFile', 'sourceMapContents', 'sourceMapEmbed', 'sourceMapRoot']) {
|
|
101
|
+
if (config[legacyOption] != null) {
|
|
102
|
+
return 'legacy';
|
|
167
103
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
try {
|
|
173
|
-
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
174
|
-
return {
|
|
175
|
-
filePath,
|
|
176
|
-
contents
|
|
177
|
-
};
|
|
178
|
-
} catch (err) {
|
|
179
|
-
asset.invalidateOnFileCreate({
|
|
180
|
-
filePath
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
}
|
|
104
|
+
}
|
|
105
|
+
for (let modernOption of ['loadPaths', 'sourceMapIncludeSources', 'style', 'importers']) {
|
|
106
|
+
if (config[modernOption] != null) {
|
|
107
|
+
return 'modern';
|
|
185
108
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
});
|
|
196
|
-
if (filePath) {
|
|
197
|
-
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
198
|
-
return {
|
|
199
|
-
filePath,
|
|
200
|
-
contents
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
} catch (err) {
|
|
204
|
-
continue;
|
|
109
|
+
}
|
|
110
|
+
if (typeof config.sourceMap === 'string') {
|
|
111
|
+
return 'legacy';
|
|
112
|
+
}
|
|
113
|
+
if (config.functions && typeof config.functions === 'object' && Object.keys(config.functions).length > 0) {
|
|
114
|
+
for (let key in config.functions) {
|
|
115
|
+
let fn = config.functions[key];
|
|
116
|
+
if (typeof fn === 'function' && fn.length > 1) {
|
|
117
|
+
return 'legacy';
|
|
205
118
|
}
|
|
206
119
|
}
|
|
207
120
|
}
|
|
208
|
-
return
|
|
209
|
-
const url = rawUrl.replace(/^file:\/\//, '');
|
|
210
|
-
resolvePath(url, prev).then(resolved => {
|
|
211
|
-
if (resolved) {
|
|
212
|
-
done({
|
|
213
|
-
file: resolved.filePath,
|
|
214
|
-
contents: resolved.contents
|
|
215
|
-
});
|
|
216
|
-
} else {
|
|
217
|
-
done();
|
|
218
|
-
}
|
|
219
|
-
}).catch(done);
|
|
220
|
-
};
|
|
121
|
+
return 'modern';
|
|
221
122
|
}
|
package/lib/legacy.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transformLegacy = transformLegacy;
|
|
7
|
+
function _path() {
|
|
8
|
+
const data = _interopRequireDefault(require("path"));
|
|
9
|
+
_path = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _os() {
|
|
15
|
+
const data = require("os");
|
|
16
|
+
_os = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _sourceMap() {
|
|
22
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
23
|
+
_sourceMap = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _sass() {
|
|
29
|
+
const data = _interopRequireDefault(require("sass"));
|
|
30
|
+
_sass = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _util() {
|
|
36
|
+
const data = require("util");
|
|
37
|
+
_util = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
43
|
+
// E.g: ~library/file.sass
|
|
44
|
+
const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
|
|
45
|
+
async function transformLegacy(asset, config, resolve, options) {
|
|
46
|
+
let rawConfig = config ?? {};
|
|
47
|
+
let sassRender = (0, _util().promisify)(_sass().default.render.bind(_sass().default));
|
|
48
|
+
let css;
|
|
49
|
+
try {
|
|
50
|
+
let code = await asset.getCode();
|
|
51
|
+
let result = await sassRender({
|
|
52
|
+
...rawConfig,
|
|
53
|
+
file: asset.filePath,
|
|
54
|
+
data: rawConfig.data ? rawConfig.data + _os().EOL + code : code,
|
|
55
|
+
importer: [...rawConfig.importer, resolvePathImporter({
|
|
56
|
+
asset,
|
|
57
|
+
resolve,
|
|
58
|
+
includePaths: rawConfig.includePaths,
|
|
59
|
+
options
|
|
60
|
+
})],
|
|
61
|
+
indentedSyntax: typeof rawConfig.indentedSyntax === 'boolean' ? rawConfig.indentedSyntax : asset.type === 'sass'
|
|
62
|
+
});
|
|
63
|
+
css = result.css;
|
|
64
|
+
for (let included of result.stats.includedFiles) {
|
|
65
|
+
if (included !== asset.filePath) {
|
|
66
|
+
asset.invalidateOnFileChange(included);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (result.map != null) {
|
|
70
|
+
let map = new (_sourceMap().default)(options.projectRoot);
|
|
71
|
+
map.addVLQMap(JSON.parse(result.map));
|
|
72
|
+
asset.setMap(map);
|
|
73
|
+
}
|
|
74
|
+
} catch (err) {
|
|
75
|
+
// Adapt the Error object for the reporter.
|
|
76
|
+
err.fileName = err.file;
|
|
77
|
+
err.loc = {
|
|
78
|
+
line: err.line,
|
|
79
|
+
column: err.column
|
|
80
|
+
};
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
asset.type = 'css';
|
|
84
|
+
asset.setCode(css);
|
|
85
|
+
}
|
|
86
|
+
function resolvePathImporter({
|
|
87
|
+
asset,
|
|
88
|
+
resolve,
|
|
89
|
+
includePaths,
|
|
90
|
+
options
|
|
91
|
+
}) {
|
|
92
|
+
// This is a reimplementation of the Sass resolution algorithm that uses Parcel's
|
|
93
|
+
// FS and tracks all tried files so they are watched for creation.
|
|
94
|
+
async function resolvePath(url, prev) {
|
|
95
|
+
/*
|
|
96
|
+
Imports are resolved by trying, in order:
|
|
97
|
+
* Loading a file relative to the file in which the `@import` appeared.
|
|
98
|
+
* Each custom importer.
|
|
99
|
+
* Loading a file relative to the current working directory (This rule doesn't really make sense for Parcel).
|
|
100
|
+
* Each load path in `includePaths`
|
|
101
|
+
* Each load path specified in the `SASS_PATH` environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.
|
|
102
|
+
See: https://sass-lang.com/documentation/js-api#importer
|
|
103
|
+
See also: https://github.com/sass/dart-sass/blob/006e6aa62f2417b5267ad5cdb5ba050226fab511/lib/src/importer/node/implementation.dart
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
let paths = [_path().default.dirname(prev)];
|
|
107
|
+
if (includePaths) {
|
|
108
|
+
paths.push(...includePaths);
|
|
109
|
+
}
|
|
110
|
+
asset.invalidateOnEnvChange('SASS_PATH');
|
|
111
|
+
if (options.env.SASS_PATH) {
|
|
112
|
+
paths.push(...options.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':').map(p => _path().default.resolve(options.projectRoot, p)));
|
|
113
|
+
}
|
|
114
|
+
const urls = [url];
|
|
115
|
+
const urlFileName = _path().default.basename(url);
|
|
116
|
+
if (urlFileName[0] !== '_') {
|
|
117
|
+
urls.push(_path().default.join(_path().default.dirname(url), `_${urlFileName}`));
|
|
118
|
+
}
|
|
119
|
+
if (url[0] !== '~') {
|
|
120
|
+
for (let p of paths) {
|
|
121
|
+
for (let u of urls) {
|
|
122
|
+
const filePath = _path().default.resolve(p, u);
|
|
123
|
+
try {
|
|
124
|
+
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
125
|
+
return {
|
|
126
|
+
filePath,
|
|
127
|
+
contents
|
|
128
|
+
};
|
|
129
|
+
} catch (err) {
|
|
130
|
+
asset.invalidateOnFileCreate({
|
|
131
|
+
filePath
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// If none of the default sass rules apply, try Parcel's resolver.
|
|
139
|
+
for (let u of urls) {
|
|
140
|
+
if (NODE_MODULE_ALIAS_RE.test(u)) {
|
|
141
|
+
u = u.slice(1);
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
const filePath = await resolve(prev, u, {
|
|
145
|
+
packageConditions: ['sass', 'style']
|
|
146
|
+
});
|
|
147
|
+
if (filePath) {
|
|
148
|
+
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
149
|
+
return {
|
|
150
|
+
filePath,
|
|
151
|
+
contents
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
} catch (err) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return function (rawUrl, prev, done) {
|
|
160
|
+
const url = rawUrl.replace(/^file:\/\//, '');
|
|
161
|
+
resolvePath(url, prev).then(resolved => {
|
|
162
|
+
if (resolved) {
|
|
163
|
+
done({
|
|
164
|
+
file: resolved.filePath,
|
|
165
|
+
contents: resolved.contents
|
|
166
|
+
});
|
|
167
|
+
} else {
|
|
168
|
+
done();
|
|
169
|
+
}
|
|
170
|
+
}).catch(done);
|
|
171
|
+
};
|
|
172
|
+
}
|
package/lib/modern.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.transformModern = transformModern;
|
|
7
|
+
function _path() {
|
|
8
|
+
const data = _interopRequireWildcard(require("path"));
|
|
9
|
+
_path = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _sourceMap() {
|
|
15
|
+
const data = _interopRequireDefault(require("@parcel/source-map"));
|
|
16
|
+
_sourceMap = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _sass() {
|
|
22
|
+
const data = _interopRequireDefault(require("sass"));
|
|
23
|
+
_sass = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _url() {
|
|
29
|
+
const data = require("url");
|
|
30
|
+
_url = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
36
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
37
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
38
|
+
// E.g: ~library/file.sass
|
|
39
|
+
const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
|
|
40
|
+
async function transformModern(asset, config, resolve, options) {
|
|
41
|
+
let rawConfig = config ?? {};
|
|
42
|
+
let css;
|
|
43
|
+
try {
|
|
44
|
+
let code = await asset.getCode();
|
|
45
|
+
let indentedSyntax = rawConfig.syntax === 'indented' || typeof rawConfig.indentedSyntax === 'boolean' ? rawConfig.indentedSyntax : undefined;
|
|
46
|
+
let result = await _sass().default.compileStringAsync(code, {
|
|
47
|
+
...rawConfig,
|
|
48
|
+
loadPaths: undefined,
|
|
49
|
+
url: (0, _url().pathToFileURL)(asset.filePath),
|
|
50
|
+
importers: [...(rawConfig.importers || []), resolvePathImporter({
|
|
51
|
+
asset,
|
|
52
|
+
resolve,
|
|
53
|
+
loadPaths: rawConfig.loadPaths,
|
|
54
|
+
indentedSyntax,
|
|
55
|
+
options
|
|
56
|
+
})],
|
|
57
|
+
syntax: (indentedSyntax != null ? indentedSyntax : asset.type === 'sass') ? 'indented' : 'scss',
|
|
58
|
+
sourceMap: !!asset.env.sourceMap
|
|
59
|
+
});
|
|
60
|
+
css = result.css;
|
|
61
|
+
for (let included of result.loadedUrls) {
|
|
62
|
+
let file = (0, _url().fileURLToPath)(included);
|
|
63
|
+
if (file !== asset.filePath) {
|
|
64
|
+
asset.invalidateOnFileChange(file);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (result.sourceMap != null) {
|
|
68
|
+
let map = new (_sourceMap().default)(options.projectRoot);
|
|
69
|
+
map.addVLQMap(result.sourceMap);
|
|
70
|
+
asset.setMap(map);
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
// Adapt the Error object for the reporter.
|
|
74
|
+
err.fileName = err.file;
|
|
75
|
+
err.loc = {
|
|
76
|
+
line: err.line,
|
|
77
|
+
column: err.column
|
|
78
|
+
};
|
|
79
|
+
throw err;
|
|
80
|
+
}
|
|
81
|
+
asset.type = 'css';
|
|
82
|
+
asset.setCode(css);
|
|
83
|
+
}
|
|
84
|
+
function resolvePathImporter({
|
|
85
|
+
asset,
|
|
86
|
+
resolve,
|
|
87
|
+
loadPaths,
|
|
88
|
+
indentedSyntax,
|
|
89
|
+
options
|
|
90
|
+
}) {
|
|
91
|
+
return {
|
|
92
|
+
// This is a reimplementation of the Sass resolution algorithm that uses Parcel's
|
|
93
|
+
// FS and tracks all tried files so they are watched for creation.
|
|
94
|
+
async canonicalize(url, {
|
|
95
|
+
containingUrl
|
|
96
|
+
}) {
|
|
97
|
+
/*
|
|
98
|
+
Imports are resolved by trying, in order:
|
|
99
|
+
* Loading a file relative to the file in which the `@import` appeared.
|
|
100
|
+
* Each custom importer.
|
|
101
|
+
* Loading a file relative to the current working directory (This rule doesn't really make sense for Parcel).
|
|
102
|
+
* Each load path in `includePaths`
|
|
103
|
+
* Each load path specified in the `SASS_PATH` environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.
|
|
104
|
+
See: https://sass-lang.com/documentation/js-api#importer
|
|
105
|
+
See also: https://github.com/sass/dart-sass/blob/006e6aa62f2417b5267ad5cdb5ba050226fab511/lib/src/importer/node/implementation.dart
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
let containingPath = containingUrl ? (0, _url().fileURLToPath)(containingUrl) : asset.filePath;
|
|
109
|
+
if (!containingUrl) {
|
|
110
|
+
// If containingUrl is not provided, then url should be an absolute file:/// URL.
|
|
111
|
+
let filePath = (0, _url().fileURLToPath)(url);
|
|
112
|
+
url = _path().default.relative(_path().default.dirname(containingPath), filePath);
|
|
113
|
+
}
|
|
114
|
+
let paths = [_path().default.dirname(containingPath)];
|
|
115
|
+
if (loadPaths) {
|
|
116
|
+
paths.push(...loadPaths);
|
|
117
|
+
}
|
|
118
|
+
asset.invalidateOnEnvChange('SASS_PATH');
|
|
119
|
+
if (options.env.SASS_PATH) {
|
|
120
|
+
paths.push(...options.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':').map(p => _path().default.resolve(options.projectRoot, p)));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The importer should look for stylesheets by adding the prefix _ to the URL's basename,
|
|
124
|
+
// and by adding the extensions .sass and .scss if the URL doesn't already have one of those extensions.
|
|
125
|
+
const urls = [url];
|
|
126
|
+
const urlFileName = _path().default.basename(url);
|
|
127
|
+
if (urlFileName[0] !== '_') {
|
|
128
|
+
urls.push(_path().default.posix.join(_path().default.dirname(url), `_${urlFileName}`));
|
|
129
|
+
}
|
|
130
|
+
let ext = _path().default.extname(urlFileName);
|
|
131
|
+
if (ext !== '.sass' && ext !== '.scss') {
|
|
132
|
+
for (let url of [...urls]) {
|
|
133
|
+
urls.push(url + '.sass');
|
|
134
|
+
urls.push(url + '.scss');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// If none of the possible paths is valid, the importer should perform the same resolution on the URL followed by /index.
|
|
139
|
+
urls.push(_path().default.posix.join(url, 'index.sass'));
|
|
140
|
+
urls.push(_path().default.posix.join(url, 'index.scss'));
|
|
141
|
+
urls.push(_path().default.posix.join(url, '_index.sass'));
|
|
142
|
+
urls.push(_path().default.posix.join(url, '_index.scss'));
|
|
143
|
+
if (url[0] !== '~') {
|
|
144
|
+
for (let p of paths) {
|
|
145
|
+
for (let u of urls) {
|
|
146
|
+
var _stat;
|
|
147
|
+
let filePath = _path().default.resolve(p, u);
|
|
148
|
+
let stat;
|
|
149
|
+
try {
|
|
150
|
+
stat = await asset.fs.stat(filePath);
|
|
151
|
+
} catch (err) {
|
|
152
|
+
// ignore.
|
|
153
|
+
}
|
|
154
|
+
if ((_stat = stat) !== null && _stat !== void 0 && _stat.isFile()) {
|
|
155
|
+
return (0, _url().pathToFileURL)(filePath);
|
|
156
|
+
}
|
|
157
|
+
asset.invalidateOnFileCreate({
|
|
158
|
+
filePath
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// If none of the default sass rules apply, try Parcel's resolver.
|
|
165
|
+
for (let u of urls) {
|
|
166
|
+
if (NODE_MODULE_ALIAS_RE.test(u)) {
|
|
167
|
+
u = u.slice(1);
|
|
168
|
+
}
|
|
169
|
+
try {
|
|
170
|
+
const filePath = await resolve(containingPath, u, {
|
|
171
|
+
packageConditions: ['sass', 'style']
|
|
172
|
+
});
|
|
173
|
+
return (0, _url().pathToFileURL)(filePath);
|
|
174
|
+
} catch (err) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
async load(url) {
|
|
180
|
+
let path = (0, _url().fileURLToPath)(url);
|
|
181
|
+
const contents = await asset.fs.readFile(path, 'utf8');
|
|
182
|
+
return {
|
|
183
|
+
contents,
|
|
184
|
+
syntax: (indentedSyntax != null ? indentedSyntax : (0, _path().extname)(path) === '.sass') ? 'indented' : 'scss'
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcel/transformer-sass",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
"main": "lib/SassTransformer.js",
|
|
17
17
|
"source": "src/SassTransformer.js",
|
|
18
18
|
"engines": {
|
|
19
|
-
"node": ">=
|
|
20
|
-
"parcel": "^2.
|
|
19
|
+
"node": ">= 16.0.0",
|
|
20
|
+
"parcel": "^2.13.1"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@parcel/plugin": "2.
|
|
23
|
+
"@parcel/plugin": "2.13.1",
|
|
24
24
|
"@parcel/source-map": "^2.1.1",
|
|
25
25
|
"sass": "^1.38.0"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "4a297f79db3eec74437f4c5133127c98bf303703"
|
|
28
28
|
}
|
package/src/SassTransformer.js
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
import {Transformer} from '@parcel/plugin';
|
|
3
3
|
import path from 'path';
|
|
4
|
-
import {EOL} from 'os';
|
|
5
|
-
import SourceMap from '@parcel/source-map';
|
|
6
4
|
import sass from 'sass';
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
// E.g: ~library/file.sass
|
|
10
|
-
const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
|
|
5
|
+
import {transformLegacy} from './legacy';
|
|
6
|
+
import {transformModern} from './modern';
|
|
11
7
|
|
|
12
8
|
export default (new Transformer({
|
|
13
9
|
async loadConfig({config, options}) {
|
|
@@ -27,171 +23,106 @@ export default (new Transformer({
|
|
|
27
23
|
configResult = {};
|
|
28
24
|
}
|
|
29
25
|
|
|
30
|
-
|
|
31
|
-
if (configFile && configResult.includePaths) {
|
|
32
|
-
configResult.includePaths = configResult.includePaths.map(p =>
|
|
33
|
-
path.resolve(path.dirname(configFile.filePath), p),
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
if (configResult.importer === undefined) {
|
|
38
|
-
configResult.importer = [];
|
|
39
|
-
} else if (!Array.isArray(configResult.importer)) {
|
|
40
|
-
configResult.importer = [configResult.importer];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Always emit sourcemap
|
|
44
|
-
configResult.sourceMap = true;
|
|
45
|
-
// sources are created relative to the directory of outFile
|
|
46
|
-
configResult.outFile = path.join(options.projectRoot, 'style.css.map');
|
|
47
|
-
configResult.omitSourceMapUrl = true;
|
|
48
|
-
configResult.sourceMapContents = false;
|
|
26
|
+
let version = detectVersion(configResult);
|
|
49
27
|
|
|
50
|
-
|
|
51
|
-
|
|
28
|
+
if (version === 'legacy') {
|
|
29
|
+
// Resolve relative paths from config file
|
|
30
|
+
if (configFile && configResult.includePaths) {
|
|
31
|
+
configResult.includePaths = configResult.includePaths.map(p =>
|
|
32
|
+
path.resolve(path.dirname(configFile.filePath), p),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
52
35
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
let code = await asset.getCode();
|
|
59
|
-
let result = await sassRender({
|
|
60
|
-
...rawConfig,
|
|
61
|
-
file: asset.filePath,
|
|
62
|
-
data: rawConfig.data ? rawConfig.data + EOL + code : code,
|
|
63
|
-
importer: [
|
|
64
|
-
...rawConfig.importer,
|
|
65
|
-
resolvePathImporter({
|
|
66
|
-
asset,
|
|
67
|
-
resolve,
|
|
68
|
-
includePaths: rawConfig.includePaths,
|
|
69
|
-
options,
|
|
70
|
-
}),
|
|
71
|
-
],
|
|
72
|
-
indentedSyntax:
|
|
73
|
-
typeof rawConfig.indentedSyntax === 'boolean'
|
|
74
|
-
? rawConfig.indentedSyntax
|
|
75
|
-
: asset.type === 'sass',
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
css = result.css;
|
|
79
|
-
for (let included of result.stats.includedFiles) {
|
|
80
|
-
if (included !== asset.filePath) {
|
|
81
|
-
asset.invalidateOnFileChange(included);
|
|
82
|
-
}
|
|
36
|
+
if (configResult.importer === undefined) {
|
|
37
|
+
configResult.importer = [];
|
|
38
|
+
} else if (!Array.isArray(configResult.importer)) {
|
|
39
|
+
configResult.importer = [configResult.importer];
|
|
83
40
|
}
|
|
84
41
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
42
|
+
// Always emit sourcemap
|
|
43
|
+
configResult.sourceMap = true;
|
|
44
|
+
// sources are created relative to the directory of outFile
|
|
45
|
+
configResult.outFile = path.join(options.projectRoot, 'style.css.map');
|
|
46
|
+
configResult.omitSourceMapUrl = true;
|
|
47
|
+
configResult.sourceMapContents = false;
|
|
48
|
+
} else if (version === 'modern') {
|
|
49
|
+
// Resolve relative paths from config file
|
|
50
|
+
if (configFile && configResult.loadPaths) {
|
|
51
|
+
configResult.loadPaths = configResult.loadPaths.map(p =>
|
|
52
|
+
path.resolve(path.dirname(configFile.filePath), p),
|
|
53
|
+
);
|
|
89
54
|
}
|
|
90
|
-
|
|
91
|
-
//
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
55
|
+
|
|
56
|
+
// Always emit sourcemap
|
|
57
|
+
configResult.sourceMap = true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {version, config: configResult};
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
async transform({asset, options, config: {version, config}, resolve}) {
|
|
64
|
+
if (version === 'legacy') {
|
|
65
|
+
await transformLegacy(asset, config, resolve, options);
|
|
66
|
+
} else {
|
|
67
|
+
await transformModern(asset, config, resolve, options);
|
|
99
68
|
}
|
|
100
69
|
|
|
101
|
-
asset.type = 'css';
|
|
102
|
-
asset.setCode(css);
|
|
103
70
|
return [asset];
|
|
104
71
|
},
|
|
105
72
|
}): Transformer);
|
|
106
73
|
|
|
107
|
-
function
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
url,
|
|
112
|
-
prev,
|
|
113
|
-
): Promise<{filePath: string, contents: string, ...} | void> {
|
|
114
|
-
/*
|
|
115
|
-
Imports are resolved by trying, in order:
|
|
116
|
-
* Loading a file relative to the file in which the `@import` appeared.
|
|
117
|
-
* Each custom importer.
|
|
118
|
-
* Loading a file relative to the current working directory (This rule doesn't really make sense for Parcel).
|
|
119
|
-
* Each load path in `includePaths`
|
|
120
|
-
* Each load path specified in the `SASS_PATH` environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.
|
|
121
|
-
|
|
122
|
-
See: https://sass-lang.com/documentation/js-api#importer
|
|
123
|
-
See also: https://github.com/sass/dart-sass/blob/006e6aa62f2417b5267ad5cdb5ba050226fab511/lib/src/importer/node/implementation.dart
|
|
124
|
-
*/
|
|
125
|
-
|
|
126
|
-
let paths = [path.dirname(prev)];
|
|
127
|
-
if (includePaths) {
|
|
128
|
-
paths.push(...includePaths);
|
|
129
|
-
}
|
|
74
|
+
function detectVersion(config: any) {
|
|
75
|
+
if (!sass.compileStringAsync) {
|
|
76
|
+
return 'legacy';
|
|
77
|
+
}
|
|
130
78
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
79
|
+
for (let legacyOption of [
|
|
80
|
+
'data',
|
|
81
|
+
'indentType',
|
|
82
|
+
'indentWidth',
|
|
83
|
+
'linefeed',
|
|
84
|
+
'outputStyle',
|
|
85
|
+
'importer',
|
|
86
|
+
'pkgImporter',
|
|
87
|
+
'includePaths',
|
|
88
|
+
'omitSourceMapUrl',
|
|
89
|
+
'outFile',
|
|
90
|
+
'sourceMapContents',
|
|
91
|
+
'sourceMapEmbed',
|
|
92
|
+
'sourceMapRoot',
|
|
93
|
+
]) {
|
|
94
|
+
if (config[legacyOption] != null) {
|
|
95
|
+
return 'legacy';
|
|
138
96
|
}
|
|
97
|
+
}
|
|
139
98
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
99
|
+
for (let modernOption of [
|
|
100
|
+
'loadPaths',
|
|
101
|
+
'sourceMapIncludeSources',
|
|
102
|
+
'style',
|
|
103
|
+
'importers',
|
|
104
|
+
]) {
|
|
105
|
+
if (config[modernOption] != null) {
|
|
106
|
+
return 'modern';
|
|
144
107
|
}
|
|
108
|
+
}
|
|
145
109
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const filePath = path.resolve(p, u);
|
|
150
|
-
try {
|
|
151
|
-
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
152
|
-
return {
|
|
153
|
-
filePath,
|
|
154
|
-
contents,
|
|
155
|
-
};
|
|
156
|
-
} catch (err) {
|
|
157
|
-
asset.invalidateOnFileCreate({filePath});
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
}
|
|
110
|
+
if (typeof config.sourceMap === 'string') {
|
|
111
|
+
return 'legacy';
|
|
112
|
+
}
|
|
162
113
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (filePath) {
|
|
173
|
-
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
174
|
-
return {filePath, contents};
|
|
175
|
-
}
|
|
176
|
-
} catch (err) {
|
|
177
|
-
continue;
|
|
114
|
+
if (
|
|
115
|
+
config.functions &&
|
|
116
|
+
typeof config.functions === 'object' &&
|
|
117
|
+
Object.keys(config.functions).length > 0
|
|
118
|
+
) {
|
|
119
|
+
for (let key in config.functions) {
|
|
120
|
+
let fn = config.functions[key];
|
|
121
|
+
if (typeof fn === 'function' && fn.length > 1) {
|
|
122
|
+
return 'legacy';
|
|
178
123
|
}
|
|
179
124
|
}
|
|
180
125
|
}
|
|
181
126
|
|
|
182
|
-
return
|
|
183
|
-
const url = rawUrl.replace(/^file:\/\//, '');
|
|
184
|
-
resolvePath(url, prev)
|
|
185
|
-
.then(resolved => {
|
|
186
|
-
if (resolved) {
|
|
187
|
-
done({
|
|
188
|
-
file: resolved.filePath,
|
|
189
|
-
contents: resolved.contents,
|
|
190
|
-
});
|
|
191
|
-
} else {
|
|
192
|
-
done();
|
|
193
|
-
}
|
|
194
|
-
})
|
|
195
|
-
.catch(done);
|
|
196
|
-
};
|
|
127
|
+
return 'modern';
|
|
197
128
|
}
|
package/src/legacy.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type {MutableAsset, ResolveFn, PluginOptions} from '@parcel/types';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import {EOL} from 'os';
|
|
5
|
+
import SourceMap from '@parcel/source-map';
|
|
6
|
+
import sass from 'sass';
|
|
7
|
+
import {promisify} from 'util';
|
|
8
|
+
|
|
9
|
+
// E.g: ~library/file.sass
|
|
10
|
+
const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
|
|
11
|
+
|
|
12
|
+
export async function transformLegacy(
|
|
13
|
+
asset: MutableAsset,
|
|
14
|
+
config: any,
|
|
15
|
+
resolve: ResolveFn,
|
|
16
|
+
options: PluginOptions,
|
|
17
|
+
) {
|
|
18
|
+
let rawConfig = config ?? {};
|
|
19
|
+
let sassRender = promisify(sass.render.bind(sass));
|
|
20
|
+
let css;
|
|
21
|
+
try {
|
|
22
|
+
let code = await asset.getCode();
|
|
23
|
+
let result = await sassRender({
|
|
24
|
+
...rawConfig,
|
|
25
|
+
file: asset.filePath,
|
|
26
|
+
data: rawConfig.data ? rawConfig.data + EOL + code : code,
|
|
27
|
+
importer: [
|
|
28
|
+
...rawConfig.importer,
|
|
29
|
+
resolvePathImporter({
|
|
30
|
+
asset,
|
|
31
|
+
resolve,
|
|
32
|
+
includePaths: rawConfig.includePaths,
|
|
33
|
+
options,
|
|
34
|
+
}),
|
|
35
|
+
],
|
|
36
|
+
indentedSyntax:
|
|
37
|
+
typeof rawConfig.indentedSyntax === 'boolean'
|
|
38
|
+
? rawConfig.indentedSyntax
|
|
39
|
+
: asset.type === 'sass',
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
css = result.css;
|
|
43
|
+
for (let included of result.stats.includedFiles) {
|
|
44
|
+
if (included !== asset.filePath) {
|
|
45
|
+
asset.invalidateOnFileChange(included);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (result.map != null) {
|
|
50
|
+
let map = new SourceMap(options.projectRoot);
|
|
51
|
+
map.addVLQMap(JSON.parse(result.map));
|
|
52
|
+
asset.setMap(map);
|
|
53
|
+
}
|
|
54
|
+
} catch (err) {
|
|
55
|
+
// Adapt the Error object for the reporter.
|
|
56
|
+
err.fileName = err.file;
|
|
57
|
+
err.loc = {
|
|
58
|
+
line: err.line,
|
|
59
|
+
column: err.column,
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
asset.type = 'css';
|
|
66
|
+
asset.setCode(css);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function resolvePathImporter({asset, resolve, includePaths, options}) {
|
|
70
|
+
// This is a reimplementation of the Sass resolution algorithm that uses Parcel's
|
|
71
|
+
// FS and tracks all tried files so they are watched for creation.
|
|
72
|
+
async function resolvePath(
|
|
73
|
+
url,
|
|
74
|
+
prev,
|
|
75
|
+
): Promise<{filePath: string, contents: string, ...} | void> {
|
|
76
|
+
/*
|
|
77
|
+
Imports are resolved by trying, in order:
|
|
78
|
+
* Loading a file relative to the file in which the `@import` appeared.
|
|
79
|
+
* Each custom importer.
|
|
80
|
+
* Loading a file relative to the current working directory (This rule doesn't really make sense for Parcel).
|
|
81
|
+
* Each load path in `includePaths`
|
|
82
|
+
* Each load path specified in the `SASS_PATH` environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.
|
|
83
|
+
|
|
84
|
+
See: https://sass-lang.com/documentation/js-api#importer
|
|
85
|
+
See also: https://github.com/sass/dart-sass/blob/006e6aa62f2417b5267ad5cdb5ba050226fab511/lib/src/importer/node/implementation.dart
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
let paths = [path.dirname(prev)];
|
|
89
|
+
if (includePaths) {
|
|
90
|
+
paths.push(...includePaths);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
asset.invalidateOnEnvChange('SASS_PATH');
|
|
94
|
+
if (options.env.SASS_PATH) {
|
|
95
|
+
paths.push(
|
|
96
|
+
...options.env.SASS_PATH.split(
|
|
97
|
+
process.platform === 'win32' ? ';' : ':',
|
|
98
|
+
).map(p => path.resolve(options.projectRoot, p)),
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const urls = [url];
|
|
103
|
+
const urlFileName = path.basename(url);
|
|
104
|
+
if (urlFileName[0] !== '_') {
|
|
105
|
+
urls.push(path.join(path.dirname(url), `_${urlFileName}`));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (url[0] !== '~') {
|
|
109
|
+
for (let p of paths) {
|
|
110
|
+
for (let u of urls) {
|
|
111
|
+
const filePath = path.resolve(p, u);
|
|
112
|
+
try {
|
|
113
|
+
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
114
|
+
return {
|
|
115
|
+
filePath,
|
|
116
|
+
contents,
|
|
117
|
+
};
|
|
118
|
+
} catch (err) {
|
|
119
|
+
asset.invalidateOnFileCreate({filePath});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// If none of the default sass rules apply, try Parcel's resolver.
|
|
126
|
+
for (let u of urls) {
|
|
127
|
+
if (NODE_MODULE_ALIAS_RE.test(u)) {
|
|
128
|
+
u = u.slice(1);
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const filePath = await resolve(prev, u, {
|
|
132
|
+
packageConditions: ['sass', 'style'],
|
|
133
|
+
});
|
|
134
|
+
if (filePath) {
|
|
135
|
+
const contents = await asset.fs.readFile(filePath, 'utf8');
|
|
136
|
+
return {filePath, contents};
|
|
137
|
+
}
|
|
138
|
+
} catch (err) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return function (rawUrl, prev, done) {
|
|
145
|
+
const url = rawUrl.replace(/^file:\/\//, '');
|
|
146
|
+
resolvePath(url, prev)
|
|
147
|
+
.then(resolved => {
|
|
148
|
+
if (resolved) {
|
|
149
|
+
done({
|
|
150
|
+
file: resolved.filePath,
|
|
151
|
+
contents: resolved.contents,
|
|
152
|
+
});
|
|
153
|
+
} else {
|
|
154
|
+
done();
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
.catch(done);
|
|
158
|
+
};
|
|
159
|
+
}
|
package/src/modern.js
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
import type {MutableAsset, ResolveFn, PluginOptions} from '@parcel/types';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import {extname} from 'path';
|
|
5
|
+
import SourceMap from '@parcel/source-map';
|
|
6
|
+
import sass from 'sass';
|
|
7
|
+
import {fileURLToPath, pathToFileURL} from 'url';
|
|
8
|
+
|
|
9
|
+
// E.g: ~library/file.sass
|
|
10
|
+
const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
|
|
11
|
+
|
|
12
|
+
export async function transformModern(
|
|
13
|
+
asset: MutableAsset,
|
|
14
|
+
config: any,
|
|
15
|
+
resolve: ResolveFn,
|
|
16
|
+
options: PluginOptions,
|
|
17
|
+
) {
|
|
18
|
+
let rawConfig = config ?? {};
|
|
19
|
+
let css;
|
|
20
|
+
try {
|
|
21
|
+
let code = await asset.getCode();
|
|
22
|
+
let indentedSyntax =
|
|
23
|
+
rawConfig.syntax === 'indented' ||
|
|
24
|
+
typeof rawConfig.indentedSyntax === 'boolean'
|
|
25
|
+
? rawConfig.indentedSyntax
|
|
26
|
+
: undefined;
|
|
27
|
+
let result = await sass.compileStringAsync(code, {
|
|
28
|
+
...rawConfig,
|
|
29
|
+
loadPaths: undefined,
|
|
30
|
+
url: pathToFileURL(asset.filePath),
|
|
31
|
+
importers: [
|
|
32
|
+
...(rawConfig.importers || []),
|
|
33
|
+
resolvePathImporter({
|
|
34
|
+
asset,
|
|
35
|
+
resolve,
|
|
36
|
+
loadPaths: rawConfig.loadPaths,
|
|
37
|
+
indentedSyntax,
|
|
38
|
+
options,
|
|
39
|
+
}),
|
|
40
|
+
],
|
|
41
|
+
syntax: (indentedSyntax != null ? indentedSyntax : asset.type === 'sass')
|
|
42
|
+
? 'indented'
|
|
43
|
+
: 'scss',
|
|
44
|
+
sourceMap: !!asset.env.sourceMap,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
css = result.css;
|
|
48
|
+
for (let included of result.loadedUrls) {
|
|
49
|
+
let file = fileURLToPath(included);
|
|
50
|
+
if (file !== asset.filePath) {
|
|
51
|
+
asset.invalidateOnFileChange(file);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (result.sourceMap != null) {
|
|
56
|
+
let map = new SourceMap(options.projectRoot);
|
|
57
|
+
map.addVLQMap(result.sourceMap);
|
|
58
|
+
asset.setMap(map);
|
|
59
|
+
}
|
|
60
|
+
} catch (err) {
|
|
61
|
+
// Adapt the Error object for the reporter.
|
|
62
|
+
err.fileName = err.file;
|
|
63
|
+
err.loc = {
|
|
64
|
+
line: err.line,
|
|
65
|
+
column: err.column,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
asset.type = 'css';
|
|
72
|
+
asset.setCode(css);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function resolvePathImporter({
|
|
76
|
+
asset,
|
|
77
|
+
resolve,
|
|
78
|
+
loadPaths,
|
|
79
|
+
indentedSyntax,
|
|
80
|
+
options,
|
|
81
|
+
}) {
|
|
82
|
+
return {
|
|
83
|
+
// This is a reimplementation of the Sass resolution algorithm that uses Parcel's
|
|
84
|
+
// FS and tracks all tried files so they are watched for creation.
|
|
85
|
+
async canonicalize(url, {containingUrl}) {
|
|
86
|
+
/*
|
|
87
|
+
Imports are resolved by trying, in order:
|
|
88
|
+
* Loading a file relative to the file in which the `@import` appeared.
|
|
89
|
+
* Each custom importer.
|
|
90
|
+
* Loading a file relative to the current working directory (This rule doesn't really make sense for Parcel).
|
|
91
|
+
* Each load path in `includePaths`
|
|
92
|
+
* Each load path specified in the `SASS_PATH` environment variable, which should be semicolon-separated on Windows and colon-separated elsewhere.
|
|
93
|
+
|
|
94
|
+
See: https://sass-lang.com/documentation/js-api#importer
|
|
95
|
+
See also: https://github.com/sass/dart-sass/blob/006e6aa62f2417b5267ad5cdb5ba050226fab511/lib/src/importer/node/implementation.dart
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
let containingPath = containingUrl
|
|
99
|
+
? fileURLToPath(containingUrl)
|
|
100
|
+
: asset.filePath;
|
|
101
|
+
if (!containingUrl) {
|
|
102
|
+
// If containingUrl is not provided, then url should be an absolute file:/// URL.
|
|
103
|
+
let filePath = fileURLToPath(url);
|
|
104
|
+
url = path.relative(path.dirname(containingPath), filePath);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
let paths = [path.dirname(containingPath)];
|
|
108
|
+
if (loadPaths) {
|
|
109
|
+
paths.push(...loadPaths);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
asset.invalidateOnEnvChange('SASS_PATH');
|
|
113
|
+
if (options.env.SASS_PATH) {
|
|
114
|
+
paths.push(
|
|
115
|
+
...options.env.SASS_PATH.split(
|
|
116
|
+
process.platform === 'win32' ? ';' : ':',
|
|
117
|
+
).map(p => path.resolve(options.projectRoot, p)),
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// The importer should look for stylesheets by adding the prefix _ to the URL's basename,
|
|
122
|
+
// and by adding the extensions .sass and .scss if the URL doesn't already have one of those extensions.
|
|
123
|
+
const urls = [url];
|
|
124
|
+
const urlFileName = path.basename(url);
|
|
125
|
+
if (urlFileName[0] !== '_') {
|
|
126
|
+
urls.push(path.posix.join(path.dirname(url), `_${urlFileName}`));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let ext = path.extname(urlFileName);
|
|
130
|
+
if (ext !== '.sass' && ext !== '.scss') {
|
|
131
|
+
for (let url of [...urls]) {
|
|
132
|
+
urls.push(url + '.sass');
|
|
133
|
+
urls.push(url + '.scss');
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// If none of the possible paths is valid, the importer should perform the same resolution on the URL followed by /index.
|
|
138
|
+
urls.push(path.posix.join(url, 'index.sass'));
|
|
139
|
+
urls.push(path.posix.join(url, 'index.scss'));
|
|
140
|
+
urls.push(path.posix.join(url, '_index.sass'));
|
|
141
|
+
urls.push(path.posix.join(url, '_index.scss'));
|
|
142
|
+
|
|
143
|
+
if (url[0] !== '~') {
|
|
144
|
+
for (let p of paths) {
|
|
145
|
+
for (let u of urls) {
|
|
146
|
+
let filePath = path.resolve(p, u);
|
|
147
|
+
let stat;
|
|
148
|
+
try {
|
|
149
|
+
stat = await asset.fs.stat(filePath);
|
|
150
|
+
} catch (err) {
|
|
151
|
+
// ignore.
|
|
152
|
+
}
|
|
153
|
+
if (stat?.isFile()) {
|
|
154
|
+
return pathToFileURL(filePath);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
asset.invalidateOnFileCreate({filePath});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// If none of the default sass rules apply, try Parcel's resolver.
|
|
163
|
+
for (let u of urls) {
|
|
164
|
+
if (NODE_MODULE_ALIAS_RE.test(u)) {
|
|
165
|
+
u = u.slice(1);
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
const filePath = await resolve(containingPath, u, {
|
|
169
|
+
packageConditions: ['sass', 'style'],
|
|
170
|
+
});
|
|
171
|
+
return pathToFileURL(filePath);
|
|
172
|
+
} catch (err) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
async load(url) {
|
|
178
|
+
let path = fileURLToPath(url);
|
|
179
|
+
const contents = await asset.fs.readFile(path, 'utf8');
|
|
180
|
+
return {
|
|
181
|
+
contents,
|
|
182
|
+
syntax: (
|
|
183
|
+
indentedSyntax != null ? indentedSyntax : extname(path) === '.sass'
|
|
184
|
+
)
|
|
185
|
+
? 'indented'
|
|
186
|
+
: 'scss',
|
|
187
|
+
};
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|