Skip to content

Commit

Permalink
Merge pull request #334 from personnummer/fix-cjs
Browse files Browse the repository at this point in the history
Fix cjs
  • Loading branch information
frozzare authored Jan 19, 2023
2 parents cc9fc7c + e35443a commit 7ce5394
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@personnummer/generate",
"version": "1.0.2",
"version": "1.0.3",
"description": "Generate Swedish Personal Identity Numbers",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand All @@ -10,7 +10,7 @@
"url": "git://github.com/personnummer/js-generate.git"
},
"files": [
"dist"
"dist/**/*"
],
"scripts": {
"build": "pine build",
Expand Down
21 changes: 20 additions & 1 deletion pinefile.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import { run, log, series } from '@pinefile/pine';
import isCI from 'is-ci';
import { build } from 'esbuild';
import { build as esbuild } from 'esbuild';
import fs from 'fs';

const buildOptions = (format) => ({
entryPoints: ['./src/index.ts'],
bundle: true,
format,
outfile: `./dist/${format}/index.js`,
write: false,
});

const build = async (options) => {
fs.mkdirSync(`./dist/${options.format}`);

const result = await esbuild(options);

for (let out of result.outputFiles) {
fs.writeFileSync(
out.path,
options.format === 'cjs'
? // fixes #468
out.text.replace('module.exports = __toCommonJS(src_exports);', '') +
'module.exports = src_default;'
: out.text
);
}
};

export default {
build: async () => {
await run('rimraf dist');
Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import personnummer from 'personnummer';

const lib = require(process.env.FILE);
const generate = lib.default ? lib.default : lib;
const generate = process.env.FILE?.includes('esm') ? lib.default : lib;

const padZero = (i: number): string => (i < 10 ? `0${i}` : `${i}`);

Expand Down

0 comments on commit 7ce5394

Please sign in to comment.