Skip to content

Commit

Permalink
Bundle/minify extension code with webpack for quicker load (fixes #83).
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Aug 9, 2019
1 parent 4063c9f commit f209fef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .vscodeignore
@@ -1,7 +1,10 @@
.vscode
node_modules
.eslintrc.json
.gitignore
.markdownlint.json
default-config.json
extension.js
generate-config-schema.js
jsconfig.json
webpack.config.js
45 changes: 14 additions & 31 deletions extension.js
Expand Up @@ -2,23 +2,16 @@

// Requires
const vscode = require("vscode");
const markdownlint = require("markdownlint");
const fs = require("fs");
const path = require("path");

// Lazy requires
function lazyRequire (name) {
let module = null;
return new Proxy({}, {
"get": (obj, prop) => {
module = module || require(name);
return module[prop];
}
});
}
const markdownlint = lazyRequire("markdownlint");
const minimatch = lazyRequire("minimatch");
const jsYaml = lazyRequire("js-yaml");
const fs = lazyRequire("fs");
const os = lazyRequire("os");
const path = lazyRequire("path");
// Optional requires are inlined to avoid startup cost

// @ts-ignore
// eslint-disable-next-line camelcase, multiline-ternary, no-undef
const nodeRequire = (typeof __non_webpack_require__ === "undefined") ? require : __non_webpack_require__;
// Capture Node.js require implementation for dynamic loading of custom rules

// Constants
const extensionDisplayName = "markdownlint";
Expand All @@ -36,8 +29,7 @@ const documentSelector = {
};
const configParsers = [
JSON.parse,
// @ts-ignore
(content) => jsYaml.safeLoad(content)
(content) => require("js-yaml").safeLoad(content)
];

const clickForInfo = "Click for more information about ";
Expand Down Expand Up @@ -145,7 +137,6 @@ function outputLine (message, show) {
// Returns rule configuration from nearest config file or workspace
function getConfig (document) {
const name = document.fileName;
// @ts-ignore
let dir = path.dirname(name);
let workspaceDetail = "not in a workspace folder";

Expand All @@ -159,9 +150,7 @@ function getConfig (document) {
if (configMap[dir] === undefined) {
// Look for config file in current directory
for (const configFileName of configFileNames) {
// @ts-ignore
const configFilePath = path.join(dir, configFileName);
// @ts-ignore
if (fs.existsSync(configFilePath)) {
outputLine("INFO: Loading custom configuration from '" + configFilePath +
"', overrides user/workspace/custom configuration for directory and its children.");
Expand All @@ -180,7 +169,6 @@ function getConfig (document) {
// Remember missing or invalid file
configMap[dir] = null;
}
// @ts-ignore
const parent = path.dirname(dir);
// Move to parent directory, stop if no parent
if (dir === parent) {
Expand Down Expand Up @@ -212,8 +200,7 @@ function getConfig (document) {

// Bootstrap extend behavior into readConfigSync
if (userWorkspaceConfig && userWorkspaceConfig.extends) {
// @ts-ignore
const extendPath = path.resolve(os.homedir(), userWorkspaceConfig.extends);
const extendPath = path.resolve(require("os").homedir(), userWorkspaceConfig.extends);
try {
// @ts-ignore
const extendConfig = markdownlint.readConfigSync(extendPath, configParsers);
Expand Down Expand Up @@ -276,13 +263,11 @@ function getCustomRules () {
if (!extension) {
throw new Error(`Extension '${extensionName}' not installed`);
}
// @ts-ignore
resolvedPath = path.resolve(extension.extensionPath, relativePath);
} else {
// @ts-ignore
resolvedPath = path.resolve(workspacePath, rulePath);
}
const exports = require(resolvedPath);
const exports = nodeRequire(resolvedPath);
const rules = Array.isArray(exports) ?
exports :
[ exports ];
Expand Down Expand Up @@ -319,8 +304,7 @@ function getIgnores () {
const configuration = vscode.workspace.getConfiguration(extensionDisplayName);
const ignorePaths = configuration.get("ignore");
ignorePaths.forEach((ignorePath) => {
// @ts-ignore
const ignore = minimatch.makeRe(ignorePath, {
const ignore = require("minimatch").makeRe(ignorePath, {
"dot": true,
"nocomment": true
});
Expand Down Expand Up @@ -348,7 +332,6 @@ function lint (document) {
// Check ignore list
const diagnostics = [];
const relativePath = vscode.workspace.asRelativePath(document.uri, false);
// @ts-ignore
const normalizedPath = relativePath.split(path.sep).join("/");
if (getIgnores().every((ignore) => !ignore.test(normalizedPath))) {

Expand All @@ -368,8 +351,8 @@ function lint (document) {
// Lint and create Diagnostics
try {
markdownlint
// @ts-ignore
.sync(options)[uri]
// @ts-ignore
.forEach((result) => {
const ruleName = result.ruleNames[0];
const ruleDescription = result.ruleDescription;
Expand Down
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -16,7 +16,8 @@
"url": "https://github.com/DavidAnson/vscode-markdownlint/issues"
},
"scripts": {
"lint": "eslint *.js",
"compile": "webpack --mode production",
"lint": "eslint --ignore-pattern bundle.js *.js",
"schema": "node generate-config-schema.js"
},
"categories": [
Expand All @@ -25,18 +26,20 @@
"engines": {
"vscode": "^1.31.0"
},
"main": "./extension",
"main": "./bundle",
"dependencies": {
"js-yaml": "3.13.1",
"markdownlint": "0.16.0",
"markdown-it-katex": "2.0.3",
"minimatch": "3.0.4"
},
"devDependencies": {
"@types/node": "~12.6.9",
"@types/vscode": "~1.31.0",
"eslint": "~6.1.0",
"vsce": "~1.66.0",
"@types/node": "~12.6.9",
"@types/vscode": "~1.31.0"
"webpack": "~4.39.1",
"webpack-cli": "~3.3.6"
},
"keywords": [
"markdown",
Expand Down
15 changes: 15 additions & 0 deletions webpack.config.js
@@ -0,0 +1,15 @@
"use strict";

const config = {
"target": "node",
"entry": "./extension.js",
"output": {
"path": __dirname,
"filename": "bundle.js",
"libraryTarget": "commonjs2"
},
"externals": {
"vscode": "commonjs vscode"
}
};
module.exports = config;

0 comments on commit f209fef

Please sign in to comment.