Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2896209

Browse files
Get the Analyzer fully running in browsers (#4322)
1 parent 216f0a2 commit 2896209

27 files changed

+456
-45
lines changed

.changeset/eleven-ducks-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lit-labs/analyzer': minor
3+
---
4+
5+
Remove dependencies on Node-specific libaries. This change requries passing a path separator to `absoluteToPackage()`.

.eslintrc.json

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,7 @@
3535
// Enforces that when a module is imported, that module is listed in the
3636
// package's immediate "dependencies". Note internal-only files are excluded
3737
// in an "overrides" configuration below.
38-
"import/no-extraneous-dependencies": [
39-
"error",
40-
{
41-
// These modules are also allowed to import from their immediate
42-
// "devDependencies".
43-
"devDependencies": [
44-
// This package has a type-only import of "react", but it's not a
45-
// dependency.
46-
"packages/labs/react/**"
47-
]
48-
}
49-
],
38+
"import/no-extraneous-dependencies": ["error"],
5039
"@typescript-eslint/consistent-generic-constructors": "error"
5140
},
5241
"overrides": [
@@ -87,6 +76,7 @@
8776
"files": [
8877
"**/goldens/**",
8978
"**/rollup.config.js",
79+
"**/rollup.config.*.js",
9080
"**/src/test-gen/**",
9181
"**/src/test/**",
9282
"**/src/tests/**",

package-lock.json

Lines changed: 112 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/labs/analyzer/package.json

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
"type": "module",
1818
"scripts": {
1919
"build": "wireit",
20-
"test": "wireit"
20+
"test": "wireit",
21+
"test:server": "wireit",
22+
"test:browser": "wireit"
2123
},
2224
"wireit": {
2325
"build": {
@@ -35,9 +37,28 @@
3537
],
3638
"clean": "if-file-deleted"
3739
},
40+
"build:ts-bundle": {
41+
"#comment": "This builds a JS module of typescript for use in browser tests",
42+
"command": "rollup -c rollup.config.typescript.js",
43+
"files": [
44+
"rollup.config.typescript.js"
45+
],
46+
"output": [
47+
"test/browser/typescript.js"
48+
]
49+
},
3850
"test": {
51+
"dependencies": [
52+
"test:server",
53+
"test:browser"
54+
]
55+
},
56+
"test:server": {
3957
"#comment": "The quotes around the file regex must be double quotes on windows!",
40-
"command": "cross-env NODE_OPTIONS=--enable-source-maps uvu test \"_test\\.js$\"",
58+
"command": "uvu test/server \"_test\\.js$\"",
59+
"env": {
60+
"NODE_OPTIONS": "--enable-source-maps"
61+
},
4162
"dependencies": [
4263
"build",
4364
"../../lit:build"
@@ -46,6 +67,22 @@
4667
"test-files/"
4768
],
4869
"output": []
70+
},
71+
"test:browser": {
72+
"command": "node ../../tests/run-web-tests.js \"test/browser/**/*_test.js\" --config ../../tests/web-test-runner.config.js",
73+
"dependencies": [
74+
"build",
75+
"build:ts-bundle",
76+
"../../tests:build"
77+
],
78+
"env": {
79+
"BROWSERS": {
80+
"external": true
81+
},
82+
"MODE": "dev"
83+
},
84+
"files": [],
85+
"output": []
4986
}
5087
},
5188
"files": [
@@ -61,5 +98,9 @@
6198
"dependencies": {
6299
"package-json-type": "^1.0.3",
63100
"typescript": "~5.2.0"
101+
},
102+
"devDependencies": {
103+
"@rollup/plugin-commonjs": "^25.0.7",
104+
"path-module": "^0.1.2"
64105
}
65106
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import commonjs from '@rollup/plugin-commonjs';
2+
import {createRequire} from 'module';
3+
4+
const require = createRequire(import.meta.url);
5+
const typescriptLibPath = require.resolve('typescript/lib/typescript.js');
6+
7+
/**
8+
* This Rollup config generates a JS module version of typescript from its
9+
* CommonJS version.
10+
*/
11+
export default [
12+
{
13+
input: typescriptLibPath,
14+
output: {
15+
file: 'test/browser/typescript.js',
16+
format: 'esm',
17+
},
18+
plugins: [
19+
commonjs({
20+
ignore: (_id) => true,
21+
}),
22+
],
23+
},
24+
];

packages/labs/analyzer/src/lib/analyzer.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class Analyzer implements AnalyzerInterface {
103103
export const getCommandLineFromProgram = (
104104
analyzer: Analyzer
105105
): ts.ParsedCommandLine => {
106-
const {program, typescript, path} = analyzer;
106+
const {program, typescript, path, fs} = analyzer;
107107
const compilerOptions = program.getCompilerOptions();
108108
const files = program.getRootFileNames();
109109
const json = {
@@ -115,7 +115,7 @@ export const getCommandLineFromProgram = (
115115
const packageRoot = path.basename(compilerOptions.configFilePath as string);
116116
return typescript.parseJsonConfigFileContent(
117117
json,
118-
typescript.sys,
118+
fs,
119119
packageRoot,
120120
undefined,
121121
compilerOptions.configFilePath as string
@@ -131,10 +131,6 @@ export const getCommandLineFromProgram = (
131131
// means we can't use ts.getOutputFileNames(), which we isn't needed in
132132
// JS program
133133
);
134-
return typescript.parseJsonConfigFileContent(
135-
json,
136-
typescript.sys,
137-
packageRoot
138-
);
134+
return typescript.parseJsonConfigFileContent(json, fs, packageRoot);
139135
}
140136
};

packages/labs/analyzer/src/lib/javascript/modules.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ export const getModuleInfo = (
6363
);
6464
const jsPath =
6565
absJsPath !== undefined
66-
? absoluteToPackage(absJsPath, rootDir)
66+
? absoluteToPackage(absJsPath, rootDir, analyzer.path.sep)
6767
: ('not/implemented' as PackagePath);
6868
const sourcePath = absoluteToPackage(
6969
analyzer.path.normalize(modulePath) as AbsolutePath,
70-
rootDir
70+
rootDir,
71+
analyzer.path.sep
7172
);
7273
return {
7374
jsPath,

packages/labs/analyzer/src/lib/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ export interface AnalyzerInterface {
781781
| 'parse'
782782
| 'normalize'
783783
| 'isAbsolute'
784+
| 'sep'
784785
>;
785786

786787
addDiagnostic(diagnostic: ts.Diagnostic): void;

packages/labs/analyzer/src/lib/paths.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
import * as pathlib from 'path';
87
import {AnalyzerInterface} from './model.js';
98

109
/**
@@ -26,15 +25,16 @@ export type PackagePath = string & {
2625
*/
2726
export const absoluteToPackage = (
2827
path: AbsolutePath,
29-
packageRoot: AbsolutePath
28+
packageRoot: AbsolutePath,
29+
seperator: string
3030
): PackagePath => {
3131
if (!path.startsWith(packageRoot)) {
3232
throw new Error(`path ${path} is not contained in ${packageRoot}`);
3333
}
3434
let packagePath = path.substring(packageRoot.length);
35-
if (!packageRoot.endsWith(pathlib.sep)) {
35+
if (!packageRoot.endsWith(seperator)) {
3636
// Make sure we don't have path='/abc/def' and root='/ab'
37-
if (!packagePath.startsWith(pathlib.sep)) {
37+
if (!packagePath.startsWith(seperator)) {
3838
throw new Error(`path ${path} is not contained in ${packageRoot}`);
3939
}
4040
packagePath = packagePath.substring(1, packagePath.length);

packages/labs/analyzer/src/lib/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ const getSpecifierFromTypeImport = (
207207
name,
208208
packageJson: {main, module},
209209
} = getPackageInfo(specifier as AbsolutePath, analyzer);
210-
let modulePath = absoluteToPackage(specifier as AbsolutePath, rootDir);
210+
let modulePath = absoluteToPackage(
211+
specifier as AbsolutePath,
212+
rootDir,
213+
analyzer.path.sep
214+
);
211215
const packageMain = module ?? main;
212216
if (packageMain !== undefined && modulePath === packageMain) {
213217
modulePath = '' as PackagePath;

0 commit comments

Comments
 (0)