Comments (9)
I think this is a very wonderful approach.
I will verify it tomorrow.
from kaze-style.
Idea
from kaze-style.
I think I understand.
You want to collect all identifiers that are using call expressions to style
and globalStyle
, make them exported, and then import them in the extraction process, instead of just evaluating the user source file like it's done today.
That's smart - and as long as we don't have to emit new file, it should work I think!
from kaze-style.
Just thinking out loud, but the transform
plugin could import a class from "@kaze-style/core" and add an export declaration;
// @kaze-style/core
export class Collector {
constructor(public calls: any[]) {}
}
// In source file after transform
import { __style, __globalStyle, Collector } from '@kaze-style/core';
const c = __style({});
const $1 = __globalStyle({});
// Can be named anything
export const collector = new Collector([c, $1]);
// In run.ts that's being evaluated, and only collects the stuff we're interested in:
import { Collector } from '@kaze-style/core';
import * as everything from './user-file.ts';
// Pseudo code - but we can use find, since we should only have one Collector
const found = Object.values(everything).find((value) => value instanceof Collector);
if (found) {
// Collect css
found.calls.map((x) => x);
}
Collector should be considered dead code in the users code, and not be a part of the finished bundle, since run.ts would be eval()-ed to collect the css.
from kaze-style.
I thought of a simple way to do this.
I have only checked with esbuild, but tree shaking also works.
// @kaze-style/core
export const styleCollector = (...x:unknown[]) => x;
// pre-transform './user-file.ts'
import { __preStyle, __preGlobalStyle, styleCollector } from '@kaze-style/core';
const classes = __preStyle({});
const __globalStyle1 = __preGlobalStyle({});
export const __style_collector = styleCollector(classes, __globalStyle1);
// run.ts
import { __style_collector } from './user-file';
((x) => x)(__style_collector);
from kaze-style.
That will probably work for all the bundlers. The reason I suggested a class is that the name we are giving the new export could in theory be occupied by user code. Then the plugins would have to create a new name, and it would be hard to communicate it back from the plugin to the style extraction and run.ts.
from kaze-style.
I tried to verify the Class syntax, but I could not tree shaking and everything was bundled.
If tree shaking is not possible, it will evaluate unnecessary code.
Is there a solution to resolve both?
from kaze-style.
Hmm, I can't think of a way right now. I'll have to do some experiments of my own. But instead of giving the style_collector a pre-determined name, we could just generate a safe name in js, and send it as arguments to the transformer.
from kaze-style.
Currently, we have the same problem with __forBuildByKazeStyle passed as the second argument of __preStyle,__preGlobalStyle in pre-transform.
from kaze-style.
Related Issues (8)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kaze-style.