packages/cli: make src-relative imports work

This commit is contained in:
Patrik Oldsberg
2020-04-06 15:36:38 +02:00
parent bc4f6ea3ad
commit 28b576967c
6 changed files with 87 additions and 10 deletions
+48
View File
@@ -5,12 +5,60 @@ const path = require('path');
// Figure out whether we're running inside the backstage repo or as an installed dependency
const isLocal = require('fs').existsSync(path.resolve(__dirname, '../src'));
if (!isLocal) {
// src-relative imports are a pain to get to work with plain tsc compilation, as the
// transpiled code will maintain the imports as they are in the source. Which means an
// import for `helpers/paths` will start like that in the output, which won't work in NodeJS.
//
// This is a solution for getting src-relative imports to work with typescript/node in
// the published package. We're using `module: "amd"` to ship the entire cli implementation
// in one file, and it also happens to generate correct module definition and import statements.
// Minimal AMD implementation
const moduleFactories = {};
const moduleCache = {};
global.define = (name, deps, moduleFunc) => {
moduleFactories[name] = () => {
const exportsObj = {};
const impls = deps.slice(2).map(dep => {
const factory = moduleFactories[dep];
if (!factory) {
return require(dep);
}
if (!moduleCache[dep]) {
moduleCache[dep] = factory();
}
return moduleCache[dep];
});
moduleFunc(require, exportsObj, ...impls);
return exportsObj;
};
};
require('../dist');
// index module is the entrypoint
moduleFactories.index();
} else {
const tsConfigPath = path.resolve(__dirname, '../tsconfig.json');
const {
baseUrl: relativeBaseUrl,
paths = {},
} = require(tsConfigPath).compilerOptions;
const baseUrl = path.resolve(__dirname, '..', relativeBaseUrl);
// Use tsconfig-paths to resolve src-relative paths during development
const cleanupPaths = require('tsconfig-paths').register({ baseUrl, paths });
require('ts-node').register({
project: path.resolve(__dirname, '../tsconfig.json'),
compilerOptions: {
module: 'CommonJS',
},
transpileOnly: true,
});
require('../src');
cleanupPaths();
}
+3 -1
View File
@@ -24,7 +24,9 @@ if (fs.existsSync('jest.config.js')) {
} else if (fs.existsSync('jest.config.ts')) {
module.exports = require(path.resolve('jest.config.ts'));
} else {
const extraOptions = {};
const extraOptions = {
modulePaths: ['<rootDir>'],
};
// Use src/setupTests.ts as the default location for configuring test env
if (fs.existsSync('src/setupTests.ts')) {
+2 -1
View File
@@ -39,7 +39,8 @@
"@types/webpack-dev-server": "^3.10.0",
"del": "^5.1.0",
"nodemon": "^2.0.2",
"ts-node": "^8.6.2"
"ts-node": "^8.6.2",
"tsconfig-paths": "^3.9.0"
},
"bin": {
"backstage-cli": "bin/backstage-cli"
+15 -1
View File
@@ -77,8 +77,22 @@ export function findRootPath(topPath: string): string {
);
}
// Finds the root of the cli package itself
export function findOwnDir() {
// Known relative locations of package in dist/dev
const pathDist = '..';
const pathDev = '../..';
// Check the closest dir first
const pkgInDist = resolvePath(__dirname, pathDist, 'package.json');
const isDist = fs.pathExistsSync(pkgInDist);
const path = isDist ? pathDist : pathDev;
return resolvePath(__dirname, path);
}
export function findPaths(): Paths {
const ownDir = resolvePath(__dirname, '../..');
const ownDir = findOwnDir();
const targetDir = fs.realpathSync(process.cwd());
// We're not always running in a monorepo, so we lazy init this to only crash commands
+3 -6
View File
@@ -2,11 +2,8 @@
"extends": "../../tsconfig.json",
"include": ["src"],
"compilerOptions": {
"outDir": "dist",
"module": "CommonJS",
"baseUrl": "src",
"paths": {
"*": ["src/*"]
}
"outFile": "dist/index.js",
"module": "amd",
"baseUrl": "src"
}
}