|
1 | | -import path from "path/posix"; |
2 | 1 | import { getProxiedObject } from "./helper.js"; |
3 | 2 |
|
4 | 3 | const foundInLayout = (layoutClassList, className) => { |
5 | | - for (const key in layoutClassList) { |
6 | | - for (const layoutClassName in layoutClassList[key]) { |
7 | | - if (layoutClassName === className) { |
| 4 | + for (const original in layoutClassList) { |
| 5 | + for (const minified in layoutClassList[original]) { |
| 6 | + if (minified === className) { |
8 | 7 | return true; |
9 | 8 | } |
10 | 9 | } |
11 | 10 | } |
12 | 11 | return false; |
13 | 12 | }; |
14 | 13 |
|
15 | | -export const hoistClass = (classCache, hoistedClassCache) => { |
16 | | - const layoutFilename = "__layout.svelte"; |
| 14 | +const removeComponentDecl = (layoutFilename, base, declarationCache) => { |
| 15 | + if (base === layoutFilename) { |
| 16 | + return declarationCache; |
| 17 | + } |
| 18 | + |
| 19 | + return {}; |
| 20 | +}; |
17 | 21 |
|
18 | | - // no need for in loop here for filename? |
19 | | - for (const filename in classCache) { |
20 | | - const { dir, base } = path.parse(filename); |
| 22 | +const hasReachedSrc = (path) => { |
| 23 | + const dirList = path.split("/"); |
| 24 | + return dirList[dirList.length - 1] === "src"; |
| 25 | +}; |
21 | 26 |
|
22 | | - const currentComponentClassList = classCache[filename]; |
| 27 | +const goPreviousDir = (path) => { |
| 28 | + return path.substring(0, path.lastIndexOf("/")); |
| 29 | +}; |
23 | 30 |
|
24 | | - if (base === layoutFilename) { |
25 | | - hoistedClassCache[dir][base] = currentComponentClassList; |
26 | | - continue; |
| 31 | +const findClosestLayout = (dir, layoutFilename, classCache) => { |
| 32 | + if (classCache[dir][layoutFilename]) { |
| 33 | + return classCache[dir][layoutFilename]; |
| 34 | + } else { |
| 35 | + if (hasReachedSrc(dir)) { |
| 36 | + return false; |
27 | 37 | } |
| 38 | + return findClosestLayout(goPreviousDir(dir), layoutFilename, classCache); |
| 39 | + } |
| 40 | +}; |
28 | 41 |
|
29 | | - const layoutClassList = hoistedClassCache[dir][layoutFilename]; |
| 42 | +export const hoistDeclaration = ( |
| 43 | + opts, |
| 44 | + { dir: curDir, base: curBase }, |
| 45 | + classCache, |
| 46 | + declarationCache |
| 47 | +) => { |
| 48 | + if (!opts.lazyLoad) { |
| 49 | + return removeComponentDecl(opts.layoutFilename, curBase, declarationCache); |
| 50 | + } |
30 | 51 |
|
31 | | - if (!layoutClassList) { |
32 | | - hoistedClassCache[dir][base] = currentComponentClassList; |
33 | | - continue; |
34 | | - } |
| 52 | + if (curBase === opts.layoutFilename) { |
| 53 | + return declarationCache; |
| 54 | + } |
| 55 | + |
| 56 | + const currentComponentDecl = classCache[curDir][curBase]; |
| 57 | + const layoutDecl = findClosestLayout(curDir, opts.layoutFilename, classCache); |
| 58 | + |
| 59 | + // hoisting won't work without a __layout.svelte |
| 60 | + if (!layoutDecl) { |
| 61 | + return declarationCache; |
| 62 | + } |
35 | 63 |
|
36 | | - const filteredClassList = getProxiedObject(); |
| 64 | + const list = []; |
37 | 65 |
|
38 | | - for (const original in currentComponentClassList) { |
39 | | - for (const minified in currentComponentClassList[original]) { |
40 | | - if (!foundInLayout(layoutClassList, minified)) { |
41 | | - filteredClassList[original][minified] = true; |
42 | | - } |
| 66 | + for (const original in currentComponentDecl) { |
| 67 | + for (const minified in currentComponentDecl[original]) { |
| 68 | + if (!foundInLayout(layoutDecl, minified)) { |
| 69 | + list.push(minified); |
43 | 70 | } |
44 | 71 | } |
45 | | - |
46 | | - hoistedClassCache[dir][base] = filteredClassList; |
47 | 72 | } |
| 73 | + |
| 74 | + return filterDeclaration(list, declarationCache); |
48 | 75 | }; |
49 | 76 |
|
50 | | -export const hoistDeclaration = ( |
51 | | - filename, |
52 | | - hoistedClassCache, |
53 | | - declarationCache |
54 | | -) => { |
55 | | - const { dir, base } = path.parse(filename); |
56 | | - const hoistedDeclarationCache = getProxiedObject(); |
57 | | - const currentComponentCache = hoistedClassCache[dir][base]; |
58 | | - // This is hell |
59 | | - for (const original in currentComponentCache) { |
60 | | - for (const minified of Object.keys(currentComponentCache[original])) { |
61 | | - for (const mediaQuery in declarationCache) { |
62 | | - for (const decl in declarationCache[mediaQuery]) { |
63 | | - if (minified === declarationCache[mediaQuery][decl]) { |
64 | | - hoistedDeclarationCache[mediaQuery][decl] = minified; |
65 | | - } |
66 | | - } |
| 77 | +const filterDeclaration = (list, declarationCache) => { |
| 78 | + const result = getProxiedObject(); |
| 79 | + |
| 80 | + for (const mediaQuery in declarationCache) { |
| 81 | + for (const originalDecl in declarationCache[mediaQuery]) { |
| 82 | + const token = declarationCache[mediaQuery][originalDecl]; |
| 83 | + if (list.includes(token)) { |
| 84 | + result[mediaQuery][originalDecl] = token; |
67 | 85 | } |
68 | 86 | } |
69 | 87 | } |
70 | | - return hoistedDeclarationCache; |
| 88 | + |
| 89 | + return result; |
71 | 90 | }; |
0 commit comments