Fix legacy build

The build process failed because webpack does not allow failed imports
when it is run as a module.
This detects modules with default exports and only generates import
statements for default exports where they are available.
This commit is contained in:
Maximilian Krög
2021-05-02 23:18:19 +02:00
parent 8ff40c40e8
commit 40f8e69675
12 changed files with 77 additions and 63 deletions
+6 -5
View File
@@ -14,12 +14,13 @@
]
},
"plugins": [
"config/jsdoc/api/plugins/markdown.cjs",
"config/jsdoc/plugins/markdown.cjs",
"jsdoc-plugin-typescript",
"config/jsdoc/api/plugins/inline-options.cjs",
"config/jsdoc/api/plugins/events.cjs",
"config/jsdoc/api/plugins/observable.cjs",
"config/jsdoc/api/plugins/api.cjs"
"config/jsdoc/plugins/inline-options.cjs",
"config/jsdoc/plugins/events.cjs",
"config/jsdoc/plugins/observable.cjs",
"config/jsdoc/plugins/api.cjs",
"config/jsdoc/plugins/default-export.cjs"
],
"typescript": {
"moduleRoot": "src"
+3 -2
View File
@@ -11,8 +11,9 @@
},
"plugins": [
"jsdoc-plugin-typescript",
"config/jsdoc/info/define-plugin.cjs",
"config/jsdoc/info/virtual-plugin.cjs"
"config/jsdoc/plugins/define-plugin.cjs",
"config/jsdoc/plugins/virtual-plugin.cjs",
"config/jsdoc/plugins/default-export.cjs"
],
"typescript": {
"moduleRoot": "src"
+3
View File
@@ -137,6 +137,9 @@ exports.publish = function (data, opts) {
return true;
});
}
if (doc.isDefaultExport) {
symbol.isDefaultExport = true;
}
const target = isExterns ? externs : symbols;
const existingSymbol = symbolsByName[symbol.name];
@@ -104,26 +104,6 @@ function includeTypes(doclet) {
}
}
const defaultExports = {};
const path = require('path');
const moduleRoot = path.join(process.cwd(), 'src');
// Tag default exported Identifiers because their name should be the same as the module name.
exports.astNodeVisitor = {
visitNode: function (node, e, parser, currentSourceName) {
if (node.parent && node.parent.type === 'ExportDefaultDeclaration') {
const modulePath = path
.relative(moduleRoot, currentSourceName)
.replace(/\.js$/, '');
const exportName =
'module:' +
modulePath.replace(/\\/g, '/') +
(node.name ? '~' + node.name : '');
defaultExports[exportName] = true;
}
},
};
function sortOtherMembers(doclet) {
if (doclet.fires) {
doclet.fires.sort(function (a, b) {
@@ -198,18 +178,4 @@ exports.handlers = {
}
}
},
processingComplete(e) {
const byLongname = e.doclets.index.longname;
for (const name in defaultExports) {
if (!(name in byLongname)) {
throw new Error(
`missing ${name} in doclet index, did you forget a @module tag?`
);
}
byLongname[name].forEach(function (doclet) {
doclet.isDefaultExport = true;
});
}
},
};
+35
View File
@@ -0,0 +1,35 @@
const defaultExports = {};
const path = require('path');
const moduleRoot = path.join(process.cwd(), 'src');
// Tag default exported Identifiers because their name should be the same as the module name.
exports.astNodeVisitor = {
visitNode: function (node, e, parser, currentSourceName) {
if (node.parent && node.parent.type === 'ExportDefaultDeclaration') {
const modulePath = path
.relative(moduleRoot, currentSourceName)
.replace(/\.js$/, '');
const exportName =
'module:' +
modulePath.replace(/\\/g, '/') +
(node.name ? '~' + node.name : '');
defaultExports[exportName] = true;
}
},
};
exports.handlers = {
processingComplete(e) {
const byLongname = e.doclets.index.longname;
for (const name in defaultExports) {
if (!(name in byLongname)) {
throw new Error(
`missing ${name} in doclet index, did you forget a @module tag?`
);
}
byLongname[name].forEach(function (doclet) {
doclet.isDefaultExport = true;
});
}
},
};