Skip to content

Commit

Permalink
Build: get rid of webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 15, 2020
1 parent d47a225 commit 97b8f80
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1,455 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,7 @@
"snapshot-diff": "0.8.0",
"strip-ansi": "6.0.0",
"synchronous-promise": "2.0.12",
"tempy": "0.5.0",
"terser-webpack-plugin": "3.0.1",
"webpack": "4.43.0"
"tempy": "0.5.0"
},
"scripts": {
"prepublishOnly": "echo \"Error: must publish from dist/\" && exit 1",
Expand Down
65 changes: 5 additions & 60 deletions scripts/build/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const execa = require("execa");
const path = require("path");
const { rollup } = require("rollup");
const webpack = require("webpack");
const resolve = require("@rollup/plugin-node-resolve");
const rollupPluginAlias = require("@rollup/plugin-alias");
const commonjs = require("@rollup/plugin-commonjs");
Expand Down Expand Up @@ -178,7 +177,9 @@ function getRollupConfig(bundle) {
externals(bundle.externals),
bundle.target === "universal" && nodeGlobals(),
babel(babelConfig),
bundle.minify !== false && bundle.target === "universal" && terser(),
bundle.minify !== false &&
bundle.target === "universal" &&
terser(bundle.terserOptions),
].filter(Boolean);

if (bundle.target === "node") {
Expand All @@ -204,58 +205,6 @@ function getRollupOutputOptions(bundle) {
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 = {
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: ["prettierPlugins", bundle.name],
libraryTarget: "umd",
// https://github.com/webpack/webpack/issues/6642
globalObject: 'new Function("return this")()',
},
};

if (bundle.terserOptions) {
const TerserPlugin = require("terser-webpack-plugin");

config.optimization = {
minimizer: [new TerserPlugin(bundle.terserOptions)],
};
}

return config;
}

function runWebpack(config) {
return new Promise((resolve, reject) => {
webpack(config, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}

module.exports = async function createBundle(bundle, cache) {
const inputOptions = getRollupConfig(bundle);
const outputOptions = getRollupOutputOptions(bundle);
Expand All @@ -277,12 +226,8 @@ module.exports = async function createBundle(bundle, cache) {
}
}

if (bundle.bundler === "webpack") {
await runWebpack(getWebpackConfig(bundle));
} else {
const result = await rollup(inputOptions);
await result.write(outputOptions);
}
const result = await rollup(inputOptions);
await result.write(outputOptions);

return { bundled: true };
};
17 changes: 5 additions & 12 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 UMD 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 @@ -42,18 +41,12 @@ 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: {
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,
},
},
},
Expand Down
Loading

0 comments on commit 97b8f80

Please sign in to comment.