electron-userland / electron-compile

DEPRECATED: Electron supporting package to compile JS and CSS in Electron applications
1.01k stars 99 forks source link

Modifying globalPaths - require() vs import from #248

Closed tkambler closed 7 years ago

tkambler commented 7 years ago

I'm in the habit of modifying my application's globalPaths setting like so (within the renderer's HTML document):

const path = require('path');
require('module').globalPaths.push(path.resolve(__dirname, 'src'));

With this in place, I can now do the following:

require('app'); // Loads module located at ./src/app/index.js

This works great. The following generates an error though:

import App from 'app'; // Cannot find module 'app'

Is there an extra setting for babel, etc..., that I need to know about?

=======

Update...

I've since found out about the babel-plugin-module-resolver module. I've installed it and updated my app's .compilerc like so:

{
    "application/javascript": {
        "presets": [
            "es2016-node5"
        ],
        "plugins": [
            ["module-resolver", {
                "root": ["./src"]
            }],
            "transform-class-properties",
            "transform-async-to-generator",
            "transform-runtime"
        ],
        "sourceMaps": "inline"
    }
}

After removing my earlier code that modified globalPaths, everything seems to be working as expected now.