Skip to content

Commit

Permalink
feat(TypeScript): remove original ts/tsx files and only leave type de…
Browse files Browse the repository at this point in the history
…finition files (#1292)

* feat(TypeScript): remove original ts/tsx files and only leave type definition files

* Omit creating index files and let tsc do the job

* Ignore DS_Store files

* Re-order and export because of tsc error

* Test for d.ts files instead of tsx

* Only copy .d.ts files

* Fixup logs

* Clean up pre-build logic

* Rename folder from prepub to prebuild

* Rename folder from release to postbuild
  • Loading branch information
tujoworker committed Feb 17, 2022
1 parent 60b2ed6 commit 79bdfb4
Show file tree
Hide file tree
Showing 84 changed files with 271 additions and 213 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"publish:dry": "yarn workspace @dnb/eufemia publish:dry",
"reset": "rm -rf ./node_modules && yarn workspace @dnb/eufemia reset && yarn workspace dnb-design-system-portal reset && yarn install",
"build": "yarn workspace @dnb/eufemia build",
"build:ci": "yarn workspace @dnb/eufemia prebuild:ci && yarn workspace @dnb/eufemia postbuild:ci && yarn workspace @dnb/eufemia publish:prepare",
"build:ci": "yarn workspace @dnb/eufemia build:ci",
"test": "yarn workspace @dnb/eufemia test",
"test:update": "yarn workspace @dnb/eufemia test:update",
"test:types": "yarn workspace @dnb/eufemia test:types",
Expand Down
2 changes: 1 addition & 1 deletion packages/dnb-design-system-portal/scripts/version.js
Expand Up @@ -8,7 +8,7 @@ const path = require('path')
const { isCI } = require('ci-info')
const {
getNextReleaseVersion,
} = require('@dnb/eufemia/scripts/release/getNextReleaseVersion')
} = require('@dnb/eufemia/scripts/postbuild/getNextReleaseVersion')

