Skip to content

Commit e680dd5

Browse files
committed
build: adjust for new structure of contentful-sdk-core
1 parent 2ca610e commit e680dd5

File tree

5 files changed

+69
-34
lines changed

5 files changed

+69
-34
lines changed

.eslintrc.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module.exports = {
33
plugins: ['standard', 'promise'],
44
env: {
55
node: true,
6-
'vitest-globals/env': true,
76
},
87
globals: {
98
__VERSION__: true,

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"exports": {
88
".": {
99
"import": "./dist/esm/index.js",
10-
"require": "./dist/contentful.cjs.cjs"
10+
"require": "./dist/contentful.cjs"
1111
}
1212
},
1313
"browser": "./dist/contentful.browser.min.js",
@@ -56,7 +56,7 @@
5656
"check": "npm run check:cjs && npm run check:esm && npm run check:browser && npm run check:browser-min",
5757
"check:browser": "es-check es2018 ./dist/contentful.browser.js",
5858
"check:browser-min": "es-check es2018 ./dist/contentful.browser.min.js",
59-
"check:cjs": "es-check es2017 './dist/contentful.cjs.cjs'",
59+
"check:cjs": "es-check es2017 './dist/contentful.cjs'",
6060
"check:esm": "es-check es2017 --module './dist/esm/*.js'",
6161
"docs:build": "typedoc --options typedoc.json",
6262
"docs:watch": "npm run docs:build -- --watch",
@@ -68,7 +68,8 @@
6868
"test": "npm run test:unit && npm run test:integration && npm run lint && npm run test:types",
6969
"test:unit": "vitest --run ./test/unit",
7070
"test:unit:watch": "vitest ./test/unit",
71-
"test:integration": "vitest ./test/integration",
71+
"test:integration": "vitest --run ./test/integration",
72+
"test:integration:watch": "vitest ./test/integration",
7273
"test:size": "bundlesize",
7374
"test:demo-projects": "npm run test:demo-node && npm run test:demo-browser",
7475
"test:demo-node": "rimraf ./test/output-integration/node/node-modules && npm --prefix ./test/output-integration/node ci && vitest --run ./test/output-integration/node",
@@ -92,6 +93,7 @@
9293
"@babel/preset-env": "^7.24.3",
9394
"@optimize-lodash/rollup-plugin": "^4.0.4",
9495
"@rollup/plugin-alias": "^5.1.0",
96+
"@rollup/plugin-babel": "^6.0.4",
9597
"@rollup/plugin-commonjs": "^25.0.7",
9698
"@rollup/plugin-json": "^6.1.0",
9799
"@rollup/plugin-node-resolve": "^15.2.3",
@@ -139,11 +141,11 @@
139141
"bundlesize": [
140142
{
141143
"path": "./dist/contentful.browser.js",
142-
"maxSize": "60Kb"
144+
"maxSize": "70Kb"
143145
},
144146
{
145147
"path": "./dist/contentful.browser.min.js",
146-
"maxSize": "31Kb"
148+
"maxSize": "36Kb"
147149
}
148150
],
149151
"release": {
@@ -177,4 +179,4 @@
177179
"overrides": {
178180
"fast-copy": "^3.0.2"
179181
}
180-
}
182+
}

rollup.config.js

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ import terser from '@rollup/plugin-terser'
1111
import replace from '@rollup/plugin-replace'
1212
import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin'
1313
import { visualizer } from 'rollup-plugin-visualizer'
14+
import { babel } from '@rollup/plugin-babel'
1415

1516
const __dirname = dirname(fileURLToPath(import.meta.url))
1617

1718
const baseConfig = {
1819
input: 'dist/esm/index.js',
1920
output: {
20-
file: 'dist/contentful.cjs.cjs',
21+
file: 'dist/contentful.cjs',
2122
format: 'cjs',
2223
},
2324
plugins: [
25+
optimizeLodashImports(),
2426
replace({
2527
preventAssignment: true,
2628
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
@@ -33,12 +35,7 @@ const baseConfig = {
3335
ignoreDynamicRequires: true,
3436
requireReturnsDefault: 'auto',
3537
}),
36-
nodeResolve({
37-
preferBuiltins: false,
38-
browser: true,
39-
}),
4038
json(),
41-
optimizeLodashImports(),
4239
],
4340
}
4441

@@ -47,7 +44,33 @@ const cjsConfig = {
4744
output: {
4845
...baseConfig.output,
4946
},
50-
external: ['axios', '@contentful/rich-text-types', 'json-stringify-safe'],
47+
plugins: [
48+
...baseConfig.plugins,
49+
nodeResolve({
50+
preferBuiltins: true,
51+
browser: false,
52+
}),
53+
babel({
54+
babelHelpers: 'bundled',
55+
// exclude: 'node_modules/**',
56+
presets: [
57+
[
58+
'@babel/preset-env',
59+
// Please note: This is set to Node.js v8 in order to satisfy ECMA2017 requirements
60+
// However, we cannot ensure it will operate without issues on Node.js v8
61+
{ targets: { node: 8 }, modules: false, bugfixes: true },
62+
],
63+
],
64+
}),
65+
alias({
66+
entries: [
67+
{
68+
find: 'axios',
69+
replacement: resolve(__dirname, './node_modules/axios/dist/node/axios.cjs'),
70+
},
71+
],
72+
}),
73+
],
5174
}
5275

5376
const browserConfig = {
@@ -56,9 +79,12 @@ const browserConfig = {
5679
file: 'dist/contentful.browser.js',
5780
format: 'iife',
5881
name: 'contentful',
59-
// intro: 'const global = window;',
6082
},
6183
plugins: [
84+
nodeResolve({
85+
preferBuiltins: false,
86+
browser: true,
87+
}),
6288
alias({
6389
entries: [
6490
{
@@ -72,6 +98,20 @@ const browserConfig = {
7298
],
7399
}),
74100
...baseConfig.plugins,
101+
babel({
102+
babelHelpers: 'bundled',
103+
exclude: 'node_modules/**',
104+
presets: [
105+
[
106+
'@babel/preset-env',
107+
{
108+
targets: pkg.browserslist,
109+
modules: false,
110+
bugfixes: true,
111+
},
112+
],
113+
],
114+
}),
75115
],
76116
}
77117

@@ -84,9 +124,14 @@ const browserMinConfig = {
84124
plugins: [
85125
...browserConfig.plugins,
86126
terser({
127+
// sourceMap: {
128+
// content: 'inline',
129+
// includeSources: true,
130+
// url: 'inline'
131+
// },
87132
compress: {
88133
passes: 5,
89-
ecma: 2017,
134+
ecma: 2018,
90135
drop_console: true,
91136
drop_debugger: true,
92137
sequences: true,
@@ -105,23 +150,14 @@ const browserMinConfig = {
105150
keep_fargs: false,
106151
keep_infinity: false,
107152
},
108-
mangle: {
109-
reserved: ['contentful'],
110-
toplevel: true,
111-
module: true,
112-
properties: {
113-
reserved: ['contentful'],
114-
regex: /^_/, // Only mangle properties that start with an underscore
115-
},
116-
},
117153
format: {
118154
comments: false, // Remove all comments
119155
beautify: false, // Minify output
120156
},
121157
}),
122158
visualizer({
123159
emitFile: true,
124-
filename: 'stats.browser.min.html',
160+
filename: 'stats-browser-min.html',
125161
}),
126162
],
127163
}

test/unit/contentful.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
import { MockedFunction, vi } from 'vitest'
1+
import { vi, test, expect, describe, MockedFunction, beforeEach, afterEach } from 'vitest'
22
import { createClient } from '../../lib/contentful'
3-
import * as SdkCore from 'contentful-sdk-core'
3+
import { default as createHttpClient } from 'contentful-sdk-core/dist/create-http-client'
44
import * as CreateContentfulApi from '../../lib/create-contentful-api'
55

66
// eslint-disable-next-line @typescript-eslint/no-var-requires
77
const version = require('../../package.json').version
88

99
vi.mock('../../lib/create-contentful-api')
10+
vi.mock('contentful-sdk-core/dist/create-http-client')
1011

11-
// @ts-ignore
12-
SdkCore.createHttpClient = vi.fn()
13-
const createHttpClientMock = <MockedFunction<typeof SdkCore.createHttpClient>>(
14-
(<unknown>SdkCore.createHttpClient)
15-
)
12+
// @todo this is broken now - proper mocks
13+
const createHttpClientMock = <MockedFunction<typeof createHttpClient>>(<unknown>createHttpClient)
1614
const createContentfulApiMock = <MockedFunction<typeof CreateContentfulApi.default>>(
1715
(<unknown>CreateContentfulApi.default)
1816
)
@@ -58,7 +56,6 @@ describe('contentful', () => {
5856
expect(createHttpClientMock).toHaveBeenCalledTimes(1)
5957

6058
const callConfig = createHttpClientMock.mock.calls[0][1]
61-
6259
expect(callConfig.headers['Content-Type']).toBeDefined()
6360
expect(callConfig.headers['X-Contentful-User-Agent']).toBeDefined()
6461

0 commit comments

Comments
 (0)