2021-11-24 12:42:48 -08:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import fs from 'fs';
|
2022-02-25 15:43:58 -08:00
|
|
|
import url from 'url';
|
|
|
|
import { transformHook, resolveHook } from './transform';
|
2021-11-24 12:42:48 -08:00
|
|
|
|
|
|
|
async function resolve(specifier: string, context: { parentURL: string }, defaultResolve: any) {
|
2022-02-25 15:43:58 -08:00
|
|
|
if (context.parentURL && context.parentURL.startsWith('file://')) {
|
|
|
|
const filename = url.fileURLToPath(context.parentURL);
|
|
|
|
const resolved = resolveHook(filename, specifier);
|
|
|
|
if (resolved !== undefined)
|
|
|
|
specifier = url.pathToFileURL(resolved).toString();
|
2021-12-14 19:25:07 -08:00
|
|
|
}
|
2021-11-24 12:42:48 -08:00
|
|
|
return defaultResolve(specifier, context, defaultResolve);
|
|
|
|
}
|
|
|
|
|
2022-02-25 15:43:58 -08:00
|
|
|
async function load(moduleUrl: string, context: any, defaultLoad: any) {
|
|
|
|
if (moduleUrl.startsWith('file://') && (moduleUrl.endsWith('.ts') || moduleUrl.endsWith('.tsx'))) {
|
|
|
|
const filename = url.fileURLToPath(moduleUrl);
|
2021-11-24 12:42:48 -08:00
|
|
|
const code = fs.readFileSync(filename, 'utf-8');
|
2022-06-01 16:50:23 -07:00
|
|
|
const source = transformHook(code, filename, moduleUrl);
|
2021-11-24 12:42:48 -08:00
|
|
|
return { format: 'module', source };
|
|
|
|
}
|
2022-02-25 15:43:58 -08:00
|
|
|
return defaultLoad(moduleUrl, context, defaultLoad);
|
2021-11-24 12:42:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { resolve, load };
|