@dcloudio/uni-cli-shared 3.0.0-alpha-3041020220513001 → 3.0.0-alpha-3041020220516001

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.
@@ -40,6 +40,8 @@ export declare function cssPostPlugin(config: ResolvedConfig, { chunkCssFilename
40
40
  }): Plugin;
41
41
  export declare function formatPostcssSourceMap(rawMap: ExistingRawSourceMap, file: string): ExistingRawSourceMap;
42
42
  export declare const cssUrlRE: RegExp;
43
+ export declare const cssDataUriRE: RegExp;
44
+ export declare const importCssRE: RegExp;
43
45
  export declare function minifyCSS(css: string, config: ResolvedConfig): Promise<string>;
44
46
  export declare function hoistAtRules(css: string): Promise<string>;
45
47
  export interface StylePreprocessorResults {
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.hoistAtRules = exports.minifyCSS = exports.cssUrlRE = exports.formatPostcssSourceMap = exports.cssPostPlugin = exports.cssPlugin = exports.isDirectCSSRequest = exports.isCSSRequest = exports.commonjsProxyRE = exports.cssLangRE = void 0;
29
+ exports.hoistAtRules = exports.minifyCSS = exports.importCssRE = exports.cssDataUriRE = exports.cssUrlRE = exports.formatPostcssSourceMap = exports.cssPostPlugin = exports.cssPlugin = exports.isDirectCSSRequest = exports.isCSSRequest = exports.commonjsProxyRE = exports.cssLangRE = void 0;
30
30
  const fs_1 = __importDefault(require("fs"));
31
31
  const path_1 = __importDefault(require("path"));
32
32
  const fast_glob_1 = __importDefault(require("fast-glob"));
@@ -516,6 +516,8 @@ async function resolvePostcssConfig(config) {
516
516
  }
517
517
  // https://drafts.csswg.org/css-syntax-3/#identifier-code-point
518
518
  exports.cssUrlRE = /(?<=^|[^\w\-\u0080-\uffff])url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/;
519
+ exports.cssDataUriRE = /(?<=^|[^\w\-\u0080-\uffff])data-uri\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/;
520
+ exports.importCssRE = /@import ('[^']+\.css'|"[^"]+\.css"|[^'")]+\.css)/;
519
521
  const cssImageSetRE = /(?<=image-set\()((?:[\w\-]+\([^\)]*\)|[^)])*)(?=\))/;
