Skip to content

Commit

Permalink
Merge pull request #198 from organisationsnummer/fix-cjs
Browse files Browse the repository at this point in the history
Fix cjs build
  • Loading branch information
frozzare authored Jan 19, 2023
2 parents 547546c + 5b1f824 commit 37a4379
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
main.ts
dist
/main.ts
dist
/index.js
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "organisationsnummer",
"description": "Validate Swedish organization numbers",
"version": "1.1.1",
"version": "1.1.2",
"license": "MIT",
"homepage": "https://github.com/organisationsnummer/js",
"author": {
Expand Down
27 changes: 23 additions & 4 deletions pinefile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
const isCI = require('is-ci');
const { run, log, series } = require('@pinefile/pine');
const { build } = require('esbuild');
import { run, log, series } from '@pinefile/pine';
import isCI from 'is-ci';
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,
});

module.exports = {
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 personnummer#468
out.text.replace('module.exports = __toCommonJS(src_exports);', '') +
'module.exports = src_default2;'
: out.text
);
}
};

export default {
build: async () => {
await run('rimraf dist');

Expand Down
5 changes: 3 additions & 2 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Personnummer = require('personnummer').default;
const lib = require(process.env.FILE);
const Organisationsnummer = lib.default ? lib.default : lib;
const Organisationsnummer = process.env.FILE?.includes('esm')
? lib.default
: lib;

it('should validate valid organization numbers', () => {
const numbers = ['556016-0680', '556103-4249', '5561034249', '559244-0001'];
Expand Down

0 comments on commit 37a4379

Please sign in to comment.