Skip to content

Commit

Permalink
Add CommonJS build for legacy tooling (jest)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-markl committed Dec 26, 2023
1 parent 68ff7bc commit 5211256
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
index.tsx
index.js
index.js.map
index.cjs
index.cjs.map
index.d.ts
index.d.ts.map
docs/demo.js
Expand Down
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@integreat-app/react-sticky-headroom",
"version": "2.0.0",
"version": "2.1.0",
"engines": {
"node": ">=18",
"npm": ">=10"
Expand All @@ -15,10 +15,17 @@
"files": [
"index.js",
"index.js.map",
"index.cjs",
"index.cjs.map",
"index.d.ts",
"index.d.ts.map",
"index.tsx"
],
"exports": {
"import": "./index.js",
"require": "./index.cjs",
"types": "./index.d.ts"
},
"publishConfig": {
"access": "public"
},
Expand Down
53 changes: 49 additions & 4 deletions tools/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ function compile (fileNames: string[], options: CompilerOptions): Record<string,
// * index.d.ts.map (the source map for index.d.ts) - using typescript
// * index.js (which strips the typescript annotations and optimizes styled-components) - using swc
// * index.js.map (the source map for index.js) - using swc
// * index.cjs (a CommonJS version of index.js for legacy tooling) - using swc
// * index.cjs.map (the source map for index.cjs) - using swc

{
// Generating index.tsx, index.d.ts, and index.d.ts.map
Expand Down Expand Up @@ -55,7 +57,10 @@ function compile (fileNames: string[], options: CompilerOptions): Record<string,
{
// Generating index.js and index.js.map
const transpiled = transformFileSync('index.tsx', {
minify: false,
minify: false,
module: {
type: 'es6'
},
jsc: {
target: 'es2022',
experimental: {
Expand All @@ -79,12 +84,52 @@ function compile (fileNames: string[], options: CompilerOptions): Record<string,
const codeWithSourceMapUrl = transpiled.code + '//# sourceMappingURL=index.js.map'

writeFileSync('./index.js', codeWithSourceMapUrl)
console.log('emitted index.dist.js')
console.log('emitted index.js')

const sourceMap = transpiled.map
if (!sourceMap) {
throw Error('Failed to generate index.js.map')
}
writeFileSync('./index.js.map', sourceMap)
console.log('emitted index.dist.js.map')
writeFileSync('./index.cjs.map', sourceMap)
console.log('emitted index.js.map')
}

{
// Generating index.cjs and index.cjs.map
const transpiled = transformFileSync('index.tsx', {
minify: false,
module: {
type: 'commonjs'
},
jsc: {
target: 'es2022',
experimental: {
plugins: [
[
'@swc/plugin-styled-components',
{
displayName: false,
ssr: false
}
]
]
}
},
sourceMaps: true
})

if (!transpiled) {
throw Error('Failed to generate index.cjs')
}
const codeWithSourceMapUrl = transpiled.code + '//# sourceMappingURL=index.cjs.map'

writeFileSync('./index.cjs', codeWithSourceMapUrl)
console.log('emitted index.cjs')

const sourceMap = transpiled.map
if (!sourceMap) {
throw Error('Failed to generate index.cjs.map')
}
writeFileSync('./index.cjs.map', sourceMap)
console.log('emitted index.cjs.map')
}

0 comments on commit 5211256

Please sign in to comment.