Galooshi / import-js

A tool to simplify importing JS modules
MIT License
526 stars 70 forks source link

active forks #570

Closed milahu closed 7 months ago

milahu commented 3 years ago
trotzig commented 3 years ago

Happy to see others benefitting from this project! Is there anything specific you wanted to merge into the main project?

milahu commented 3 years ago

nah .. i tried to fix import paths of fiduswriter fiduswriter is organized in modules, which are overlaid/merged in the build step i ended up simply using the original build process (merge the module folders)

why not importjs? importjs made some errors, probably some exports were ambiguous and:

importjs can only parse dynamic imports like

var someModule = await import("./some/file.js");

but importjs fails to parse dynamic imports a la

import("./some/file.js").then(({ someProp }) => { someProp(); });

i started writing a parser for that ...

```js // lib/findCurrentImports.js // function convertToImportStatement // import("../../exporter/docx").then(({ DocxExporter }) => { DocxExporter.foo(); }); if (node.type === 'CallExpression') { if (node.callee.type !== 'MemberExpression') return undefined; if (node.callee.object.type !== 'CallExpression') return undefined; if (node.callee.object.callee.type !== 'Import') return undefined; console.log(`found CallExpression + Import`) if (node.callee.object.arguments.length !== 1) return undefined; if (node.callee.object.arguments[0].type !== 'StringLiteral') return undefined; const path = node.callee.object.arguments[0].value; // must be relative path if (!path.startsWith('./') && !path.startsWith('../')) return undefined; console.log(`found relative path:`) console.dir(path); if (node.callee.property.type !== 'Identifier') return undefined; if (node.callee.property.name !== 'then') return undefined; console.log(`found CallExpression + Import + then`) if (node.callee.arguments.length !== 1) return undefined; if (node.callee.arguments[0].type !== 'ArrowFunctionExpression') return undefined; console.log(`found CallExpression + Import + then + ArrowFunctionExpression`) if (node.callee.arguments[0].params.length !== 1) return undefined; // TODO params[0].type can also be Identifier to import the default export // TODO support prop renaming: import("./x").then( ({ foo: bar }) => { bar(); } ); if (node.callee.arguments[0].params[0].type !== 'ObjectPattern') return undefined; console.log(`found CallExpression + Import + then + ArrowFunctionExpression + ObjectPattern`) if (node.callee.arguments[0].params[0].properties.length > 0) return undefined; const namedImports = node.callee.arguments[0].params[0].properties.map((p: Object): Object => ({ //localName: p.key.name, // prop renaming: key != value localName: p.value.name, })); console.log(`found namedImports:`) console.dir(namedImports); //const defaultImport = declaration.id.type === 'Identifier' // ? declaration.id.name // : undefined; //const namedImports = declaration.id.type === 'ObjectPattern' // ? declaration.id.properties.map((p: Object): Object => ({ // localName: p.value.name, // })) // : undefined; return new ImportStatement({ declarationKeyword: node.kind, defaultImport: undefined, // TODO hasSideEffects: false, importFunction: undefined, // node.callee.object.callee.name, // always undefined namedImports, path, danglingCommas: false, }); } ```
mikabytes commented 7 months ago

Hey all,

I'm closing this due to inactivity, but if you feel there's still something here we ought to have a look on then please reopen it. Thanks.