Skip to content

Commit

Permalink
fix: test build stage and provide cjs build with correct type in a cu…
Browse files Browse the repository at this point in the history
…stom package.json
  • Loading branch information
tujoworker committed Oct 29, 2021
1 parent 3f6741d commit 82794f7
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/dnb-eufemia/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const config = {
testRegex: '(/__tests__/\\.js|(\\.|/)(test|spec))\\.(js|jsx|ts)?$',
modulePathIgnorePatterns: [
'not_in_use',
'<rootDir>/scripts/release/__tests__/postbuild.test*',
'<rootDir>/build/',
'<rootDir>/assets/',
'<rootDir>/stories/',
Expand Down
3 changes: 2 additions & 1 deletion packages/dnb-eufemia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"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": "yarn build:types && yarn build:cjs && yarn build:es && yarn build:umd && yarn build:esm && yarn build:copy && yarn prettier:other && echo \"Can be enabled in future -> yarn build:resources\"",
"postbuild:ci": "./scripts/release/postbuild.sh",
"build:cjs": "./scripts/release/babel-cjs.sh",
"build:clean": "rm -rf build/**",
"build:copy": "./scripts/release/copy-build-artifacts.sh",
Expand Down Expand Up @@ -63,6 +63,7 @@
"skeleton:font": "nodemon --exec 'babel-node ./scripts/tools/createSkeletonFont.js'",
"start": "yarn workspace dnb-eufemia-sandbox start",
"test": "jest",
"test:build": "jest --ci --rootDir ./scripts/release",
"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
120 changes: 120 additions & 0 deletions packages/dnb-eufemia/scripts/release/__tests__/postbuild.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Test postbuild stage
* check if important files do exists
*
*/

import fs from 'fs-extra'
import path from 'path'

const buildStages = ['es', 'esm', 'cjs']

describe('type definitions', () => {
it.each(buildStages)('has d.ts index file on stage %s', (stage) => {
const file = path.resolve(`./build/${stage}/index.d.ts`)
const exists = fs.existsSync(file)
expect(exists).toBe(true)
})

it.each(buildStages)(
'has correct Modal type definitions on stage %s',
(stage) => {
expect(
fs.existsSync(
path.resolve(`./build/${stage}/components/Modal.d.ts`)
)
).toBe(true)

// To ensure babel did not compile the d.ts file
expect(
fs.readFileSync(
path.resolve(`./build/${stage}/components/modal/Modal.d.ts`),
'utf-8'
)
).toMatch(/export interface/g)
}
)
})

describe('babel build', () => {
it.each(buildStages)('has correctly compiled on stage %s', (stage) => {
expect(
fs.existsSync(path.resolve(`./build/${stage}/components/Modal.js`))
).toBe(true)

switch (stage) {
case 'cjs':
{
{
const content = fs.readFileSync(
path.resolve(`./build/${stage}/index.js`),
'utf-8'
)
expect(content).toContain(
'Object.defineProperty(exports, "__esModule", {'
)
expect(content).toContain(`var _default = {};`)

// Has extra cjs package
expect(
fs.existsSync(path.resolve(`./build/${stage}/package.json`))
).toBe(true)
}

{
const content = fs.readFileSync(
path.resolve(`./build/${stage}/components/modal/Modal.js`),
'utf-8'
)
expect(content).toContain('var Modal = function')
expect(content).toMatch(/^"use strict";/g)
}
}
break

case 'esm':
{
{
const content = fs.readFileSync(
path.resolve(`./build/${stage}/index.js`),
'utf-8'
)
expect(content).toContain('export default {};')
}

const content = fs.readFileSync(
path.resolve(`./build/${stage}/components/modal/Modal.js`),
'utf-8'
)
expect(content).toContain('export { Modal as default };')
expect(content).toContain('core-js/modules/es')
expect(content).toContain(
'import _extends from "@babel/runtime/helpers/esm/extends";'
)
}
break

case 'es':
{
{
const content = fs.readFileSync(
path.resolve(`./build/${stage}/index.js`),
'utf-8'
)
expect(content).toContain('export default {};')
}

const content = fs.readFileSync(
path.resolve(`./build/${stage}/components/modal/Modal.js`),
'utf-8'
)
expect(content).toMatch(/export default class Modal extends/g)
expect(content).not.toContain('core-js/modules/es')
expect(content).toContain(
'import _extends from "@babel/runtime/helpers/esm/extends";'
)
}
break
}
})
})
6 changes: 5 additions & 1 deletion packages/dnb-eufemia/scripts/release/babel-cjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ babel ./src \

echo 'Building cjs done!'

echo 'Copy .d.ts files ...'
echo 'Copy .d.ts files to cjs ...'

OUT_DIR=./build/cjs babel-node ./scripts/release/copyTypeScriptDefinitionFiles.js

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

cp ./src/cjs/package.json ./build/cjs/package.json
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/release/babel-es.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ babel ./src \

echo 'Building es done!'

echo 'Copy .d.ts files ...'
echo 'Copy .d.ts files to es ...'

OUT_DIR=./build/es babel-node ./scripts/release/copyTypeScriptDefinitionFiles.js
2 changes: 1 addition & 1 deletion packages/dnb-eufemia/scripts/release/babel-esm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ babel ./src \

echo 'Building esm done!'

echo 'Copy .d.ts files ...'
echo 'Copy .d.ts files to esm ...'

OUT_DIR=./build/esm babel-node ./scripts/release/copyTypeScriptDefinitionFiles.js
15 changes: 15 additions & 0 deletions packages/dnb-eufemia/scripts/release/postbuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

echo 'Postbuild started ...'

yarn build:types
yarn build:cjs
yarn build:es
yarn build:esm
yarn build:umd
echo 'Can be enabled in future if needed -> yarn build:resources'
yarn build:copy
yarn prettier:other
yarn test:build

echo 'Postbuild done!'

0 comments on commit 82794f7

Please sign in to comment.