Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Convert 6 utils to TypeScript #1023

Merged
merged 13 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ module.exports = {
'plugin:@intlify/vue-i18n/recommended',
],
// required to lint *.vue files
plugins: ['@typescript-eslint', 'vue', 'vuejs-accessibility', 'unicorn'],
plugins: [
'@typescript-eslint',
'eslint-plugin-tsdoc',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think we might want to use https://www.npmjs.com/package/eslint-plugin-jsdoc instead of a TSDoc one. We have no real idea of how long Vue files will be excluded from TS and it'd be nice to have consistency across the stack with our JSDoc comments.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's address the JSDoc changes in #1029.

'vue',
'vuejs-accessibility',
'unicorn',
],
// add your custom rules here
rules: {
semi: [2, 'never'],
Expand Down Expand Up @@ -75,6 +81,14 @@ module.exports = {
'@typescript-eslint/ban-ts-comment': ['warn'],
'@typescript-eslint/no-var-requires': ['warn'],
},
overrides: [
{
files: ['*.ts'],
rules: {
'tsdoc/syntax': 'warn',
},
},
],
settings: {
'vue-i18n': {
localeDir: './src/locales/*.{json}',
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ COPY pnpm-lock.yaml .
COPY .npmrc .

# copy the nuxt configuration file
COPY --from=builder /usr/app/nuxt.config.js .
COPY --from=builder /usr/app/nuxt.config.ts .
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱 For some reason I didn't actually think we could use TypeScript for the nuxt config until Nuxt bridge/Nuxt 3 but this is great!!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: if the previous stage is the builder and its entrypoint is npm run build, does that means that we shouldn't have any .ts file at this point?

Copy link
Contributor

@sarayourfriend sarayourfriend Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question 🤔 The Nuxt config is used both to generate the Webpack config but also to configure some runtime properties for Nuxt, so it's used both for building and for runtime. Nuxt's runtime doesn't use TypeScript though, at least not in Nuxt 2, I don't think. If this is working I'm not entire sure why unless the @nuxtjs/typescript module added support for it.

Copy link
Contributor

@rbadillap rbadillap Feb 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I understand now, I went to the docs, and npm start can differ its behavior depending the way you build the application (nuxt build).

Since the deployment target is not being specified in the config file, then the default value is server which means, we need Node.js to run the application from the generated .nuxt folder.

nuxt start - Start the production server (after running nuxt build). Use it for Node.js hosting like Heroku, Digital Ocean, etc.

That means, at this point, if we want to keep using typescript (after building the application from the previous stage) we will require ts-node. Or, my preferred option, as you said Sara, don't convert the config file so it can be read by Node.js runtime without any external dependency.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'd prefer if we kept it as TS for now until we move to Nuxt bridge. To use TS for this properly it looks like we'd have to bring in another module for it: https://typescript.nuxtjs.org/guide/runtime/

We can always add type-checking to the file even if it stays as JSDoc though; it just can't consume actual TS files (which means anything we use in the nuxt.config.js needs to stay as regular JS with JSDoc types instead of getting converted to TypeScript).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't use TS for nuxt-config, we cannot import the TypeScript versions of the utils node-env, env and sentry-config in it. I'm not entirely sure why making the nuxt-config file TS works but if I change it to JS, it can no longer import any TS files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS files can't import TS files without the JS files being compiled first. Because a .js nuxt.config doesn't get compiled, it won't be able to import TS directly I guess (because it's not importing anything from the compiled bundle).

Considering the e2e tests are passing (they run on the pnpm build && pnpm start version of the app, for @rbadillap's context) I guess it's safe.

Also don't know why it's working though (which scares me a little bit 😅)


# copy distribution directory with the static content
COPY --from=builder /usr/app/.nuxt /usr/app/.nuxt
Expand Down Expand Up @@ -106,4 +106,3 @@ EXPOSE 8443

# run the application in static mode
ENTRYPOINT ["pnpm", "run", "start"]

2 changes: 1 addition & 1 deletion nuxt-template-overrides/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Nuxt vue-app template overrides

Due to a CSS ordering bug that we haven't been able to find any other solutions for, we've had to override the Nuxt templates for the `App.js` and `index.js` files to prevent any Vue components from being imported before the static CSS assets in the `nuxt.config.js`.
Due to a CSS ordering bug that we haven't been able to find any other solutions for, we've had to override the Nuxt templates for the `App.js` and `index.js` files to prevent any Vue components from being imported before the static CSS assets in the `nuxt.config.ts`.

## Described changes

Expand Down
23 changes: 15 additions & 8 deletions nuxt.config.js → nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { NuxtConfig } from '@nuxt/types'

import pkg from './package.json'
import locales from './src/locales/scripts/valid-locales.json'

import { VIEWPORTS } from './src/constants/screens'
import { dev } from './src/utils/dev'

import { isProd } from './src/utils/node-env'
import { sentryConfig } from './src/utils/sentry-config'
import { env } from './src/utils/env'
import { sentry } from './src/utils/sentry-config'

/**
* The default metadata for the site. Can be extended and/or overwritten per page. And even in components!
Expand Down Expand Up @@ -51,6 +55,7 @@ const meta = [

if (process.env.NODE_ENV === 'production') {
meta.push({
// @ts-expect-error: 'http-equiv' isn't allowed here by Nuxt
'http-equiv': 'Content-Security-Policy',
content: 'upgrade-insecure-requests',
})
Expand Down Expand Up @@ -99,7 +104,7 @@ const head = {
],
}

export default {
const config: NuxtConfig = {
// eslint-disable-next-line no-undef
version: pkg.version, // used to purge cache :)
cache: {
Expand All @@ -116,9 +121,9 @@ export default {
router: {
middleware: 'middleware',
},
components: {
dirs: [{ path: '~/components', extensions: ['vue'], pathPrefix: false }],
},
components: [
{ path: '~/components', extensions: ['vue'], pathPrefix: false },
],
plugins: [
{ src: '~/plugins/url-change.js' },
{ src: '~/plugins/migration-notice.js' },
Expand All @@ -134,7 +139,7 @@ export default {
],
head,
env,
dev,
dev: !isProd,
buildModules: [
'@nuxt/typescript-build',
'@nuxtjs/composition-api/module',
Expand Down Expand Up @@ -195,7 +200,7 @@ export default {
* {@link https://github.com/nuxt-community/redirect-module#usage}
*/
redirect: [{ from: '^/photos/(.*)$', to: '/image/$1', statusCode: 301 }],
sentry,
sentry: sentryConfig,
build: {
templates: [
{
Expand Down Expand Up @@ -245,3 +250,5 @@ export default {
},
},
}

export default config
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"e2e:ci": "start-server-and-test run-server http://localhost:8443 test:e2e",
"generate-e2e-tests": "playwright codegen localhost:8443/",
"types": "tsc --noEmit",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore --ignore-path .eslintignore .",
"lintfix": "eslint --fix --ext .js,.vue --ignore-path .gitignore --ignore-path .eslintignore .",
"lint": "eslint --ext .js,.ts,.vue --ignore-path .gitignore --ignore-path .eslintignore .",
"lintfix": "eslint --fix --ext .js,.ts,.vue --ignore-path .gitignore --ignore-path .eslintignore .",
"format": "prettier --write .",
"format:test": "prettier --check .",
"vue-i18n-check": "vue-i18n-extract report --vueFiles './src/**/*.?(js|vue)' --languageFiles './src/locales/*.?(json)'",
Expand Down Expand Up @@ -108,6 +108,7 @@
"eslint-config-prettier": "^8.1.0",
"eslint-loader": "^4.0.2",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-tsdoc": "^0.2.14",
"eslint-plugin-unicorn": "^35.0.0",
"eslint-plugin-vue": "^7.8.0",
"eslint-plugin-vuejs-accessibility": "^0.6.1",
Expand Down
33 changes: 33 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/components/SketchFabViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<script>
import loadScript from '~/utils/load-script'
import { log } from '~/utils/console'

const sketchfabUrl =
'https://static.sketchfab.com/api/sketchfab-viewer-1.10.1.js'
Expand All @@ -34,11 +35,11 @@ export default {
success: (api) => {
api.addEventListener('viewerready', () => {
this.loading = false
console.log('LOADED!')
log('LOADED!')
})
},
error: () => {
console.log('Viewer error')
log('Viewer error')
},
},
loading: true,
Expand Down
2 changes: 1 addition & 1 deletion src/components/VButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
computed,
} from '@nuxtjs/composition-api'
import VLink from '~/components/VLink.vue'
import { warn } from '@/utils/warn'
import { warn } from '~/utils/console'

/**
* A button component that behaves just like a regular HTML `button` element
Expand Down
4 changes: 3 additions & 1 deletion src/components/VIcon/VIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
</template>

<script>
import { isTest } from '~/utils/node-env'

/**
* Displays the given icon in a 24px × 24px square.
*/
Expand All @@ -33,7 +35,7 @@ export default {
/**
* In `jest` our icons get transformed to Vue components
*/
type: process.env.NODE_ENV === 'test' ? Object : String,
type: isTest ? Object : String,
Comment on lines -36 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to check that dead code elimination still works when you don't inline the NODE_ENV check. I don't think we have environment based dead code currently but it would be good to add that. We might end up having to revert these checks back to the inline but I'm not sure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll make a new issue for this, it seems that this should work because it'll evaluate to

if (false) {
  // ...
}

in the compiled JS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #1030.

required: true,
},
/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/VItemGroup/VItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import {
VItemGroupFocusContextKey,
} from './VItemGroup.vue'
import { VPopoverContentContextKey } from '~/components/VPopover/VPopoverContent.vue'
import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'

export default defineComponent({
name: 'VItem',
Expand Down
2 changes: 1 addition & 1 deletion src/components/VModal/VMobileModalContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import { defineComponent, toRefs, ref } from '@nuxtjs/composition-api'
import { FocusTrap } from 'focus-trap-vue'
import { useDialogContent } from '~/composables/use-dialog-content'
import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'

import closeIcon from '~/assets/icons/close.svg'

Expand Down
2 changes: 1 addition & 1 deletion src/components/VModal/VModalContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { defineComponent, toRefs, ref } from '@nuxtjs/composition-api'
import { FocusTrap } from 'focus-trap-vue'
import VTeleport from '~/components/VTeleport/VTeleport'
import { useDialogContent } from '~/composables/use-dialog-content'
import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'
import VButton from '~/components/VButton.vue'
import VIcon from '~/components/VIcon/VIcon.vue'
import closeIcon from '~/assets/icons/close.svg'
Expand Down
2 changes: 1 addition & 1 deletion src/components/VPopover/VPopoverContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<script>
import { defineComponent, toRefs, ref, provide } from '@nuxtjs/composition-api'
import { usePopoverContent } from '~/composables/use-popover-content'
import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'

import { propTypes } from './VPopoverContent.types'

Expand Down
6 changes: 4 additions & 2 deletions src/components/VPopover/meta/VPopover.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import VPopover from '~/components/VPopover/VPopover.vue'
import VButton from '~/components/VButton'
import { placements as popoverPlacements } from '@popperjs/core'

import { log } from '~/utils/console'

export default {
component: VPopover,
title: 'Components/VPopover',
Expand Down Expand Up @@ -37,8 +39,8 @@ const SinglePopoverStory = (args, { argTypes }) => ({
`,
components: { VPopover, VButton },
setup() {
const onOpen = () => console.log('opened!')
const onClose = () => console.log('closed!')
const onOpen = () => log('opened!')
const onClose = () => log('closed!')

return { onOpen, onClose }
},
Expand Down
2 changes: 1 addition & 1 deletion src/components/VTeleport/VTeleportTarget.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { defineComponent } from '@nuxtjs/composition-api'
import { targets } from './meta/targets'
import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'

export default defineComponent({
name: 'VTeleportTarget',
Expand Down
6 changes: 4 additions & 2 deletions src/components/meta/DropdownButton.stories.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import DropdownButton from '~/components/DropdownButton'

import { log } from '~/utils/console'

export default {
title: 'Components/DropdownButton',
component: DropdownButton,
Expand Down Expand Up @@ -41,13 +43,13 @@ export const Default = (args, { argTypes }) => ({
},
computed: {
activeItem() {
console.log(this)
log(this)
return this.items.find((item) => item.active)
},
},
methods: {
onClick(event) {
console.log(event)
log(event)
alert('clicked!')
},
setActive(toActivate) {
Expand Down
2 changes: 1 addition & 1 deletion src/composables/use-focus-on-show.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getFirstTabbableIn } from 'reakit-utils/tabbable'
import { hasFocusWithin } from 'reakit-utils/hasFocusWithin'
import { ensureFocus } from 'reakit-utils/ensureFocus'

import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'

export const noFocusableElementWarning =
"It's recommended to have at least one tabbable element inside dialog. The dialog element has been automatically focused. If this is the intended behavior, pass `tabIndex={0}` to the dialog element to disable this warning."
Expand Down
2 changes: 1 addition & 1 deletion src/data/api-service.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { warn } from '~/utils/warn'
import { warn } from '~/utils/console'
import { AUDIO, IMAGE } from '~/constants/media'

const DEFAULT_REQUEST_TIMEOUT = 30000
Expand Down
4 changes: 2 additions & 2 deletions src/locales/scripts/get-validated-locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const { addFetchedTranslationStatus } = require('./get-translations-status')
/**
* Returns a list of locale objects with at least one translated string
* @returns {{
* translated: import('./types').i18nLocaleProps[],
* untranslated: import('./types').i18nLocaleProps[]
* translated: import('./types').I18nLocaleProps[],
* untranslated: import('./types').I18nLocaleProps[]
* }}
*/
const getValidatedLocales = async () => {
Expand Down
12 changes: 9 additions & 3 deletions src/locales/scripts/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* @typedef {{ code: string, wpLocale?: string, file?: string, iso?: string, name: string, dir?: string, translated?: number }} i18nLocaleProps
*/
export interface I18nLocaleProps {
code: string
name: string
wpLocale?: string
file?: string
iso?: string
dir?: string
translated?: number
}
Loading