Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: get rid of webpack #8314

Merged
merged 11 commits into from
Nov 10, 2020
Merged
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@
"snapshot-diff": "0.8.1",
"strip-ansi": "6.0.0",
"synchronous-promise": "2.0.15",
"tempy": "1.0.0",
"terser-webpack-plugin": "5.0.3",
"webpack": "5.4.0"
"tempy": "1.0.0"
},
"scripts": {
"prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1",
Expand Down
110 changes: 5 additions & 105 deletions scripts/build/bundler.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -203,6 +180,7 @@ function getRollupConfig(bundle) {
output: {
ascii_only: true,
},
...bundle.terserOptions,
}),
].filter(Boolean);

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 };
};
26 changes: 5 additions & 21 deletions scripts/build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
Expand Down
Loading