From bd57e37aeb6db70e860c0c9eb0716b780d3ceb91 Mon Sep 17 00:00:00 2001 From: Pig Fang Date: Fri, 15 Feb 2019 11:35:30 +0800 Subject: [PATCH] Build: switch from browserify to webpack (fixes #11366) --- Makefile.js | 49 ++++++++++++++++++++++++++++++++++++++----------- karma.conf.js | 13 ++++++++----- package.json | 9 +++++---- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/Makefile.js b/Makefile.js index fa9227a5b1c..23f5bb3a6a4 100644 --- a/Makefile.js +++ b/Makefile.js @@ -25,6 +25,7 @@ const lodash = require("lodash"), semver = require("semver"), ejs = require("ejs"), loadPerf = require("load-perf"), + webpack = require("webpack"), yaml = require("js-yaml"); const { cat, cd, cp, echo, exec, exit, find, ls, mkdir, pwd, rm, test } = require("shelljs"); @@ -820,17 +821,43 @@ target.browserify = function() { mkdir(BUILD_DIR); } - // 2. browserify the temp directory - exec(`${getBinFile("browserify")} -x espree lib/linter.js -o ${BUILD_DIR}eslint.js -s eslint --global-transform [ babelify --presets [ @babel/preset-env ] ]`); - - // 3. Browserify espree - exec(`${getBinFile("browserify")} -r espree -o ${TEMP_DIR}espree.js --global-transform [ babelify --presets [ @babel/preset-env ] ]`); - - // 4. Concatenate Babel polyfill, Espree, and ESLint files together - cat("./node_modules/@babel/polyfill/dist/polyfill.js", `${TEMP_DIR}espree.js`, `${BUILD_DIR}eslint.js`).to(`${BUILD_DIR}eslint.js`); - - // 5. remove temp directory - rm("-rf", TEMP_DIR); + // 2. run webpack + webpack({ + mode: "none", + entry: ["@babel/polyfill", "./lib/linter.js"], + output: { + path: path.join(__dirname, BUILD_DIR), + filename: "eslint.js", + library: "eslint", + libraryTarget: "umd", + globalObject: "this" + }, + module: { + rules: [ + { + test: path.resolve("./lib/linter.js"), + loader: "string-replace-loader", + options: { + search: "require(parserName)", + replace: "(parserName === \"espree\" ? require(\"espree\") : require(parserName))" + } + }, + { + test: /\.js$/, + loader: "babel-loader", + options: { + presets: ["@babel/preset-env"] + }, + exclude: /node_modules/ + } + ] + } + }, (err, stats) => { + if (err || stats.hasErrors()) { + console.error("webpack build failed."); + exit(1); + } + }); }; target.checkRuleFiles = function() { diff --git a/karma.conf.js b/karma.conf.js index 9b26395e988..43ec559ac5a 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -32,12 +32,15 @@ module.exports = function(config) { // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - "tests/lib/linter.js": ["babel"] + "tests/lib/linter.js": ["webpack"] }, - babelPreprocessor: { - options: { - presets: ["@babel/preset-env"] - } + webpack: { + mode: "none", + stats: "errors-only" + }, + webpackMiddleware: { + stats: "errors-only", + clientLogLevel: "error" }, diff --git a/package.json b/package.json index cd25a4013a8..0be3cb7a127 100644 --- a/package.json +++ b/package.json @@ -76,10 +76,9 @@ "@babel/core": "^7.2.2", "@babel/polyfill": "^7.2.5", "@babel/preset-env": "^7.3.1", - "babelify": "^10.0.0", + "babel-loader": "^8.0.5", "beefy": "^2.1.8", "brfs": "^2.0.0", - "browserify": "^16.2.2", "chai": "^4.0.1", "cheerio": "^0.22.0", "common-tags": "^1.8.0", @@ -96,10 +95,10 @@ "istanbul": "^0.4.5", "jsdoc": "^3.5.5", "karma": "^3.1.4", - "karma-babel-preprocessor": "^8.0.0", "karma-chrome-launcher": "^2.2.0", "karma-mocha": "^1.3.0", "karma-mocha-reporter": "^2.2.3", + "karma-webpack": "^3.0.5", "leche": "^2.2.3", "load-perf": "^0.2.0", "markdownlint": "^0.12.0", @@ -110,8 +109,10 @@ "puppeteer": "^1.12.2", "shelljs": "^0.8.2", "sinon": "^3.3.0", + "string-replace-loader": "^2.1.1", "temp": "^0.9.0", - "through": "^2.3.8" + "through": "^2.3.8", + "webpack": "^4.29.3" }, "keywords": [ "ast",