Skip to content

Commit

Permalink
Fix cjs build after switch to esbuild
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Jan 19, 2023
1 parent 547546c commit 0d79f73
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 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
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 0d79f73

Please sign in to comment.