-
Notifications
You must be signed in to change notification settings - Fork 382
/
module-resolver.ts
executable file
·183 lines (157 loc) · 6.3 KB
/
module-resolver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import path from 'path';
import { Plugin } from 'rollup';
import { ModuleResolutionErrors, generateCompilerError } from '@lwc/errors';
import { hasOwnProperty } from '@lwc/shared';
import { NormalizedCompileOptions, BundleFiles } from '../options';
const EMPTY_IMPLICIT_CSS_CONTENT = '';
const EMPTY_IMPLICIT_HTML_CONTENT = 'export default void 0';
const IMPLICIT_DEFAULT_HTML_PATH = '@lwc/resources/empty_html.js';
const IMPLICIT_DEFAULT_CSS_PATH = '@lwc/resources/empty_css.css';
const VALID_EXTENSIONS = ['.js', '.ts', '.html', '.css']; // order of resolution is important
function isRelativeImport(id: string) {
return id.startsWith('.');
}
function isImplicitCssImport(id: string, importee: string) {
return (
path.extname(id) === '.css' &&
path.extname(importee) === '.html' &&
path.basename(id, '.css') === path.basename(importee, '.html')
);
}
function isImplicitHTMLImport(importee: string, importer: string) {
return (
importer &&
path.extname(importer) === '.js' &&
path.extname(importee) === '.html' &&
path.dirname(importer) === path.dirname(importee) &&
path.basename(importer, '.js') === path.basename(importee, '.html')
);
}
function isFirstCharacterUppercased(importee: string) {
const upperCaseRegex = /^[A-Z]/;
return importee && upperCaseRegex.test(importee);
}
function inferExtension(fileName: string, files: BundleFiles) {
if (!path.extname(fileName)) {
const ext = VALID_EXTENSIONS.find(ext => hasOwnProperty.call(files, fileName + ext)) || '';
return fileName + ext;
}
return fileName;
}
function fileExists(fileName: string, { files }: NormalizedCompileOptions): boolean {
return hasOwnProperty.call(files, fileName);
}
function readFile(filename: string, options: NormalizedCompileOptions): string {
const { files } = options;
if (fileExists(filename, options)) {
return files[filename];
} else {
throw generateCompilerError(ModuleResolutionErrors.NONEXISTENT_FILE, {
messageArgs: [filename],
origin: { filename },
});
}
}
function generateModuleResolutionError(
importee: string,
importer: string,
options: NormalizedCompileOptions
) {
const absPath = getAbsolutePath(importee, importer, options);
const caseIgnoredFilename = getCaseIgnoredFilenameMatch(options.files, absPath);
return caseIgnoredFilename
? generateCompilerError(ModuleResolutionErrors.IMPORT_AND_FILE_NAME_CASE_MISMATCH, {
messageArgs: [
importee,
importer,
caseIgnoredFilename.substr(
0,
caseIgnoredFilename.length - path.extname(caseIgnoredFilename).length
),
],
origin: { filename: importer },
})
: generateCompilerError(ModuleResolutionErrors.IMPORTEE_RESOLUTION_FROM_IMPORTER_FAILED, {
messageArgs: [importee, importer, absPath],
origin: { filename: importer },
});
}
function generateEntryResolutionError(
importee: string,
importer: string,
options: NormalizedCompileOptions
) {
const absPath = getAbsolutePath(importee, importer, options);
const caseIgnoredFilename = getCaseIgnoredFilenameMatch(options.files, absPath);
return caseIgnoredFilename
? generateCompilerError(ModuleResolutionErrors.FOLDER_AND_FILE_NAME_CASE_MISMATCH, {
messageArgs: [caseIgnoredFilename, importee],
origin: { filename: importer },
})
: generateCompilerError(ModuleResolutionErrors.IMPORTEE_RESOLUTION_FAILED, {
messageArgs: [importee],
origin: { filename: importer },
});
}
function getAbsolutePath(importee: string, importer: string, options: NormalizedCompileOptions) {
const { baseDir, files } = options;
const relPath = importer ? path.dirname(importer) : baseDir || '';
return inferExtension(path.join(relPath, importee), files);
}
function getCaseIgnoredFilenameMatch(files: { [key: string]: string }, nameToMatch: string) {
return Object.keys(files).find(
(bundleFile: string) => bundleFile.toLowerCase() === nameToMatch
);
}
export default function({ options }: { options: NormalizedCompileOptions }): Plugin {
return {
name: 'lwc-module-resolver',
resolveId(importee: string, importer: string) {
// Mark non-relative imports (eg. 'lwc' or 'x/foo') as external dependencies.
if (!isRelativeImport(importee) && importer) {
return false;
}
if (isFirstCharacterUppercased(importee)) {
throw generateCompilerError(
ModuleResolutionErrors.FOLDER_NAME_STARTS_WITH_CAPITAL_LETTER,
{
messageArgs: [
importee,
importee.charAt(0).toLowerCase() + importee.slice(1),
],
}
);
}
const absPath = getAbsolutePath(importee, importer, options);
if (!fileExists(absPath, options)) {
if (isImplicitCssImport(importee, importer)) {
return IMPLICIT_DEFAULT_CSS_PATH;
}
if (isImplicitHTMLImport(absPath, importer)) {
return IMPLICIT_DEFAULT_HTML_PATH;
}
throw importer
? generateModuleResolutionError(importee, importer, options)
: generateEntryResolutionError(importee, importer, options);
}
return absPath;
},
load(id: string) {
if (id === IMPLICIT_DEFAULT_CSS_PATH) {
return EMPTY_IMPLICIT_CSS_CONTENT;
}
if (id === IMPLICIT_DEFAULT_HTML_PATH) {
return EMPTY_IMPLICIT_HTML_CONTENT;
}
return path.extname(id) === '.css' && !fileExists(id, options)
? EMPTY_IMPLICIT_CSS_CONTENT
: readFile(id, options);
},
};
}