drop support for src-relative imports

This commit is contained in:
Patrik Oldsberg
2020-05-06 16:38:07 +02:00
parent a8584e7cec
commit 70040b22a7
73 changed files with 153 additions and 249 deletions
-56
View File
@@ -21,68 +21,12 @@ const path = require('path');
const isLocal = require('fs').existsSync(path.resolve(__dirname, '../src'));
if (!isLocal || process.env.BACKSTAGE_E2E_CLI_TEST) {
// 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 = {};
// require() that first searches for locally defined amd modules
const requireFunc = name => {
let factory = moduleFactories[name];
if (!factory) {
// Check /index as well, to mirror nodejs resolution
const index = `${name}/index`;
if (!moduleFactories[index]) {
return require(name);
}
name = index;
factory = moduleFactories[name];
}
if (!moduleCache[name]) {
moduleCache[name] = factory();
}
return moduleCache[name];
};
const impls = deps.slice(2).map(requireFunc);
moduleFunc(requireFunc, 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.build.json'),
compilerOptions: {
module: 'CommonJS',
},
transpileOnly: true,
});
require('../src');
cleanupPaths();
}