balm-core 4.30.0 → 4.32.0

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.
Files changed (43) hide show
  1. package/README.md +1 -1
  2. package/lib/balm.js +2 -2
  3. package/lib/bundler/webpack/config/env.js +1 -1
  4. package/lib/config/globals.js +14 -1
  5. package/lib/plugins/imagemin.js +2 -2
  6. package/lib/plugins/index.js +2 -0
  7. package/lib/plugins/postcss.js +2 -2
  8. package/lib/plugins/replace.js +1 -1
  9. package/lib/plugins/sass-legacy.js +131 -0
  10. package/lib/plugins/sass.js +66 -125
  11. package/lib/tasks/foundation/balm.js +1 -1
  12. package/lib/tasks/foundation/balm_style.js +1 -1
  13. package/lib/tasks/private/build.js +1 -1
  14. package/lib/tasks/private/cache.js +1 -1
  15. package/lib/tasks/private/clean.js +3 -3
  16. package/lib/tasks/private/end.js +1 -1
  17. package/lib/tasks/private/extra.js +1 -1
  18. package/lib/tasks/private/font.js +1 -1
  19. package/lib/tasks/private/image.js +1 -1
  20. package/lib/tasks/private/lint.js +1 -1
  21. package/lib/tasks/private/media.js +1 -1
  22. package/lib/tasks/private/modernizr.js +3 -3
  23. package/lib/tasks/private/pwa-cache.js +1 -1
  24. package/lib/tasks/private/sprite.js +2 -2
  25. package/lib/tasks/private/start.js +1 -1
  26. package/lib/tasks/private/workbox-sw.js +1 -1
  27. package/lib/tasks/public/html.js +2 -2
  28. package/lib/tasks/public/publish.js +2 -2
  29. package/lib/tasks/public/remove.js +2 -2
  30. package/lib/tasks/public/sass.js +1 -2
  31. package/lib/tasks/public/server.js +2 -2
  32. package/lib/tasks/public/url.js +2 -2
  33. package/lib/utilities/compiling.js +2 -2
  34. package/lib/utilities/loading.js +2 -2
  35. package/lib/utilities/logger.js +2 -2
  36. package/package.json +44 -43
  37. package/tslib/config/globals.js +21 -1
  38. package/tslib/plugins/imagemin.js +1 -1
  39. package/tslib/plugins/index.js +2 -0
  40. package/tslib/plugins/sass-legacy.js +95 -0
  41. package/tslib/plugins/sass.js +62 -124
  42. package/tslib/tasks/foundation/balm_style.js +3 -1
  43. package/tslib/tasks/public/sass.js +1 -2
@@ -1,86 +1,61 @@
1
- import fs from 'node:fs';
2
- import { pathToFileURL } from 'node:url';
3
1
  import { Transform } from 'node:stream';
4
- import semver from 'semver';
5
2
  import * as sass from 'sass';
6
3
  import replaceExtension from 'replace-ext';
7
4
  import stripAnsi from 'strip-ansi';
8
5
  import applySourceMap from 'vinyl-sourcemaps-apply';
9
6
  import ansiColors from 'ansi-colors';
10
- process.env.BALM_CWD = process.env.INIT_CWD || process.cwd();
11
- const localSassModule = node.path.join(process.env.BALM_CWD, 'node_modules', 'sass');
12
- let isLegacy = false;
13
- try {
14
- const sassPkg = requireModule(node.path.join(localSassModule, 'package.json'));
15
- isLegacy = semver.lt(sassPkg.version, '1.40.0');
16
- }
17
- catch (_) { }
18
- const nodeLocalSassModule = node.path.join(localSassModule, 'sass.node.js');
19
- const defaultLocalSassModule = node.path.join(localSassModule, 'sass.default.js');
20
- const dartLocalSassModule = node.path.join(localSassModule, 'sass.dart.js');
21
- const sassModule = fs.existsSync(nodeLocalSassModule)
22
- ? requireModule(nodeLocalSassModule)
23
- : fs.existsSync(defaultLocalSassModule)
24
- ? requireModule(defaultLocalSassModule)
25
- : fs.existsSync(dartLocalSassModule)
26
- ? requireModule(dartLocalSassModule)
27
- : sass;
7
+ const sassModule = node.getSassModule(sass);
28
8
  const PLUGIN_NAME = 'sass';
29
9
  const transformObj = (transform) => new Transform({ transform, objectMode: true });
