Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { shallowEqual } from './util';
import { flatten, partition, values } from 'lodash';
import {
NodeJsInputFileSystem,
CachedInputFileSystem,
ResolverFactory
} from 'enhanced-resolve';
import pkgUp from 'pkg-up';
import { dirname } from 'path';
import BundleConfig from './bundle-config';
import { AbstractInputFileSystem } from 'enhanced-resolve/lib/common-types';
const debug = makeDebug('ember-auto-import:splitter');
const resolver = ResolverFactory.createResolver({
// upstream types seem to be broken here
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000) as unknown as AbstractInputFileSystem,
extensions: ['.js', '.json'],
mainFields: ['browser', 'module', 'main']
});
export interface ResolvedImport {
specifier: string;
entrypoint: string;
importedBy: Import[];
}
export interface BundleDependencies {
staticImports: ResolvedImport[];
dynamicImports: ResolvedImport[];
}
export interface SplitterOptions {
// symlink === !preserveSynlinks - but using it that way
// breaks backwards compatibility
//
// Someday we'll rely on this and remove the code that manually
// does this in extract/resolve/index.js
symlinks: false,
// if a webpack config overrides extensions, there's probably
// good cause. The scannableExtensions are an educated guess
// anyway, that works well in most circumstances.
// Note that if extract/transpile/index gets an unknown extension
// passed, it'll fall back to the javascript parser
extensions: [".js"],
};
const NON_OVERRIDABLE_RESOLVE_OPTIONS: ResolverOption = {
fileSystem: new enhancedResolve.CachedInputFileSystem(new enhancedResolve.NodeJsInputFileSystem(), CACHE_DURATION) as any, // TODO
useSyncFileSystemCalls: true,
};
const plugins: ResolverOption["plugins"] = [];
if (extraOption.tsConfig) {
plugins.push(new TsConfigPathsPlugin({ configFile: extraOption.tsConfig }));
}
if (extraOption.externalModuleResolutionStrategy === "yarn-pnp") {
plugins.push(PnpWebpackPlugin);
}
return { ...DEFAULT_RESOLVE_OPTIONS, ...{ plugins }, ...option, ...NON_OVERRIDABLE_RESOLVE_OPTIONS };
};
// for typescript projects that import stuff that's only in
// node_modules/@types we need:
// - the inclusion of .d.ts to the extensions (see above)
// - an explicit inclusion of node_modules/@types to the spots
// to look for modules (in addition to "node_modules" which
// is the default for enhanced-resolve)
modules: ["node_modules", "node_modules/@types"]
};
const NON_OVERRIDABLE_RESOLVE_OPTIONS = {
// This should cover most of the bases of dependency-cruiser's
// uses. Not overridable for now because for other
// file systems it's not sure we can use sync system calls
// Also: passing a non-cached filesystem makes performance
// worse.
fileSystem: new enhancedResolve.CachedInputFileSystem(
new enhancedResolve.NodeJsInputFileSystem(),
CACHE_DURATION
),
// our code depends on sync behavior, so having this
// overriden is not an option
useSyncFileSystemCalls: true
};
function pushPlugin(pPlugins, pPluginToPush) {
return (pPlugins || []).concat(pPluginToPush);
}
function compileResolveOptions(pResolveOptions, pTSConfig) {
let lResolveOptions = {};
// TsConfigPathsPlugin requires a baseUrl to be present in the
describe('LuaModuleResolverPlugin', () => {
const luaRoot = __dirname;
const localRoot = path.resolve(__dirname, '..');
const context = path.join(__dirname, 'fixtures');
const resolver = ResolverFactory.createResolver({
extensions: ['.js', '.lua'],
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
plugins: [new LuaModuleResolverPlugin(localRoot, luaRoot)],
useSyncFileSystemCalls: true,
});
const resolve = request => resolver.resolveSync({}, context, request);
test('should resolve relative to luaRoot', () => {
expect(resolve('./test.js')).toBe('fixtures.test');
});
test('should resolve .lua and .js', () => {
expect(resolve('./test.js')).toBe('fixtures.test');
expect(resolve('./test.lua')).toBe('fixtures.test');
});
test('should resolve node modules', () => {
expect(resolve('enhanced-resolve')).toBe('node_modules.enhanced-resolve.lib.node');
module.exports.createResolver = function (compiler) {
const resolver = ResolverFactory.createResolver(
Object.assign(
{
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
extensions: ['.js', '.json']
},
compiler.options.resolve
)
)
return (context, request) => {
return new Promise((resolve, reject) => {
resolver.resolve({}, context, request, {}, (err, res) => err ? reject(err) : resolve(res))
})
}
}
createResolver (compiler) {
const resolver = ResolverFactory.createResolver(
Object.assign(
{
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
extensions: ['.js', '.json']
},
compiler.options.resolve
)
)
this.resolver = (context, request) => {
return new Promise((resolve, reject) => {
resolver.resolve({}, context, request, {}, (err, res) => err ? reject(err) : resolve(res))
})
}
}
modules: [
'node_modules',
path.join(options.cmlRoot, '/node_modules')
],
"unsafeCache": true,
"mainFiles": ["index"],
"aliasFields": ["browser"],
"mainFields": ["browser", "module", "main"],
"cacheWithContext": false}
options.config = options.config || {}
options.config.resolve = options.config.resolve || {};
return ResolverFactory.createResolver(Object.assign(
{
useSyncFileSystemCalls: true,
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000)
},
defaultOptions,
options.config.resolve
))
}
);
return obj;
}
if (typeof webpackConfig === "function") {
const config = webpackConfig();
return config ? getWebpackResolveRules(config) : {};
} else {
return webpackConfig.resolve || {};
}
};
const resolveRules = getWebpackResolveRules(webpackConfig);
const resolver = ResolverFactory.createResolver(
Object.assign(
{
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
useSyncFileSystemCalls: true
},
resolveRules
)
);
module.exports = function(value, options) {
return resolver.resolveSync({}, options.basedir, value);
};
import resolve from 'resolve';
import { get } from 'lodash';
import { join, dirname } from 'path';
import {
NodeJsInputFileSystem,
CachedInputFileSystem,
ResolverFactory
} from 'enhanced-resolve';
const resolver = ResolverFactory.createResolver({
fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
extensions: ['.js', '.json'],
mainFields: ['browser', 'module', 'main']
});
export default class DepFinder {
private deps;
private nonDevDeps;
private pkgs = new Map();
private paths = new Map();
constructor(private project, private insideAddon) {
let pkg = project.pkg;
this.deps = Object.assign({}, pkg.dependencies, pkg.devDependencies);
this.nonDevDeps = pkg.dependencies;
}
module.exports = (config = {}) => {
const defaultConfig = {
extensions: ['.css'],
mainFields: ['style', 'main'],
modules: ['node_modules'],
fileSystem: config.fileSystem
? null
: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
useSyncFileSystemCalls: true
}
const resolver = ResolverFactory.createResolver(
Object.assign(defaultConfig, config)
)
return (id, basedir) => resolver.resolveSync({}, basedir, id)
}