From d65c9869a961baa268b8cf9c9c9b7dde89f74a98 Mon Sep 17 00:00:00 2001 From: Adaline Valentina Simonian Date: Tue, 28 Sep 2021 20:30:30 -0700 Subject: [PATCH] fix: add syntax entry points to fix lazy load bug (#238) Fixes #237 The bundler cannot recognize the requires in the stylelint library for syntaxes, due to the lazy loads in the form `importLazy('package')` obscuring the require that is taking place. This patch adds each syntax as its own entry point to bypass this limitation. It will no longer be necessary when targeting stylelint 14. see: https://github.com/stylelint/stylelint/blob/13.13.1/lib/syntaxes/index.js --- bundle.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 bundle.js diff --git a/bundle.js b/bundle.js new file mode 100644 index 00000000..0c0c32d2 --- /dev/null +++ b/bundle.js @@ -0,0 +1,45 @@ +'use strict'; + +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable no-process-exit */ +/* eslint-disable node/no-unpublished-require */ + +const fs = require('fs-extra'); +const esbuild = require('esbuild'); +const glob = require('fast-glob'); + +const args = new Set(process.argv.slice(2)); + +async function bundle() { + const entryPoints = [ + 'index.js', + 'server.js', + // Necessary because bundler cannot recognize lazy loads in + // https://github.com/stylelint/stylelint/blob/13.13.1/lib/syntaxes/index.js#L9-L15 + ...(await glob('node_modules/stylelint/lib/syntaxes/syntax-*.js')), + ]; + + for (const path of await glob('dist/*')) { + await fs.remove(path); + } + + try { + await esbuild.build({ + entryPoints, + entryNames: '[name]', + bundle: true, + outdir: 'dist', + external: ['vscode'], + format: 'cjs', + platform: 'node', + logLevel: 'info', + watch: args.has('--watch'), + sourcemap: args.has('--sourcemap'), + minify: args.has('--minify'), + }); + } catch { + process.exit(1); + } +} + +bundle(); diff --git a/package.json b/package.json index 1e20e8ee..622679ce 100644 --- a/package.json +++ b/package.json @@ -233,7 +233,7 @@ }, "scripts": { "ci:download-vscode": "node ci-download-vscode.js", - "bundle-base": "esbuild ./index.js ./server.js --bundle --outdir=dist --external:vscode --format=cjs --platform=node", + "bundle-base": "node bundle.js", "bundle": "npm run bundle-base -- --sourcemap", "bundle-watch": "npm run bundle-base -- --sourcemap --watch", "format": "prettier . --write",