1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const fs = require('fs');
|
8 | const { resolve, extname, join, dirname, basename } = require('path');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | let resolver;
|
19 | try {
|
20 | resolver = require('jest-resolve/build/default_resolver').default;
|
21 | } catch (e) {
|
22 | resolver = require('jest-resolve/build/defaultResolver').default;
|
23 | }
|
24 |
|
25 | const EMPTY_CSS_MOCK = resolve(__dirname, '..', 'resources', 'emptyStyleMock.js');
|
26 | const EMPTY_HTML_MOCK = resolve(__dirname, '..', 'resources', 'emptyHtmlMock.js');
|
27 |
|
28 | const WHITELISTED_LWC_PACKAGES = {
|
29 | lwc: '@lwc/engine',
|
30 | 'wire-service': '@lwc/wire-service',
|
31 | 'wire-service-jest-util': 'lwc-wire-service-jest-util',
|
32 | };
|
33 |
|
34 |
|
35 |
|
36 | function isImplicitHTMLImport(importee, { basedir }) {
|
37 | const ext = extname(importee);
|
38 | const isHTML = ext === '.html';
|
39 | const fileName = basename(importee, '.html');
|
40 | const absPath = resolve(basedir, importee);
|
41 | const jsFile = join(dirname(absPath), fileName + '.js');
|
42 |
|
43 | return (
|
44 | isHTML &&
|
45 | fs.existsSync(jsFile) &&
|
46 | !fs.existsSync(absPath)
|
47 | );
|
48 | }
|
49 |
|
50 | function getLwcPath(path, options) {
|
51 |
|
52 | if (WHITELISTED_LWC_PACKAGES[path]) {
|
53 | return require.resolve(WHITELISTED_LWC_PACKAGES[path]);
|
54 | }
|
55 |
|
56 |
|
57 | if (extname(path) === '.css') {
|
58 | return EMPTY_CSS_MOCK;
|
59 | }
|
60 |
|
61 |
|
62 | if (isImplicitHTMLImport(path, options)) {
|
63 | return EMPTY_HTML_MOCK;
|
64 | }
|
65 |
|
66 | return path;
|
67 | }
|
68 |
|
69 | module.exports = function(path, options) {
|
70 | return resolver(getLwcPath(path, options), options);
|
71 | };
|