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

Package data for NPM as ESM and CJS #12169

Merged
merged 6 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/// <reference path="./types.d.ts"/>

import { CompatData } from './types';

// This is necessary to have intellisense in projects which
// import data from this package.
declare const compatData: CompatData;
export = compatData;
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
export default CompatData;
26 changes: 19 additions & 7 deletions scripts/release-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ function createDataBundle() {
}

// Returns a promise for writing the data to JSON file
async function writeData() {
async function writeData(data) {
const dest = path.resolve(directory, 'data.json');
const data = createDataBundle();
await fs.writeFile(dest, data);
}

async function writeIndex() {
const dest = path.resolve(directory, 'index.js');
async function writeIndexCJS() {
const dest = path.resolve(directory, 'index.cjs');
const content = `module.exports = require("./data.json");\n`;
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
await fs.writeFile(dest, content);
}

async function writeIndexJS(data) {
lucacasonato marked this conversation as resolved.
Show resolved Hide resolved
const dest = path.resolve(directory, 'index.js');
const content = `export default ${data};\n`;
await fs.writeFile(dest, content);
}

// Returns an array of promises for copying of all files that don't need transformation
async function copyFiles() {
for (const file of verbatimFiles) {
Expand All @@ -38,7 +43,11 @@ async function copyFiles() {

function createManifest() {
const full = require('../package.json');
const minimal = { main: 'index.js' };
const minimal = {
main: 'index.cjs',
exports: { import: './index.js', require: './index.cjs' },
queengooborg marked this conversation as resolved.
Show resolved Hide resolved
type: 'module',
};

const minimalKeys = [
'name',
Expand Down Expand Up @@ -84,9 +93,12 @@ async function main() {
// Crate a new directory
await fs.mkdir(directory);

const data = createDataBundle();

await writeManifest();
await writeData();
await writeIndex();
await writeData(data);
await writeIndexCJS();
await writeIndexJS(data);
await copyFiles();

console.log('Data bundle is ready');
Expand Down
20 changes: 15 additions & 5 deletions scripts/release-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
const assert = require('assert');
const { execSync } = require('child_process');

const prebuiltPath = '../build';
const prebuiltCjsPath = '../build';
const prebuiltJsPath = '../build/index.js';

const regular = require('..');

describe('release-build', () => {
it('pre-built bundles are identical to the source', () => {
before(() => {
execSync('npm run release-build');
const regular = require('..');
const bundled = require(prebuiltPath);
});

it('pre-built cjs bundles are identical to the source', () => {
const bundled = require(prebuiltCjsPath);
assert.deepEqual(regular, bundled);
}).timeout(5000); // Timeout must be long enough for all the file I/O;

it('pre-built esm bundles are identical to the source', async () => {
const { default: bundled } = await import(prebuiltJsPath);
assert.deepEqual(regular, bundled);
}).timeout(5000); // Timeout must be long enough for all the file I/O
}).timeout(5000); // Timeout must be long enough for all the file I/O;
});