30
10
  function filePush(file, result, callback) {
31
- if (result.sourceMap) {
32
- const sassMap = result.sourceMap;
33
- sassMap.file = replaceExtension(file.relative, '.css');
34
- applySourceMap(file, sassMap);
35
- }
36
11
  file.contents = Buffer.from(result.css);
37
12
  file.path = replaceExtension(file.path, '.css');
38
- if (file.stat) {
39
- file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
40
- }
41
- callback(null, file);
42
- }
43
- function legacyFilePush(file, sassObject, callback) {
44
- if (sassObject.map) {
45
- const sassMap = JSON.parse(sassObject.map.toString());
46
- const sassMapFile = sassMap.file.replace(/^stdout$/, 'stdin');
47
- const sassFileSrc = file.relative;
48
- const sassFileSrcPath = node.path.dirname(sassFileSrc);
49
- if (sassFileSrcPath) {
50
- const sourceFileIndex = sassMap.sources.indexOf(sassMapFile);
51
- sassMap.sources = sassMap.sources.map((source, index) => index === sourceFileIndex
52
- ? source
53
- : node.path.join(sassFileSrcPath, source));
13
+ if (result.sourceMap) {
14
+ const proto = /^file:\/\/?/;
15
+ const leadingSlash = /^\//;
16
+ const sassMap = result.sourceMap;
17
+ const base = node.path.resolve(file.cwd, file.base);
18
+ if (!sassMap.file) {
19
+ sassMap.file = file.history[0]
20
+ .replace(base + node.path.sep, '')
21
+ .replace(proto, '');
54
22
  }
55
- sassMap.sources = sassMap.sources.filter((src) => src !== 'stdin' && src);
23
+ sassMap.sources = sassMap.sources.map((src) => {
24
+ const baseUri = base.replace(/\\/g, '/');
25
+ if (src.startsWith('data:')) {
26
+ return file.history[0]
27
+ .replace(/\\/g, '/')
28
+ .replace(`${baseUri}/`, '')
29
+ .replace(proto, '')
30
+ .replace(leadingSlash, '');
31
+ }
32
+ return src
33
+ .replace(proto, '')
34
+ .replace(`${baseUri}/`, '')
35
+ .replace(leadingSlash, '');
36
+ });
37
+ const sassFileSrc = file.relative;
56
38
  sassMap.file = replaceExtension(sassFileSrc, '.css');
39
+ if (file.sourceMap &&
40
+ file.sourceMap.sourcesContent &&
41
+ !sassMap.sourcesContent) {
42
+ sassMap.sourcesContent = file.sourceMap.sourcesContent;
43
+ }
57
44
  applySourceMap(file, sassMap);
58
45
  }
59
- file.contents = sassObject.css;
60
- file.path = replaceExtension(file.path, '.css');
61
46
  if (file.stat) {
62
47
  file.stat.atime = file.stat.mtime = file.stat.ctime = new Date();
63
48
  }
64
49
  callback(null, file);
65
50
  }
66
51
  function handleError(error, file, callback) {
67
- const relativePath = node.path.relative(process.cwd(), file.path);
68
- const message = `${ansiColors.underline(relativePath)}\n${error.message}`;
69
- error.message = stripAnsi(message);
70
- return callback(new PluginError(PLUGIN_NAME, error));
71
- }
72
- function legacyHandleError(error, file, callback) {
73
- const filePath = ((error.file === 'stdin' ? file.path : error.file) ||
74
- file.path);
52
+ const filePath = (error.file === 'stdin' ? file.path : error.file) || file.path;
75
53
  const relativePath = node.path.relative(process.cwd(), filePath);
76
- const message = `${ansiColors.underline(relativePath)}\n${error.formatted}`;
77
- error.messageFormatted = message;
78
- error.messageOriginal = error.message;
54
+ const message = `${ansiColors.underline(relativePath)}\n${error.message}`;
79
55
  error.message = stripAnsi(message);
80
- error.relativePath = relativePath;
81
56
  return callback(new PluginError(PLUGIN_NAME, error));
82
57
  }
83
- const gulpSass = (options) => transformObj((file, encoding, callback) => {
58
+ const gulpSass = (options, sync = false) => transformObj((file, encoding, callback) => {
84
59
  if (file.isNull()) {
85
60
  return callback(null, file);
86
61
  }
@@ -95,77 +70,40 @@ const gulpSass = (options) => transformObj((file, encoding, callback) => {
95
70
  return callback(null, file);
96
71
  }
97
72
  const opts = (options || {});
98
- if (isLegacy) {
99
- opts.data = file.contents.toString();
100
- opts.file = file.path;
101
- if (node.path.extname(file.path) === '.sass') {
102
- opts.indentedSyntax = true;
103
- }
104
- if (opts.includePaths) {
105
- if (typeof opts.includePaths === 'string') {
106
- opts.includePaths = [opts.includePaths];
107
- }
108
- }
109
- else {
110
- opts.includePaths = [];
111
- }
112
- opts.includePaths.unshift(node.path.dirname(file.path));
113
- if (file.sourceMap) {
114
- opts.sourceMap = file.path;
115
- opts.omitSourceMapUrl = true;
116
- opts.sourceMapContents = true;
117
- }
118
- if (!opts.importer) {
119
- const aliasKeys = Object.keys(BalmJS.config.scripts.alias).filter((key) => /^@[a-z0-9-]{1,213}/.test(key));
120
- if (aliasKeys.length) {
121
- const aliasKeyRegex = new RegExp(`^(${aliasKeys.join('|')})/`);
122
- opts.importer = [
123
- function (url) {
124
- let file = url;
125
- const result = url.match(aliasKeyRegex);
126
- const key = result ? result[1] : null;
127
- if (key) {
128
- file = url.replace(key, BalmJS.config.scripts.alias[key]);
129
- BalmJS.logger.debug(`${PLUGIN_NAME} alias`, `${key} -> ${url} => ${file}`);
130
- }
131
- return { file };
132
- }
133
- ];
134
- }
73
+ if (node.path.extname(file.path) === '.sass') {
74
+ opts.syntax = 'indented';
75
+ }
76
+ if (opts.loadPaths) {
77
+ if (typeof opts.loadPaths === 'string') {
78
+ opts.loadPaths = [opts.loadPaths];
135
79
  }
136
80
  }
137
81
  else {
138
- if (!options.importers) {
139
- const aliasKeys = Object.keys(BalmJS.config.scripts.alias).filter((key) => /^@[a-z0-9-]{1,213}/.test(key));
140
- if (aliasKeys.length) {
141
- const aliasKeyRegex = new RegExp(`^(${aliasKeys.join('|')})/`);
142
- options.importers = [
143
- {
144
- findFileUrl(url) {
145
- let file = url;
146
- const result = url.match(aliasKeyRegex);
147
- const key = result ? result[1] : null;
148
- if (key) {
149
- file = url.replace(key, BalmJS.config.scripts.alias[key]);
150
- BalmJS.logger.debug(`${PLUGIN_NAME} alias`, `${key} -> ${url} => ${file}`);
151
- }
152
- return pathToFileURL(file);
153
- }
154
- }
155
- ];
156
- }
157
- }
82
+ opts.loadPaths = [];
158
83
  }
159
- try {
160
- isLegacy
161
- ? legacyFilePush(file, sassModule.renderSync(opts), callback)
162
- : filePush(file, sassModule.compile(file.path, options), callback);
84
+ opts.loadPaths.unshift(node.path.dirname(file.path));
85
+ if (file.sourceMap) {
86
+ opts.sourceMap = true;
87
+ opts.sourceMapIncludeSources = true;
163
88
  }
164
- catch (error) {
165
- isLegacy
166
- ? legacyHandleError(error, file, callback)
167
- : handleError(error, file, callback);
89
+ const fileContents = file.contents.toString();
90
+ if (sync !== true) {
91
+ sassModule
92
+ .compileStringAsync(fileContents, opts)
93
+ .then((compileResult) => {
94
+ filePush(file, compileResult, callback);
95
+ })
96
+ .catch((error) => {
97
+ handleError(error, file, callback);
98
+ });
99
+ }
100
+ else {
101
+ try {
102
+ filePush(file, sassModule.compileString(fileContents, opts), callback);
103
+ }
104
+ catch (error) {
105
+ handleError(error, file, callback);
106
+ }
168
107
  }
169
108
  });
170
109
  export default gulpSass;
171
- export { isLegacy };
@@ -47,7 +47,9 @@ class BalmStyleTask extends BalmTask {
47
47
  }));
48
48
  switch (style) {
49
49
  case 'sass':
50
- stream = stream.pipe(BalmJS.plugins.sass(options));
50
+ stream = stream.pipe(node.isLegacySass
51
+ ? BalmJS.plugins.legacySass(options)
52
+ : BalmJS.plugins.sass(options));
51
53
  break;
52
54
  case 'less':
53
55
  stream = stream.pipe(BalmJS.plugins.less(options));
@@ -1,10 +1,9 @@
1
- import { isLegacy } from '../../plugins/sass.js';
2
1
  class SassTask extends BalmJS.BalmStyleTask {
3
2
  constructor() {
4
3
  super('sass');
5
4
  }
6
5
  get options() {
7
- return Object.assign(isLegacy
6
+ return Object.assign(node.isLegacySass
8
7
  ? {
9
8
  includePaths: BalmJS.file.stylePaths,
10
9
  outputStyle: 'expanded'