const init = async () => {
if (!isCI) {
Expand Down
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/jest.config.js
Expand Up @@ -4,7 +4,7 @@ const config = {
testRegex: '(/__tests__/\\.js|(\\.|/)(test|spec))\\.(js|jsx|ts|tsx)?$',
modulePathIgnorePatterns: [
'not_in_use',
'<rootDir>/scripts/release/*',
'<rootDir>/scripts/postbuild/*',
'<rootDir>/build/',
'<rootDir>/assets/',
'<rootDir>/stories/',
Expand Down
41 changes: 22 additions & 19 deletions packages/dnb-eufemia/package.json
Expand Up @@ -18,27 +18,30 @@
"audit:ci": "yarn audit:ci:yarn",
"audit:ci:npm": "audit-ci --config ./audit-ci.json --report-type summary",
"audit:ci:yarn": "audit-ci --config ./audit-ci.json --package-manager=yarn --report-type summary",
"audit:ci:yarn-outdated": "babel-node ./scripts/prepub/audit/toOpt && audit-ci --config ./audit-ci.json --package-manager=yarn --report-type full && babel-node ./scripts/prepub/audit/toDev",
"build": "yarn build:prebuild && yarn build:esm && yarn build:copy",
"prebuild:ci": "yarn build",
"postbuild:ci": "./scripts/release/postbuild.sh",
"build:cjs": "./scripts/release/babel-cjs.sh",
"audit:ci:yarn-outdated": "babel-node ./scripts/prebuild/audit/toOpt && audit-ci --config ./audit-ci.json --package-manager=yarn --report-type full && babel-node ./scripts/prebuild/audit/toDev",
"build": "./scripts/prebuild/local-prebuild.sh",
"prebuild:ci": "yarn build:prebuild",
"build:ci": "yarn prebuild:ci && yarn postbuild:ci && yarn publish:prepare",
"postbuild:ci": "./scripts/postbuild/postbuild.sh",
"build:cjs": "./scripts/postbuild/babel-cjs.sh",
"build:clean": "rm -rf build/**",
"build:copy": "./scripts/release/copy-build-artifacts.sh",
"build:es": "./scripts/release/babel-es.sh",
"build:esm": "./scripts/release/babel-esm.sh",
"build:copy": "./scripts/postbuild/copy-build-artifacts.sh",
"build:es": "./scripts/postbuild/babel-es.sh",
"build:esm": "./scripts/postbuild/babel-esm.sh",
"prebuild:figma:ci": "yarn figma",
"postbuild:figma:ci": "yarn test:update && yarn test:screenshots:update && babel-node ./scripts/figma/tasks/commitChanges",
"build:pack": "yarn build && yarn publish:prepare && cd ./build && yarn pack",
"build:prebuild": "babel-node ./scripts/prepub/runPrepub.js",
"build:resources": "babel-node ./scripts/prepub/resources/makeResourcesPackage.js",
"build:types": "babel-node ./scripts/prepub/generateTypes.js && yarn test:auto-generated-types",
"build:types:dev": "nodemon --exec 'babel-node ./scripts/prepub/generateTypes.js' --ext js --watch './src/**/*' --watch './scripts/**/*'",
"build:umd": "./scripts/release/babel-umd.sh",
"build:prebuild": "babel-node ./scripts/prebuild/runPrepub.js",
"build:resources": "babel-node ./scripts/prebuild/resources/makeResourcesPackage.js",
"build:types": "./scripts/prebuild/generate-types.sh",
"build:types:definitions": "yarn tsc --project tsconfig.definitions.json",
"build:types:dev": "nodemon --exec 'babel-node ./scripts/prebuild/generateTypes.js' --ext js --watch './src/**/*' --watch './scripts/**/*'",
"build:types:esm": "yarn tsc --project tsconfig.definitions.json --declarationDir ./build/esm --rootDir src",
"build:umd": "./scripts/postbuild/babel-umd.sh",
"precommit": "yarn lint-staged",
"dev:icons": "nodemon --exec 'babel-node ./scripts/tools/convertIcons' --ignore '/icons/**' --ignore '*.json'",
"dev:resources": "nodemon --exec 'babel-node ./scripts/prepub/resources/makeResourcesPackage.js' --ext js,html,json,css,scss --watch './build/style/**/*' --watch './scripts/**/*' --ignore '*.json'",
"dev:tasks": "nodemon --exec 'babel-node ./scripts/prepub/dev.js' --watch 'rollup.config.js' --ext js,html,json,css,scss --watch './src/components/**/*' --watch './src/style/**/*' --watch './scripts/**/*' --ignore '*.json'",
"dev:resources": "nodemon --exec 'babel-node ./scripts/prebuild/resources/makeResourcesPackage.js' --ext js,html,json,css,scss --watch './build/style/**/*' --watch './scripts/**/*' --ignore '*.json'",
"dev:tasks": "nodemon --exec 'babel-node ./scripts/prebuild/dev.js' --watch 'rollup.config.js' --ext js,html,json,css,scss --watch './src/components/**/*' --watch './src/style/**/*' --watch './scripts/**/*' --ignore '*.json'",
"dev:umd": "nodemon --exec 'yarn build:umd && yarn build:copy' --ext js,html,json,css,scss --watch './src/**/*' --ignore './umd/*'",
"figma": "babel-node ./scripts/figma/updateAll",
"figma:dev:commit": "nodemon --exec 'babel-node ./scripts/figma/tasks/commitChanges'",
Expand All @@ -55,15 +58,15 @@
"lint:lockfile": "yarn lockfile-lint --path ../../yarn.lock --type yarn --validate-https --allowed-hosts yarn github.com codeload.github.com",
"lint:styles": "stylelint './src/**/*.{js,scss}'",
"lint:styles:staged": "stylelint './{src,scripts}/**/*.{js,scss}'",
"make-properties": "babel-node ./scripts/prepub/makeProperties",
"publish:ci": "yarn publish:prepare && ./scripts/release/publish-release.sh",
"make-properties": "babel-node ./scripts/prebuild/makeProperties",
"publish:ci": "yarn publish:prepare && ./scripts/postbuild/publish-release.sh",
"publish:dry": "dotenv semantic-release --no-ci --dry-run",
"publish:prepare": "./scripts/release/publish-prepare.sh",
"publish:prepare": "./scripts/postbuild/publish-prepare.sh",
"reset": "rm -rf ./node_modules ./components ./elements ./es ./icons ./extensions ./shared ./style ./umd",
"skeleton:font": "nodemon --exec 'babel-node ./scripts/tools/createSkeletonFont.js'",
"start": "start-storybook -s / -p 8002 --ci --no-release-notes --no-version-updates",
"test": "jest",
"test:auto-generated-types": "TEST_POST_TYPES=1 jest --ci --rootDir ./scripts/prepub ./postTypeGeneration.test.js",
"test:auto-generated-types": "TEST_POST_TYPES=1 jest --ci --rootDir ./scripts/prebuild ./postTypeGeneration.test.js",
"test:ci": "jest --ci",
"test:screenshots": "jest --config=./jest.config.screenshots.js --forceExit --detectOpenHandles",
"test:screenshots:ci": "jest --config=./jest.config.screenshots.js --ci --forceExit --detectOpenHandles",
Expand Down
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/figma/FigmaAPI.js
Expand Up @@ -9,7 +9,7 @@ import {
extractIconsAsPDF,
} from './tasks/assetsExtractors'
import { getFigmaDoc } from './helpers/docHelpers'
import { getRequiredBranchName } from './../prepub/commitToBranch'
import { getRequiredBranchName } from './../prebuild/commitToBranch'
import { log, ErrorHandler } from '../lib'

log.start('> Figma: Preparing for connecting to the Figma API ...')
Expand Down
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/figma/tasks/commitChanges.js
Expand Up @@ -3,7 +3,7 @@
*
*/

import { commitToBranch } from '../../prepub/commitToBranch'
import { commitToBranch } from '../../prebuild/commitToBranch'

if (require.main === module) {
commitToBranch({
Expand Down
Expand Up @@ -39,6 +39,42 @@ describe('type definitions', () => {
'utf-8'
)
).toMatch(/export interface/g)

// Test the output of js files
const dtsInput = path.resolve(
packpath.self(),
`build/${stage}/components/input/Input.d.ts`
)

expect(fs.existsSync(dtsInput)).toBe(true)

const contentInput = fs.readFileSync(dtsInput, 'utf-8')
expect(contentInput).toContain(
'export interface InputProps extends React.HTMLProps<HTMLElement>'
)
}
)

it.each(buildStages)(
'has correct Breadcrumb type definitions on stage %s',
(stage) => {
// Test the output of tsx files
const tsxBreadcrumb = path.resolve(
packpath.self(),
`build/${stage}/components/breadcrumb/Breadcrumb.tsx`
)
const dtsBreadcrumb = path.resolve(
packpath.self(),
`build/${stage}/components/breadcrumb/Breadcrumb.d.ts`
)

expect(fs.existsSync(tsxBreadcrumb)).toBe(false)
expect(fs.existsSync(dtsBreadcrumb)).toBe(true)

const contentBreadcrumb = fs.readFileSync(dtsBreadcrumb, 'utf-8')
expect(contentBreadcrumb).toContain(
'export interface BreadcrumbProps'
)
}
)
})
Expand Down Expand Up @@ -191,24 +227,5 @@ describe('babel build', () => {
}
break
}

if (stage == 'cjs') {
const exists = fs.existsSync(
path.resolve(
packpath.self(),
`build/${stage}/components/breadcrumb/Breadcrumb.tsx`
)
)
expect(exists).toBe(false)
} else {
const content = fs.readFileSync(
path.resolve(
packpath.self(),
`build/${stage}/components/breadcrumb/Breadcrumb.tsx`
),
'utf-8'
)
expect(content).toContain('export interface BreadcrumbProps')
}
})
})
Expand Up @@ -19,8 +19,8 @@ echo 'Building cjs bundle done!'

