diff --git a/package.json b/package.json index 46611d004..fe31f51e3 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,7 @@ "outdent": "0.7.1", "parse-srcset": "ikatyang/parse-srcset#54eb9c1cb21db5c62b4d0e275d7249516df6f0ee", "please-upgrade-node": "3.2.0", - "postcss": "8.1.1", + "postcss": "8.1.2", "postcss-less": "3.1.4", "postcss-media-query-parser": "0.2.3", "postcss-scss": "3.0.2", diff --git a/scripts/build/bundler.js b/scripts/build/bundler.js index 046120924..0f3547f1d 100644 --- a/scripts/build/bundler.js +++ b/scripts/build/bundler.js @@ -56,14 +56,26 @@ const entries = [ }, ]; -const webpackNativeShims = (modules) => { - const shims = {}; - for (const module of modules) { - const file = path.join(__dirname, `shims/${module}.mjs`); - shims[module] = fs.existsSync(file) ? file : false; +function webpackNativeShims(config, modules) { + if (!config.resolve) { + config.resolve = {}; } - return shims; -}; + 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 = { @@ -256,28 +268,28 @@ function getWebpackConfig(bundle) { output: { path: path.resolve(root, "dist"), filename: bundle.output, - library: ["prettierPlugins", bundle.name], - libraryTarget: "umd", + 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: [], - fallback: webpackNativeShims(["os", "path", "util", "url"]), }, }; if (bundle.terserOptions) { const TerserPlugin = require("terser-webpack-plugin"); - - config.optimization = { - minimizer: [new TerserPlugin(bundle.terserOptions)], - }; + config.optimization.minimizer = [new TerserPlugin(bundle.terserOptions)]; } + // config.optimization.minimize = false; - return config; + return webpackNativeShims(config, ["os", "path", "util", "url", "fs"]); } function runWebpack(config) { diff --git a/scripts/build/shims/fs.mjs b/scripts/build/shims/fs.mjs new file mode 100644 index 000000000..2ee6ac3c9 --- /dev/null +++ b/scripts/build/shims/fs.mjs @@ -0,0 +1,2 @@ +export const existsSync = () => false; +export const readFileSync = () => ""; diff --git a/yarn.lock b/yarn.lock index a11c12061..a5f19e435 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5865,10 +5865,10 @@ postcss-values-parser@2.0.1: indexes-of "^1.0.1" uniq "^1.0.1" -postcss@8.1.1, postcss@^8.1.0: - version "8.1.1" - resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.1.1.tgz#c3a287dd10e4f6c84cb3791052b96a5d859c9389" - integrity sha512-9DGLSsjooH3kSNjTZUOt2eIj2ZTW0VI2PZ/3My+8TC7KIbH2OKwUlISfDsf63EP4aiRUt3XkEWMWvyJHvJelEg== +postcss@8.1.2, postcss@^8.1.0: + version "8.1.2" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.1.2.tgz#9731fcaa4f7b0bef47121821bdae9eeb609a324c" + integrity sha512-mToqEVFq8jF9TFhlIK4HhE34zknFJuNTgqtsr60vUvrWn+9TIYugCwiV1JZRxCuOrej2jjstun1bn4Bc7/1HkA== dependencies: colorette "^1.2.1" line-column "^1.0.2"