Skip to content

Export ThemeConfig and writeDarkSwitch #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

slaweet
Copy link
Contributor

@slaweet slaweet commented Sep 29, 2020

This change allows that ThemeConfig and writeDarkSwitch can be imported in a project that uses bootstrap-darkmode by

import { ThemeConfig, writeDarkSwitch } from 'bootstrap-darkmode/theme';

I'm probably missing something, because from the README With NPM/Yarn/PNPM
section
I don't see how the import could work. The above code snippet, is how I would expect this to work.

One further step would be to rename theme.ts to index.ts, so that the import can be just [...] from 'bootstrap-darkmode'. But I didn't want to go that far, since I feel like I'm missing some context here.

I'm not familiar with how https://unpkg.com/ works, so please make sure this change doesn't break https://unpkg.com/

So that it can be imported in a project that uses bootstrap-darkmode
@Clashsoft
Copy link
Owner

I used to have export, but removed it in 54a67e3. Indeed because I couldn't figure out how to make it work with unpkg.com. Script tags + unpkg.com are the primary way of using this library anyway. Since the JavaScript doesn't do a lot of magic, you might as well just replicate its behaviour in whatever framework you are using. For Angular, there is ng-bootstrap-angular.

@slaweet
Copy link
Contributor Author

slaweet commented Sep 29, 2020

@Clashsoft Thank you for the explanation. I'll try a bit find a way to make it work in both, but I don't have too high hopes knowing you tried already 🙂

@Clashsoft
Copy link
Owner

Clashsoft commented Sep 29, 2020

I played around a bit today, and found that I could put "module": "UMD" in tsconfig.json to make it generate a UMD module. Unfortunately that is not browser compatible for some reason (microsoft/TypeScript#8436). A workaround would be two tsconfig.json, one for the browser and the other with UMD, and just call tsc twice in the build script. In addition, one would probably want a ESModule as an .mjs file...

@Clashsoft Clashsoft added the feature New feature or request label Sep 29, 2020
@Clashsoft
Copy link
Owner

I like the idea of index.ts. Maybe it could be just this:

export * from 'theme`;

But going that route, it may be desirable to put ThemeConfig in theme-config.ts and writeDarkSwitch in dark-switch.ts.

@slaweet
Copy link
Contributor Author

slaweet commented Sep 30, 2020

I played around a bit today, and found that I could put "module": "UMD" in tsconfig.json to make it generate a UMD module. Unfortunately that is not browser compatible for some reason (microsoft/TypeScript#8436). A workaround would be two tsconfig.json, one for the browser and the other with UMD, and just call tsc twice in the build script. In addition, one would probably want a ESModule as an .mjs file...

I tried different values of module tsconfig, including UMD, but none of them produces theme.js with globally defined ThemeConfig and writeDarkSwitch (if they are exported) that would work when loaded with script tag in a browser (as with unpkg). 🙁

I like the idea of index.ts. Maybe it could be just this:

export * from 'theme`;

But going that route, it may be desirable to put ThemeConfig in theme-config.ts and writeDarkSwitch in dark-switch.ts.

This would be nice, assuming there is a solution to the exports problem described above.

@Clashsoft
Copy link
Owner

Clashsoft commented Oct 2, 2020

I've been playing around with ng-packagr for bundling the JavaScript in different module formats. Here's what's in the package using that:

.
|-- CHANGELOG.md
|-- README.md
|-- bootstrap-darkmode.d.ts
|-- bootstrap-darkmode.metadata.json
|-- bundles
|   |-- bootstrap-darkmode.umd.js
|   |-- bootstrap-darkmode.umd.js.map
|   |-- bootstrap-darkmode.umd.min.js
|   `-- bootstrap-darkmode.umd.min.js.map
|-- css
|   |-- darktheme.css
|   `-- darktheme.css.map
|-- esm2015
|   |-- bootstrap-darkmode.js
|   |-- lib
|   |   |-- dark-switch.js
|   |   `-- theme-config.js
|   `-- public_api.js
|-- fesm2015
|   |-- bootstrap-darkmode.js
|   `-- bootstrap-darkmode.js.map
|-- lib
|   |-- dark-switch.d.ts
|   `-- theme-config.d.ts
|-- package.json
|-- public_api.d.ts
`-- scss
    `-- darktheme.scss

Usage in browser is then:

<script src="dist/bundles/bootstrap-darkmode.umd.min.js"></script>
<script>
	const BootstrapDarkmode = window['bootstrap-darkmode'];
	const themeConfig = new BootstrapDarkmode.ThemeConfig();
	themeConfig.initTheme();
</script>

@Clashsoft Clashsoft closed this Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants