Debian Codegraph @

node/esbuild-sass-plugin/lib/utils.js

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.posixRelative = exports.DEFAULT_FILTER = void 0;
4 exports.modulesPaths = modulesPaths;
5 exports.fileSyntax = fileSyntax;
6 exports.getContext = getContext;
7 exports.sourceMappingURL = sourceMappingURL;
8 exports.makeModule = makeModule;
9 exports.parseNonce = parseNonce;
10 exports.postcssModules = postcssModules;
11 exports.createResolver = createResolver;
12 exports.safeExport = safeExport;
13 const fs_1 = require("fs");
14 const path_1 = require("path");
15 const cwd = process.cwd();
16 exports.DEFAULT_FILTER = /\.(s[ac]ss|css)$/;
17 exports.posixRelative = require('path').sep === '/'
18 ? (path) => (0, path_1.relative)(cwd, path)
19 : (path) => (0, path_1.relative)(cwd, path).replace(/\\/g, '/');
20 function modulesPaths(absWorkingDir) {
21 let path = absWorkingDir || process.cwd();
22 let { root } = (0, path_1.parse)(path);
23 let found = [];
24 while (path !== root) {
25 const filename = (0, path_1.resolve)(path, 'node_modules');
26 if ((0, fs_1.existsSync)(filename)) {
27 found.push(filename);
28 }
29 path = (0, path_1.resolve)(path, '..');
30 }
31 return [...found];
32 }
33 function fileSyntax(filename) {
34 if (filename.endsWith('.scss')) {
35 return 'scss';
36 }
37 else if (filename.endsWith('.css')) {
38 return 'css';
39 }
40 else {
41 return 'indented';
42 }
43 }
44 const SASS_PLUGIN_CONTEXT = Symbol();
45 function getContext(buildOptions) {
46 let descriptor = Object.getOwnPropertyDescriptor(buildOptions, SASS_PLUGIN_CONTEXT);
47 if (descriptor === undefined) {
48 Object.defineProperty(buildOptions, SASS_PLUGIN_CONTEXT, descriptor = {
49 value: {
50 instances: 0
51 }
52 });
53 }
54 const instance = descriptor.value.instances++;
55 return {
56 instance,
57 namespace: `sass-plugin-${instance}`,
58 sourcemap: !!buildOptions.sourcemap,
59 watched: {}
60 };
61 }
62 function sourceMappingURL(sourceMap) {
63 const data = Buffer.from(JSON.stringify(sourceMap), 'utf-8').toString('base64');
64 return `/*# sourceMappingURL=data:application/json;charset=utf-8;base64,${data} */`;
65 }
66 function requireTool(module, basedir) {
67 try {
68 return require(module);
69 }
70 catch (ignored) {
71 }
72 if (basedir)
73 try {
74 return require(require.resolve(module, { paths: [basedir] }));
75 }
76 catch (ignored) {
77 }
78 try {
79 return require(require.resolve(module, { paths: [process.cwd()] }));
80 }
81 catch (e) {
82 try {
83 return require(module);
84 }
85 catch (ignored) {
86 console.error(`Cannot find module '${module}', make sure it's installed. e.g. yarn add -D ${module}`, e);
87 process.exit(1);
88 }
89 }
90 }
91 const cssTextModule = cssText => `\
92 export default \`${cssText.replace(/([$`\\])/g, '\\$1')}\`;
93 `;
94 const cssResultModule = cssText => `\
95 import {css} from "lit-element/lit-element.js";
96 export default css\`${cssText.replace(/([$`\\])/g, '\\$1')}\`;
97 `;
98 const styleModule = (cssText, nonce) => nonce ? `\
99 const css = \`${cssText.replace(/([$`\\])/g, '\\$1')}\`;
100 const style = document.createElement("style");
101 style.setAttribute("nonce", ${nonce});
102 style.appendChild(document.createTextNode(css));
103 document.head.appendChild(style);
104 export {css};
105 ` : `\
106 const css = \`${cssText.replace(/([$`\\])/g, '\\$1')}\`;
107 document.head
108 .appendChild(document.createElement("style"))
109 .appendChild(document.createTextNode(css));
110 export {css};
111 `;
112 function makeModule(contents, type, nonce) {
113 switch (type) {
114 case 'style':
115 return styleModule(contents, nonce);
116 case 'lit-css':
117 return cssResultModule(contents);
118 case 'css-text':
119 return cssTextModule(contents);
120 case 'css':
121 case 'local-css':
122 return contents;
123 default:
124 return type(contents, nonce);
125 }
126 }
127 function parseNonce(nonce) {
128 if (nonce) {
129 if (nonce.startsWith('window.') || nonce.startsWith('process.') || nonce.startsWith('globalThis.')) {
130 return nonce;
131 }
132 else {
133 return JSON.stringify(nonce);
134 }
135 }
136 else {
137 return nonce;
138 }
139 }
140 function postcssModules(options, plugins = []) {
141 const postcss = requireTool('postcss', options.basedir);
142 const postcssModulesPlugin = requireTool('postcss-modules', options.basedir);
143 return async function (source, dirname, path) {
144 let cssModule;
145 const { css } = await postcss([
146 postcssModulesPlugin({
147 ...options,
148 getJSON(cssFilename, json, outputFileName) {
149 var _a;
150 cssModule = JSON.stringify(json, null, 2);
151 (_a = options.getJSON) === null || _a === void 0 ? void 0 : _a.call(options, cssFilename, json, outputFileName);
152 }
153 }),
154 ...plugins
155 ]).process(source, { from: path, map: false });
156 return {
157 contents: css,
158 pluginData: { exports: cssModule },
159 loader: 'js'
160 };
161 };
162 }
163 function createResolver(options = {}, loadPaths) {
164 if (options.prefer) {
165 const resolve = require('resolve');
166 const cache = {};
167 const prefer = options.prefer;
168 const opts = {
169 paths: ['.', ...loadPaths],
170 readPackageSync(readFileSync, pkgfile) {
171 let cached = cache[pkgfile];
172 if (!cached) {
173 const pkg = JSON.parse(readFileSync(pkgfile));
174 cached = cache[pkgfile] = { main: pkg[prefer] || pkg.main };
175 }
176 return cached;
177 }
178 };
179 return (id, basedir) => {
180 try {
181 opts.basedir = basedir;
182 return resolve.sync(id, opts);
183 }
184 catch (ignored) {
185 return id;
186 }
187 };
188 }
189 else {
190 const opts = {
191 paths: ['.', ...loadPaths]
192 };
193 return (id, basedir) => {
194 try {
195 opts.paths[0] = basedir;
196 const resolved = require.resolve(id, opts);
197 if (!resolved.endsWith('.js')) {
198 return resolved;
199 }
200 try {
201 const parts = id.split('/');
202 const pkgName = id.startsWith('@') ? parts[0] + '/' + parts[1] : parts[0];
203 const pkgPath = require.resolve(pkgName + '/package.json', opts);
204 const pkg = JSON.parse((0, fs_1.readFileSync)(pkgPath, 'utf8'));
205 for (const field of ['style', 'sass', 'scss']) {
206 if (typeof pkg[field] === 'string') {
207 const stylePath = (0, path_1.resolve)((0, path_1.dirname)(pkgPath), pkg[field]);
208 if ((0, fs_1.existsSync)(stylePath))
209 return stylePath;
210 }
211 }
212 }
213 catch (ignored) { }
214 const base = resolved.slice(0, -3);
215 for (const ext of ['.scss', '.sass', '.css']) {
216 if ((0, fs_1.existsSync)(base + ext))
217 return base + ext;
218 }
219 return id;
220 }
221 catch (ignored) {
222 return id;
223 }
224 };
225 }
226 }
227 const reserved = {
228 await: true,
229 break: true,
230 case: true,
231 catch: true,
232 class: true,
233 const: true,
234 continue: true,
235 debugger: true,
236 default: true,
237 delete: true,
238 do: true,
239 else: true,
240 enum: true,
241 export: true,
242 extends: true,
243 false: true,
244 finally: true,
245 for: true,
246 function: true,
247 if: true,
248 implements: true,
249 import: true,
250 in: true,
251 instanceof: true,
252 interface: true,
253 let: true,
254 new: true,
255 null: true,
256 package: true,
257 private: true,
258 protected: true,
259 public: true,
260 return: true,
261 static: true,
262 super: true,
263 switch: true,
264 this: true,
265 throw: true,
266 true: true,
267 try: true,
268 typeof: true,
269 var: true,
270 void: true,
271 while: true,
272 with: true,
273 yield: true
274 };
275 function safeExport(identifier) {
276 if (reserved[identifier]) {
277 return '_' + identifier;
278 }
279 else {
280 return identifier.trim().replace(/\W+([a-z])/g, (esc, c) => esc[0] === '-' ? c.toUpperCase() : '_');
281 }
282 }
283 //# sourceMappingURL=utils.js.map