Planning to create a repository for npm, similar to react-leaflet and react-d3, that will consist of various modules. Developers can include these modules from the npm package using require
/import
. For example:
import { ModuleOne, ModuleTwo } from 'myNpmPackage`;
The challenge is packaging CSS along with each module, compiled from Sass files in each one.
Considering a folder structure like this for myNpmPackage
:
├── src
│ ├── ModuleOne
│ │ ├── index.js
│ │ ├── style.scss
│ ├── ModuleTwo
│ │ ├── index.js
│ │ ├── style.scss
├── package.json
How can we publish these .scss
files (compiled into
.css</code) without requiring consumers to manually include / <code>@import
/ link rel="stylesheet"
the CSS?
Utilizing gulp and browserify pipeline is preferred.
UPDATE: Discovered parcelify
offers partial solution. Adding the following configuration to myNpmPackage/package.json
:
"style": "src/**/*.scss",
"transforms": [
"sass-css-stream"
]
Including parcelify
in dependencies so it's installed with myNpmPackage
.
Consumers need to update their gulpfile
with:
parcelify(b, {
bundles: {
style: './build/modules.css'
}
});
parcelify
uses the glob in myNpmPackage/package.json
to bundle all .scss
files into ./build/modules.css
.
This solution has drawbacks:
- All CSS files are included in consumer application build, even if not all modules are used;
- Requires manual addition of code to
gulpfile
by consumer developers.