Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Oct 25, 2022
1 parent f7ee867 commit 014ac82
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export async function handler({ files, outFile, configFile }) {

await writeFile(
file,
replaceStyleTemplates({}, file, content, changeset).code,
replaceStyleTemplates(file, content, changeset, false).code,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/inline-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ module.exports = async function loader(
await cache.set(resourcePath, styles);

const result = replaceStyleTemplates(
this,
resourcePath,
content,
changeset,
this.sourceMap,
);

cb(null, result.code, result.map as any);
Expand Down
11 changes: 7 additions & 4 deletions src/utils/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,10 @@ export function collectStyles(
}

export function replaceStyleTemplates(
loaderContext: any,
filename: string,
src: string,
locations: { start?: number; end?: number; code?: string }[],
// content: Map<string, string>,
sourceMap = true,
) {
locations = sortBy(locations, (i) => i.start || 0);

Expand All @@ -170,8 +169,12 @@ export function replaceStyleTemplates(

return {
code: magic.toString(),
map: loaderContext.sourceMap
? magic.generateMap({ includeContent: true, source: filename })
map: sourceMap
? magic.generateMap({
includeContent: true,
source: filename,
hires: true,
})
: null,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/vfs-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ module.exports = async function loader(
});

const result = replaceStyleTemplates(
this,
resourcePath,
content,
changeset,
this.sourceMap,
);

cb(null, result.code, result.map as any);
Expand Down
93 changes: 93 additions & 0 deletions src/vite-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// import { createFilter } from '@rollup/pluginutils';

// import { collectStyles, replaceStyleTemplates } from './utils/loaders';

// function astroturf({ include, exclude, ...rest } = {}) {
// const filter = createFilter(include || /\.(jsx?|tsx?)/i, exclude);
// const cssLookup = new Map();
// const pathMap = new Map();
// const sourceMetadata = new Map();

// function transform(content, filename) {
// const metadata = collectStyles(content, filename, undefined, rest);

// const { code, map } = replaceStyleTemplates(
// filename,
// content,
// metadata.changeset,
// true,
// );

// sourceMetadata.set(filename, metadata);

// metadata.styles.forEach(({ absoluteFilePath, requirePath, value }) => {
// cssLookup.set(absoluteFilePath, value);
// pathMap.set(requirePath, absoluteFilePath);
// });

// return {
// code,
// map,
// styles: metadata.styles,
// };
// }

// return {
// name: 'astroturf',

// load(id) {
// return cssLookup.get(id) || cssLookup.get(process.cwd() + id);
// },

// resolveId(importee) {
// importee = importee.split('?')[0];
// const id = cssLookup.get(importee) || pathMap.get(importee);
// return id;
// },

// async handleHotUpdate(hmr) {
// if (!filter(hmr.file)) return undefined;

// // console.log("ff", hmr, hmr.modules);
// const prev = sourceMetadata.get(hmr.file);

// if (!prev) return undefined;

// const module = hmr.modules.find((m) => m.file === hmr.file);
// const affectedModules = new Set(hmr.modules);

// const content = await hmr.read();
// const { styles } = transform(content, hmr.file);

// try {
// module.importedModules?.forEach((m) => {
// if (
// styles.find(
// (s) =>
// s.absoluteFilePath === m.file ||
// s.absoluteFilePath === process.cwd() + m.file,
// )
// ) {
// affectedModules.add(m);
// }
// });
// } catch (err) {
// console.log(err);
// }

// return [...affectedModules];
// },

// transform(content, id) {
// if (!filter(id) || !content.includes('astroturf')) {
// return undefined;
// }

// const { code, map } = transform(content, id);

// return { code, map };
// },
// };
// }

// export default astroturf;
46 changes: 21 additions & 25 deletions www/src/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 1 addition & 47 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1725,14 +1725,6 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/loader-utils@^2.0.3":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@types/loader-utils/-/loader-utils-2.0.3.tgz#fbc2337358f8f4a7dc532ac0a3646c74275edf2d"
integrity sha512-sDXXzZnTLXgdso54/iOpAFSDgqhVXabCvwGAt77Agadh/Xk0QYgOk520r3tpOouI098gyqGIFywx8Op1voc3vQ==
dependencies:
"@types/node" "*"
"@types/webpack" "^4"

"@types/lodash@^4.14.172":
version "4.14.172"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.172.tgz#aad774c28e7bfd7a67de25408e03ee5a8c3d028a"
Expand Down Expand Up @@ -1816,56 +1808,18 @@
dependencies:
schema-utils "*"

"@types/source-list-map@*":
version "0.1.2"
resolved "https://registry.yarnpkg.com/@types/source-list-map/-/source-list-map-0.1.2.tgz#0078836063ffaf17412349bba364087e0ac02ec9"
integrity sha512-K5K+yml8LTo9bWJI/rECfIPrGgxdpeNbj+d53lwN4QjW1MCwlkhUms+gtdzigTeUyBr09+u8BwOIY3MXvHdcsA==

"@types/stack-utils@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
integrity sha512-RJJrrySY7A8havqpGObOB4W92QXKJo63/jFLLgpvOtsGUqbQZ9Sbgl35KMm1DjC6j7AvmmU2bIno+3IyEaemaw==

"@types/tapable@^1":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@types/tapable/-/tapable-1.0.8.tgz#b94a4391c85666c7b73299fd3ad79d4faa435310"
integrity sha512-ipixuVrh2OdNmauvtT51o3d8z12p6LtFW9in7U79der/kwejjdNchQC5UMn5u/KxNoM7VHHOs/l8KS8uHxhODQ==

"@types/through@*":
version "0.0.30"
resolved "https://registry.yarnpkg.com/@types/through/-/through-0.0.30.tgz#e0e42ce77e897bd6aead6f6ea62aeb135b8a3895"
integrity sha512-FvnCJljyxhPM3gkRgWmxmDZyAQSiBQQWLI0A0VFL0K7W1oRUrPJSqNO0NvTnLkBcotdlp3lKvaT0JrnyRDkzOg==
dependencies:
"@types/node" "*"

"@types/uglify-js@*":
version "3.11.0"
resolved "https://registry.yarnpkg.com/@types/uglify-js/-/uglify-js-3.11.0.tgz#2868d405cc45cd9dc3069179052103032c33afbc"
integrity sha512-I0Yd8TUELTbgRHq2K65j8rnDPAzAP+DiaF/syLem7yXwYLsHZhPd+AM2iXsWmf9P2F2NlFCgl5erZPQx9IbM9Q==
dependencies:
source-map "^0.6.1"

"@types/webpack-sources@*":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/webpack-sources/-/webpack-sources-2.0.0.tgz#08216ab9be2be2e1499beaebc4d469cec81e82a7"
integrity sha512-a5kPx98CNFRKQ+wqawroFunvFqv7GHm/3KOI52NY9xWADgc8smu4R6prt4EU/M4QfVjvgBkMqU4fBhw3QfMVkg==
dependencies:
"@types/node" "*"
"@types/source-list-map" "*"
source-map "^0.7.3"

"@types/webpack@^4":
version "4.41.32"
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.41.32.tgz#a7bab03b72904070162b2f169415492209e94212"
integrity sha512-cb+0ioil/7oz5//7tZUSwbrSAN/NWHrQylz5cW8G0dWTcF/g+/dSdMlKVZspBYuMAN1+WnwHrkxiRrLcwd0Heg==
dependencies:
"@types/node" "*"
"@types/tapable" "^1"
"@types/uglify-js" "*"
"@types/webpack-sources" "*"
anymatch "^3.0.0"
source-map "^0.6.0"

"@types/webpack@^5.28.0":
version "5.28.0"
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-5.28.0.tgz#78dde06212f038d77e54116cfe69e88ae9ed2c03"
Expand Down Expand Up @@ -2312,7 +2266,7 @@ anymatch@^2.0.0:
micromatch "^3.1.4"
normalize-path "^2.1.1"

anymatch@^3.0.0, anymatch@^3.0.3, anymatch@~3.1.2:
anymatch@^3.0.3, anymatch@~3.1.2:
version "3.1.2"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.2.tgz#c0557c096af32f106198f4f4e2a383537e378716"
integrity sha512-P43ePfOAIupkguHUycrc4qJ9kz8ZiuOUijaETwX7THt0Y/GNK7v0aa8rY816xWjZ7rJdA5XdMcpVFTKMq+RvWg==
Expand Down

0 comments on commit 014ac82

Please sign in to comment.