Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: support multi-entry build #1

Closed
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
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@
},
"license": "MIT",
"main": "dist/index.js",
"module": "dist/storybook-addon-apollo-client.esm.js",
"module": "dist/index.esm.js",
"exports": {
".": {
"import": "./dist/index.esm.js",
"require": "./dist/index.js"
},
"./register": {
"import": "./dist/register.esm.js",
"require": "./dist/register.js"
}
},
Comment on lines +42 to +51
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not tested this out yet, https://nodejs.org/api/packages.html#packages_writing_dual_packages_while_avoiding_or_minimizing_hazards might be useful but "type": "module" does not play well with TSDX

"name": "storybook-addon-apollo-client",
"peerDependencies": {
"@apollo/client": "3.x",
Expand All @@ -52,7 +62,8 @@
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"build": "tsdx build --entry src/index.tsx --entry src/register.tsx && npm run post-build",
"post-build": "node scripts/post-tsdx.js",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed a post tsdx step to fix the cjs enty files (dist/index.js points at the wrong files and dist/register.js does not exist)

"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build"
Expand Down
31 changes: 31 additions & 0 deletions scripts/post-tsdx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('fs');
const path = require('path');

const distDir = path.join(__dirname, '../dist/');
const cjsIndexFile = path.join(distDir, 'index.js');
const cjsModuleBase = `storybook-addon-apollo-client.cjs`;
const cjsModuleProd = `${cjsModuleBase}.production`;

const cjsIndexContent = fs.readFileSync(cjsIndexFile, 'utf8');

const cjsEntryContent = moduleName =>
cjsIndexContent.replace(
RegExp(`(${cjsModuleProd}|${cjsModuleBase})`, 'gi'),
moduleName
);

// write register cjs entry file for register
fs.writeFile(
path.join(distDir, 'register.js'),
cjsEntryContent('register'),
err => {
if (err) return console.log(err);
console.log('created dist/register.js');
}
);

// update register cjs entry file for index to match the custom `tsdx.config.js`
fs.writeFile(path.join(distDir, 'index.js'), cjsEntryContent('index'), err => {
if (err) return console.log(err);
console.log('updated dist/index.js');
});
16 changes: 16 additions & 0 deletions tsdx.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// TSDX does not currently support multiple entry files out of the box,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TSDX does not currently support multiple entry files out of the box, so I had to overwrite the Rollup config
Ref: jaredpalmer/tsdx#175 (comment)

// so we need to overwrite the Rollup config
// Ref: https://github.com/formium/tsdx/issues/175#issuecomment-564814325

module.exports = {
rollup(config) {
const outputDir = process.cwd() + '/dist/';
const extension = config.output.file.match(/.+(\..+\..+)$/)[1];
const filename = config.input.match(/^src\/(.+)\..+$/)[1]; // remove `src/` and extension

config.output.file = outputDir + filename + extension;
// replace / with __ for UMD names
config.output.name = filename.replace('/', '__');
return config;
},
};