Build: get rid of webpack (#8314)

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
This commit is contained in:
fisker Cheunganddependabot[bot] authored and GitHub committed 2020-11-10 09:15:53 +08:00
1 parent 2f86c13cac
commit e9fc39eeeb
4 files changed
+23 -443

No files matched your search

+5 -105
View File
@@ -1,10 +1,8 @@
"use strict";
const path = require("path");
const fs = require("fs");
const execa = require("execa");
const { rollup } = require("rollup");
const webpack = require("webpack");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
const rollupPluginAlias = require("@rollup/plugin-alias");
const commonjs = require("@rollup/plugin-commonjs");
@@ -56,27 +54,6 @@ const entries = [
},
];
function webpackNativeShims(config, modules) {
if (!config.resolve) {
config.resolve = {};
}
const { resolve } = config;
resolve.alias = resolve.alias || {};
resolve.fallback = resolve.fallback || {};
for (const module of modules) {
if (module in resolve.alias || module in resolve.fallback) {
throw new Error(`fallback/alias for "${module}" already exists.`);
}
const file = path.join(__dirname, `shims/${module}.mjs`);
if (fs.existsSync(file)) {
resolve.alias[module] = file;
} else {
resolve.fallback[module] = false;
}
}
return config;
}
function getBabelConfig(bundle) {
const config = {
babelrc: false,
@@ -203,6 +180,7 @@ function getRollupConfig(bundle) {
output: {
ascii_only: true,
},
...bundle.terserOptions,
}),
].filter(Boolean);
@@ -226,7 +204,7 @@ function getRollupOutputOptions(bundle, buildOptions) {
options.name =
bundle.type === "plugin" ? `prettierPlugins.${bundle.name}` : bundle.name;
if (!bundle.format && bundle.bundler !== "webpack") {
if (!bundle.format) {
return [
{
...options,
@@ -242,86 +220,12 @@ function getRollupOutputOptions(bundle, buildOptions) {
options.format = bundle.format;
}
if (buildOptions.playground && bundle.bundler !== "webpack") {
if (buildOptions.playground) {
return { skipped: true };
}
return [options];
}
function getWebpackConfig(bundle) {
if (bundle.type !== "plugin" || bundle.target !== "universal") {
throw new Error("Must use rollup for this bundle");
}
const root = path.resolve(__dirname, "..", "..");
const config = {
mode: "production",
performance: { hints: false },
entry: path.resolve(root, bundle.input),
module: {
rules: [
{
test: /\.js$/,
use: {
loader: "babel-loader",
options: getBabelConfig(bundle),
},
},
],
},
output: {
path: path.resolve(root, "dist"),
filename: bundle.output,
library: {
type: "umd",
name: ["prettierPlugins", bundle.name],
},
// https://github.com/webpack/webpack/issues/6642
globalObject: 'new Function("return this")()',
},
optimization: {},
resolve: {
// Webpack@5 can't resolve "postcss/lib/parser" and "postcss/lib/stringifier"" imported by `postcss-scss`
// Ignore `exports` field to fix bundle script
exportsFields: [],
},
};
if (bundle.terserOptions) {
const TerserPlugin = require("terser-webpack-plugin");
config.optimization.minimizer = [new TerserPlugin(bundle.terserOptions)];
}
// config.optimization.minimize = false;
return webpackNativeShims(config, ["os", "path", "util", "url", "fs"]);
}
function runWebpack(config) {
return new Promise((resolve, reject) => {
webpack(config, (error, stats) => {
if (error) {
reject(error);
return;
}
if (stats.hasErrors()) {
const { errors } = stats.toJson();
const error = new Error(errors[0].message);
error.errors = errors;
reject(error);
return;
}
if (stats.hasWarnings()) {
const { warnings } = stats.toJson();
console.warn(warnings);
}
resolve();
});
});
}
async function checkCache(cache, inputOptions, outputOption) {
const useCache = await cache.checkBundle(
outputOption.file,
@@ -362,12 +266,8 @@ module.exports = async function createBundle(bundle, cache, options) {
return { cached: true };
}
if (bundle.bundler === "webpack") {
await runWebpack(getWebpackConfig(bundle));
} else {
const result = await rollup(inputOptions);
await Promise.all(outputOptions.map((option) => result.write(option)));
}
const result = await rollup(inputOptions);
await Promise.all(outputOptions.map((option) => result.write(option)));
return { bundled: true };
};
+5 -21
View File
@@ -9,7 +9,6 @@ const path = require("path");
* @property {string?} name - name for the UMD bundle (for plugins, it'll be `prettierPlugins.${name}`)
* @property {'node' | 'universal'} target - should generate a CJS only for node or universal bundle
* @property {'core' | 'plugin'} type - it's a plugin bundle or core part of prettier
* @property {'rollup' | 'webpack'} [bundler='rollup'] - define which bundler to use
* @property {CommonJSConfig} [commonjs={}] - options for `rollup-plugin-commonjs`
* @property {string[]} externals - array of paths that should not be included in the final bundle
* @property {Object.<string, string>} replace - map of strings to replace when processing the bundle
@@ -53,30 +52,15 @@ const parsers = [
},
{
input: "src/language-css/parser-postcss.js",
// postcss has dependency cycles that don't work with rollup
bundler: "webpack",
terserOptions: {
// prevent terser generate extra .LICENSE file
extractComments: false,
terserOptions: {
// prevent U+FFFE in the output
output: {
ascii_only: true,
},
mangle: {
// postcss need keep_fnames when minify
keep_fnames: true,
// we don't transform class anymore, so we need keep_classnames too
keep_classnames: true,
},
mangle: {
// postcss need keep_fnames when minify
keep_fnames: true,
// we don't transform class anymore, so we need keep_classnames too
keep_classnames: true,
},
},
},
{
input: "dist/parser-postcss.js",
output: "esm/parser-postcss.mjs",
format: "esm",
},
{
input: "src/language-graphql/parser-graphql.js",
},