Skip to content

Commit 6b77ad1

Browse files
authored
Merge pull request #45 from openstax/fix-preload-data-paths
Fix deploy: point preload-data at the cjs build output
2 parents d363ca6 + f5e5310 commit 6b77ad1

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/orn-locator/script/preload-data.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
const fs = require('fs');
22
const path = require('path');
33
const fetch = require('node-fetch');
4-
const { getReleaseJson, bookCacheKey, getArchiveInfo } = require('../dist/resolvers/books.js')
4+
const { getReleaseJson, bookCacheKey, getArchiveInfo } = require('../dist/cjs/resolvers/books.js')
5+
6+
// shared by both builds: dist/{cjs,esm}/resolvers/books.js reads this back via
7+
// `import('../../data/' + file)`
8+
const dataDir = path.join(__dirname, '../dist/data');
59

610
const preloadData = async() => {
711
const releaseJson = await getReleaseJson();
@@ -20,12 +24,12 @@ const preloadData = async() => {
2024
})
2125
];
2226

23-
fs.mkdirSync(path.join(__dirname, '../dist/data'));
27+
fs.mkdirSync(dataDir);
2428

2529
for (const load of files) {
2630
const [fileName, data] = await load();
2731
console.log('writing ' + fileName);
28-
fs.writeFileSync(path.join(__dirname, '../dist/data', fileName), data);
32+
fs.writeFileSync(path.join(dataDir, fileName), data);
2933
}
3034
};
3135

packages/orn-locator/src/resolvers/books.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import { TitleParts, titleSplit } from '../utils/browsersafe-title-split';
1212
const oswebUrl = 'https://openstax.org/apps/cms/api/v2/pages';
1313
const fields = 'cnx_id,authors,publish_date,cover_color,amazon_link,book_state,book_subjects,book_categories,promote_image,webview_rex_link,cover_url,title_image_url';
1414

15-
const preloadedData = (file: string) => import('../data/' + file);
15+
// the extra `..` is deliberate: this path is relative to where the compiled file
16+
// runs, dist/{cjs,esm}/resolvers/, which is one level deeper than src/resolvers/.
17+
// that resolves to dist/data for both builds, so the data written by
18+
// script/preload-data.js at deploy time is shared rather than duplicated per build.
19+
const preloadedData = (file: string) => import('../../data/' + file);
1620

1721
export const getReleaseJson = memoize(async () => preloadedData('release.json').catch(() => {
1822
return fetch('https://openstax.org/rex/release.json')

0 commit comments

Comments
 (0)