Debian Codegraph @

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

raw
1 "use strict";
2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.sassPlugin = sassPlugin;
4 const cache_1 = require("./cache");
5 const path_1 = require("path");
6 const render_1 = require("./render");
7 const utils_1 = require("./utils");
8 let nextId = 0;
9 function sassPlugin(options = {}) {
10 var _a;
11 if (!options.basedir) {
12 options.basedir = process.cwd();
13 }
14 if (options.includePaths) {
15 console.log(`'includePaths' option is deprecated, please use 'loadPaths' instead`);
16 }
17 const type = (_a = options.type) !== null && _a !== void 0 ? _a : 'css';
18 if (options['picomatch'] || options['exclude'] || typeof type !== 'string' && typeof type !== 'function') {
19 console.log('The type array, exclude and picomatch options are no longer supported, please refer to the README for alternatives.');
20 }
21 const nonce = (0, utils_1.parseNonce)(options.nonce);
22 return {
23 name: 'sass-plugin',
24 async setup({ initialOptions, onResolve, onLoad, resolve, onStart, onDispose }) {
25 var _a, _b;
26 options.loadPaths = Array.from(new Set([
27 ...options.loadPaths || (0, utils_1.modulesPaths)(initialOptions.absWorkingDir),
28 ...options.includePaths || []
29 ]));
30 const { sourcemap, watched } = (0, utils_1.getContext)(initialOptions);
31 if (options.cssImports) {
32 onResolve({ filter: /^~.*\.css$/ }, ({ path, importer, resolveDir }) => {
33 return resolve(path.slice(1), { importer, resolveDir, kind: 'import-rule' });
34 });
35 }
36 const transform = options.transform ? options.transform.bind(options) : null;
37 const namedExports = options.namedExports === 'safe' ? utils_1.safeExport : options.namedExports;
38 const cssChunks = {};
39 const pluginSuffix = nextId ? `-${nextId++}` : (nextId = 1) && '';
40 const cssChunkPrefix = 'css-chunk' + pluginSuffix;
41 if (transform) {
42 const namespace = 'esbuild-sass-plugin' + pluginSuffix;
43 onResolve({ filter: new RegExp(`^${cssChunkPrefix}`) }, ({ path, resolveDir }) => ({
44 path,
45 namespace,
46 pluginData: { resolveDir }
47 }));
48 onLoad({ filter: /./, namespace }, ({ path, pluginData: { resolveDir } }) => ({
49 contents: cssChunks[path],
50 resolveDir,
51 loader: 'css'
52 }));
53 }
54 const renderSass = await (0, render_1.createRenderer)(options, (_a = options.sourceMap) !== null && _a !== void 0 ? _a : sourcemap, onDispose);
55 const [cached, resetCache] = (0, cache_1.useCache)(options);
56 onStart(resetCache);
57 onLoad({ filter: (_b = options.filter) !== null && _b !== void 0 ? _b : utils_1.DEFAULT_FILTER }, cached(async ({ path }) => {
58 var _a;
59 try {
60 let { cssText, watchFiles, warnings } = await renderSass(path);
61 if (!warnings) {
62 warnings = [];
63 }
64 watched[path] = watchFiles;
65 const resolveDir = (0, path_1.dirname)(path);
66 if (transform) {
67 const out = await transform(cssText, resolveDir, path);
68 if (typeof out !== 'string') {
69 if (out.loader && out.loader !== 'js') {
70 return {
71 ...out,
72 resolveDir,
73 watchFiles: [...watchFiles, ...(out.watchFiles || [])],
74 watchDirs: out.watchDirs || []
75 };
76 }
77 let { contents, pluginData } = out;
78 if (type === 'css') {
79 let name = cssChunkPrefix + (0, utils_1.posixRelative)(path);
80 cssChunks[name] = contents;
81 contents = `import '${name}';`;
82 }
83 else if (type === 'style') {
84 contents = (0, utils_1.makeModule)(String(contents), 'style', nonce);
85 }
86 else {
87 return {
88 errors: [{ text: `unsupported type '${type}' for postCSS modules` }]
89 };
90 }
91 if (namedExports) {
92 const json = JSON.parse(pluginData.exports);
93 const keys = Object.keys(json);
94 if (typeof namedExports === 'function') {
95 const vars = [];
96 for (const name of keys) {
97 const safe = namedExports(name);
98 if (safe) {
99 if (safe !== name) {
100 if (json[safe]) {
101 return { errors: [{ text: `clash detected in safe named export '${safe}'` }] };
102 }
103 console.log(`exported '${name}' as '${safe}' in '${path}'`);
104 }
105 vars.push([safe, name]);
106 }
107 }
108 if (vars.length > 0) {
109 contents += `export const ${vars.map(([name, key]) => {
110 const assignment = `${name}=${JSON.stringify(json[key])}`;
111 json[key] = name;
112 return assignment;
113 }).join(',')};`;
114 }
115 }
116 else {
117 contents += `export const ${keys.map(key => {
118 const assignment = `${key}=${JSON.stringify(json[key])}`;
119 json[key] = key;
120 return assignment;
121 }).join(',')};`;
122 }
123 contents += `export default {${keys.map(key => `${JSON.stringify(key)}:${json[key]}`).join(',')}};`;
124 }
125 else {
126 contents += `export default ${pluginData.exports};`;
127 }
128 return {
129 contents,
130 loader: 'js',
131 resolveDir,
132 watchFiles: [...watchFiles, ...(out.watchFiles || [])],
133 watchDirs: out.watchDirs || []
134 };
135 }
136 else {
137 cssText = out;
138 }
139 }
140 return type === 'css' || type === 'local-css' ? {
141 contents: cssText,
142 loader: type,
143 resolveDir,
144 warnings,
145 watchFiles
146 } : {
147 contents: (0, utils_1.makeModule)(cssText, type, nonce),
148 loader: 'js',
149 resolveDir,
150 warnings,
151 watchFiles
152 };
153 }
154 catch (err) {
155 return {
156 errors: [{ text: err.message }],
157 watchFiles: (_a = watched[path]) !== null && _a !== void 0 ? _a : [path]
158 };
159 }
160 }));
161 }
162 };
163 }
164 //# sourceMappingURL=plugin.js.map