-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
}, | ||
"name": "storybook-addon-apollo-client", | ||
"peerDependencies": { | ||
"@apollo/client": "3.x", | ||
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needed a post tsdx step to fix the cjs enty files ( |
||
"test": "tsdx test", | ||
"lint": "tsdx lint", | ||
"prepare": "tsdx build" | ||
|
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'); | ||
}); |
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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// 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; | ||
}, | ||
}; |
There was a problem hiding this comment.
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