Skip to content

Commit 56a3e18

Browse files
authored
Merge pull request #8001 from plotly/cam/7996/include-types-partial-bundles
fix: Include type defs in appropriate partial bundles
2 parents cc143a1 + 9678204 commit 56a3e18

2 files changed

Lines changed: 143 additions & 111 deletions

File tree

draftlogs/8001_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Include type defs in appropriate partial bundles [[#8001](https://github.com/plotly/plotly.js/pull/8001)]

tasks/sync_packages.js

Lines changed: 142 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ var common = require('./util/common');
88
var constants = require('./util/constants');
99
var pkg = require('../package.json');
1010
var rc = pkg.version.split('-')[1];
11-
var tag = rc ? (' --tag ' + rc.split('.')[0]) : '';
11+
var tag = rc ? ' --tag ' + rc.split('.')[0] : '';
1212

13-
var year = (new Date()).getFullYear();
13+
var year = new Date().getFullYear();
1414

1515
var copyrightAndLicense = [
1616
'## Copyright and license',
@@ -23,11 +23,15 @@ var copyrightAndLicense = [
2323
''
2424
].join('\n');
2525

26+
// Type info for the packages that carry types (plotly.js-dist and plotly.js-dist-min)
27+
const TYPES_MAIN = 'lib/index.d.ts';
28+
const TYPES_FILES = [TYPES_MAIN, 'src/types'];
29+
2630
var partialBundlePaths = constants.partialBundleNames.map(constants.makePartialBundleOpts);
2731

2832
// sync "partial bundle" packages
2933
partialBundlePaths
30-
.map(function(d) {
34+
.map(function (d) {
3135
return {
3236
name: 'plotly.js-' + d.name + '-dist',
3337
index: d.index,
@@ -37,19 +41,22 @@ partialBundlePaths
3741
traceList: constants.partialBundleTraces[d.name]
3842
};
3943
})
40-
.concat([{
41-
name: 'plotly.js-dist',
42-
index: path.join(constants.pathToLib, 'index.js'),
43-
main: 'plotly.js',
44-
dist: constants.pathToPlotlyDist,
45-
desc: 'Ready-to-use plotly.js distributed bundle.',
46-
traceList: constants.allTraces
47-
}])
44+
.concat([
45+
{
46+
name: 'plotly.js-dist',
47+
index: path.join(constants.pathToLib, 'index.js'),
48+
main: 'plotly.js',
49+
dist: constants.pathToPlotlyDist,
50+
desc: 'Ready-to-use plotly.js distributed bundle.',
51+
traceList: constants.allTraces,
52+
includeTypes: true
53+
}
54+
])
4855
.forEach(syncPartialBundlePkg);
4956

5057
// sync "minified partial bundle" packages
5158
partialBundlePaths
52-
.map(function(d) {
59+
.map(function (d) {
5360
return {
5461
name: 'plotly.js-' + d.name + '-dist-min',
5562
index: d.index,
@@ -59,22 +66,25 @@ partialBundlePaths
5966
traceList: constants.partialBundleTraces[d.name]
6067
};
6168
})
62-
.concat([{
63-
name: 'plotly.js-dist-min',
64-
index: path.join(constants.pathToLib, 'index.js'),
65-
main: 'plotly.min.js',
66-
dist: constants.pathToPlotlyDistMin,
67-
desc: 'Ready-to-use minified plotly.js distributed bundle.',
68-
traceList: constants.allTraces
69-
}])
69+
.concat([
70+
{
71+
name: 'plotly.js-dist-min',
72+
index: path.join(constants.pathToLib, 'index.js'),
73+
main: 'plotly.min.js',
74+
dist: constants.pathToPlotlyDistMin,
75+
desc: 'Ready-to-use minified plotly.js distributed bundle.',
76+
traceList: constants.allTraces,
77+
includeTypes: true
78+
}
79+
])
7080
.forEach(syncPartialBundlePkg);
7181

7282
// sync "locales" package
7383
syncLocalesPkg({
7484
name: 'plotly.js-locales',
7585
dir: path.join(constants.pathToLib, 'locales'),
7686
main: 'index.js',
77-
desc: 'Ready-to-use plotly.js locales',
87+
desc: 'Ready-to-use plotly.js locales'
7888
});
7989

8090
function syncPartialBundlePkg(d) {
@@ -93,21 +103,16 @@ function syncPartialBundlePkg(d) {
93103
bugs: pkg.bugs,
94104
author: pkg.author,
95105
keywords: pkg.keywords,
96-
files: [
97-
'LICENSE',
98-
'README.md',
99-
d.main
100-
]
106+
files: ['LICENSE', 'README.md', d.main]
101107
};
108+
if (d.includeTypes) {
109+
cnt.types = TYPES_MAIN;
110+
cnt.files.push(...TYPES_FILES);
111+
}
102112

103-
fs.writeFile(
104-
path.join(pkgPath, 'package.json'),
105-
JSON.stringify(cnt, null, 2) + '\n',
106-
cb
107-
);
113+
fs.writeFile(path.join(pkgPath, 'package.json'), JSON.stringify(cnt, null, 2) + '\n', cb);
108114
}
109115

110-
111116
function writeREADME(cb) {
112117
var cnt = [
113118
'# ' + d.name,
@@ -127,40 +132,74 @@ function syncPartialBundlePkg(d) {
127132
'',
128133
'```js',
129134
'// ES6 module',
130-
'import Plotly from \'' + d.name + '\'',
135+
"import Plotly from '" + d.name + "'",
131136
'',
132137
'// CommonJS',
133-
'var Plotly = require(\'' + d.name + '\')',
138+
"var Plotly = require('" + d.name + "')",
134139
'```',
135140
'',
141+
...(d.includeTypes
142+
? [
143+
'## TypeScript',
144+
'',
145+
'This package includes its own TypeScript declarations.',
146+
'',
147+
'```ts',
148+
"import Plotly from '" + d.name + "'",
149+
"import type { Data, Layout } from '" + d.name + "'",
150+
'',
151+
"const data: Data[] = [{ type: 'scatter', x: [1, 2, 3], y: [4, 5, 6] }]",
152+
"const layout: Partial<Layout> = { title: { text: 'Demo' } }",
153+
'',
154+
'await Plotly.newPlot(div, data, layout)',
155+
'```',
156+
''
157+
]
158+
: []),
136159
copyrightAndLicense,
137-
'Please visit [complete list of dependencies](https://www.npmjs.com/package/plotly.js/v/' + pkg.version + '?activeTab=dependencies).'
160+
'Please visit [complete list of dependencies](https://www.npmjs.com/package/plotly.js/v/' +
161+
pkg.version +
162+
'?activeTab=dependencies).'
138163
];
139164

140-
fs.writeFile(
141-
path.join(pkgPath, 'README.md'),
142-
cnt.join('\n'),
143-
cb
144-
);
165+
fs.writeFile(path.join(pkgPath, 'README.md'), cnt.join('\n'), cb);
145166
}
146167

147168
function copyMain(cb) {
148169
fs.copy(d.dist, path.join(pkgPath, d.main), cb);
149170
}
150171

172+
function copyTypes(cb) {
173+
if (!d.includeTypes) {
174+
cb();
175+
return;
176+
}
177+
178+
// Only include type related files in copy
179+
const isDeclaration = (src) => fs.statSync(src).isDirectory() || src.endsWith('.d.ts');
180+
181+
// Match the type file structure from the full library
182+
runSeries(
183+
[
184+
(cb) => fs.copy(path.join(constants.pathToLib, 'index.d.ts'), path.join(pkgPath, TYPES_MAIN), cb),
185+
(cb) =>
186+
fs.copy(
187+
path.join(constants.pathToSrc, 'types'),
188+
path.join(pkgPath, 'src', 'types'),
189+
{ filter: isDeclaration },
190+
cb
191+
)
192+
],
193+
cb
194+
);
195+
}
196+
151197
var copyLicense = _copyLicense(d, pkgPath);
152198

153199
var publishToNPM = _publishToNPM(d, pkgPath);
154200

155-
runSeries([
156-
initDirectory,
157-
writePackageJSON,
158-
writeREADME,
159-
copyMain,
160-
copyLicense,
161-
publishToNPM
162-
], function(err) {
163-
if(err) throw err;
201+
runSeries([initDirectory, writePackageJSON, writeREADME, copyMain, copyTypes, copyLicense, publishToNPM], (err) => {
202+
if (err) throw err;
164203
});
165204
}
166205

@@ -172,12 +211,14 @@ function syncLocalesPkg(d) {
172211
var localeFiles;
173212
function listLocalFiles(cb) {
174213
var localeGlob = path.join(constants.pathToLib, 'locales', '*.js');
175-
glob(localeGlob).then(function(_localeFiles) {
176-
localeFiles = _localeFiles;
177-
cb();
178-
}).catch(function(err) {
179-
cb(null);
180-
});
214+
glob(localeGlob)
215+
.then(function (_localeFiles) {
216+
localeFiles = _localeFiles;
217+
cb();
218+
})
219+
.catch(function (err) {
220+
cb(null);
221+
});
181222
}
182223

183224
function writePackageJSON(cb) {
@@ -191,18 +232,14 @@ function syncLocalesPkg(d) {
191232
bugs: pkg.bugs,
192233
author: pkg.author,
193234
keywords: pkg.keywords,
194-
files: [
195-
'LICENSE',
196-
'README.md',
197-
d.main
198-
].concat(localeFiles.map(function(f) { return path.basename(f); }))
235+
files: ['LICENSE', 'README.md', d.main].concat(
236+
localeFiles.map(function (f) {
237+
return path.basename(f);
238+
})
239+
)
199240
};
200241

201-
fs.writeFile(
202-
path.join(pkgPath, 'package.json'),
203-
JSON.stringify(cnt, null, 2) + '\n',
204-
cb
205-
);
242+
fs.writeFile(path.join(pkgPath, 'package.json'), JSON.stringify(cnt, null, 2) + '\n', cb);
206243
}
207244

208245
function writeREADME(cb) {
@@ -224,72 +261,70 @@ function syncLocalesPkg(d) {
224261
'',
225262
'```js',
226263
'// ES6 module',
227-
'import Plotly from \'plotly.js\'',
228-
'import locale from \'' + d.name + '/fr' + '\'',
264+
"import Plotly from 'plotly.js'",
265+
"import locale from '" + d.name + '/fr' + "'",
229266
'',
230267
'// CommonJS',
231-
'var Plotly = require(\'plotly.js\')',
232-
'var locale = require(\'' + d.name + '/fr\')',
268+
"var Plotly = require('plotly.js')",
269+
"var locale = require('" + d.name + "/fr')",
233270
'',
234271
'// then',
235272
'Plotly.register(locale)',
236-
'Plotly.setPlotConfig({locale: \'fr\'})',
273+
"Plotly.setPlotConfig({locale: 'fr'})",
237274
'```',
238275
'',
239276
copyrightAndLicense
240277
];
241278

242-
fs.writeFile(
243-
path.join(pkgPath, 'README.md'),
244-
cnt.join('\n'),
245-
cb
246-
);
279+
fs.writeFile(path.join(pkgPath, 'README.md'), cnt.join('\n'), cb);
247280
}
248281

249282
function writeMain(cb) {
250283
var cnt = [constants.licenseDist, ''];
251-
localeFiles.forEach(function(f) {
284+
localeFiles.forEach(function (f) {
252285
var n = path.basename(f, '.js');
253-
cnt.push('exports[\'' + n + '\'] = require(\'./' + n + '.js\')');
286+
cnt.push("exports['" + n + "'] = require('./" + n + ".js')");
254287
});
255288
cnt.push('');
256289

257-
fs.writeFile(
258-
path.join(pkgPath, d.main),
259-
cnt.join('\n'),
260-
cb
261-
);
290+
fs.writeFile(path.join(pkgPath, d.main), cnt.join('\n'), cb);
262291
}
263292

264293
function copyLocaleFiles(cb) {
265-
runSeries(localeFiles.map(function(f) {
266-
return function(cb) {
267-
fs.copy(f, path.join(pkgPath, path.basename(f)), cb);
268-
};
269-
}), cb);
294+
runSeries(
295+
localeFiles.map(function (f) {
296+
return function (cb) {
297+
fs.copy(f, path.join(pkgPath, path.basename(f)), cb);
298+
};
299+
}),
300+
cb
301+
);
270302
}
271303

272304
var copyLicense = _copyLicense(d, pkgPath);
273305

274306
var publishToNPM = _publishToNPM(d, pkgPath);
275307

276-
runSeries([
277-
initDirectory,
278-
listLocalFiles,
279-
writePackageJSON,
280-
writeREADME,
281-
writeMain,
282-
copyLocaleFiles,
283-
copyLicense,
284-
publishToNPM
285-
], function(err) {
286-
if(err) throw err;
287-
});
308+
runSeries(
309+
[
310+
initDirectory,
311+
listLocalFiles,
312+
writePackageJSON,
313+
writeREADME,
314+
writeMain,
315+
copyLocaleFiles,
316+
copyLicense,
317+
publishToNPM
318+
],
319+
function (err) {
320+
if (err) throw err;
321+
}
322+
);
288323
}
289324

290325
function _initDirectory(d, pkgPath) {
291-
return function(cb) {
292-
if(common.doesDirExist(pkgPath)) {
326+
return function (cb) {
327+
if (common.doesDirExist(pkgPath)) {
293328
cb();
294329
} else {
295330
fs.mkdir(pkgPath, cb);
@@ -298,22 +333,18 @@ function _initDirectory(d, pkgPath) {
298333
}
299334

300335
function _copyLicense(d, pkgPath) {
301-
return function(cb) {
302-
fs.copy(
303-
path.join(constants.pathToRoot, 'LICENSE'),
304-
path.join(pkgPath, 'LICENSE'),
305-
cb
306-
);
336+
return function (cb) {
337+
fs.copy(path.join(constants.pathToRoot, 'LICENSE'), path.join(pkgPath, 'LICENSE'), cb);
307338
};
308339
}
309340

310341
function _publishToNPM(d, pkgPath) {
311-
return function(cb) {
312-
if(process.env.DRYRUN) {
342+
return function (cb) {
343+
if (process.env.DRYRUN) {
313344
console.log('dry run, did not publish ' + d.name);
314-
cb();
345+
exec('npm publish --dry-run' + tag, { cwd: pkgPath }, cb).stderr.pipe(process.stderr);
315346
return;
316347
}
317-
exec('npm publish' + tag, {cwd: pkgPath}, cb).stdout.pipe(process.stdout);
348+
exec('npm publish' + tag, { cwd: pkgPath }, cb).stdout.pipe(process.stdout);
318349
};
319350
}

0 commit comments

Comments
 (0)