echo 'Copy additional files to cjs ...'

OUT_DIR=./build/cjs ONLY_DEFINITION_FILES=1 babel-node ./scripts/release/copyTypeScriptFiles.js
OUT_DIR=./build/cjs babel-node ./scripts/release/copyStyles.js
OUT_DIR=./build/cjs babel-node ./scripts/postbuild/copyTypeScriptFiles.js
OUT_DIR=./build/cjs babel-node ./scripts/postbuild/copyStyles.js

echo 'Copy extra cjs package.json ...'

Expand Down
Expand Up @@ -19,5 +19,5 @@ echo 'Building es bundle done!'

echo 'Copy additional files to es ...'

OUT_DIR=./build/es babel-node ./scripts/release/copyTypeScriptFiles.js
OUT_DIR=./build/es babel-node ./scripts/release/copyStyles.js
OUT_DIR=./build/es babel-node ./scripts/postbuild/copyTypeScriptFiles.js
OUT_DIR=./build/es babel-node ./scripts/postbuild/copyStyles.js
Expand Up @@ -19,5 +19,5 @@ echo 'Building esm bundle done!'

echo 'Copy additional files to esm ...'

OUT_DIR=./build/esm babel-node ./scripts/release/copyTypeScriptFiles.js
OUT_DIR=./build/esm babel-node ./scripts/release/copyStyles.js
OUT_DIR=./build/esm babel-node ./scripts/postbuild/copyTypeScriptFiles.js
OUT_DIR=./build/esm babel-node ./scripts/postbuild/copyStyles.js
Expand Up @@ -2,12 +2,12 @@

