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

Bad export directive since 42f54e8010c077c5375f0c96ab35a12bad35d3f4 (Jan 23) #142

Closed
meunierfrederic opened this issue Mar 30, 2022 · 4 comments

Comments

@meunierfrederic
Copy link

Hello,

Thanks for your very useful code.

in src/main.ts
from './parse-domain'
becomes
from './parse-domain.js'
which throws an error when we are using your module.

Same for
from './sanitize.js'

Thanks,

Frédéric

@jhnns
Copy link
Member

jhnns commented Jun 30, 2022

Not sure what you mean :) The import is working fine: https://stackblitz.com/edit/node-3ejlnp?file=package.json,index.js

Maybe you need to switch to ECMAScript modules to make it work. See #146 (comment)

@jhnns jhnns closed this as completed Jun 30, 2022
@meunierfrederic
Copy link
Author

Thanks @jhnns, #146 comment was useful.
I'm not really confident with my understanding about subtle differences between typescript and ECMA Script. Our need is to use parse-domain in a typescript/commonJs project. Here an example of code that run successfully. Do you know if there are some better ways to use parse-domain in typescript/commonJs project?

src/index.ts:

import type { parseDomain, fromUrl, ParseResultType } from "parse-domain";
import("parse-domain").then((parseDomainApi) => {
  main(parseDomainApi);
});

async function main(parseDomainApi: { parseDomain: typeof parseDomain; ParseResultType: typeof ParseResultType; fromUrl: typeof fromUrl }) {
  const parseResult = parseDomainApi.parseDomain(parseDomainApi.fromUrl("https://www.münchen.de?query"));

  if (parseResult.type === "LISTED") {
    console.log("subDomains:", parseResult.subDomains);
    console.log("domain:", parseResult.domain); // "xn--mnchen-3ya"
    console.log("topLevelDomains:", parseResult.topLevelDomains); // ["de"]
  }
}

@jhnns
Copy link
Member

jhnns commented Jul 1, 2022

Yes, that's basically it, though I'd rewrite that example to

const parseDomainModule = import('parse-domain');

async function parseDomain(url) {
  const { parseDomain, fromUrl } = await parseDomainModule;

  return parseDomain(fromUrl(url));
}

parseDomain('https://www.münchen.de?query').then((result) => {
  console.log(result);
});

https://stackblitz.com/edit/node-q4wvvq?file=package.json,index.js

@jhnns
Copy link
Member

jhnns commented Jul 1, 2022

Alternatively, you could write a helper function that initializes all ESM dependencies before starting the application:

// the helper
const modules = {
  ['parse-domain']: import('parse-domain'),
};

const initializedModules = Object.create(null);

exports.require = (moduleName) => {
  if (moduleName in initializedModules === false) {
    throw new Error(`Unknown module ${moduleName}`);
  }
  return initializedModules[moduleName];
};

exports.isReady = (async () => {
  for (const [moduleName, modulePromise] of Object.entries(modules)) {
    initializedModules[moduleName] = await modulePromise;
  }
})();
// on application start
const esm = require('./esm.js');

esm.isReady.then(() => {
  require('./app.js');
});
// in your app
const esm = require('./esm.js');
const { parseDomain, fromUrl } = esm.require('parse-domain');

console.log(parseDomain(fromUrl('www.münchen.de')));

https://stackblitz.com/edit/node-whxrib?file=esm.js,start.js,app.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants