Skip to content

Commit

Permalink
use webpack to build extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Oct 26, 2019
1 parent 0a06e93 commit a9c1a86
Show file tree
Hide file tree
Showing 9 changed files with 1,768 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
out
dist
node_modules
.vscode-test
*.vsix
Expand Down
12 changes: 5 additions & 7 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,31 @@
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"preLaunchTask": "npm: compile"
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: webpack"
},
{
"name": "Run Extension (With Other Extensions)",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"preLaunchTask": "npm: compile"
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"preLaunchTask": "npm: webpack"
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"stopOnEntry": false,
"sourceMaps": true,
"args": [
"${workspaceFolder}/test-fixtures/test.code-workspace",
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
"preLaunchTask": "npm: compile"
"preLaunchTask": "npm: test-compile"
}
]
}
43 changes: 11 additions & 32 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
.vscode/**
.vscode-test/**
out/test/**
src/**
test-fixtures/**
testWorkspaceFolder/**
testWorkspace.code-workspace
.vscode
.vscode-test
out/
src/
test-fixtures/
tsconfig.json
webpack.config.js
.prettierignore
azure-pipelines.yml
scripts/**
**/*.map
.gitignore
.github/**
tsconfig.json
Expand All @@ -17,27 +16,7 @@ CONTRIBUTORS.md
CONTRIBUTING.md
CODE_OF_CONDUCT.md

node_modules/**/*.yml
node_modules/**/*.yaml
node_modules/**/*.md
node_modules/**/*.markdown
node_modules/**/*.mailmap
node_modules/**/*.txt
node_modules/**/*.lock
node_modules/**/*/*.spec.js
node_modules/**/*/*.test.js

node_modules/**/*/.*ignore
node_modules/**/*/.*rc

node_modules/**/*/test/
node_modules/**/*/tests/
node_modules/**/*/spec/
node_modules/**/*/specs/

node_modules/**/*/docs/
node_modules/**/*/example/
node_modules/**/*/examples/

node_modules/**/*/.editorconfig
node_modules/**/*/bower.json
node_modules
!node_modules/prettier
!node_modules/spdx-exceptions
!node_modules/spdx-license-ids
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,19 @@
"*"
],
"icon": "icon.png",
"main": "./out/extension",
"main": "./dist/extension",
"scripts": {
"clean": "rm -rf out",
"vscode:prepublish": "yarn compile",
"compile": "yarn clean && tsc -p ./",
"watch": "tsc --watch -p ./",
"clean": "rm -rf out && rm -rf dist",
"lint": "tslint --project .",
"pretest": "yarn compile && cd test-fixtures/eslint && yarn install && cd ../tslint && yarn install && cd ../plugins && yarn install",
"pretest": "yarn test-compile && cd test-fixtures/eslint && yarn install && cd ../tslint && yarn install && cd ../plugins && yarn install",
"prettier": "prettier --write '**/*.{ts,json,md,yml,js}'",
"test-compile": "yarn clean && tsc -p ./ && yarn webpack",
"test": "node ./out/test/runTests.js",
"version": "node ./scripts/version.js && git add CHANGELOG.md",
"prettier": "prettier --write '**/*.{ts,json,md,yml,js}'"
"vscode:prepublish": "webpack --mode production",
"watch": "tsc --watch -p ./",
"webpack-dev": "webpack --mode development --watch",
"webpack": "webpack --mode development"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -72,10 +74,13 @@
"lint-staged": "^9.4.2",
"mocha": "^6.2.1",
"mocha-junit-reporter": "^1.23.1",
"ts-loader": "^6.2.1",
"tslint": "^5.20.0",
"tslint-config-prettier": "^1.18.0",
"vscode-nls-dev": "^3.3.1",
"vscode-test": "^1.2.0"
"vscode-test": "^1.2.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.9"
},
"dependencies": {
"ignore": "^5.1.4",
Expand Down
19 changes: 18 additions & 1 deletion src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import * as resolve from "resolve";
import { LoggingService } from "./LoggingService";
import { PrettierModule } from "./types";

declare const __webpack_require__: typeof require;
declare const __non_webpack_require__: typeof require;

export class ModuleResolver {
constructor(private loggingService: LoggingService) {}

Expand Down Expand Up @@ -48,7 +51,7 @@ export class ModuleResolver {
`Loaded module '${pkgName}' from '${modulePath}'.`,
"INFO"
);
return require(modulePath);
return this.loadNodeModule(modulePath);
}
} catch (e) {
this.loggingService.appendLine(
Expand All @@ -58,6 +61,20 @@ export class ModuleResolver {
}
}

// Source: https://github.com/microsoft/vscode-eslint/blob/master/server/src/eslintServer.ts#L209
private loadNodeModule(moduleName: string): any | undefined {
const r =
typeof __webpack_require__ === "function"
? __non_webpack_require__
: require;
try {
return r(moduleName);
} catch (err) {
this.loggingService.appendObject(err.stack);
}
return undefined;
}

/**
* Recursively search for a package.json upwards containing given package
* as a dependency or devDependency.
Expand Down
13 changes: 9 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ import {

// the application insights key (also known as instrumentation key)
const telemetryKey = "93c48152-e880-42c1-8652-30ad62ce8b49";
const extensionName = process.env.EXTENSION_NAME || "prettier.prettier-vscode";
const extensionVersion = process.env.EXTENSION_VERSION || "0.0.0";

// telemetry reporter
let reporter: TelemetryReporter;

export function activate(context: ExtensionContext) {
const loggingService = new LoggingService();

loggingService.appendLine(`Extension Name: ${extensionName}.`, "INFO");
loggingService.appendLine(`Extension Version: ${extensionVersion}.`, "INFO");

// create telemetry reporter on extension activation
const extensionPackage = require(context.asAbsolutePath("./package.json"));
reporter = new TelemetryReporter(
extensionPackage.name,
extensionPackage.version,
extensionName,
extensionVersion,
telemetryKey
);

Expand All @@ -39,7 +45,6 @@ export function activate(context: ExtensionContext) {
tslint: config.tslintIntegration ? 1 : 0
});

const loggingService = new LoggingService();
const moduleResolver = new ModuleResolver(loggingService);
const ignoreReslver = new IgnorerResolver(loggingService);
const configResolver = new ConfigResolver(loggingService);
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@
"sourceMap": true,
"rootDir": "src"
},
"exclude": ["node_modules", ".vscode-test", "test-fixtures"]
"exclude": ["node_modules", ".vscode-test", "test-fixtures"],
"include": [
"src/**/*.ts",
"./node_modules/vscode/vscode.d.ts",
"./node_modules/vscode/lib/*"
]
}
63 changes: 63 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//@ts-check

"use strict";

const webpack = require("webpack");
const path = require("path");
// @ts-ignore
const extensionPackage = require("./package.json");

/**@type {import('webpack').Configuration}*/
const config = {
target: "node", // vscode extensions run in a Node.js-context πŸ“– -> https://webpack.js.org/configuration/node/

entry: "./src/extension.ts", // the entry point of this extension, πŸ“– -> https://webpack.js.org/configuration/entry-context/
output: {
// the bundle is stored in the 'dist' folder (check package.json), πŸ“– -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, "dist"),
filename: "extension.js",
libraryTarget: "commonjs2",
devtoolModuleFilenameTemplate: "../[resource-path]"
},
plugins: [
new webpack.DefinePlugin({
EXTENSION_NAME: JSON.stringify(extensionPackage.name),
EXTENSION_VERSION: JSON.stringify(extensionPackage.version)
})
],
devtool: "source-map",
externals: {
vscode: "commonjs vscode", // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, πŸ“– -> https://webpack.js.org/configuration/externals/
prettier: "commonjs prettier",
"spdx-exceptions": "spdx-exceptions",
"spdx-license-ids": "spdx-license-ids",
"spdx-license-ids/deprecated": "spdx-license-ids/deprecated",
"applicationinsights-native-metrics": "applicationinsights-native-metrics" // This isn't actually used, it is just to disable a webpack error we don't care about.
},
resolve: {
// support reading TypeScript and JavaScript files, πŸ“– -> https://github.com/TypeStrong/ts-loader
extensions: [".ts", ".js"]
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader"
}
]
},
{
// vscode-nls-dev loader:
// * rewrite nls-calls
loader: "vscode-nls-dev/lib/webpack-loader",
options: {
base: path.join(__dirname, "src")
}
}
]
}
};
module.exports = config;
Loading

0 comments on commit a9c1a86

Please sign in to comment.