From 7de4cefe12ebc8b774b8f9ad81de973a28a769bb Mon Sep 17 00:00:00 2001 From: jankapunkt Date: Wed, 6 Dec 2023 16:08:39 +0100 Subject: [PATCH 1/2] migration: remove Fibers; use async/await --- plugin/minify-css.js | 80 +++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/plugin/minify-css.js b/plugin/minify-css.js index d257f1c..63b77de 100644 --- a/plugin/minify-css.js +++ b/plugin/minify-css.js @@ -1,14 +1,15 @@ // Makes sure we can load peer dependencies from app's directory. // See: https://github.com/juliancwirko/meteor-postcss/issues/15 // https://github.com/meteor/meteor/issues/10827 +import postCSS from 'postcss' + Npm.require('app-module-path/cwd'); import { checkNpmVersions } from 'meteor/tmeasday:check-npm-versions'; -import Future from 'fibers/future'; import sourcemap from 'source-map'; checkNpmVersions({ - 'postcss': '8.3.x', + 'postcss': '8.4.x', 'postcss-load-config': '3.1.x' }, 'juliancwirko:postcss'); @@ -28,14 +29,14 @@ var postcssConfigPlugins = []; var postcssConfigParser = null; var postcssConfigExcludedPackages = []; -var loadPostcssConfig = function () { +var loadPostcssConfig = async function () { if (!loaded) { loaded = true; var config; try { const load = require('postcss-load-config'); - config = Promise.await(load({meteor: true})); + config = await load({meteor: true}); postcssConfigPlugins = config.plugins || []; postcssConfigParser = config.options.parser || null; postcssConfigExcludedPackages = config.options.excludedPackages || []; @@ -70,8 +71,8 @@ var isNotImport = function (inputFileUrl) { function CssToolsMinifier() {}; -CssToolsMinifier.prototype.processFilesForBundle = function (files, options) { - loadPostcssConfig(); +CssToolsMinifier.prototype.processFilesForBundle = async function (files, options) { + await loadPostcssConfig(); var mode = options.minifyMode; @@ -85,7 +86,7 @@ CssToolsMinifier.prototype.processFilesForBundle = function (files, options) { } }); - var merged = mergeCss(filesToMerge); + var merged = await mergeCss(filesToMerge); if (mode === 'development') { files[0].addStylesheet({ @@ -110,18 +111,18 @@ CssToolsMinifier.prototype.processFilesForBundle = function (files, options) { // Lints CSS files and merges them into one file, fixing up source maps and // pulling any @import directives up to the top since the CSS spec does not // allow them to appear in the middle of a file. -var mergeCss = function (css) { +var mergeCss = async function (css) { // Filenames passed to AST manipulator mapped to their original files var originals = {}; - var postCSS = require('postcss'); + var cssAsts = [] - var cssAsts = css.map(function (file) { + // use for..of to sequencially process + // async results, which won't work in .map + for (const file of css) { var filename = file.getPathInBundle(); originals[filename] = file; - var f = new Future; - - var css; + var currentCss; var postres; var isFileForPostCSS; @@ -131,41 +132,21 @@ var mergeCss = function (css) { isFileForPostCSS = false; } - postCSS(isFileForPostCSS ? postcssConfigPlugins : []) - .process(file.getContentsAsString(), { - from: process.cwd() + file._source.url?.replace('/__cordova', ''), - parser: postcssConfigParser - }) - .then(function (result) { - result.warnings().forEach(function (warn) { - process.stderr.write(warn.toString()); - }); - f.return(result); - }) - .catch(function (error) { - var errMsg = error.message; - if (error.name === 'CssSyntaxError') { - errMsg = error.message + '\n\n' + 'Css Syntax Error.' + '\n\n' + error.message + error.showSourceCode() - } - error.message = errMsg; - f.return(error); - }); - try { var parseOptions = { source: filename, position: true }; - postres = f.wait(); + postres = await processFile({ file, isFileForPostCSS }) if (postres.name === 'CssSyntaxError') { throw postres; } - css = postres.css; + currentCss = postres.css; - var ast = CssTools.parseCss(css, parseOptions); + var ast = CssTools.parseCss(currentCss, parseOptions); ast.filename = filename; } catch (e) { @@ -197,8 +178,8 @@ var mergeCss = function (css) { }; } - return ast; - }); + cssAsts.push(ast); + } var warnCb = function (filename, msg) { // XXX make this a buildmessage.warning call rather than a random log. @@ -254,3 +235,26 @@ var mergeCss = function (css) { sourceMap: newMap.toString() }; }; + +const processFile = async ({ file, isFileForPostCSS }) => { + let result + + try { + result = await postCSS(isFileForPostCSS ? postcssConfigPlugins : []) + .process(file.getContentsAsString(), { + from: process.cwd() + file._source.url?.replace('/__cordova', ''), + parser: postcssConfigParser + }) + result.warnings().forEach(function (warn) { + process.stderr.write(warn.toString()); + }); + return result + } catch (error) { + var errMsg = error.message; + if (error.name === 'CssSyntaxError') { + errMsg = error.message + '\n\n' + 'Css Syntax Error.' + '\n\n' + error.message + error.showSourceCode() + } + error.message = errMsg; + return error + } +} \ No newline at end of file From 7c3b424443dcd7d534e9aebb8b76eb2a37eae23e Mon Sep 17 00:00:00 2001 From: jankapunkt Date: Wed, 6 Dec 2023 16:09:09 +0100 Subject: [PATCH 2/2] migration: fix version conflicts with Meteor 3.0 alpha packages --- package.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.js b/package.js index c2c6339..8b75908 100644 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ summary: 'Minifier for Meteor with PostCSS processing - use Autoprefixer and others with ease', - version: '2.0.8', + version: '3.0.0-alpha.19', name: 'juliancwirko:postcss', git: 'https://github.com/juliancwirko/meteor-postcss.git' }); @@ -9,7 +9,7 @@ Package.registerBuildPlugin({ name: 'minifier-postcss', use: [ 'ecmascript@0.15.2', - 'minifier-css@1.5.4', + 'minifier-css@1.5.4 || 2.0.0-alpha300.19', 'tmeasday:check-npm-versions@1.0.2' ], npmDependencies: {