Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

fix: add suport to ESM entrypoint and build it in separated chunks #696

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 21 additions & 12 deletions .esbuild/build.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const baseConfig = require('./config');
const config = require('./config');
const esbuild = require('esbuild');

const config = Object.assign({}, baseConfig, {
minify: true,
drop: ['debugger', 'console'],
});
(async () => {
try {
await Promise.all([
esbuild.build({
...config,
format: 'cjs',
outdir: 'lib',
outExtension: { '.js': '.cjs' },
}),

require('esbuild')
.build({
...config,
outfile: 'lib/index.js',
})
.catch((error) => {
esbuild.build({
...config,
format: 'esm',
outdir: 'lib',
splitting: true,
}),
]);
} catch (error) {
console.error(error);
process.exit(1);
});
}
})();
17 changes: 13 additions & 4 deletions .esbuild/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
require('dotenv').config();
const { style } = require('./plugins/style-loader');
const glob = require('glob');

const entries = Object.entries(process.env).filter((key) => key[0].startsWith('SDK_'));
const env = Object.fromEntries(entries);
const entryPoints = glob
.sync('./src/**/*.ts')
.filter((file) => !file.endsWith('.d.ts') && !file.endsWith('.test.ts'));

module.exports = {
entryPoints: ['./src/index.ts'],
const config = {
entryPoints,
loader: {
'.png': 'file',
'.svg': 'file',
Expand All @@ -16,9 +20,14 @@ module.exports = {
},
plugins: [style()],
bundle: true,
target: 'es6',
format: 'esm',
color: true,
minify: false,
logLevel: 'info',
sourcemap: true,
chunkNames: 'chunks/[name]-[hash]',
define: {
'process.env': JSON.stringify(env),
},
};

module.exports = config;
32 changes: 19 additions & 13 deletions .esbuild/watch.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
const baseConfig = require('./config');
const config = require('./config');
const esbuild = require('esbuild');

const config = Object.assign({}, baseConfig, {
outfile: 'dist/index.js',
});

(async () => {
try {
const context = await esbuild.context(config);
context.watch();
const [cjsContext, esmContext] = await Promise.all([
esbuild.context({
...config,
format: 'cjs',
outdir: 'dist',
outExtension: { '.js': '.cjs' },
}),

esbuild.context({
...config,
format: 'esm',
outdir: 'dist',
splitting: true,
}),
]);

cjsContext.watch();
esmContext.watch();
} catch (error) {
console.error(error);
process.exit(1);
}
})();

// .build(config)
// .catch((error) => {
// console.error(error);
// process.exit(1);
// });
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"description": "SuperViz SDK",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
"import": "./lib/index.js",
"require": "./lib/index.cjs.js"
},
"files": [
"lib"
],
Expand Down
Loading