chocola 1.3.8 → 1.3.10

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/compiler/index.js CHANGED
@@ -93,14 +93,17 @@ async function loadAndDisplayComponents(srcComponentsPath) {
93
93
  async function processAssets(doc, rootDir, srcDir, outDirPath) {
94
94
  const { stylesheets, icons } = getAssetLinks(doc);
95
95
  const fileIds = [];
96
+ let cssContents = [];
96
97
 
97
98
  for (const link of stylesheets) {
98
- await processStylesheet(link, rootDir, srcDir, outDirPath, fileIds);
99
+ const css = await processStylesheet(link, rootDir, srcDir, outDirPath, fileIds);
100
+ cssContents.push(css);
99
101
  }
100
102
 
101
103
  for (const link of icons) {
102
104
  await processIcons(link, rootDir, srcDir, outDirPath, fileIds);
103
105
  }
106
+ return cssContents
104
107
  }
105
108
 
106
109
  /**
@@ -137,7 +140,7 @@ export default async function runtime(rootDir, buildConfig) {
137
140
 
138
141
  const { runtimeScript, scopesCss } = processAllComponents(appElements, loadedComponents);
139
142
  const runtimeFilename = await generateRuntimeScript(runtimeScript, paths.outDir);
140
- await processAssets(doc, rootDir, config.srcDir, paths.outDir);
143
+ const globalCss = (await processAssets(doc, rootDir, config.srcDir, paths.outDir)).join("\n");
141
144
 
142
145
  if (scopesCss) {
143
146
  const fileName = "sc-" + genRandomId(null, 6) + ".css";
@@ -149,7 +152,7 @@ export default async function runtime(rootDir, buildConfig) {
149
152
  const html = await serializeDOM(dom);
150
153
  await writeHTMLOutput(html, paths.outDir);
151
154
 
152
- await copyResources(rootDir, config.srcDir, paths.outDir);
155
+ await copyResources(rootDir, scopesCss, globalCss, config.srcDir, paths.outDir);
153
156
 
154
157
  !isHotReload && logSuccess(paths.outDir);
155
158
  isHotReload && console.log("Dev server updated");
@@ -109,6 +109,8 @@ export async function processStylesheet(link, rootDir, srcDir, outDirPath, fileI
109
109
 
110
110
  await fs.writeFile(path.join(outDirPath, cssFileName), css);
111
111
  link.setAttribute("href", "./" + cssFileName);
112
+
113
+ return css;
112
114
  } catch (err) {
113
115
  throwError(err);
114
116
  }
@@ -132,6 +134,51 @@ export async function processIcons(link, rootDir, srcDir, outDirPath) {
132
134
  }
133
135
  }
134
136
 
137
+ export function getCssAssets(css) {
138
+ const results = new Set();
139
+
140
+ const urlRegex = /url\(([^)]+)\)/gi;
141
+
142
+ const importStringRegex = /@import\s+["']([^"']+)["']/gi;
143
+
144
+ const importUrlRegex = /@import\s+url\(([^)]+)\)/gi;
145
+
146
+ const clean = (raw) => {
147
+ let p = raw.trim();
148
+
149
+ if (
150
+ (p.startsWith('"') && p.endsWith('"')) ||
151
+ (p.startsWith("'") && p.endsWith("'"))
152
+ ) {
153
+ p = p.slice(1, -1);
154
+ }
155
+
156
+ if (p.startsWith("data:")) return null;
157
+
158
+ return p;
159
+ };
160
+
161
+ let match;
162
+
163
+ while ((match = urlRegex.exec(css))) {
164
+ const p = clean(match[1]);
165
+ if (p) results.add(p);
166
+ }
167
+
168
+ while ((match = importStringRegex.exec(css))) {
169
+ const p = clean(match[1]);
170
+ if (p) results.add(p);
171
+ }
172
+
173
+ while ((match = importUrlRegex.exec(css))) {
174
+ const p = clean(match[1]);
175
+ if (p) results.add(p);
176
+ }
177
+
178
+ return [...results];
179
+ }
180
+
181
+
135
182
  /**
136
183
  * Copies all local resources (images, fonts, etc.) to output directory
137
184
  * Excludes web links and script/link tags
@@ -140,11 +187,12 @@ export async function processIcons(link, rootDir, srcDir, outDirPath) {
140
187
  * @param {import("fs").PathLike} outDirPath - Output directory path
141
188
  * @throws {Error} if resources cannot be copied
142
189
  */
143
- export async function copyResources(rootDir, srcDir, outDirPath) {
190
+ export async function copyResources(rootDir, scopesCss, globalCss, srcDir, outDirPath) {
144
191
  try {
145
192
  const newIndex = await fs.readFile(path.join(outDirPath, "index.html"), "utf8");
146
193
  const newDoc = new JSDOM(newIndex);
147
194
  const newElements = Array.from(newDoc.window.document.querySelectorAll("*"));
195
+ const inlinePathsRegex = /url\((.*?)\)/gi;
148
196
 
149
197
  for (const el of newElements) {
150
198
  if (el.tagName === "LINK" || el.tagName === "SCRIPT") continue;
@@ -158,6 +206,41 @@ export async function copyResources(rootDir, srcDir, outDirPath) {
158
206
  await fs.mkdir(path.dirname(destPath), { recursive: true });
159
207
  await fs.copyFile(srcPath, destPath);
160
208
  }
209
+
210
+ const styles = el.getAttribute("style");
211
+
212
+ if (styles) {
213
+ let stylesMatch;
214
+ while ((stylesMatch = inlinePathsRegex.exec(styles)) !== null) {
215
+ let filePath = stylesMatch[1].trim();
216
+
217
+ if (
218
+ (filePath.startsWith('"') && filePath.endsWith('"')) ||
219
+ (filePath.startsWith("'") && filePath.endsWith("'"))
220
+ ) {
221
+ filePath = filePath.slice(1, -1);
222
+ }
223
+
224
+ const srcPath = path.join(rootDir, srcDir, filePath);
225
+ const destPath = path.join(outDirPath, filePath);
226
+
227
+ await fs.mkdir(path.dirname(destPath), { recursive: true });
228
+ await fs.copyFile(srcPath, destPath);
229
+ }
230
+ }
231
+ }
232
+
233
+ const cssAssets = [...getCssAssets(scopesCss), ...getCssAssets(globalCss)];
234
+ for (const assetPath of cssAssets) {
235
+ try {
236
+ const srcPath = path.join(rootDir, srcDir, assetPath);
237
+ const destPath = path.join(outDirPath, assetPath);
238
+
239
+ await fs.mkdir(path.dirname(destPath), { recursive: true });
240
+ await fs.copyFile(srcPath, destPath);
241
+ } catch (err) {
242
+ throwError(err)
243
+ }
161
244
  }
162
245
  } catch (err) {
163
246
  throwError(err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chocola",
3
- "version": "1.3.8",
3
+ "version": "1.3.10",
4
4
  "description": "The sweetest way to build reactive web apps",
5
5
  "keywords": [
6
6
  "web",