520
522
  const UrlRewritePostcssPlugin = (opts) => {
521
523
  if (!opts) {
@@ -550,26 +552,44 @@ const UrlRewritePostcssPlugin = (opts) => {
550
552
  };
551
553
  UrlRewritePostcssPlugin.postcss = true;
552
554
  function rewriteCssUrls(css, replacer) {
553
- return (0, utils_1.asyncReplace)(css, cssImageSetRE, async (match) => {
555
+ return (0, utils_1.asyncReplace)(css, exports.cssUrlRE, async (match) => {
556
+ const [matched, rawUrl] = match;
557
+ return await doUrlReplace(rawUrl, matched, replacer);
558
+ });
559
+ }
560
+ function rewriteCssDataUris(css, replacer) {
561
+ return (0, utils_1.asyncReplace)(css, exports.cssDataUriRE, async (match) => {
562
+ const [matched, rawUrl] = match;
563
+ return await doUrlReplace(rawUrl, matched, replacer, 'data-uri');
564
+ });
565
+ }
566
+ function rewriteImportCss(css, replacer) {
567
+ return (0, utils_1.asyncReplace)(css, exports.importCssRE, async (match) => {
568
+ const [matched, rawUrl] = match;
569
+ return await doImportCSSReplace(rawUrl, matched, replacer);
570
+ });
571
+ }
572
+ // TODO: image and cross-fade could contain a "url" that needs to be processed
573
+ // https://drafts.csswg.org/css-images-4/#image-notation
574
+ // https://drafts.csswg.org/css-images-4/#cross-fade-function
575
+ const cssNotProcessedRE = /(gradient|element|cross-fade|image)\(/;
576
+ async function rewriteCssImageSet(css, replacer) {
577
+ return await (0, utils_1.asyncReplace)(css, cssImageSetRE, async (match) => {
554
578
  const [, rawUrl] = match;
555
579
  const url = await (0, utils_1.processSrcSet)(rawUrl, async ({ url }) => {
556
580
  // the url maybe url(...)
557
581
  if (exports.cssUrlRE.test(url)) {
558
582
  return await rewriteCssUrls(url, replacer);
559
583
  }
560
- return await doUrlReplace(url, url, replacer);
584
+ if (!cssNotProcessedRE.test(url)) {
585
+ return await doUrlReplace(url, url, replacer);
586
+ }
587
+ return url;
561
588
  });
562
589
  return url;
563
590
  });
564
591
  }
565
- function rewriteCssImageSet(css, replacer) {
566
- return (0, utils_1.asyncReplace)(css, cssImageSetRE, async (match) => {
567
- const [matched, rawUrl] = match;
568
- const url = await (0, utils_1.processSrcSet)(rawUrl, ({ url }) => doUrlReplace(url, matched, replacer));
569
- return `image-set(${url})`;
570
- });
571
- }
572
- async function doUrlReplace(rawUrl, matched, replacer) {
592
+ async function doUrlReplace(rawUrl, matched, replacer, funcName = 'url') {
573
593
  let wrap = '';
574
594
  const first = rawUrl[0];
575
595
  if (first === `"` || first === `'`) {
@@ -582,7 +602,24 @@ async function doUrlReplace(rawUrl, matched, replacer) {
582
602
  varRE.test(rawUrl)) {
583
603
  return matched;
584
604
  }
585
- return `url(${wrap}${await replacer(rawUrl)}${wrap})`;
605
+ const newUrl = await replacer(rawUrl);
606
+ if (wrap === '' && newUrl !== encodeURI(newUrl)) {
607
+ // The new url might need wrapping even if the original did not have it, e.g. if a space was added during replacement
608
+ wrap = "'";
609
+ }
610
+ return `${funcName}(${wrap}${newUrl}${wrap})`;
611
+ }
612
+ async function doImportCSSReplace(rawUrl, matched, replacer) {
613
+ let wrap = '';
614
+ const first = rawUrl[0];
615
+ if (first === `"` || first === `'`) {
616
+ wrap = first;
617
+ rawUrl = rawUrl.slice(1, -1);
618
+ }
619
+ if ((0, utils_1.isExternalUrl)(rawUrl) || (0, utils_1.isDataUrl)(rawUrl) || rawUrl.startsWith('#')) {
620
+ return matched;
621
+ }
622
+ return `@import ${wrap}${await replacer(rawUrl)}${wrap}`;
586
623
  }
587
624
  async function minifyCSS(css, config) {
588
625
  try {
@@ -734,8 +771,8 @@ function preprocessCss(content, isNVue = false) {
734
771
  */
735
772
  async function rebaseUrls(file, rootFile, alias, isNVue = false) {
736
773
  file = path_1.default.resolve(file); // ensure os-specific flashes
737
- // 条件编译
738
- const contents = preprocessCss(fs_1.default.readFileSync(file, 'utf-8'), isNVue);
774
+ // fixed by xxxxxx 条件编译
775
+ let contents = preprocessCss(fs_1.default.readFileSync(file, 'utf-8'), isNVue);
739
776
  // in the same dir, no need to rebase
740
777
  const fileDir = path_1.default.dirname(file);
741
778
  const rootDir = path_1.default.dirname(rootFile);
@@ -743,10 +780,16 @@ async function rebaseUrls(file, rootFile, alias, isNVue = false) {
743
780
  return { file, contents };
744
781
  }
745
782
  // no url()
746
- if (!exports.cssUrlRE.test(contents)) {
783
+ const hasUrls = exports.cssUrlRE.test(contents);
784
+ // data-uri() calls
785
+ const hasDataUris = exports.cssDataUriRE.test(contents);
786
+ // no @import xxx.css
787
+ const hasImportCss = exports.importCssRE.test(contents);
788
+ if (!hasUrls && !hasDataUris && !hasImportCss) {
747
789
  return { file, contents };
748
790
  }
749
- const rebased = await rewriteCssUrls(contents, (url) => {
791
+ let rebased;
792
+ const rebaseFn = (url) => {
750
793
  if (url.startsWith('/'))
751
794
  return url;
752
795
  // match alias, no need to rewrite
@@ -759,10 +802,20 @@ async function rebaseUrls(file, rootFile, alias, isNVue = false) {
759
802
  const absolute = path_1.default.resolve(fileDir, url);
760
803
  const relative = path_1.default.relative(rootDir, absolute);
761
804
  return (0, utils_1.normalizePath)(relative);
762
- });
805
+ };
806
+ // fix css imports in less such as `@import "foo.css"`
807
+ if (hasImportCss) {
808
+ contents = await rewriteImportCss(contents, rebaseFn);
809
+ }
810
+ if (hasUrls) {
811
+ contents = await rewriteCssUrls(rebased || contents, rebaseFn);
812
+ }
813
+ if (hasDataUris) {
814
+ contents = await rewriteCssDataUris(rebased || contents, rebaseFn);
815
+ }
763
816
  return {
764
817
  file,
765
- contents: rebased,
818
+ contents,
766
819
  };
767
820
  }
768
821
  // .less
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcloudio/uni-cli-shared",
3
- "version": "3.0.0-alpha-3041020220513001",
3
+ "version": "3.0.0-alpha-3041020220516001",
4
4
  "description": "@dcloudio/uni-cli-shared",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,8 +22,8 @@
22
22
  "@babel/core": "^7.17.9",
23
23
  "@babel/parser": "^7.17.9",
24
24
  "@babel/types": "^7.17.0",
25
- "@dcloudio/uni-i18n": "3.0.0-alpha-3041020220513001",
26
- "@dcloudio/uni-shared": "3.0.0-alpha-3041020220513001",
25
+ "@dcloudio/uni-i18n": "3.0.0-alpha-3041020220516001",
26
+ "@dcloudio/uni-shared": "3.0.0-alpha-3041020220516001",
27
27
  "@intlify/core-base": "9.1.9",
28
28
  "@intlify/shared": "9.1.9",
29
29
  "@intlify/vue-devtools": "9.1.9",