Skip to content

Commit

Permalink
feat: got it working
Browse files Browse the repository at this point in the history
  • Loading branch information
amritk committed May 24, 2024
1 parent b370259 commit 8eb6290
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
3 changes: 1 addition & 2 deletions examples/react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import '@scalar/api-client/style.css'
import { ApiReferenceReact } from '@scalar/api-reference-react'
// import '@scalar/api-reference-react/style.css'
import '@scalar/api-reference-react/style.css'
import React from 'react'

function App() {
Expand Down
5 changes: 5 additions & 0 deletions packages/api-client/src/components/ApiClient/ApiClient.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ watch(
</template>
<style>
/** Used to check if css is loaded */
:root {
--scalar-loaded-api-client: true;
}
.scalar-api-client,
#headlessui-portal-root {
background: var(--scalar-background-1);
Expand Down
9 changes: 7 additions & 2 deletions packages/api-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export default defineConfig({
* We need the setTimeout so we can do a quick check to see if the css file has already been loaded
*
* @see https://stackoverflow.com/a/68954980/1624255
*
* TODO make this into a rollup/vite plugin
*
* copied from api-reference/vite.config
*/
{
name: 'autoload-css',
Expand All @@ -38,7 +42,6 @@ export default defineConfig({

const {
['style.css']: { source: css },
['index.js']: component,
} = bundle

const IIFEcss = `
Expand All @@ -48,7 +51,7 @@ export default defineConfig({
return
setTimeout(() => {
if (getComputedStyle(document.body).getPropertyValue('${STYLE_LOADED_VAR}') === 'true') return console.log('style not loaded')
if (getComputedStyle(document.body).getPropertyValue('${STYLE_LOADED_VAR}') === 'true') return
const elementStyle = document.createElement('style')
elementStyle.setAttribute('id', '${STYLE_ID}')
Expand All @@ -61,6 +64,8 @@ export default defineConfig({
}
})()`

const component =
bundle['index.js'] || bundle['index.cjs'] || bundle['index.mjs']
if ('code' in component) component.code += IIFEcss
},
},
Expand Down
2 changes: 0 additions & 2 deletions packages/api-reference-react/src/ApiReferenceReact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import '@scalar/api-reference/style.css'
import React, { useEffect, useRef } from 'react'
import { createApp } from 'vue/dist/vue.esm-bundler.js'

import './style.css'

// These are required for the vue bundler version
globalThis.__VUE_OPTIONS_API__ = true
globalThis.__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ = true
Expand Down
3 changes: 0 additions & 3 deletions packages/api-reference-react/src/style.css

This file was deleted.

6 changes: 6 additions & 0 deletions packages/api-reference/src/components/ApiReferenceLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ useDeprecationWarnings(props.configuration)
</ResetStyles>
<ScalarToasts />
</template>
<style>
/** Used to check if css is loaded */
:root {
--scalar-loaded-api-reference: true;
}
</style>
<style scoped>
/* Configurable Layout Variables */
.scalar-api-reference {
Expand Down
53 changes: 51 additions & 2 deletions packages/api-reference/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import vue from '@vitejs/plugin-vue'
import path from 'path'
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
import { defineConfig } from 'vitest/config'

import pkg from './package.json'

const STYLE_ID = 'scalar-style-api-reference'
const STYLE_LOADED_VAR = '--scalar-loaded-api-reference'

export default defineConfig({
define: {
'process.env.NODE_ENV': '"production"',
},
// plugins: [vue(), cssInjectedByJsPlugin()],
plugins: [vue()],
build: {
emptyOutDir: true,
Expand All @@ -26,6 +27,54 @@ export default defineConfig({
item.match(/^(?!@scalar\/(?!components\b)).*/),
),
],
plugins: [
/**
* Automatic css injection via js
* We need the setTimeout so we can do a quick check to see if the css file has already been loaded
*
* @see https://stackoverflow.com/a/68954980/1624255
*
* TODO make this into a rollup/vite plugin
*
* copied from client/vite.config
*/
{
name: 'autoload-css',
generateBundle(_, bundle) {
if (!('source' in bundle['style.css'])) return

const {
['style.css']: { source: css },
} = bundle

const IIFEcss = `
(function() {
try {
if (typeof document === 'undefined' || document.getElementById('${STYLE_ID}'))
return
setTimeout(() => {
if (getComputedStyle(document.body).getPropertyValue('${STYLE_LOADED_VAR}') === 'true') return
const elementStyle = document.createElement('style')
elementStyle.setAttribute('id', '${STYLE_ID}')
elementStyle.appendChild(document.createTextNode(${JSON.stringify(css)}))
document.head.appendChild(elementStyle)
console.warn('Auto-loading the css through js has been deprecated. Please import the css directly. Visit https://github.com/scalar/scalar for more info.')
}, 0)
} catch (error) {
console.error(error, 'unable to concat style inside the bundled file')
}
})()`

const component =
bundle['index.js'] || bundle['index.cjs'] || bundle['index.mjs']
if ('code' in component) component.code += IIFEcss
},
},
],
},
},
resolve: {
Expand Down

0 comments on commit 8eb6290

Please sign in to comment.