UNPKG

2.04 kBJavaScriptView Raw
1const path = require(`path`);
2let PnpWebpackPlugin = require(`./index`);
3
4function makeResolver(resolverPlugins, options = {}) {
5 const {
6 NodeJsInputFileSystem,
7 CachedInputFileSystem,
8 ResolverFactory
9 } = require('enhanced-resolve');
10
11 const resolver = ResolverFactory.createResolver({
12 fileSystem: new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000),
13 extensions: ['.js', '.json'],
14 ... options,
15 });
16
17 for (const {apply} of resolverPlugins)
18 apply(resolver);
19
20 return resolver;
21}
22
23function makeRequest(resolver, request, issuer) {
24 return new Promise((resolve, reject) => {
25 resolver.resolve({}, issuer, request, {}, (err, filepath) => {
26 if (err) {
27 reject(err);
28 } else {
29 resolve(filepath);
30 }
31 });
32 });
33}
34
35describe(`Regular Plugin`, () => {
36 it(`should correctly resolve a relative require`, async () => {
37 const resolver = makeResolver([PnpWebpackPlugin]);
38 const resolution = await makeRequest(resolver, `./index.js`, __dirname);
39
40 expect(resolution).toEqual(path.normalize(`${__dirname}/index.js`));
41 });
42
43 it(`shouldn't prevent the 'extensions' option from working`, async () => {
44 const resolver = makeResolver([PnpWebpackPlugin]);
45 const resolution = await makeRequest(resolver, `./index`, __dirname);
46
47 expect(resolution).toEqual(path.normalize(`${__dirname}/index.js`));
48 });
49
50 it(`shouldn't prevent the 'alias' option from working`, async () => {
51 const resolver = makeResolver([PnpWebpackPlugin], {alias: {[`foo`]: `./fixtures/index.js`}});
52 const resolution = await makeRequest(resolver, `foo`, __dirname);
53
54 expect(resolution).toEqual(path.normalize(`${__dirname}/fixtures/index.js`));
55 });
56
57 it(`shouldn't prevent the 'modules' option from working`, async () => {
58 const resolver = makeResolver([PnpWebpackPlugin], {modules: [`./fixtures`]});
59 const resolution = await makeRequest(resolver, `file`, __dirname);
60
61 expect(resolution).toEqual(path.normalize(`${__dirname}/fixtures/file.js`));
62 });
63});