-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
124 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,8 @@ | ||
"use strict"; | ||
const { processCss } = require("../../dist/index.cjs"); | ||
|
||
// This is a custom Jest transformer turning style imports into empty objects. | ||
// http://facebook.github.io/jest/docs/en/webpack.html | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
module.exports = { | ||
process(src, filename) { | ||
// NOTE: Jest only run process once | ||
// console.log("cssTransform"); | ||
// console.log("src", src); | ||
// console.log("filename", filename); | ||
if (!fs.existsSync("./node_modules/.cache/jest-preview-dom")) { | ||
fs.mkdirSync("./node_modules/.cache/jest-preview-dom", { | ||
recursive: true, | ||
}); | ||
} | ||
fs.writeFileSync( | ||
`./node_modules/.cache/jest-preview-dom/${path.basename(filename)}`, | ||
src, | ||
{ | ||
flag: "w", | ||
} | ||
); | ||
return "module.exports = {};"; | ||
return processCss(src, filename); | ||
}, | ||
// TODO: MEDIUM PRIORITY To research about getCacheKey | ||
// Reference: | ||
// - https://jestjs.io/docs/code-transformation#writing-custom-transformers | ||
// - https://github.com/swc-project/jest/blob/17cf883b46c050a485975d8ce96a05277cf6032f/index.ts#L37-L52 | ||
// getCacheKey(src, filename) { | ||
// // The output is always the same. | ||
// return filename; | ||
// }, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,9 @@ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
const fs = require("fs"); | ||
const { processFile } = require("../../dist/index.cjs"); | ||
|
||
// This is a custom Jest transformer turning file imports into filenames. | ||
// http://facebook.github.io/jest/docs/en/webpack.html | ||
const { writeFileSync } = require("fs"); | ||
module.exports = { | ||
// TODO1: We can support image import by convert image to base 64 | ||
// or just copy image to preview folder | ||
// Currently, we copy to preview folder. But convert to base64 might be a better idea | ||
// Since we can avoid the File I/O operations and keep images in the memory | ||
// To experiment about this idea but low priority. First, make it work. | ||
|
||
// TODO2: HIGH PRIORITY What about files that are not images? e.g. pdf, doc, mp3? | ||
// I suppose it's OK. Because as I recalled, webpack still convert pdf, doc, mp3 => link (file-loader?) | ||
process(src, filename) { | ||
const assetFilename = JSON.stringify(path.basename(filename)); | ||
// console.log("fileTransform"); | ||
// console.log("src", src); | ||
// console.log("filename", filename); | ||
// TODO: MEDIUM PRIORITY: For PoC, we only copy image file to preview folder and keep the BASENAME. | ||
// That means `assets/images/abc.png` will be copied to `./node_modules/.cache/jest-preview-dom/abc.png` | ||
// We should keep the structure. i.e: `./node_modules/.cache/jest-preview-dom/assets/images/abc.png` | ||
// The reason we need to keep the structure since there might be 2 images with the same name in 2 different folders. | ||
// E.g: `assets/images/abc.png` vs `demo/abc.png` => only 1 `abc.png` | ||
// However, we might not need to make it nested if we using hashing to differentiate. | ||
// Hash might be a better solution for 0.0.1 | ||
// Same for cssTransform | ||
// assets/images/abc.png => fdsfs343r3.png | ||
// demo/abc.png => dfdfsgs.png | ||
if (!fs.existsSync("./node_modules/.cache/jest-preview-dom")) { | ||
fs.mkdirSync("./node_modules/.cache/jest-preview-dom", { | ||
recursive: true, | ||
}); | ||
} | ||
fs.writeFileSync( | ||
`./node_modules/.cache/jest-preview-dom/${path.basename(filename)}`, | ||
src, | ||
{ | ||
flag: "w", | ||
} | ||
); | ||
|
||
return `module.exports = ${assetFilename};`; | ||
return processFile(src, filename); | ||
}, | ||
}; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/src/favicon.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/demo/favicon.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Vite App</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/demo/main.tsx"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// import fs from "fs"; | ||
// import path from "path"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
export function processCss(src: string, filename: string): string { | ||
// NOTE: Jest only run process once | ||
// console.log("cssTransform"); | ||
// console.log("src", src); | ||
// console.log("filename", filename); | ||
if (!fs.existsSync("./node_modules/.cache/jest-preview-dom")) { | ||
fs.mkdirSync("./node_modules/.cache/jest-preview-dom", { | ||
recursive: true, | ||
}); | ||
} | ||
fs.writeFileSync( | ||
`./node_modules/.cache/jest-preview-dom/${path.basename(filename)}`, | ||
src, | ||
{ | ||
flag: "w", | ||
} | ||
); | ||
return "module.exports = {};"; | ||
} | ||
|
||
// TODO: MEDIUM PRIORITY To research about getCacheKey | ||
// Reference: | ||
// - https://jestjs.io/docs/code-transformation#writing-custom-transformers | ||
// - https://github.com/swc-project/jest/blob/17cf883b46c050a485975d8ce96a05277cf6032f/index.ts#L37-L52 | ||
// getCacheKey(src, filename) { | ||
// // The output is always the same. | ||
// return filename; | ||
// }, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// import fs from "fs"; | ||
// import path from "path"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
// TODO1: We can support image import by convert image to base 64 | ||
// or just copy image to preview folder | ||
// Currently, we copy to preview folder. But convert to base64 might be a better idea | ||
// Since we can avoid the File I/O operations and keep images in the memory | ||
// To experiment about this idea but low priority. First, make it work. | ||
|
||
// TODO2: HIGH PRIORITY What about files that are not images? e.g. pdf, doc, mp3? | ||
// I suppose it's OK. Because as I recalled, webpack still convert pdf, doc, mp3 => link (file-loader?) | ||
export function processFile(src: string, filename: string): string { | ||
const assetFilename = JSON.stringify(path.basename(filename)); | ||
// console.log("fileTransform"); | ||
// console.log("src", src); | ||
// console.log("filename", filename); | ||
// TODO: MEDIUM PRIORITY: For PoC, we only copy image file to preview folder and keep the BASENAME. | ||
// That means `assets/images/abc.png` will be copied to `./node_modules/.cache/jest-preview-dom/abc.png` | ||
// We should keep the structure. i.e: `./node_modules/.cache/jest-preview-dom/assets/images/abc.png` | ||
// The reason we need to keep the structure since there might be 2 images with the same name in 2 different folders. | ||
// E.g: `assets/images/abc.png` vs `demo/abc.png` => only 1 `abc.png` | ||
// However, we might not need to make it nested if we using hashing to differentiate. | ||
// Hash might be a better solution for 0.0.1 | ||
// Same for cssTransform | ||
// assets/images/abc.png => fdsfs343r3.png | ||
// demo/abc.png => dfdfsgs.png | ||
if (!fs.existsSync("./node_modules/.cache/jest-preview-dom")) { | ||
fs.mkdirSync("./node_modules/.cache/jest-preview-dom", { | ||
recursive: true, | ||
}); | ||
} | ||
fs.writeFileSync( | ||
`./node_modules/.cache/jest-preview-dom/${path.basename(filename)}`, | ||
src, | ||
{ | ||
flag: "w", | ||
} | ||
); | ||
|
||
return `module.exports = ${assetFilename};`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { processCss } from "./cssTransform"; | ||
import { processFile } from "./fileTransform"; | ||
|
||
export { processCss, processFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig", | ||
"exclude": ["./src/__tests__/**", "./src/__mocks__/**", "./src/test-utils"] | ||
"exclude": ["./demo/__tests__/**", "./demo/__mocks__/**", "./demo/test-utils"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters