@parcel/transformer-sass 2.16.4 → 3.0.0-alpha.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/package.json CHANGED
@@ -1,28 +1,38 @@
1
1
  {
2
2
  "name": "@parcel/transformer-sass",
3
- "version": "2.16.4",
3
+ "description": "Compiles Sass and SCSS in Parcel v3",
4
+ "version": "3.0.0-alpha.1",
4
5
  "license": "MIT",
5
- "publishConfig": {
6
- "access": "public"
7
- },
8
- "funding": {
9
- "type": "opencollective",
10
- "url": "https://opencollective.com/parcel"
11
- },
12
6
  "repository": {
13
7
  "type": "git",
14
- "url": "https://github.com/parcel-bundler/parcel.git"
8
+ "url": "https://github.com/parcel-bundler/parcel.git",
9
+ "directory": "crates/sass"
15
10
  },
16
- "main": "lib/SassTransformer.js",
17
- "source": "src/SassTransformer.js",
11
+ "files": [],
18
12
  "engines": {
19
- "node": ">= 16.0.0",
20
- "parcel": "^2.16.4"
13
+ "parcel": "^3.0.0-0"
21
14
  },
22
- "dependencies": {
23
- "@parcel/plugin": "2.16.4",
24
- "@parcel/source-map": "^2.1.1",
25
- "sass": "^1.88.0"
15
+ "parcel": {
16
+ "abi": 1,
17
+ "artifacts": {
18
+ "aarch64-apple-darwin": "@parcel/transformer-sass-darwin-arm64",
19
+ "x86_64-apple-darwin": "@parcel/transformer-sass-darwin-x64",
20
+ "aarch64-pc-windows-msvc": "@parcel/transformer-sass-win32-arm64",
21
+ "x86_64-pc-windows-msvc": "@parcel/transformer-sass-win32-x64",
22
+ "x86_64-unknown-linux-gnu": "@parcel/transformer-sass-linux-x64",
23
+ "aarch64-unknown-linux-gnu": "@parcel/transformer-sass-linux-arm64",
24
+ "x86_64-unknown-linux-musl": "@parcel/transformer-sass-linux-x64-musl",
25
+ "aarch64-unknown-linux-musl": "@parcel/transformer-sass-linux-arm64-musl"
26
+ }
26
27
  },
27
- "gitHead": "59484858a1a0bcbb71f74088956bb437a2db6505"
28
+ "optionalDependencies": {
29
+ "@parcel/transformer-sass-darwin-arm64": "3.0.0-alpha.1",
30
+ "@parcel/transformer-sass-darwin-x64": "3.0.0-alpha.1",
31
+ "@parcel/transformer-sass-win32-arm64": "3.0.0-alpha.1",
32
+ "@parcel/transformer-sass-win32-x64": "3.0.0-alpha.1",
33
+ "@parcel/transformer-sass-linux-x64": "3.0.0-alpha.1",
34
+ "@parcel/transformer-sass-linux-arm64": "3.0.0-alpha.1",
35
+ "@parcel/transformer-sass-linux-x64-musl": "3.0.0-alpha.1",
36
+ "@parcel/transformer-sass-linux-arm64-musl": "3.0.0-alpha.1"
37
+ }
28
38
  }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2017-present Devon Govett
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,122 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- function _plugin() {
8
- const data = require("@parcel/plugin");
9
- _plugin = function () {
10
- return data;
11
- };
12
- return data;
13
- }
14
- function _path() {
15
- const data = _interopRequireDefault(require("path"));
16
- _path = 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
- var _legacy = require("./legacy");
29
- var _modern = require("./modern");
30
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
31
- var _default = exports.default = new (_plugin().Transformer)({
32
- async loadConfig({
33
- config,
34
- options
35
- }) {
36
- let configFile = await config.getConfig(['.sassrc', '.sassrc.json', '.sassrc.js', '.sassrc.cjs', '.sassrc.mjs'], {
37
- packageKey: 'sass'
38
- });
39
- let configResult = configFile ? configFile.contents : {};
40
-
41
- // Some packages in the wild declare a field `sass` in the package.json that
42
- // is a relative path to the sass entrypoint. In those cases we simply
43
- // initialize the config to an empty object.
44
- if (typeof configResult === 'string') {
45
- configResult = {};
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
- }
58
-
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
- }
70
-
71
- // Always emit sourcemap
72
- configResult.sourceMap = true;
73
- }
74
- return {
75
- version,
76
- config: configResult
77
- };
78
- },
79
- async transform({
80
- asset,
81
- options,
82
- config: {
83
- version,
84
- config
85
- },
86
- resolve
87
- }) {
88
- if (version === 'legacy') {
89
- await (0, _legacy.transformLegacy)(asset, config, resolve, options);
90
- } else {
91
- await (0, _modern.transformModern)(asset, config, resolve, options);
92
- }
93
- return [asset];
94
- }
95
- });
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';
103
- }
104
- }
105
- for (let modernOption of ['loadPaths', 'sourceMapIncludeSources', 'style', 'importers']) {
106
- if (config[modernOption] != null) {
107
- return 'modern';
108
- }
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';
118
- }
119
- }
120
- }
121
- return 'modern';
122
- }
package/lib/legacy.js DELETED
@@ -1,172 +0,0 @@
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 DELETED
@@ -1,188 +0,0 @@
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 && url.startsWith('file:')) {
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
- }
@@ -1,128 +0,0 @@
1
- // @flow
2
- import {Transformer} from '@parcel/plugin';
3
- import path from 'path';
4
- import sass from 'sass';
5
- import {transformLegacy} from './legacy';
6
- import {transformModern} from './modern';
7
-
8
- export default (new Transformer({
9
- async loadConfig({config, options}) {
10
- let configFile = await config.getConfig(
11
- ['.sassrc', '.sassrc.json', '.sassrc.js', '.sassrc.cjs', '.sassrc.mjs'],
12
- {
13
- packageKey: 'sass',
14
- },
15
- );
16
-
17
- let configResult = configFile ? configFile.contents : {};
18
-
19
- // Some packages in the wild declare a field `sass` in the package.json that
20
- // is a relative path to the sass entrypoint. In those cases we simply
21
- // initialize the config to an empty object.
22
- if (typeof configResult === 'string') {
23
- configResult = {};
24
- }
25
-
26
- let version = detectVersion(configResult);
27
-
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
- }
35
-
36
- if (configResult.importer === undefined) {
37
- configResult.importer = [];
38
- } else if (!Array.isArray(configResult.importer)) {
39
- configResult.importer = [configResult.importer];
40
- }
41
-
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
- );
54
- }
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);
68
- }
69
-
70
- return [asset];
71
- },
72
- }): Transformer);
73
-
74
- function detectVersion(config: any) {
75
- if (!sass.compileStringAsync) {
76
- return 'legacy';
77
- }
78
-
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';
96
- }
97
- }
98
-
99
- for (let modernOption of [
100
- 'loadPaths',
101
- 'sourceMapIncludeSources',
102
- 'style',
103
- 'importers',
104
- ]) {
105
- if (config[modernOption] != null) {
106
- return 'modern';
107
- }
108
- }
109
-
110
- if (typeof config.sourceMap === 'string') {
111
- return 'legacy';
112
- }
113
-
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';
123
- }
124
- }
125
- }
126
-
127
- return 'modern';
128
- }
package/src/legacy.js DELETED
@@ -1,159 +0,0 @@
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 DELETED
@@ -1,190 +0,0 @@
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 && url.startsWith('file:')) {
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
- }