@parcel/transformer-sass 2.0.0-beta.2 → 2.0.0-nightly.1004

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.
@@ -55,16 +55,6 @@ function _sass() {
55
55
  return data;
56
56
  }
57
57
 
58
- function _url() {
59
- const data = require("url");
60
-
61
- _url = function () {
62
- return data;
63
- };
64
-
65
- return data;
66
- }
67
-
68
58
  function _util() {
69
59
  const data = require("util");
70
60
 
@@ -78,14 +68,14 @@ function _util() {
78
68
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
79
69
 
80
70
  // E.g: ~library/file.sass
81
- const WEBPACK_ALIAS_RE = /^~[^/]/;
71
+ const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
82
72
 
83
73
  var _default = new (_plugin().Transformer)({
84
74
  async loadConfig({
85
75
  config,
86
76
  options
87
77
  }) {
88
- let configFile = await config.getConfig(['.sassrc', '.sassrc.js'], {
78
+ let configFile = await config.getConfig(['.sassrc', '.sassrc.json', '.sassrc.js'], {
89
79
  packageKey: 'sass'
90
80
  });
91
81
  let configResult = configFile ? configFile.contents : {}; // Resolve relative paths from config file
@@ -95,7 +85,7 @@ var _default = new (_plugin().Transformer)({
95
85
  }
96
86
 
97
87
  if (configFile && _path().default.extname(configFile.filePath) === '.js') {
98
- config.shouldInvalidateOnStartup();
88
+ config.invalidateOnStartup();
99
89
  }
100
90
 
101
91
  if (configResult.importer === undefined) {
@@ -110,7 +100,7 @@ var _default = new (_plugin().Transformer)({
110
100
  configResult.outFile = _path().default.join(options.projectRoot, 'style.css.map');
111
101
  configResult.omitSourceMapUrl = true;
112
102
  configResult.sourceMapContents = false;
113
- config.setResult(configResult);
103
+ return configResult;
114
104
  },
115
105
 
116
106
  async transform({
@@ -140,13 +130,13 @@ var _default = new (_plugin().Transformer)({
140
130
 
141
131
  for (let included of result.stats.includedFiles) {
142
132
  if (included !== asset.filePath) {
143
- asset.addIncludedFile(included);
133
+ asset.invalidateOnFileChange(included);
144
134
  }
145
135
  }
146
136
 
147
137
  if (result.map != null) {
148
138
  let map = new (_sourceMap().default)(options.projectRoot);
149
- map.addRawMappings(JSON.parse(result.map));
139
+ map.addVLQMap(JSON.parse(result.map));
150
140
  asset.setMap(map);
151
141
  }
152
142
  } catch (err) {
@@ -196,51 +186,70 @@ function resolvePathImporter({
196
186
  asset.invalidateOnEnvChange('SASS_PATH');
197
187
 
198
188
  if (options.env.SASS_PATH) {
199
- paths.push(...options.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':'));
189
+ paths.push(...options.env.SASS_PATH.split(process.platform === 'win32' ? ';' : ':').map(p => _path().default.resolve(options.projectRoot, p)));
200
190
  }
201
191
 
202
- let filePath;
203
- let contents;
192
+ const urls = [url];
193
+
194
+ const urlFileName = _path().default.basename(url);
195
+
196
+ if (urlFileName[0] !== '_') {
197
+ urls.push(_path().default.join(_path().default.dirname(url), `_${urlFileName}`));
198
+ }
204
199
 
205
200
  if (url[0] !== '~') {
206
201
  for (let p of paths) {
207
- filePath = _path().default.resolve(p, url);
208
-
209
- try {
210
- contents = await asset.fs.readFile(filePath, 'utf8');
211
- break;
212
- } catch (err) {
213
- asset.invalidateOnFileCreate({
214
- filePath
215
- });
202
+ for (let u of urls) {
203
+ const filePath = _path().default.resolve(p, u);
204
+
205
+ try {
206
+ const contents = await asset.fs.readFile(filePath, 'utf8');
207
+ return {
208
+ filePath,
209
+ contents
210
+ };
211
+ } catch (err) {
212
+ asset.invalidateOnFileCreate({
213
+ filePath
214
+ });
215
+ }
216
216
  }
217
217
  }
218
218
  } // If none of the default sass rules apply, try Parcel's resolver.
219
219
 
220
220
 
221
- if (!contents) {
222
- filePath = await resolve(prev, url);
223
- contents = await asset.fs.readFile(filePath, 'utf8');
224
- }
221
+ for (let u of urls) {
222
+ if (NODE_MODULE_ALIAS_RE.test(u)) {
223
+ u = u.slice(1);
224
+ }
225
225
 
226
- if (filePath) {
227
- return {
228
- file: (0, _url().pathToFileURL)(filePath).toString(),
229
- contents
230
- };
226
+ try {
227
+ const filePath = await resolve(prev, u);
228
+
229
+ if (filePath) {
230
+ const contents = await asset.fs.readFile(filePath, 'utf8');
231
+ return {
232
+ filePath,
233
+ contents
234
+ };
235
+ }
236
+ } catch (err) {
237
+ continue;
238
+ }
231
239
  }
232
240
  }
233
241
 
234
242
  return function (rawUrl, prev, done) {
235
- let url = rawUrl.replace(/^file:\/\//, '');
236
-
237
- if (WEBPACK_ALIAS_RE.test(url)) {
238
- const correctPath = url.replace(/^~/, '');
239
- const error = new Error(`The @import path "${url}" is using webpack specific syntax, which isn't supported by Parcel.\n\nTo @import files from node_modules, use "${correctPath}"`);
240
- done(error);
241
- return;
242
- }
243
-
244
- resolvePath(url, prev).then(done, done);
243
+ const url = rawUrl.replace(/^file:\/\//, '');
244
+ resolvePath(url, prev).then(resolved => {
245
+ if (resolved) {
246
+ done({
247
+ file: resolved.filePath,
248
+ contents: resolved.contents
249
+ });
250
+ } else {
251
+ done();
252
+ }
253
+ }).catch(done);
245
254
  };
246
255
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parcel/transformer-sass",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-nightly.1004+47379bf8",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,12 +17,12 @@
17
17
  "source": "src/SassTransformer.js",
18
18
  "engines": {
19
19
  "node": ">= 12.0.0",
20
- "parcel": "^2.0.0-beta.1"
20
+ "parcel": "2.0.0-nightly.1002+47379bf8"
21
21
  },
22
22
  "dependencies": {
23
- "@parcel/plugin": "2.0.0-beta.2",
24
- "@parcel/source-map": "2.0.0-alpha.4.21",
25
- "sass": "^1.32.4"
23
+ "@parcel/plugin": "2.0.0-nightly.1004+47379bf8",
24
+ "@parcel/source-map": "^2.0.0",
25
+ "sass": "^1.38.0"
26
26
  },
27
- "gitHead": "a3aa86815e098c90fda72a08d29646ff059ab74d"
27
+ "gitHead": "47379bf8fabeb2cfe03ade8802d942388b153e5b"
28
28
  }
@@ -4,17 +4,19 @@ import path from 'path';
4
4
  import {EOL} from 'os';
5
5
  import SourceMap from '@parcel/source-map';
6
6
  import sass from 'sass';
7
- import {pathToFileURL} from 'url';
8
7
  import {promisify} from 'util';
9
8
 
10
9
  // E.g: ~library/file.sass
11
- const WEBPACK_ALIAS_RE = /^~[^/]/;
10
+ const NODE_MODULE_ALIAS_RE = /^~[^/\\]/;
12
11
 
13
12
  export default (new Transformer({
14
13
  async loadConfig({config, options}) {
15
- let configFile = await config.getConfig(['.sassrc', '.sassrc.js'], {
16
- packageKey: 'sass',
17
- });
14
+ let configFile = await config.getConfig(
15
+ ['.sassrc', '.sassrc.json', '.sassrc.js'],
16
+ {
17
+ packageKey: 'sass',
18
+ },
19
+ );
18
20
 
19
21
  let configResult = configFile ? configFile.contents : {};
20
22
 
@@ -26,7 +28,7 @@ export default (new Transformer({
26
28
  }
27
29
 
28
30
  if (configFile && path.extname(configFile.filePath) === '.js') {
29
- config.shouldInvalidateOnStartup();
31
+ config.invalidateOnStartup();
30
32
  }
31
33
 
32
34
  if (configResult.importer === undefined) {
@@ -42,7 +44,7 @@ export default (new Transformer({
42
44
  configResult.omitSourceMapUrl = true;
43
45
  configResult.sourceMapContents = false;
44
46
 
45
- config.setResult(configResult);
47
+ return configResult;
46
48
  },
47
49
 
48
50
  async transform({asset, options, config, resolve}) {
@@ -73,13 +75,13 @@ export default (new Transformer({
73
75
  css = result.css;
74
76
  for (let included of result.stats.includedFiles) {
75
77
  if (included !== asset.filePath) {
76
- asset.addIncludedFile(included);
78
+ asset.invalidateOnFileChange(included);
77
79
  }
78
80
  }
79
81
 
80
82
  if (result.map != null) {
81
83
  let map = new SourceMap(options.projectRoot);
82
- map.addRawMappings(JSON.parse(result.map));
84
+ map.addVLQMap(JSON.parse(result.map));
83
85
  asset.setMap(map);
84
86
  }
85
87
  } catch (err) {
@@ -102,7 +104,10 @@ export default (new Transformer({
102
104
  function resolvePathImporter({asset, resolve, includePaths, options}) {
103
105
  // This is a reimplementation of the Sass resolution algorithm that uses Parcel's
104
106
  // FS and tracks all tried files so they are watched for creation.
105
- async function resolvePath(url, prev) {
107
+ async function resolvePath(
108
+ url,
109
+ prev,
110
+ ): Promise<{filePath: string, contents: string, ...} | void> {
106
111
  /*
107
112
  Imports are resolved by trying, in order:
108
113
  * Loading a file relative to the file in which the `@import` appeared.
@@ -125,51 +130,63 @@ function resolvePathImporter({asset, resolve, includePaths, options}) {
125
130
  paths.push(
126
131
  ...options.env.SASS_PATH.split(
127
132
  process.platform === 'win32' ? ';' : ':',
128
- ),
133
+ ).map(p => path.resolve(options.projectRoot, p)),
129
134
  );
130
135
  }
131
136
 
132
- let filePath;
133
- let contents;
137
+ const urls = [url];
138
+ const urlFileName = path.basename(url);
139
+ if (urlFileName[0] !== '_') {
140
+ urls.push(path.join(path.dirname(url), `_${urlFileName}`));
141
+ }
134
142
 
135
143
  if (url[0] !== '~') {
136
144
  for (let p of paths) {
137
- filePath = path.resolve(p, url);
138
- try {
139
- contents = await asset.fs.readFile(filePath, 'utf8');
140
- break;
141
- } catch (err) {
142
- asset.invalidateOnFileCreate({filePath});
145
+ for (let u of urls) {
146
+ const filePath = path.resolve(p, u);
147
+ try {
148
+ const contents = await asset.fs.readFile(filePath, 'utf8');
149
+ return {
150
+ filePath,
151
+ contents,
152
+ };
153
+ } catch (err) {
154
+ asset.invalidateOnFileCreate({filePath});
155
+ }
143
156
  }
144
157
  }
145
158
  }
146
159
 
147
160
  // If none of the default sass rules apply, try Parcel's resolver.
148
- if (!contents) {
149
- filePath = await resolve(prev, url);
150
- contents = await asset.fs.readFile(filePath, 'utf8');
151
- }
152
-
153
- if (filePath) {
154
- return {
155
- file: pathToFileURL(filePath).toString(),
156
- contents,
157
- };
161
+ for (let u of urls) {
162
+ if (NODE_MODULE_ALIAS_RE.test(u)) {
163
+ u = u.slice(1);
164
+ }
165
+ try {
166
+ const filePath = await resolve(prev, u);
167
+ if (filePath) {
168
+ const contents = await asset.fs.readFile(filePath, 'utf8');
169
+ return {filePath, contents};
170
+ }
171
+ } catch (err) {
172
+ continue;
173
+ }
158
174
  }
159
175
  }
160
176
 
161
- return function(rawUrl, prev, done) {
162
- let url = rawUrl.replace(/^file:\/\//, '');
163
-
164
- if (WEBPACK_ALIAS_RE.test(url)) {
165
- const correctPath = url.replace(/^~/, '');
166
- const error = new Error(
167
- `The @import path "${url}" is using webpack specific syntax, which isn't supported by Parcel.\n\nTo @import files from node_modules, use "${correctPath}"`,
168
- );
169
- done(error);
170
- return;
171
- }
172
-
173
- resolvePath(url, prev).then(done, done);
177
+ return function (rawUrl, prev, done) {
178
+ const url = rawUrl.replace(/^file:\/\//, '');
179
+ resolvePath(url, prev)
180
+ .then(resolved => {
181
+ if (resolved) {
182
+ done({
183
+ file: resolved.filePath,
184
+ contents: resolved.contents,
185
+ });
186
+ } else {
187
+ done();
188
+ }
189
+ })
190
+ .catch(done);
174
191
  };
175
192
  }