set -e # Exit immediately if a command exits with a non-zero status.

echo 'Copy artifacts ...'
echo 'Copy build artifacts ...'

rm -rf build/**/{__tests__,cjs}
cp -r ./assets/ ./build/assets
cp .npmignore ./build/.npmignore
cp README README.md LICENSE ./build
babel-node ./scripts/release/copyFinaleBuild.js
babel-node ./scripts/postbuild/copyFinaleBuild.js

echo 'Copy artifacts done!'
echo 'Copy build artifacts done!'
Expand Up @@ -14,10 +14,6 @@ if (require.main === module) {
async function copyTypeScriptFiles(dist) {
const globbyFiles = ['./src/**/*.d.ts']

if (process.env.ONLY_DEFINITION_FILES !== '1') {
globbyFiles.push('./src/**/*.ts', './src/**/*.tsx')
}

const files = await globby(globbyFiles)

for await (const file of files) {
Expand Down
Expand Up @@ -4,8 +4,8 @@
*/

// When on a "release" branch:
// run: yarn nodemon --exec 'babel-node ./scripts/release/getNextReleaseVersion.js' --ext js --watch './scripts/**/*'
// run (mjs): yarn nodemon --exec 'node --experimental-import-meta-resolve ./scripts/release/getNextReleaseVersion.mjs' --ext mjs --watch './scripts/**/*'
// run: yarn nodemon --exec 'babel-node ./scripts/postbuild/getNextReleaseVersion.js' --ext js --watch './scripts/**/*'
// run (mjs): yarn nodemon --exec 'node --experimental-import-meta-resolve ./scripts/postbuild/getNextReleaseVersion.mjs' --ext mjs --watch './scripts/**/*'

const { exec } = require('child_process')
const getBranchName = require('current-git-branch')
Expand Down
Expand Up @@ -15,6 +15,6 @@ yarn prettier:other

echo 'Testing the postbuild ...'

jest --ci --rootDir ./scripts/release ./postbuild.test.js
jest --ci --rootDir ./scripts/postbuild ./postbuild.test.js

echo 'Postbuild done!'
Expand Up @@ -4,10 +4,10 @@ set -e # Exit immediately if a command exits with a non-zero status.

echo 'Prepare before publish has started ...'

babel-node ./scripts/release/prepareForRelease.js
babel-node ./scripts/postbuild/prepareForRelease.js

echo 'Testing the postbuild before publish ...'

jest --ci --rootDir ./scripts/release ./prepareForRelease.test.js
jest --ci --rootDir ./scripts/postbuild ./prepareForRelease.test.js

echo 'Prepare before publish is done!'
File renamed without changes.
11 changes: 11 additions & 0 deletions packages/dnb-eufemia/scripts/prebuild/generate-types.sh
@@ -0,0 +1,11 @@
#!/bin/bash

set -e # Exit immediately if a command exits with a non-zero status.

echo 'Generating type definitions ...'

babel-node ./scripts/prebuild/generateTypes.js
yarn build:types:definitions
yarn test:auto-generated-types

echo 'Generating type definitions done!'
Expand Up @@ -11,19 +11,19 @@ import { generateTypes } from './index'

const runGenerateTypesTasks = async ({ doRefetch } = {}) => {
process.env.NODE_ENV = 'production'
log.start('Starting to build types...', doRefetch)
log.start('Starting to generate type definitions ...', doRefetch)
try {
await generateTypes()
log.succeed('Types build has succeeded!')
log.succeed('Type definitions are successfully generated!')
} catch (e) {
log.fail(new ErrorHandler('Failed to build types!', e))
log.fail(new ErrorHandler('Failed to generate type definitions!', e))
}
return true
}

const run = async () => {
process.env.NODE_ENV = 'production'
log.start('Starting making JS properties file ...')
log.start('Make JavaScript properties file ...')

await runGenerateTypesTasks()

Expand Down
13 changes: 13 additions & 0 deletions packages/dnb-eufemia/scripts/prebuild/local-prebuild.sh
@@ -0,0 +1,13 @@
#!/bin/bash

set -e # Exit immediately if a command exits with a non-zero status.

echo 'Prebuild started ...'

yarn build:prebuild
yarn build:esm
yarn build:types:esm
yarn build:copy
rm -rf build/esm

echo 'Prebuild done!'
Expand Up @@ -6,11 +6,11 @@
import fs from 'fs-extra'
import { makeReleaseVersion } from '../makeReleaseVersion'
import * as getBranchName from 'current-git-branch'
import * as getNextReleaseVersion from '../../../release/getNextReleaseVersion'
import * as getNextReleaseVersion from '../../../postbuild/getNextReleaseVersion'

jest.mock('../../../release/getNextReleaseVersion', () => {
jest.mock('../../../postbuild/getNextReleaseVersion', () => {
return {
...jest.requireActual('../../../release/getNextReleaseVersion'),
...jest.requireActual('../../../postbuild/getNextReleaseVersion'),
getNextReleaseVersion: jest.fn().mockResolvedValue(),
}
})
Expand Down
Expand Up @@ -52,13 +52,6 @@ export default async function generateTypes({
}
}

function isIndexFile({ file, basename }) {
return (
/^index/.test(basename) ||
/src\/(components|fragments|extensions)\/[A-Z][^.]*\.js/.test(file)
)
}

export const createTypes = async (
listOfAllFiles,
{ isTest = false, ...opts } = {}
Expand Down Expand Up @@ -107,11 +100,7 @@ export const createTypes = async (
}
}

if (isIndexFile({ file, basename })) {
if (!fs.existsSync(destFile)) {
await fs.copyFile(file, destFile)
}
} else if (
if (
/^[A-Z]/.test(basename) &&
(await fileContains(file, 'propTypes'))
) {
Expand Down
Expand Up @@ -9,7 +9,7 @@ import getBranchName from 'current-git-branch'
import {
getNextReleaseVersion,
releaseBranches,
} from '../../release/getNextReleaseVersion'
} from '../../postbuild/getNextReleaseVersion'

export async function makeReleaseVersion() {
const branchName = getBranchName()
Expand Down
Expand Up @@ -277,7 +277,7 @@ const runFactory = async ({
}
processToNamesList = processToNamesList
.filter(({ file }) => {
if (/not_in_use|__tests__/g.test(file)) {
if (/not_in_use|__tests__|DS_Store/g.test(file)) {
return false
}
return !processToNamesIgnoreList.includes(file)
Expand Down
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/tools/convertIcons.js
Expand Up @@ -4,5 +4,5 @@
*
*/

import convertSvgToJsx from '../prepub/tasks/convertSvgToJsx'
import convertSvgToJsx from '../prebuild/tasks/convertSvgToJsx'
convertSvgToJsx({ preventDelete: true })
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/tools/convertWebStyles.js
Expand Up @@ -4,5 +4,5 @@
*
*/

import makeEveryComponentStyle from '../prepub/tasks/makeEveryComponentStyle'
import makeEveryComponentStyle from '../prebuild/tasks/makeEveryComponentStyle'
makeEveryComponentStyle({ preventDelete: true })

0 comments on commit 79bdfb4

Please sign in to comment.