Skip to content

Commit

Permalink
VSCode extension: Use rollup for dev build too (#2730)
Browse files Browse the repository at this point in the history
Problem is vscode still doesn't allow loading es modules so we had all
those hacks to make it work but loading es modules from comon js is
always problematic. Using rollup to generate a commonjs output solve the
problem and we are using that for the published version just not in dev.
Issue with this new approach is `rush watch` won't build the extension
but that's also not something you often need to touch.
  • Loading branch information
timotheeguerin authored Dec 14, 2023
1 parent 9f15ba5 commit 216d28e
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/internal-build-utils",
"comment": "Fix third party generation when there is invalid sourcemaps",
"type": "none"
}
],
"packageName": "@typespec/internal-build-utils"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "typespec-vscode",
"comment": "",
"type": "none"
}
],
"packageName": "typespec-vscode"
}
4 changes: 4 additions & 0 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions packages/internal-build-utils/src/generate-third-party-notice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ async function findThirdPartyPackages() {
const contents = JSON.parse(await readFile(map, "utf-8"));
const sources = contents.sources;
for (const source of sources) {
const sourcePath = join(dirname(map), source);
const sourcePath = resolve(dirname(map), source);
const packageRoot = await getPackageRoot(sourcePath);
if (packageRoot === undefined) {
continue;
}
const pkg = JSON.parse(await readFile(join(packageRoot, "package.json"), "utf-8"));

if (pkg.name === rootName || /microsoft/i.test(JSON.stringify(pkg.author))) {
Expand Down Expand Up @@ -80,15 +83,18 @@ async function* projectSourcemaps(rootPath: string): any {

yield* projectSourcemaps(filepath);
} else {
if (file.name.endsWith(".js.map")) {
if (file.name.endsWith(".js.map") || file.name.endsWith(".cjs.map")) {
yield filepath;
}
}
}
}

async function getPackageRoot(filename: string): Promise<any> {
async function getPackageRoot(filename: string): Promise<string | undefined> {
const dir = dirname(filename);
if (dir === "/") {
return undefined;
}
try {
const pkgPath = join(dir, "package.json");
await stat(pkgPath);
Expand Down
2 changes: 1 addition & 1 deletion packages/typespec-vscode/.vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# Then explicitly include what we want to ship in VSIX
!dist/**/*.tmLanguage
!dist/**/*.js
!dist/**/*.cjs
!dist/**/*.js.map
!dist/**/language-configuration.json
!extension-shim.js
!markdown-typespec.json
!README.md
!ThirdPartyNotices.txt
Expand Down
3 changes: 0 additions & 3 deletions packages/typespec-vscode/extension-shim.js

This file was deleted.

14 changes: 7 additions & 7 deletions packages/typespec-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"categories": [
"Programming Languages"
],
"type": "commonjs",
"main": "./extension-shim.js",
"type": "module",
"main": "./dist/src/extension.cjs",
"engines": {
"vscode": "^1.85.0"
},
Expand Down Expand Up @@ -106,20 +106,20 @@
]
},
"scripts": {
"clean": "rimraf ./dist ./dist-dev ./temp",
"build": "npm run compile && npm run rollup && npm run copy-tmlanguage && npm run generate-language-configuration && npm run generate-third-party-notices && npm run package-vsix",
"compile": "tsc -p .",
"watch": "tsc -p . --watch",
"clean": "rimraf ./dist ./temp",
"build": "npm run compile && npm run copy-tmlanguage && npm run generate-language-configuration && npm run generate-third-party-notices && npm run package-vsix",
"compile": "rollup --config rollup.config.ts --configPlugin typescript --failAfterWarnings 2>&1",
"watch": "rollup --config rollup.config.ts --configPlugin typescript --watch",
"dogfood": "node scripts/dogfood.js",
"copy-tmlanguage": "node scripts/copy-tmlanguage.js",
"generate-language-configuration": "node scripts/generate-language-configuration.js",
"generate-third-party-notices": "typespec-build-tool generate-third-party-notices",
"rollup": "rollup --config --failAfterWarnings 2>&1",
"package-vsix": "vsce package"
},
"devDependencies": {
"@rollup/plugin-commonjs": "~25.0.4",
"@rollup/plugin-node-resolve": "~15.2.1",
"@rollup/plugin-typescript": "~11.1.5",
"@types/mocha": "~10.0.6",
"@types/node": "~18.11.9",
"@types/vscode": "~1.85.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
// @ts-check
import commonjs from "@rollup/plugin-commonjs";
import resolve from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";

import { defineConfig } from "rollup";

export default defineConfig({
input: "dist-dev/src/extension.js",
input: "src/extension.ts",
output: {
file: "dist/src/extension.js",
file: "dist/src/extension.cjs",
format: "commonjs",
sourcemap: true,
exports: "named",
inlineDynamicImports: true,
},
external: ["fs/promises", "vscode"],
plugins: [resolve({ preferBuiltins: true }), commonjs()],
plugins: [
(resolve as any)({ preferBuiltins: true }),
(commonjs as any)(),
(typescript as any)({ tsconfig: "./tsconfig.build.json" }),
],
onwarn: (warning, warn) => {
if (warning.code === "CIRCULAR_DEPENDENCY") {
// filter out warnings about circular dependencies out of our control
Expand Down
13 changes: 13 additions & 0 deletions packages/typespec-vscode/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// tsconfig for building the playground
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"declaration": false,
"declarationMap": false,
"sourceRoot": ".."
},
"include": ["src/**/*.ts"]
}
17 changes: 9 additions & 8 deletions packages/typespec-vscode/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"extends": "../tsconfig.json",
"references": [{ "path": "../tmlanguage-generator/tsconfig.json" }],
"compilerOptions": {
"outDir": "dist-dev",
"rootDir": ".",
"tsBuildInfoFile": "temp/tsconfig.tsbuildinfo",
"module": "ES2022",
"moduleResolution": "Bundler",
"skipLibCheck": true,
"types": ["node", "mocha"]
"incremental": false,
"composite": false,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"sourceMap": false,
"resolveJsonModule": true,
"outDir": "dist",
"rootDir": "."
},
"include": ["src/**/*.ts", "test/**/*.ts"]
"include": ["rollup.config.ts", "src/**/*.ts", "test/**/*.ts"]
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
{ "path": "packages/openapi3/tsconfig.json" },
{ "path": "packages/lint/tsconfig.json" },
{ "path": "packages/migrate/tsconfig.json" },
{ "path": "packages/typespec-vscode/tsconfig.json" },
{ "path": "packages/internal-build-utils/tsconfig.json" },
{ "path": "packages/bundle-uploader/tsconfig.json" },
{ "path": "packages/tmlanguage-generator/tsconfig.json" },
Expand Down

0 comments on commit 216d28e

Please sign in to comment.