Skip to content

Commit

Permalink
Change: use pure esm - also for json modules
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianKoerner committed May 11, 2024
1 parent e183d8e commit 52e9c89
Show file tree
Hide file tree
Showing 26 changed files with 98 additions and 83 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/@dicebear/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"prebuild": "del-cli lib",
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "node --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
"test": "node --no-warnings --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --no-warnings --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
},
"dependencies": {
"superstruct": "^1.0.4"
Expand Down
1 change: 1 addition & 0 deletions packages/@dicebear/collection/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"devDependencies": {
"@dicebear/core": "^9.0.0-alpha.1",
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@types/fs-extra": "^11.0.4",
"change-case": "^5.4.3",
Expand Down
9 changes: 1 addition & 8 deletions packages/@dicebear/collection/scripts/build-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,10 @@ for (const definitionFile of definitionFileList) {
// Index file
indexContent += `export * from './styles/${className}.js';\n`;

// Definition loader
const definitionLoaderTarget = join(targetPath, 'loader', `${styleName}.cjs`);
const definitionLoaderContent = `module.exports = require('@dicebear/definitions/${definitionFile}');`;

await ensureDir(dirname(definitionLoaderTarget));
await writeFile(definitionLoaderTarget, definitionLoaderContent);

// Style class
const styleClassTarget = join(targetPath, 'styles', `${className}.js`);
const styleClassContent = `import { Style } from '@dicebear/core';
import definition from '../loader/${styleName}.cjs';
import definition from '@dicebear/definitions/src/${styleName}.json' with { type: 'json' };
export class ${className} extends Style {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAvatar } from '@dicebear/core';
import { identicon } from '../../../lib/index.js';
import { Identicon } from '../../../lib/index.js';

document.body.innerHTML = createAvatar(identicon, { seed: 'John Doe' });
document.body.innerHTML = createAvatar(Identicon, { seed: 'John Doe' });
34 changes: 18 additions & 16 deletions packages/@dicebear/collection/tests/tree-shaking.rollup.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';

import { fileURLToPath } from 'url';
import { test } from 'uvu';
import { equal, match } from 'uvu/assert';
import { rollup } from 'rollup';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

test(`Tree shaking with rollup`, async () => {
const bundle = await rollup({
input: `${__dirname}/fixtures/tree-shaking/index.js`,
plugins: [nodeResolve(), commonjs()],
});
describe('rollup', () => {
it('Tree shaking', async () => {
const bundle = await rollup({
input: `${__dirname}/fixtures/tree-shaking/index.js`,
plugins: [nodeResolve(), commonjs(), json()],
});

const { output } = await bundle.generate({
format: 'esm',
});
const { output } = await bundle.generate({
format: 'esm',
});

equal(output.length, 1);
assert.strictEqual(output.length, 1);

for (const module in output[0].modules) {
match(module, /@dicebear\/(core|converter|identicon|collection)/);
}
for (const module in output[0].modules) {
assert.doesNotMatch(module, /Adventurer/i);
}
});
});

test.run();
59 changes: 30 additions & 29 deletions packages/@dicebear/collection/tests/tree-shaking.webpack.test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';

import { fileURLToPath } from 'url';
import { test } from 'uvu';
import { match } from 'uvu/assert';
import webpack from 'webpack';
import { dirSync } from 'tmp';

const __dirname = fileURLToPath(new URL('.', import.meta.url));

test(`Tree shaking with webpack`, async () => {
const tmpDir = dirSync();
describe('webpack', () => {
it('Tree shaking', async () => {
const tmpDir = dirSync();

const modules = await new Promise((resolve, reject) => {
webpack(
{
entry: `${__dirname}/fixtures/tree-shaking/index.js`,
mode: 'production',
output: {
path: tmpDir.name,
const modules = await new Promise((resolve, reject) => {
webpack(
{
entry: `${__dirname}/fixtures/tree-shaking/index.js`,
mode: 'production',
output: {
path: tmpDir.name,
},
},
},
(err, stats) => {
err
? reject(err)
: resolve(
stats
.toJson()
.modules.filter((v) => v.usedExports)
.map((v) => v.identifier),
);
},
);
});
(err, stats) => {
err
? reject(err)
: resolve(
stats
.toJson()
.modules.filter((v) => v.usedExports)
.map((v) => v.identifier),
);
},
);
});

for (const module of modules) {
match(module, /@dicebear\/(core|converter|identicon|collection)/);
}
for (const module of modules) {
assert.doesNotMatch(module, /Adventurer/i);
}
});
});

test.run();
4 changes: 2 additions & 2 deletions packages/@dicebear/converter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"prebuild": "del-cli lib",
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "node --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
"test": "node --no-warnings --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --no-warnings --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
},
"dependencies": {
"@fontsource/noto-sans": "^5.0.22",
Expand Down
4 changes: 2 additions & 2 deletions packages/@dicebear/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"prebuild": "del-cli lib",
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "node --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
"test": "node --no-warnings --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --no-warnings --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
},
"dependencies": {
"ky": "^1.2.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/@dicebear/core/tests/Builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PropertyHelper } from '../lib/helpers/PropertyHelper.js';
import { OptionsCollection } from '../lib/collections/OptionsCollection.js';

import { ShapeFace } from './fixtures/definitions/styles/ShapeFace.js';
import shapeFaceDefinition from './fixtures/definitions/loader/shape-face.cjs';
import shapeFaceDefinition from './fixtures/definitions/json/shape-face.json' with { type: 'json' };

describe('Builder', () => {
const shapeFaceStyle = new ShapeFace();
Expand Down
2 changes: 1 addition & 1 deletion packages/@dicebear/core/tests/Style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it } from 'node:test';

import { createStyle } from '../lib/index.js';

import initialsDefinition from './fixtures/definitions/loader/initials.cjs';
import initialsDefinition from './fixtures/definitions/json/initials.json' with { type: 'json' };

describe('Style', () => {
const initialsStyle = createStyle(initialsDefinition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { describe, it } from 'node:test';
import { createStyle } from '../../lib/index.js';
import { OptionsCollection } from '../../lib/collections/OptionsCollection.js';

import minimalDefinition from '../fixtures/definitions/loader/minimal.cjs';
import shapeFaceDefinition from '../fixtures/definitions/loader/shape-face.cjs';
import minimalDefinition from '../fixtures/definitions/json/minimal.json' with { type: 'json' };
import shapeFaceDefinition from '../fixtures/definitions/json/shape-face.json' with { type: 'json' };

describe('OptionsCollection', () => {
const minimalStyle = createStyle(minimalDefinition);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Style } from '../../../../lib/index.js';
import definition from '../loader/initials.cjs';
import definition from '../json/initials.json' with { type: 'json' };

export class Initials extends Style {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Style } from '../../../../lib/index.js';
import definition from '../loader/minimal.cjs';
import definition from '../json/minimal.json' with { type: 'json' };

export class Minimal extends Style {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Style } from '../../../../lib/index.js';
import definition from '../loader/shape-face.cjs';
import definition from '../json/shape-face.json' with { type: 'json' };

export class ShapeFace extends Style {
constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { createStyle } from '../../lib/index.js';
import { Builder } from '../../lib/Builder.js';
import { AttributeHelper } from '../../lib/helpers/AttributeHelper.js';

import minimalDefinition from '../fixtures/definitions/loader/minimal.cjs';
import minimalDefinition from '../fixtures/definitions/json/minimal.json' with { type: 'json' };

describe('AttributeHelper', () => {
it('fillAttributes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it } from 'node:test';

import { LicenseHelper } from '../../lib/helpers/LicenseHelper.js';

import minimalDefinition from '../fixtures/definitions/loader/minimal.cjs';
import minimalDefinition from '../fixtures/definitions/json/minimal.json' with { type: 'json' };

describe('LicenseHelper', () => {
it('getLicenseAsXml', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@dicebear/core/tests/helpers/OptionHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { describe, it } from 'node:test';
import { createStyle } from '../../lib/index.js';
import { OptionHelper } from '../../lib/helpers/OptionHelper.js';

import shapeFaceDefinition from '../fixtures/definitions/loader/shape-face.cjs';
import shapeFaceDefinition from '../fixtures/definitions/json/shape-face.json' with { type: 'json' };

describe('OptionHelper', () => {
it('validateOptions', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createStyle } from '../../lib/index.js';
import { Builder } from '../../lib/Builder.js';
import { OptionsCollection } from '../../lib/collections/OptionsCollection.js';

import shapeFaceDefinition from '../fixtures/definitions/loader/shape-face.cjs';
import shapeFaceDefinition from '../fixtures/definitions/json/shape-face.json' with { type: 'json' };

describe('PropertyHelper', () => {
it('fillProperties #1', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/@dicebear/core/tests/helpers/StructHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { StructHelper } from '../../lib/helpers/StructHelper.js';
import { Struct } from 'superstruct';
import { createStyle } from '../../lib/index.js';

import shapeFaceDefinition from '../fixtures/definitions/loader/shape-face.cjs';
import initialsDefinition from '../fixtures/definitions/loader/initials.cjs';
import minimalDefinition from '../fixtures/definitions/loader/minimal.cjs';
import shapeFaceDefinition from '../fixtures/definitions/json/shape-face.json' with { type: 'json' };
import initialsDefinition from '../fixtures/definitions/json/initials.json' with { type: 'json' };
import minimalDefinition from '../fixtures/definitions/json/minimal.json' with { type: 'json' };

describe('StructHelper', () => {
it('createOptionsStruct #1', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/@dicebear/core/tests/helpers/SvgHelper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { PropertyHelper } from '../../lib/helpers/PropertyHelper.js';
import { OptionsCollection } from '../../lib/collections/OptionsCollection.js';
import { createStyle } from '../../lib/index.js';

import minimalDefinition from '../fixtures/definitions/loader/minimal.cjs';
import shapeFaceDefinition from '../fixtures/definitions/loader/shape-face.cjs';
import minimalDefinition from '../fixtures/definitions/json/minimal.json' with { type: 'json' };
import shapeFaceDefinition from '../fixtures/definitions/json/shape-face.json' with { type: 'json' };

describe('SvgHelper', () => {
const minimalStyle = createStyle(minimalDefinition);
Expand Down
2 changes: 1 addition & 1 deletion packages/@dicebear/core/tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it } from 'node:test';

import { Avatar, Style, createAvatar, createStyle } from '../lib/index.js';

import initialsDefinition from './fixtures/definitions/loader/initials.cjs';
import initialsDefinition from './fixtures/definitions/json/initials.json' with { type: 'json' };

describe('index', () => {
it('createStyle', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/@dicebear/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
"prebuild": "del-cli lib",
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "node --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
"test": "node --no-warnings --test --test-reporter=spec --test-reporter-destination=stdout tests",
"test:coverage": "node --no-warnings --test --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=lcov.info --experimental-test-coverage tests"
},
"dependencies": {
"@types/json-schema": "^7.0.15"
Expand Down

0 comments on commit 52e9c89

Please sign in to comment.