Skip to content
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

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

Merged
merged 10 commits into from
Feb 17, 2022
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
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
7 changes: 5 additions & 2 deletions packages/dnb-eufemia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
"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",
"build": "./scripts/prepub/prebuild.sh",
"prebuild:ci": "yarn build",
"build:ci": "yarn prebuild:ci && yarn postbuild:ci && yarn publish:prepare",
"postbuild:ci": "./scripts/release/postbuild.sh",
"build:cjs": "./scripts/release/babel-cjs.sh",
"build:clean": "rm -rf build/**",
Expand All @@ -32,8 +33,10 @@
"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": "./scripts/prepub/generate-types.sh",
"build:types:definitions": "yarn tsc --project tsconfig.definitions.json",
"build:types:dev": "nodemon --exec 'babel-node ./scripts/prepub/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/release/babel-umd.sh",
"precommit": "yarn lint-staged",
"dev:icons": "nodemon --exec 'babel-node ./scripts/tools/convertIcons' --ignore '/icons/**' --ignore '*.json'",
Expand Down
11 changes: 11 additions & 0 deletions packages/dnb-eufemia/scripts/prepub/generate-types.sh
Original file line number Diff line number Diff line change
@@ -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/prepub/generateTypes.js
yarn build:types:definitions
yarn test:auto-generated-types

echo 'Generating type definitions done!'
8 changes: 4 additions & 4 deletions packages/dnb-eufemia/scripts/prepub/generateTypes.js
Original file line number Diff line number Diff line change
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/prepub/prebuild.sh
Original file line number Diff line number Diff line change
@@ -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!'
13 changes: 1 addition & 12 deletions packages/dnb-eufemia/scripts/prepub/tasks/generateTypes.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
32 changes: 14 additions & 18 deletions packages/dnb-eufemia/scripts/release/__tests__/postbuild.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,23 +192,19 @@ 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')
}
const tsxFile = path.resolve(
packpath.self(),
`build/${stage}/components/breadcrumb/Breadcrumb.tsx`
)
const dtsFile = path.resolve(
packpath.self(),
`build/${stage}/components/breadcrumb/Breadcrumb.d.ts`
)

expect(fs.existsSync(tsxFile)).toBe(false)
expect(fs.existsSync(dtsFile)).toBe(true)

const content = fs.readFileSync(dtsFile, 'utf-8')
expect(content).toContain('export interface BreadcrumbProps')
})
})
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/release/babel-cjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ 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/copyTypeScriptFiles.js
OUT_DIR=./build/cjs babel-node ./scripts/release/copyStyles.js

echo 'Copy extra cjs package.json ...'
Expand Down
4 changes: 2 additions & 2 deletions packages/dnb-eufemia/scripts/release/copy-build-artifacts.sh
Original file line number Diff line number Diff line change
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

echo 'Copy artifacts done!'
echo 'Copy build artifacts done!'
4 changes: 0 additions & 4 deletions packages/dnb-eufemia/scripts/release/copyTypeScriptFiles.js
Original file line number Diff line number Diff line change
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
Loading