Use Cockpit webpack helpers from cockpit.git

Drop the cockpit-po-plugin.js and sassc-loader.js code copies, and check
them out from a stable cockpit tag, similarly to how we already get the
PatternFly CSS.
This commit is contained in:
Martin Pitt 2021-01-22 16:04:35 +01:00 committed by GitHub
parent 786fd20df5
commit bb06422996
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 108 deletions

2
.gitignore vendored
View file

@ -11,6 +11,6 @@ Test*FAIL*
bots/ bots/
test/common/ test/common/
test/images/ test/images/
src/lib/patternfly/ src/lib/
*.pot *.pot
POTFILES* POTFILES*

View file

@ -12,6 +12,8 @@ VM_IMAGE=$(CURDIR)/test/images/$(TEST_OS)
NODE_MODULES_TEST=package-lock.json NODE_MODULES_TEST=package-lock.json
# one example file in dist/ from webpack to check if that already ran # one example file in dist/ from webpack to check if that already ran
WEBPACK_TEST=dist/manifest.json WEBPACK_TEST=dist/manifest.json
# one example file in src/lib to check if it was already checked out
LIB_TEST=src/lib/cockpit-po-plugin.js
all: $(WEBPACK_TEST) all: $(WEBPACK_TEST)
@ -52,7 +54,7 @@ update-po: po/$(PACKAGE_NAME).pot
%.spec: %.spec.in %.spec: %.spec.in
sed -e 's/%{VERSION}/$(VERSION)/g' $< > $@ sed -e 's/%{VERSION}/$(VERSION)/g' $< > $@
$(WEBPACK_TEST): $(NODE_MODULES_TEST) src/lib/patternfly/_fonts.scss $(shell find src/ -type f) package.json webpack.config.js $(WEBPACK_TEST): $(NODE_MODULES_TEST) $(LIB_TEST) $(shell find src/ -type f) package.json webpack.config.js
NODE_ENV=$(NODE_ENV) npm run build NODE_ENV=$(NODE_ENV) npm run build
watch: watch:
@ -87,7 +89,7 @@ $(TARFILE): $(WEBPACK_TEST) cockpit-$(PACKAGE_NAME).spec
touch dist/* touch dist/*
tar czf cockpit-$(PACKAGE_NAME)-$(VERSION).tar.gz --transform 's,^,cockpit-$(PACKAGE_NAME)/,' \ tar czf cockpit-$(PACKAGE_NAME)-$(VERSION).tar.gz --transform 's,^,cockpit-$(PACKAGE_NAME)/,' \
--exclude cockpit-$(PACKAGE_NAME).spec.in \ --exclude cockpit-$(PACKAGE_NAME).spec.in \
$$(git ls-files) src/lib/patternfly/*.scss package-lock.json cockpit-$(PACKAGE_NAME).spec dist/ $$(git ls-files) $(LIB_TEST) src/lib/patternfly/*.scss package-lock.json cockpit-$(PACKAGE_NAME).spec dist/
mv node_modules.release node_modules mv node_modules.release node_modules
srpm: $(TARFILE) cockpit-$(PACKAGE_NAME).spec srpm: $(TARFILE) cockpit-$(PACKAGE_NAME).spec
@ -145,13 +147,13 @@ test/common:
git checkout --force FETCH_HEAD -- test/common; \ git checkout --force FETCH_HEAD -- test/common; \
git reset test/common' git reset test/common'
src/lib/patternfly/_fonts.scss: # checkout Cockpit's PF/React/build library; again this has no API stability guarantee, so check out a stable tag
$(LIB_TEST):
flock Makefile sh -ec '\ flock Makefile sh -ec '\
git fetch --depth=1 https://github.com/cockpit-project/cockpit.git 227; \ git fetch --depth=1 https://github.com/cockpit-project/cockpit.git 236; \
mkdir -p pkg/lib/patternfly && git add pkg/lib/patternfly; \ git checkout --force FETCH_HEAD -- pkg/lib; \
git checkout --force FETCH_HEAD -- pkg/lib/patternfly; \ git reset -- pkg/lib'
git reset -- pkg/lib/patternfly' mv pkg/lib src/ && rmdir -p pkg
mkdir -p src/lib && mv pkg/lib/patternfly src/lib/patternfly && rmdir -p pkg/lib
$(NODE_MODULES_TEST): package.json $(NODE_MODULES_TEST): package.json
# if it exists already, npm install won't update it; force that so that we always get up-to-date packages # if it exists already, npm install won't update it; force that so that we always get up-to-date packages

View file

@ -1,74 +0,0 @@
const path = require("path");
const glob = require("glob");
const po2json = require('po2json');
const Jed = require('jed');
module.exports = class {
apply(compiler) {
compiler.hooks.emit.tapPromise(
'CockpitPoPlugin',
compilation => Promise.all(glob.sync('po/*.po').map(f => this.buildFile(f, compilation)))
);
}
prepareHeader(header) {
if (!header)
return null;
var body, statement, ret = null;
const plurals = header["plural-forms"];
if (plurals) {
try {
/* Check that the plural forms isn't being sneaky since we build a function here */
Jed.PF.parse(plurals);
} catch(ex) {
fatal("bad plural forms: " + ex.message, 1);
}
/* A function for the front end */
statement = header["plural-forms"];
if (statement[statement.length - 1] != ';')
statement += ';';
ret = 'function(n) {\nvar nplurals, plural;\n' + statement + '\nreturn plural;\n}';
/* Added back in later */
delete header["plural-forms"];
}
/* We don't need to be transferring this */
delete header["project-id-version"];
delete header["report-msgid-bugs-to"];
delete header["pot-creation-date"];
delete header["po-revision-date"];
delete header["last-translator"];
delete header["language-team"];
delete header["mime-version"];
delete header["content-type"];
delete header["content-transfer-encoding"];
return ret;
}
buildFile(po_file, compilation) {
return new Promise((resolve, reject) => {
const jsonData = po2json.parseFileSync(po_file);
const plurals = this.prepareHeader(jsonData[""]);
let output = JSON.stringify(jsonData, null, 1);
// We know the brace in is the location to insert our function
if (plurals) {
const pos = output.indexOf('{', 1);
output = output.substr(0, pos + 1) + "'plural-forms':" + String(plurals) + "," + output.substr(pos + 1);
}
// wrap JSON output into cockpit.locale() call
output = 'cockpit.locale(' + output + ');\n';
const lang = path.basename(po_file).slice(0, -3)
compilation.assets['po.' + lang + '.js'] = { source: () => output, size: () => output.length };
resolve();
});
};
};

View file

@ -1,25 +0,0 @@
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');
/* source is not used. This must be the first loader in the chain, using this.resource, so that sassc can include the scss file's directory in the include path */
module.exports = function() {
this.cacheable();
const workdir = fs.mkdtempSync("sassc-loader.");
const out = path.join(workdir, "output.css");
childProcess.execFileSync(
'sassc',
['--load-path=node_modules', '--style=compressed', '--sourcemap', this.resource, out],
{ stdio: ['pipe', 'inherit', 'inherit'] });
const css = fs.readFileSync(out, 'utf8');
const cssmap = fs.readFileSync(out + ".map", 'utf8');
fs.unlinkSync(out);
fs.unlinkSync(out + ".map");
fs.rmdirSync(workdir);
this.callback(null, css, cssmap);
};