question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional Module Loading

See original GitHub issue

I’ve noticed that the components in this project seem to mirror each other between macOS and windows. I assume that this is to enable developers to write the same code for both platforms? What’s the suggested way of conditionally loading the correct component based upon the current platform? I don’t think that something like this is supported:

if (process.platform === 'win32') {
    import { Window, TitleBar, Text } from 'react-desktop/windows'
} else {
    import { Window, TitleBar, Text } from 'react-desktop/macOs'
}

Imports have to be top level statements.

I’ve also experimented with something like this:

const platform = (process.platform === 'win32') ? 'windows' : 'macOs';

import { Window, TitleBar, Text } from `react-desktop/${platform}`

This doesn’t work because import statements don’t support string templates (or variables for that matter).

Any other ideas? Do I need to switch to SystemJS or something?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
gabrielbullcommented, Jan 9, 2017

You can now import components based on the current OS like so:

import { Window, TitleBar, Text } from `react-desktop`;
0reactions
gabrielbullcommented, Jan 9, 2017

It decides what to import. If you want to decide you can import using react-desktop/macOs or react-desktop/windows

import * as macOs from 'react-desktop/macOs';
import * as windows from 'react-desktop/windows';

const platform = (process.platform === 'win32') ? 'windows' : 'macOs';
const TextField = platform === 'windows' ? windows.TextField : macOs.TextField;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module::Load::Conditional - MetaCPAN
DESCRIPTION. Module::Load::Conditional provides simple ways to query and possibly load any of the modules you have installed on your system during runtime.
Read more >
Loading Modules Conditionally in Angular
Loading Modules Conditionally in Angular. One of the very common features of Admin dashboard applications is Access Control. This is usually achieved ...
Read more >
Global Variables - $Module::Load::Conditional::VERBOSE
Module ::Load::Conditional provides simple ways to query and possibly load any of the modules you have installed on your system during runtime. It...
Read more >
Conditional Loading - StealJS
Conditional Loading is a way to conditionally load a module based on run-time conditions. Using a special syntax you can conditionally load code...
Read more >
How can I conditionally import an ES6 module? - Stack Overflow
You can now call the import keyword as a function (i.e. import() ) to load a module at runtime. It returns a Promise...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found