@dcloudio/uni-cli-shared 0.0.1-nvue3.3030820220125001
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/dist/checkUpdate.d.ts +30 -0
- package/dist/checkUpdate.js +243 -0
- package/dist/constants.d.ts +19 -0
- package/dist/constants.js +43 -0
- package/dist/deps.d.ts +19 -0
- package/dist/deps.js +39 -0
- package/dist/easycom.d.ts +29 -0
- package/dist/easycom.js +202 -0
- package/dist/env/define.d.ts +15 -0
- package/dist/env/define.js +45 -0
- package/dist/env/index.d.ts +2 -0
- package/dist/env/index.js +8 -0
- package/dist/env/provide.d.ts +9 -0
- package/dist/env/provide.js +24 -0
- package/dist/esbuild.d.ts +3 -0
- package/dist/esbuild.js +42 -0
- package/dist/exports.d.ts +1 -0
- package/dist/exports.js +8 -0
- package/dist/filter.d.ts +16 -0
- package/dist/filter.js +60 -0
- package/dist/fs.d.ts +1 -0
- package/dist/fs.js +25 -0
- package/dist/hbx/alias.d.ts +5 -0
- package/dist/hbx/alias.js +75 -0
- package/dist/hbx/env.d.ts +6 -0
- package/dist/hbx/env.js +44 -0
- package/dist/hbx/index.d.ts +3 -0
- package/dist/hbx/index.js +36 -0
- package/dist/hbx/log.d.ts +6 -0
- package/dist/hbx/log.js +99 -0
- package/dist/i18n.d.ts +10 -0
- package/dist/i18n.js +94 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +40 -0
- package/dist/json/app/index.d.ts +3 -0
- package/dist/json/app/index.js +17 -0
- package/dist/json/app/manifest/arguments.d.ts +2 -0
- package/dist/json/app/manifest/arguments.js +36 -0
- package/dist/json/app/manifest/confusion.d.ts +4 -0
- package/dist/json/app/manifest/confusion.js +77 -0
- package/dist/json/app/manifest/defaultManifestJson.d.ts +1 -0
- package/dist/json/app/manifest/defaultManifestJson.js +41 -0
- package/dist/json/app/manifest/env.d.ts +3 -0
- package/dist/json/app/manifest/env.js +24 -0
- package/dist/json/app/manifest/i18n.d.ts +1 -0
- package/dist/json/app/manifest/i18n.js +14 -0
- package/dist/json/app/manifest/index.d.ts +4 -0
- package/dist/json/app/manifest/index.js +49 -0
- package/dist/json/app/manifest/launchwebview.d.ts +1 -0
- package/dist/json/app/manifest/launchwebview.js +32 -0
- package/dist/json/app/manifest/merge.d.ts +1 -0
- package/dist/json/app/manifest/merge.js +17 -0
- package/dist/json/app/manifest/nvue.d.ts +4 -0
- package/dist/json/app/manifest/nvue.js +43 -0
- package/dist/json/app/manifest/plus.d.ts +1 -0
- package/dist/json/app/manifest/plus.js +83 -0
- package/dist/json/app/manifest/safearea.d.ts +1 -0
- package/dist/json/app/manifest/safearea.js +28 -0
- package/dist/json/app/manifest/splashscreen.d.ts +5 -0
- package/dist/json/app/manifest/splashscreen.js +44 -0
- package/dist/json/app/manifest/statusbar.d.ts +1 -0
- package/dist/json/app/manifest/statusbar.js +14 -0
- package/dist/json/app/manifest/tabBar.d.ts +1 -0
- package/dist/json/app/manifest/tabBar.js +27 -0
- package/dist/json/app/manifest/uniApp.d.ts +1 -0
- package/dist/json/app/manifest/uniApp.js +19 -0
- package/dist/json/app/pages/code.d.ts +3 -0
- package/dist/json/app/pages/code.js +44 -0
- package/dist/json/app/pages/definePage.d.ts +2 -0
- package/dist/json/app/pages/definePage.js +42 -0
- package/dist/json/app/pages/index.d.ts +3 -0
- package/dist/json/app/pages/index.js +28 -0
- package/dist/json/app/pages/uniConfig.d.ts +1 -0
- package/dist/json/app/pages/uniConfig.js +69 -0
- package/dist/json/app/pages/uniRoutes.d.ts +1 -0
- package/dist/json/app/pages/uniRoutes.js +8 -0
- package/dist/json/index.d.ts +5 -0
- package/dist/json/index.js +17 -0
- package/dist/json/json.d.ts +1 -0
- package/dist/json/json.js +9 -0
- package/dist/json/manifest.d.ts +28 -0
- package/dist/json/manifest.js +64 -0
- package/dist/json/mp/index.d.ts +4 -0
- package/dist/json/mp/index.js +19 -0
- package/dist/json/mp/jsonFile.d.ts +23 -0
- package/dist/json/mp/jsonFile.js +127 -0
- package/dist/json/mp/pages.d.ts +17 -0
- package/dist/json/mp/pages.js +126 -0
- package/dist/json/mp/project.d.ts +14 -0
- package/dist/json/mp/project.js +81 -0
- package/dist/json/mp/types.d.ts +118 -0
- package/dist/json/mp/types.js +2 -0
- package/dist/json/mp/utils.d.ts +3 -0
- package/dist/json/mp/utils.js +66 -0
- package/dist/json/pages.d.ts +30 -0
- package/dist/json/pages.js +383 -0
- package/dist/logs/console.d.ts +4 -0
- package/dist/logs/console.js +49 -0
- package/dist/logs/format.d.ts +11 -0
- package/dist/logs/format.js +82 -0
- package/dist/logs/index.d.ts +1 -0
- package/dist/logs/index.js +7 -0
- package/dist/messages/index.d.ts +20 -0
- package/dist/messages/index.js +23 -0
- package/dist/mp/ast.d.ts +4 -0
- package/dist/mp/ast.js +12 -0
- package/dist/mp/constants.d.ts +5 -0
- package/dist/mp/constants.js +8 -0
- package/dist/mp/event.d.ts +5 -0
- package/dist/mp/event.js +35 -0
- package/dist/mp/externalClasses.d.ts +5 -0
- package/dist/mp/externalClasses.js +42 -0
- package/dist/mp/imports.d.ts +12 -0
- package/dist/mp/imports.js +72 -0
- package/dist/mp/index.d.ts +11 -0
- package/dist/mp/index.js +38 -0
- package/dist/mp/nvue.d.ts +1 -0
- package/dist/mp/nvue.js +18 -0
- package/dist/mp/plugin.d.ts +2 -0
- package/dist/mp/plugin.js +13 -0
- package/dist/mp/style.d.ts +1 -0
- package/dist/mp/style.js +9 -0
- package/dist/mp/tags.d.ts +1 -0
- package/dist/mp/tags.js +117 -0
- package/dist/mp/template.d.ts +73 -0
- package/dist/mp/template.js +73 -0
- package/dist/mp/usingComponents.d.ts +65 -0
- package/dist/mp/usingComponents.js +402 -0
- package/dist/mp/wxs.d.ts +2 -0
- package/dist/mp/wxs.js +55 -0
- package/dist/platform.d.ts +2 -0
- package/dist/platform.js +31 -0
- package/dist/postcss/index.d.ts +10 -0
- package/dist/postcss/index.js +19 -0
- package/dist/postcss/plugins/stylePluginScoped.d.ts +3 -0
- package/dist/postcss/plugins/stylePluginScoped.js +176 -0
- package/dist/postcss/plugins/uniapp.d.ts +11 -0
- package/dist/postcss/plugins/uniapp.js +110 -0
- package/dist/preprocess/context.d.ts +3 -0
- package/dist/preprocess/context.js +81 -0
- package/dist/preprocess/index.d.ts +9 -0
- package/dist/preprocess/index.js +28 -0
- package/dist/resolve.d.ts +6 -0
- package/dist/resolve.js +102 -0
- package/dist/scripts.d.ts +10 -0
- package/dist/scripts.js +44 -0
- package/dist/ssr.d.ts +4 -0
- package/dist/ssr.js +20 -0
- package/dist/url.d.ts +2 -0
- package/dist/url.js +15 -0
- package/dist/utils.d.ts +13 -0
- package/dist/utils.js +80 -0
- package/dist/vite/features.d.ts +41 -0
- package/dist/vite/features.js +191 -0
- package/dist/vite/index.d.ts +36 -0
- package/dist/vite/index.js +17 -0
- package/dist/vite/plugins/console.d.ts +8 -0
- package/dist/vite/plugins/console.js +42 -0
- package/dist/vite/plugins/copy.d.ts +11 -0
- package/dist/vite/plugins/copy.js +51 -0
- package/dist/vite/plugins/cssScoped.d.ts +6 -0
- package/dist/vite/plugins/cssScoped.js +54 -0
- package/dist/vite/plugins/dynamicImportPolyfill.d.ts +2 -0
- package/dist/vite/plugins/dynamicImportPolyfill.js +15 -0
- package/dist/vite/plugins/index.d.ts +10 -0
- package/dist/vite/plugins/index.js +32 -0
- package/dist/vite/plugins/inject.d.ts +12 -0
- package/dist/vite/plugins/inject.js +207 -0
- package/dist/vite/plugins/jsonJs.d.ts +3 -0
- package/dist/vite/plugins/jsonJs.js +52 -0
- package/dist/vite/plugins/mainJs.d.ts +2 -0
- package/dist/vite/plugins/mainJs.js +29 -0
- package/dist/vite/plugins/vitejs/config.d.ts +1 -0
- package/dist/vite/plugins/vitejs/config.js +2 -0
- package/dist/vite/plugins/vitejs/constants.d.ts +8 -0
- package/dist/vite/plugins/vitejs/constants.js +11 -0
- package/dist/vite/plugins/vitejs/index.d.ts +1 -0
- package/dist/vite/plugins/vitejs/index.js +2 -0
- package/dist/vite/plugins/vitejs/plugin.d.ts +1 -0
- package/dist/vite/plugins/vitejs/plugin.js +2 -0
- package/dist/vite/plugins/vitejs/plugins/asset.d.ts +40 -0
- package/dist/vite/plugins/vitejs/plugins/asset.js +270 -0
- package/dist/vite/plugins/vitejs/plugins/css.d.ts +48 -0
- package/dist/vite/plugins/vitejs/plugins/css.js +760 -0
- package/dist/vite/plugins/vitejs/server/moduleGraph.d.ts +1 -0
- package/dist/vite/plugins/vitejs/server/moduleGraph.js +2 -0
- package/dist/vite/plugins/vitejs/utils.d.ts +29 -0
- package/dist/vite/plugins/vitejs/utils.js +129 -0
- package/dist/vite/utils/ast.d.ts +17 -0
- package/dist/vite/utils/ast.js +85 -0
- package/dist/vite/utils/index.d.ts +7 -0
- package/dist/vite/utils/index.js +23 -0
- package/dist/vite/utils/plugin.d.ts +11 -0
- package/dist/vite/utils/plugin.js +45 -0
- package/dist/vite/utils/url.d.ts +28 -0
- package/dist/vite/utils/url.js +69 -0
- package/dist/vite/utils/utils.d.ts +3 -0
- package/dist/vite/utils/utils.js +14 -0
- package/dist/vue/babel.d.ts +3 -0
- package/dist/vue/babel.js +17 -0
- package/dist/vue/index.d.ts +4 -0
- package/dist/vue/index.js +19 -0
- package/dist/vue/transforms/index.d.ts +13 -0
- package/dist/vue/transforms/index.js +48 -0
- package/dist/vue/transforms/templateTransformAssetUrl.d.ts +33 -0
- package/dist/vue/transforms/templateTransformAssetUrl.js +150 -0
- package/dist/vue/transforms/templateTransformSrcset.d.ts +4 -0
- package/dist/vue/transforms/templateTransformSrcset.js +122 -0
- package/dist/vue/transforms/templateUtils.d.ts +9 -0
- package/dist/vue/transforms/templateUtils.js +41 -0
- package/dist/vue/transforms/transformComponent.d.ts +3 -0
- package/dist/vue/transforms/transformComponent.js +26 -0
- package/dist/vue/transforms/transformEvent.d.ts +2 -0
- package/dist/vue/transforms/transformEvent.js +28 -0
- package/dist/vue/transforms/transformPageHead.d.ts +2 -0
- package/dist/vue/transforms/transformPageHead.js +11 -0
- package/dist/vue/transforms/transformRef.d.ts +2 -0
- package/dist/vue/transforms/transformRef.js +32 -0
- package/dist/vue/transforms/transformTag.d.ts +2 -0
- package/dist/vue/transforms/transformTag.js +18 -0
- package/dist/vue/transforms/vModel.d.ts +12 -0
- package/dist/vue/transforms/vModel.js +33 -0
- package/dist/vue/transforms/vOn.d.ts +15 -0
- package/dist/vue/transforms/vOn.js +88 -0
- package/dist/vue/utils.d.ts +31 -0
- package/dist/vue/utils.js +120 -0
- package/dist/watcher.d.ts +30 -0
- package/dist/watcher.js +92 -0
- package/lib/crypto.js +65 -0
- package/lib/nvue.css +34 -0
- package/lib/preprocess/LICENSE +13 -0
- package/lib/preprocess/README.md +410 -0
- package/lib/preprocess/lib/preprocess.js +437 -0
- package/lib/preprocess/lib/regexrules.js +121 -0
- package/lib/preprocess/package.json +68 -0
- package/package.json +68 -0
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
22
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
|
+
};
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
+
exports.minifyCSS = exports.cssUrlRE = exports.cssPostPlugin = exports.cssPlugin = exports.isDirectCSSRequest = exports.isCSSRequest = exports.commonjsProxyRE = exports.cssLangRE = void 0;
|
|
26
|
+
const fs_1 = __importDefault(require("fs"));
|
|
27
|
+
const path_1 = __importDefault(require("path"));
|
|
28
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
29
|
+
const picocolors_1 = __importDefault(require("picocolors"));
|
|
30
|
+
const postcss_load_config_1 = __importDefault(require("postcss-load-config"));
|
|
31
|
+
const pluginutils_1 = require("@rollup/pluginutils");
|
|
32
|
+
const utils_1 = require("../utils");
|
|
33
|
+
const asset_1 = require("./asset");
|
|
34
|
+
const esbuild_1 = require("esbuild");
|
|
35
|
+
const preprocess_1 = require("../../../../preprocess");
|
|
36
|
+
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`;
|
|
37
|
+
exports.cssLangRE = new RegExp(cssLangs);
|
|
38
|
+
const cssModuleRE = new RegExp(`\\.module${cssLangs}`);
|
|
39
|
+
const directRequestRE = /(\?|&)direct\b/;
|
|
40
|
+
exports.commonjsProxyRE = /\?commonjs-proxy/;
|
|
41
|
+
const isCSSRequest = (request) => exports.cssLangRE.test(request) && !directRequestRE.test(request);
|
|
42
|
+
exports.isCSSRequest = isCSSRequest;
|
|
43
|
+
const isDirectCSSRequest = (request) => exports.cssLangRE.test(request) && directRequestRE.test(request);
|
|
44
|
+
exports.isDirectCSSRequest = isDirectCSSRequest;
|
|
45
|
+
const cssModulesCache = new WeakMap();
|
|
46
|
+
const postcssConfigCache = new WeakMap();
|
|
47
|
+
/**
|
|
48
|
+
* Plugin applied before user plugins
|
|
49
|
+
*/
|
|
50
|
+
function cssPlugin(config) {
|
|
51
|
+
let server;
|
|
52
|
+
let moduleCache;
|
|
53
|
+
const resolveUrl = config.createResolver({
|
|
54
|
+
preferRelative: true,
|
|
55
|
+
tryIndex: false,
|
|
56
|
+
extensions: [],
|
|
57
|
+
});
|
|
58
|
+
const atImportResolvers = createCSSResolvers(config);
|
|
59
|
+
return {
|
|
60
|
+
name: 'vite:css',
|
|
61
|
+
configureServer(_server) {
|
|
62
|
+
server = _server;
|
|
63
|
+
},
|
|
64
|
+
buildStart() {
|
|
65
|
+
// Ensure a new cache for every build (i.e. rebuilding in watch mode)
|
|
66
|
+
moduleCache = new Map();
|
|
67
|
+
cssModulesCache.set(config, moduleCache);
|
|
68
|
+
},
|
|
69
|
+
async transform(raw, id) {
|
|
70
|
+
if (!exports.cssLangRE.test(id) || exports.commonjsProxyRE.test(id)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const urlReplacer = async (url, importer) => {
|
|
74
|
+
if (url.startsWith('/') && !url.startsWith('//')) {
|
|
75
|
+
// /static/logo.png => @/static/logo.png
|
|
76
|
+
url = '@' + url;
|
|
77
|
+
}
|
|
78
|
+
const resolved = await resolveUrl(url, importer);
|
|
79
|
+
if (resolved) {
|
|
80
|
+
return (0, asset_1.fileToUrl)(resolved, config, this, true);
|
|
81
|
+
}
|
|
82
|
+
return url;
|
|
83
|
+
};
|
|
84
|
+
const { code: css, modules, deps, } = await compileCSS(id, raw, config, urlReplacer, atImportResolvers, server);
|
|
85
|
+
if (modules) {
|
|
86
|
+
moduleCache.set(id, modules);
|
|
87
|
+
}
|
|
88
|
+
// track deps for build watch mode
|
|
89
|
+
if (config.command === 'build' && config.build.watch && deps) {
|
|
90
|
+
for (const file of deps) {
|
|
91
|
+
this.addWatchFile(file);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
code: css,
|
|
96
|
+
// TODO CSS source map
|
|
97
|
+
map: { mappings: '' },
|
|
98
|
+
};
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
exports.cssPlugin = cssPlugin;
|
|
103
|
+
function findCssModuleIds(moduleId, cssModuleIds, seen) {
|
|
104
|
+
if (!cssModuleIds) {
|
|
105
|
+
cssModuleIds = new Set();
|
|
106
|
+
}
|
|
107
|
+
if (!seen) {
|
|
108
|
+
seen = new Set();
|
|
109
|
+
}
|
|
110
|
+
if (seen.has(moduleId)) {
|
|
111
|
+
return cssModuleIds;
|
|
112
|
+
}
|
|
113
|
+
seen.add(moduleId);
|
|
114
|
+
const moduleInfo = this.getModuleInfo(moduleId);
|
|
115
|
+
if (moduleInfo) {
|
|
116
|
+
moduleInfo.importedIds.forEach((id) => {
|
|
117
|
+
if (id.includes('pages.json.js')) {
|
|
118
|
+
// 查询main.js时,需要忽略pages.json.js,否则会把所有页面样式加进来
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (exports.cssLangRE.test(id) && !exports.commonjsProxyRE.test(id)) {
|
|
122
|
+
cssModuleIds.add(id);
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
findCssModuleIds.call(this, id, cssModuleIds, seen);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return cssModuleIds;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Plugin applied after user plugins
|
|
133
|
+
*/
|
|
134
|
+
function cssPostPlugin(config, { chunkCssFilename, chunkCssCode, }) {
|
|
135
|
+
// styles initialization in buildStart causes a styling loss in watch
|
|
136
|
+
const styles = new Map();
|
|
137
|
+
let cssChunks;
|
|
138
|
+
return {
|
|
139
|
+
name: 'vite:css-post',
|
|
140
|
+
buildStart() {
|
|
141
|
+
cssChunks = new Map();
|
|
142
|
+
},
|
|
143
|
+
async transform(css, id) {
|
|
144
|
+
if (!exports.cssLangRE.test(id) || exports.commonjsProxyRE.test(id)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const modules = cssModulesCache.get(config).get(id);
|
|
148
|
+
const modulesCode = modules && (0, pluginutils_1.dataToEsm)(modules, { namedExports: true, preferConst: true });
|
|
149
|
+
// build CSS handling ----------------------------------------------------
|
|
150
|
+
styles.set(id, css);
|
|
151
|
+
return {
|
|
152
|
+
code: modulesCode || '',
|
|
153
|
+
map: { mappings: '' },
|
|
154
|
+
// avoid the css module from being tree-shaken so that we can retrieve
|
|
155
|
+
// it in renderChunk()
|
|
156
|
+
moduleSideEffects: 'no-treeshake',
|
|
157
|
+
};
|
|
158
|
+
},
|
|
159
|
+
async generateBundle() {
|
|
160
|
+
const moduleIds = Array.from(this.getModuleIds());
|
|
161
|
+
moduleIds.forEach((id) => {
|
|
162
|
+
const filename = chunkCssFilename(id);
|
|
163
|
+
if (filename) {
|
|
164
|
+
cssChunks.set(filename, findCssModuleIds.call(this, id));
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
if (!cssChunks.size) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
// resolve asset URL placeholders to their built file URLs and perform
|
|
171
|
+
// minification if necessary
|
|
172
|
+
const processChunkCSS = async (css, { filename, inlined, minify, }) => {
|
|
173
|
+
// replace asset url references with resolved url.
|
|
174
|
+
css = css.replace(asset_1.assetUrlRE, (_, fileHash, postfix = '') => {
|
|
175
|
+
return (0, utils_1.normalizePath)(path_1.default.relative(path_1.default.dirname(filename), (0, asset_1.getAssetFilename)(fileHash, config) + postfix));
|
|
176
|
+
});
|
|
177
|
+
if (minify && config.build.minify) {
|
|
178
|
+
css = await minifyCSS(css, config);
|
|
179
|
+
}
|
|
180
|
+
// 压缩后再处理,小程序平台会补充 @import nvue 代码,esbuild 的压缩会把 `@import "./nvue.css";` 的空格移除,变成 `@import"./nvue.css";` 在支付宝小程序中不支持
|
|
181
|
+
return chunkCssCode(filename, css);
|
|
182
|
+
};
|
|
183
|
+
const genCssCode = (fileName) => {
|
|
184
|
+
return [...cssChunks.get(fileName)]
|
|
185
|
+
.map((id) => styles.get(id) || '')
|
|
186
|
+
.join('\n');
|
|
187
|
+
};
|
|
188
|
+
for (const filename of cssChunks.keys()) {
|
|
189
|
+
const cssCode = genCssCode(filename);
|
|
190
|
+
let source = await processChunkCSS(cssCode, {
|
|
191
|
+
filename: filename,
|
|
192
|
+
inlined: false,
|
|
193
|
+
minify: true,
|
|
194
|
+
});
|
|
195
|
+
this.emitFile({
|
|
196
|
+
fileName: filename,
|
|
197
|
+
type: 'asset',
|
|
198
|
+
source,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
exports.cssPostPlugin = cssPostPlugin;
|
|
205
|
+
function createCSSResolvers(config) {
|
|
206
|
+
let cssResolve;
|
|
207
|
+
let sassResolve;
|
|
208
|
+
let lessResolve;
|
|
209
|
+
return {
|
|
210
|
+
get css() {
|
|
211
|
+
return (cssResolve ||
|
|
212
|
+
(cssResolve = config.createResolver({
|
|
213
|
+
extensions: ['.css'],
|
|
214
|
+
mainFields: ['style'],
|
|
215
|
+
tryIndex: false,
|
|
216
|
+
preferRelative: true,
|
|
217
|
+
})));
|
|
218
|
+
},
|
|
219
|
+
get sass() {
|
|
220
|
+
return (sassResolve ||
|
|
221
|
+
(sassResolve = config.createResolver({
|
|
222
|
+
extensions: ['.scss', '.sass', '.css'],
|
|
223
|
+
mainFields: ['sass', 'style'],
|
|
224
|
+
tryIndex: true,
|
|
225
|
+
tryPrefix: '_',
|
|
226
|
+
preferRelative: true,
|
|
227
|
+
})));
|
|
228
|
+
},
|
|
229
|
+
get less() {
|
|
230
|
+
return (lessResolve ||
|
|
231
|
+
(lessResolve = config.createResolver({
|
|
232
|
+
extensions: ['.less', '.css'],
|
|
233
|
+
mainFields: ['less', 'style'],
|
|
234
|
+
tryIndex: false,
|
|
235
|
+
preferRelative: true,
|
|
236
|
+
})));
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function getCssResolversKeys(resolvers) {
|
|
241
|
+
return Object.keys(resolvers);
|
|
242
|
+
}
|
|
243
|
+
async function compileCSS(id, code, config, urlReplacer, atImportResolvers, server) {
|
|
244
|
+
var _a;
|
|
245
|
+
const { modules: modulesOptions, preprocessorOptions } = config.css || {};
|
|
246
|
+
const isModule = modulesOptions !== false && cssModuleRE.test(id);
|
|
247
|
+
// although at serve time it can work without processing, we do need to
|
|
248
|
+
// crawl them in order to register watch dependencies.
|
|
249
|
+
const needInlineImport = code.includes('@import');
|
|
250
|
+
const hasUrl = exports.cssUrlRE.test(code) || cssImageSetRE.test(code);
|
|
251
|
+
const postcssConfig = await resolvePostcssConfig(config);
|
|
252
|
+
const lang = (_a = id.match(exports.cssLangRE)) === null || _a === void 0 ? void 0 : _a[1];
|
|
253
|
+
// 1. plain css that needs no processing
|
|
254
|
+
if (lang === 'css' &&
|
|
255
|
+
!postcssConfig &&
|
|
256
|
+
!isModule &&
|
|
257
|
+
!needInlineImport &&
|
|
258
|
+
!hasUrl) {
|
|
259
|
+
return { code };
|
|
260
|
+
}
|
|
261
|
+
let map;
|
|
262
|
+
let modules;
|
|
263
|
+
const deps = new Set();
|
|
264
|
+
// 2. pre-processors: sass etc.
|
|
265
|
+
if (isPreProcessor(lang)) {
|
|
266
|
+
const preProcessor = preProcessors[lang];
|
|
267
|
+
let opts = (preprocessorOptions && preprocessorOptions[lang]) || {};
|
|
268
|
+
// support @import from node dependencies by default
|
|
269
|
+
switch (lang) {
|
|
270
|
+
case "scss" /* scss */:
|
|
271
|
+
case "sass" /* sass */:
|
|
272
|
+
opts = {
|
|
273
|
+
includePaths: ['node_modules'],
|
|
274
|
+
alias: config.resolve.alias,
|
|
275
|
+
...opts,
|
|
276
|
+
};
|
|
277
|
+
break;
|
|
278
|
+
case "less" /* less */:
|
|
279
|
+
case "styl" /* styl */:
|
|
280
|
+
case "stylus" /* stylus */:
|
|
281
|
+
opts = {
|
|
282
|
+
paths: ['node_modules'],
|
|
283
|
+
alias: config.resolve.alias,
|
|
284
|
+
...opts,
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
// important: set this for relative import resolving
|
|
288
|
+
opts.filename = (0, utils_1.cleanUrl)(id);
|
|
289
|
+
const preprocessResult = await preProcessor(code, config.root, opts, atImportResolvers, !!config.nvue);
|
|
290
|
+
if (preprocessResult.errors.length) {
|
|
291
|
+
throw preprocessResult.errors[0];
|
|
292
|
+
}
|
|
293
|
+
code = preprocessResult.code;
|
|
294
|
+
map = preprocessResult.map;
|
|
295
|
+
if (preprocessResult.deps) {
|
|
296
|
+
preprocessResult.deps.forEach((dep) => {
|
|
297
|
+
// sometimes sass registers the file itself as a dep
|
|
298
|
+
if ((0, utils_1.normalizePath)(dep) !== (0, utils_1.normalizePath)(opts.filename)) {
|
|
299
|
+
deps.add(dep);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// 3. postcss
|
|
305
|
+
const postcssOptions = (postcssConfig && postcssConfig.options) || {};
|
|
306
|
+
const postcssPlugins = postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : [];
|
|
307
|
+
if (needInlineImport) {
|
|
308
|
+
postcssPlugins.unshift((await Promise.resolve().then(() => __importStar(require('postcss-import')))).default({
|
|
309
|
+
async resolve(id, basedir) {
|
|
310
|
+
const resolved = await atImportResolvers.css(id, path_1.default.join(basedir, '*'));
|
|
311
|
+
if (resolved) {
|
|
312
|
+
return path_1.default.resolve(resolved);
|
|
313
|
+
}
|
|
314
|
+
return id;
|
|
315
|
+
},
|
|
316
|
+
}));
|
|
317
|
+
}
|
|
318
|
+
postcssPlugins.push(UrlRewritePostcssPlugin({
|
|
319
|
+
replacer: urlReplacer,
|
|
320
|
+
}));
|
|
321
|
+
if (isModule) {
|
|
322
|
+
postcssPlugins.unshift((await Promise.resolve().then(() => __importStar(require('postcss-modules')))).default({
|
|
323
|
+
...modulesOptions,
|
|
324
|
+
getJSON(cssFileName, _modules, outputFileName) {
|
|
325
|
+
modules = _modules;
|
|
326
|
+
if (modulesOptions && typeof modulesOptions.getJSON === 'function') {
|
|
327
|
+
modulesOptions.getJSON(cssFileName, _modules, outputFileName);
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
async resolve(id) {
|
|
331
|
+
for (const key of getCssResolversKeys(atImportResolvers)) {
|
|
332
|
+
const resolved = await atImportResolvers[key](id);
|
|
333
|
+
if (resolved) {
|
|
334
|
+
return path_1.default.resolve(resolved);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return id;
|
|
338
|
+
},
|
|
339
|
+
}));
|
|
340
|
+
}
|
|
341
|
+
if (!postcssPlugins.length) {
|
|
342
|
+
return {
|
|
343
|
+
code,
|
|
344
|
+
map,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
// postcss is an unbundled dep and should be lazy imported
|
|
348
|
+
const postcssResult = await (await Promise.resolve().then(() => __importStar(require('postcss'))))
|
|
349
|
+
.default(postcssPlugins)
|
|
350
|
+
.process(code, {
|
|
351
|
+
...postcssOptions,
|
|
352
|
+
to: id,
|
|
353
|
+
from: id,
|
|
354
|
+
map: {
|
|
355
|
+
inline: false,
|
|
356
|
+
annotation: false,
|
|
357
|
+
prev: map,
|
|
358
|
+
},
|
|
359
|
+
});
|
|
360
|
+
// record CSS dependencies from @imports
|
|
361
|
+
for (const message of postcssResult.messages) {
|
|
362
|
+
if (message.type === 'dependency') {
|
|
363
|
+
deps.add(message.file);
|
|
364
|
+
}
|
|
365
|
+
else if (message.type === 'dir-dependency') {
|
|
366
|
+
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
|
|
367
|
+
const { dir, glob: globPattern = '**' } = message;
|
|
368
|
+
const pattern = (0, utils_1.normalizePath)(path_1.default.resolve(path_1.default.dirname(id), dir)) + `/` + globPattern;
|
|
369
|
+
const files = fast_glob_1.default.sync(pattern, {
|
|
370
|
+
ignore: ['**/node_modules/**'],
|
|
371
|
+
});
|
|
372
|
+
for (let i = 0; i < files.length; i++) {
|
|
373
|
+
deps.add(files[i]);
|
|
374
|
+
}
|
|
375
|
+
if (server) {
|
|
376
|
+
// register glob importers so we can trigger updates on file add/remove
|
|
377
|
+
if (!(id in server._globImporters)) {
|
|
378
|
+
;
|
|
379
|
+
server._globImporters[id] = {
|
|
380
|
+
module: server.moduleGraph.getModuleById(id),
|
|
381
|
+
importGlobs: [],
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
;
|
|
385
|
+
server._globImporters[id].importGlobs.push({
|
|
386
|
+
base: config.root,
|
|
387
|
+
pattern,
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else if (message.type === 'warning') {
|
|
392
|
+
let msg = `[vite:css] ${message.text}`;
|
|
393
|
+
if (message.line && message.column) {
|
|
394
|
+
msg += `\n${(0, utils_1.generateCodeFrame)(code, {
|
|
395
|
+
line: message.line,
|
|
396
|
+
column: message.column,
|
|
397
|
+
})}`;
|
|
398
|
+
}
|
|
399
|
+
config.logger.warn(picocolors_1.default.yellow(msg));
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
ast: postcssResult,
|
|
404
|
+
code: postcssResult.css,
|
|
405
|
+
map: postcssResult.map,
|
|
406
|
+
modules,
|
|
407
|
+
deps,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
async function resolvePostcssConfig(config) {
|
|
411
|
+
var _a;
|
|
412
|
+
let result = postcssConfigCache.get(config);
|
|
413
|
+
if (result !== undefined) {
|
|
414
|
+
return result;
|
|
415
|
+
}
|
|
416
|
+
// inline postcss config via vite config
|
|
417
|
+
const inlineOptions = (_a = config.css) === null || _a === void 0 ? void 0 : _a.postcss;
|
|
418
|
+
if ((0, utils_1.isObject)(inlineOptions)) {
|
|
419
|
+
const options = { ...inlineOptions };
|
|
420
|
+
delete options.plugins;
|
|
421
|
+
result = {
|
|
422
|
+
options,
|
|
423
|
+
plugins: inlineOptions.plugins || [],
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
else {
|
|
427
|
+
try {
|
|
428
|
+
const searchPath = typeof inlineOptions === 'string' ? inlineOptions : config.root;
|
|
429
|
+
// @ts-ignore
|
|
430
|
+
result = await (0, postcss_load_config_1.default)({}, searchPath);
|
|
431
|
+
}
|
|
432
|
+
catch (e) {
|
|
433
|
+
if (!/No PostCSS Config found/.test(e.message)) {
|
|
434
|
+
throw e;
|
|
435
|
+
}
|
|
436
|
+
result = null;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
postcssConfigCache.set(config, result);
|
|
440
|
+
return result;
|
|
441
|
+
}
|
|
442
|
+
// https://drafts.csswg.org/css-syntax-3/#identifier-code-point
|
|
443
|
+
exports.cssUrlRE = /(?<=^|[^\w\-\u0080-\uffff])url\(\s*('[^']+'|"[^"]+"|[^'")]+)\s*\)/;
|
|
444
|
+
const cssImageSetRE = /image-set\(([^)]+)\)/;
|
|
445
|
+
const UrlRewritePostcssPlugin = (opts) => {
|
|
446
|
+
if (!opts) {
|
|
447
|
+
throw new Error('base or replace is required');
|
|
448
|
+
}
|
|
449
|
+
return {
|
|
450
|
+
postcssPlugin: 'vite-url-rewrite',
|
|
451
|
+
Once(root) {
|
|
452
|
+
const promises = [];
|
|
453
|
+
root.walkDecls((declaration) => {
|
|
454
|
+
const isCssUrl = exports.cssUrlRE.test(declaration.value);
|
|
455
|
+
const isCssImageSet = cssImageSetRE.test(declaration.value);
|
|
456
|
+
if (isCssUrl || isCssImageSet) {
|
|
457
|
+
const replacerForDeclaration = (rawUrl) => {
|
|
458
|
+
var _a;
|
|
459
|
+
const importer = (_a = declaration.source) === null || _a === void 0 ? void 0 : _a.input.file;
|
|
460
|
+
return opts.replacer(rawUrl, importer);
|
|
461
|
+
};
|
|
462
|
+
const rewriterToUse = isCssUrl ? rewriteCssUrls : rewriteCssImageSet;
|
|
463
|
+
promises.push(rewriterToUse(declaration.value, replacerForDeclaration).then((url) => {
|
|
464
|
+
declaration.value = url;
|
|
465
|
+
}));
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
if (promises.length) {
|
|
469
|
+
return Promise.all(promises);
|
|
470
|
+
}
|
|
471
|
+
},
|
|
472
|
+
};
|
|
473
|
+
};
|
|
474
|
+
UrlRewritePostcssPlugin.postcss = true;
|
|
475
|
+
function rewriteCssUrls(css, replacer) {
|
|
476
|
+
return (0, utils_1.asyncReplace)(css, exports.cssUrlRE, async (match) => {
|
|
477
|
+
const [matched, rawUrl] = match;
|
|
478
|
+
return await doUrlReplace(rawUrl, matched, replacer);
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
function rewriteCssImageSet(css, replacer) {
|
|
482
|
+
return (0, utils_1.asyncReplace)(css, cssImageSetRE, async (match) => {
|
|
483
|
+
const [matched, rawUrl] = match;
|
|
484
|
+
const url = await (0, utils_1.processSrcSet)(rawUrl, ({ url }) => doUrlReplace(url, matched, replacer));
|
|
485
|
+
return `image-set(${url})`;
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
async function doUrlReplace(rawUrl, matched, replacer) {
|
|
489
|
+
let wrap = '';
|
|
490
|
+
const first = rawUrl[0];
|
|
491
|
+
if (first === `"` || first === `'`) {
|
|
492
|
+
wrap = first;
|
|
493
|
+
rawUrl = rawUrl.slice(1, -1);
|
|
494
|
+
}
|
|
495
|
+
if ((0, utils_1.isExternalUrl)(rawUrl) || (0, utils_1.isDataUrl)(rawUrl) || rawUrl.startsWith('#')) {
|
|
496
|
+
return matched;
|
|
497
|
+
}
|
|
498
|
+
return `url(${wrap}${await replacer(rawUrl)}${wrap})`;
|
|
499
|
+
}
|
|
500
|
+
async function minifyCSS(css, config) {
|
|
501
|
+
const { code, warnings } = await (0, esbuild_1.transform)(css, {
|
|
502
|
+
loader: 'css',
|
|
503
|
+
minify: true,
|
|
504
|
+
target: config.build.cssTarget || undefined,
|
|
505
|
+
});
|
|
506
|
+
if (warnings.length) {
|
|
507
|
+
const msgs = await (0, esbuild_1.formatMessages)(warnings, { kind: 'warning' });
|
|
508
|
+
config.logger.warn(picocolors_1.default.yellow(`warnings when minifying css:\n${msgs.join('\n')}`));
|
|
509
|
+
}
|
|
510
|
+
return code;
|
|
511
|
+
}
|
|
512
|
+
exports.minifyCSS = minifyCSS;
|
|
513
|
+
const AtImportHoistPlugin = () => {
|
|
514
|
+
return {
|
|
515
|
+
postcssPlugin: 'vite-hoist-at-imports',
|
|
516
|
+
Once(root) {
|
|
517
|
+
const imports = [];
|
|
518
|
+
root.walkAtRules((rule) => {
|
|
519
|
+
if (rule.name === 'import') {
|
|
520
|
+
// record in reverse so that can simply prepend to preserve order
|
|
521
|
+
imports.unshift(rule);
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
imports.forEach((i) => root.prepend(i));
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
};
|
|
528
|
+
AtImportHoistPlugin.postcss = true;
|
|
529
|
+
const loadedPreprocessors = {};
|
|
530
|
+
function loadPreprocessor(lang, root) {
|
|
531
|
+
var _a, _b;
|
|
532
|
+
if (lang in loadedPreprocessors) {
|
|
533
|
+
return loadedPreprocessors[lang];
|
|
534
|
+
}
|
|
535
|
+
try {
|
|
536
|
+
// Search for the preprocessor in the root directory first, and fall back
|
|
537
|
+
// to the default require paths.
|
|
538
|
+
const fallbackPaths = ((_b = (_a = require.resolve).paths) === null || _b === void 0 ? void 0 : _b.call(_a, lang)) || [];
|
|
539
|
+
const resolved = require.resolve(lang, { paths: [root, ...fallbackPaths] });
|
|
540
|
+
return (loadedPreprocessors[lang] = require(resolved));
|
|
541
|
+
}
|
|
542
|
+
catch (e) {
|
|
543
|
+
throw new Error(`Preprocessor dependency "${lang}" not found. Did you install it?`);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
// .scss/.sass processor
|
|
547
|
+
const scss = async (source, root, options, resolvers, isNVue) => {
|
|
548
|
+
const render = loadPreprocessor("sass" /* sass */, root).render;
|
|
549
|
+
const internalImporter = (url, importer, done) => {
|
|
550
|
+
resolvers.sass(url, importer).then((resolved) => {
|
|
551
|
+
if (resolved) {
|
|
552
|
+
rebaseUrls(resolved, options.filename, options.alias, isNVue).then(done);
|
|
553
|
+
}
|
|
554
|
+
else {
|
|
555
|
+
done && done(null);
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
};
|
|
559
|
+
const importer = [internalImporter];
|
|
560
|
+
if (options.importer) {
|
|
561
|
+
Array.isArray(options.importer)
|
|
562
|
+
? importer.push(...options.importer)
|
|
563
|
+
: importer.push(options.importer);
|
|
564
|
+
}
|
|
565
|
+
const finalOptions = {
|
|
566
|
+
...options,
|
|
567
|
+
data: await getSource(source, options.filename, options.additionalData),
|
|
568
|
+
file: options.filename,
|
|
569
|
+
outFile: options.filename,
|
|
570
|
+
importer,
|
|
571
|
+
};
|
|
572
|
+
try {
|
|
573
|
+
const result = await new Promise((resolve, reject) => {
|
|
574
|
+
render(finalOptions, (err, res) => {
|
|
575
|
+
if (err) {
|
|
576
|
+
reject(err);
|
|
577
|
+
}
|
|
578
|
+
else {
|
|
579
|
+
resolve(res);
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
const deps = result.stats.includedFiles;
|
|
584
|
+
return {
|
|
585
|
+
code: result.css.toString(),
|
|
586
|
+
errors: [],
|
|
587
|
+
deps,
|
|
588
|
+
};
|
|
589
|
+
}
|
|
590
|
+
catch (e) {
|
|
591
|
+
// normalize SASS error
|
|
592
|
+
e.id = e.file;
|
|
593
|
+
e.frame = e.formatted;
|
|
594
|
+
return { code: '', errors: [e], deps: [] };
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
const sass = (source, root, options, aliasResolver, isNVue) => scss(source, root, {
|
|
598
|
+
...options,
|
|
599
|
+
indentedSyntax: true,
|
|
600
|
+
}, aliasResolver, isNVue);
|
|
601
|
+
function preprocessCss(content, isNVue = false) {
|
|
602
|
+
if (content.includes('#endif')) {
|
|
603
|
+
return isNVue ? (0, preprocess_1.preNVueCss)(content) : (0, preprocess_1.preCss)(content);
|
|
604
|
+
}
|
|
605
|
+
return content;
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* relative url() inside \@imported sass and less files must be rebased to use
|
|
609
|
+
* root file as base.
|
|
610
|
+
*/
|
|
611
|
+
async function rebaseUrls(file, rootFile, alias, isNVue = false) {
|
|
612
|
+
file = path_1.default.resolve(file); // ensure os-specific flashes
|
|
613
|
+
// 条件编译
|
|
614
|
+
const contents = preprocessCss(fs_1.default.readFileSync(file, 'utf-8'), isNVue);
|
|
615
|
+
// in the same dir, no need to rebase
|
|
616
|
+
const fileDir = path_1.default.dirname(file);
|
|
617
|
+
const rootDir = path_1.default.dirname(rootFile);
|
|
618
|
+
if (fileDir === rootDir) {
|
|
619
|
+
return { file, contents };
|
|
620
|
+
}
|
|
621
|
+
// no url()
|
|
622
|
+
if (!exports.cssUrlRE.test(contents)) {
|
|
623
|
+
return { file, contents };
|
|
624
|
+
}
|
|
625
|
+
const rebased = await rewriteCssUrls(contents, (url) => {
|
|
626
|
+
if (url.startsWith('/'))
|
|
627
|
+
return url;
|
|
628
|
+
// match alias, no need to rewrite
|
|
629
|
+
for (const { find } of alias) {
|
|
630
|
+
const matches = typeof find === 'string' ? url.startsWith(find) : find.test(url);
|
|
631
|
+
if (matches) {
|
|
632
|
+
return url;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
const absolute = path_1.default.resolve(fileDir, url);
|
|
636
|
+
const relative = path_1.default.relative(rootDir, absolute);
|
|
637
|
+
return (0, utils_1.normalizePath)(relative);
|
|
638
|
+
});
|
|
639
|
+
return {
|
|
640
|
+
file,
|
|
641
|
+
contents: rebased,
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
// .less
|
|
645
|
+
const less = async (source, root, options, resolvers, isNVue) => {
|
|
646
|
+
const nodeLess = loadPreprocessor("less" /* less */, root);
|
|
647
|
+
const viteResolverPlugin = createViteLessPlugin(nodeLess, options.filename, options.alias, resolvers, isNVue);
|
|
648
|
+
source = await getSource(source, options.filename, options.additionalData);
|
|
649
|
+
let result;
|
|
650
|
+
try {
|
|
651
|
+
result = await nodeLess.render(source, {
|
|
652
|
+
...options,
|
|
653
|
+
plugins: [viteResolverPlugin, ...(options.plugins || [])],
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
catch (e) {
|
|
657
|
+
const error = e;
|
|
658
|
+
// normalize error info
|
|
659
|
+
const normalizedError = new Error(error.message || error.type);
|
|
660
|
+
normalizedError.loc = {
|
|
661
|
+
file: error.filename || options.filename,
|
|
662
|
+
line: error.line,
|
|
663
|
+
column: error.column,
|
|
664
|
+
};
|
|
665
|
+
return { code: '', errors: [normalizedError], deps: [] };
|
|
666
|
+
}
|
|
667
|
+
return {
|
|
668
|
+
code: result.css.toString(),
|
|
669
|
+
deps: result.imports,
|
|
670
|
+
errors: [],
|
|
671
|
+
};
|
|
672
|
+
};
|
|
673
|
+
/**
|
|
674
|
+
* Less manager, lazy initialized
|
|
675
|
+
*/
|
|
676
|
+
let ViteLessManager;
|
|
677
|
+
function createViteLessPlugin(less, rootFile, alias, resolvers, isNVue) {
|
|
678
|
+
if (!ViteLessManager) {
|
|
679
|
+
ViteLessManager = class ViteManager extends less.FileManager {
|
|
680
|
+
constructor(rootFile, resolvers, alias) {
|
|
681
|
+
super();
|
|
682
|
+
this.rootFile = rootFile;
|
|
683
|
+
this.resolvers = resolvers;
|
|
684
|
+
this.alias = alias;
|
|
685
|
+
}
|
|
686
|
+
supports() {
|
|
687
|
+
return true;
|
|
688
|
+
}
|
|
689
|
+
supportsSync() {
|
|
690
|
+
return false;
|
|
691
|
+
}
|
|
692
|
+
async loadFile(filename, dir, opts, env) {
|
|
693
|
+
const resolved = await this.resolvers.less(filename, path_1.default.join(dir, '*'));
|
|
694
|
+
if (resolved) {
|
|
695
|
+
const result = await rebaseUrls(resolved, this.rootFile, this.alias, isNVue);
|
|
696
|
+
let contents;
|
|
697
|
+
if (result && 'contents' in result) {
|
|
698
|
+
contents = result.contents;
|
|
699
|
+
}
|
|
700
|
+
else {
|
|
701
|
+
contents = fs_1.default.readFileSync(resolved, 'utf-8');
|
|
702
|
+
}
|
|
703
|
+
return {
|
|
704
|
+
filename: path_1.default.resolve(resolved),
|
|
705
|
+
contents,
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
else {
|
|
709
|
+
return super.loadFile(filename, dir, opts, env);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
return {
|
|
715
|
+
install(_, pluginManager) {
|
|
716
|
+
pluginManager.addFileManager(new ViteLessManager(rootFile, resolvers, alias));
|
|
717
|
+
},
|
|
718
|
+
minVersion: [3, 0, 0],
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
// .styl
|
|
722
|
+
const styl = async (source, root, options) => {
|
|
723
|
+
var _a;
|
|
724
|
+
const nodeStylus = loadPreprocessor("stylus" /* stylus */, root);
|
|
725
|
+
// Get source with preprocessor options.additionalData. Make sure a new line separator
|
|
726
|
+
// is added to avoid any render error, as added stylus content may not have semi-colon separators
|
|
727
|
+
source = await getSource(source, options.filename, options.additionalData, '\n');
|
|
728
|
+
// Get preprocessor options.imports dependencies as stylus
|
|
729
|
+
// does not return them with its builtin `.deps()` method
|
|
730
|
+
const importsDeps = ((_a = options.imports) !== null && _a !== void 0 ? _a : []).map((dep) => path_1.default.resolve(dep));
|
|
731
|
+
try {
|
|
732
|
+
const ref = nodeStylus(source, options);
|
|
733
|
+
// if (map) ref.set('sourcemap', { inline: false, comment: false })
|
|
734
|
+
const result = ref.render();
|
|
735
|
+
// Concat imports deps with computed deps
|
|
736
|
+
const deps = [...ref.deps(), ...importsDeps];
|
|
737
|
+
return { code: result, errors: [], deps };
|
|
738
|
+
}
|
|
739
|
+
catch (e) {
|
|
740
|
+
return { code: '', errors: [e], deps: [] };
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
function getSource(source, filename, additionalData, sep = '') {
|
|
744
|
+
if (!additionalData)
|
|
745
|
+
return source;
|
|
746
|
+
if (typeof additionalData === 'function') {
|
|
747
|
+
return additionalData(source, filename);
|
|
748
|
+
}
|
|
749
|
+
return additionalData + sep + source;
|
|
750
|
+
}
|
|
751
|
+
const preProcessors = Object.freeze({
|
|
752
|
+
["less" /* less */]: less,
|
|
753
|
+
["sass" /* sass */]: sass,
|
|
754
|
+
["scss" /* scss */]: scss,
|
|
755
|
+
["styl" /* styl */]: styl,
|
|
756
|
+
["stylus" /* stylus */]: styl,
|
|
757
|
+
});
|
|
758
|
+
function isPreProcessor(lang) {
|
|
759
|
+
return lang && lang in preProcessors;
|
|
760
|
+
}
|