0

I want to create a UI library and publish it to NPM. I don't like however the idea of bundling the code into single file and adding bundler code around my code, which is what all bundlers do by default (well, bundler must bundle I guess).

I'm using TypeScript and I build my non-ui packages with tsc. I want to keep that and let bundler or other tool just run through the result and change few things.

For example let's take this file:

import React from "react";

import styles from "./b.module.scss";

const B = () => {
    return (
        <div className={styles.index}>index</div>
    );
};

export { B };

It's getting compiled into this ESM code:

import React from "react";
import styles from "./b.module.scss";
const B = () => {
    return (React.createElement("div", { className: styles.index }, "index"));
};
export { B };
//# sourceMappingURL=b.js.map

Not much changes, but this is what I want at this step.

Now I want the tool to:

  • see the module.scss import
  • build b.css file, replacing class names with mangled ones
  • replace import styles from "./b.module.scss"; with import "./b.css";
  • replace { className: styles.index } with something like: { className: "b_index_f9cb22" } (that reflect new class name in b.css)

and that's basically all

I guess those are the steps normally taken by webpack plugins, but webpack does more than that, like I said - adding its own code, bundling things together, minimizing the code, etc. Some of these things I seem to be able to skip, but still the output is far from my original one.

Is there a tool that allows me to do what I want?

dzek
  • 203
  • 2
  • 8
  • What's the webpack config you configured? Would you be able to post that here? – PsyGik Jul 15 '21 at 08:39
  • I don't have it. I tried experimenting with webpack, but it was just "not it" and I removed it. I think webpack is a bundler in its nature and will not fit for the job, but I may be of course wrong. – dzek Jul 15 '21 at 08:49
  • So if you don't need a bundler you just want a taskrunner. There are several: you can just use npm as your taskrunner, or you can use gulp, or you can use grunt. This [question](https://stackoverflow.com/questions/35062852/npm-vs-bower-vs-browserify-vs-gulp-vs-grunt-vs-webpack) might contain what you need. – Martin Jul 15 '21 at 11:16

0 Answers0