Skip to content

Instantly share code, notes, and snippets.

@zhepa zhepa/loader.mjs
Created Feb 22, 2019

Embed
What would you like to do?
ESM Loader root path import resolve alias
import path from 'path';
import os from 'os';
const isWindows = os.platform() === 'win32';
const ROOT_PATH = path.join(process.cwd(), 'app/');
export function resolve(specifier, parentModuleURL, defaultResolver) {
if (isWindows && /^@\//.test(specifier)) {
const parentModulePath = parentModuleURL.replace(/file:\/\/\//, '');
const parentModuleDirPath = path.parse(path.resolve(parentModulePath)).dir;
const relativePath = (path.relative(parentModuleDirPath, ROOT_PATH) || './').replace(/\\/g, '/');
specifier = specifier.replace(/^@/, relativePath);
}
else specifier = specifier.replace(/^@\//, ROOT_PATH);
return defaultResolver(specifier, parentModuleURL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.