Giter Site home page Giter Site logo

Comments (9)

taishinaritomi avatar taishinaritomi commented on July 28, 2024 2

I think this is a very wonderful approach.
I will verify it tomorrow.

from kaze-style.

taishinaritomi avatar taishinaritomi commented on July 28, 2024

Idea

from kaze-style.

Zn4rK avatar Zn4rK commented on July 28, 2024

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.

Zn4rK avatar Zn4rK commented on July 28, 2024

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.

taishinaritomi avatar taishinaritomi commented on July 28, 2024

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.

Zn4rK avatar Zn4rK commented on July 28, 2024

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.

taishinaritomi avatar taishinaritomi commented on July 28, 2024

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.

Zn4rK avatar Zn4rK commented on July 28, 2024

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.

taishinaritomi avatar taishinaritomi commented on July 28, 2024

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 photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.