Skip to content

Commit

Permalink
chore: improve build responsiveness (#2992)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladitasev authored Mar 23, 2021
1 parent 8353ac2 commit 8048c9f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
17 changes: 11 additions & 6 deletions packages/tools/components-package/nps.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getScripts = (options) => {
styles: {
default: "nps build.styles.themes build.styles.components",
themes: "postcss src/**/parameters-bundle.css --config config/postcss.themes --base src --dir dist/css/",
components: "postcss src/themes/*.css --config config/postcss.components --base src --dir dist/css/",
components: "postcss src/themes/*.css --config config/postcss.components --base src --dir dist/css/", // When updating this, also update the new files script
},
i18n: {
default: "nps build.i18n.defaultsjs build.i18n.json",
Expand Down Expand Up @@ -52,8 +52,8 @@ const getScripts = (options) => {
"webcomponents-polyfill-placeholder": `node ${LIB}/polyfill-placeholder/index.js`
},
watch: {
default: 'concurrently "nps watch.templates" "nps watch.samples" "nps watch.test" "nps watch.src" "nps watch.bundle" "nps watch.styles"',
es5: 'concurrently "nps watch.templates" "nps watch.samples" "nps watch.test" "nps watch.src" "nps watch.bundle.es5" "nps watch.styles"',
default: 'concurrently "nps watch.templates" "nps watch.samples" "nps watch.test" "nps watch.src" "nps watch.bundle" "nps watch.styles" "nps watch.i18n"',
es5: 'concurrently "nps watch.templates" "nps watch.samples" "nps watch.test" "nps watch.src" "nps watch.bundle.es5" "nps watch.styles" "nps watch.i18n"',
src: 'nps "copy.src --watch --safe --skip-initial-copy"',
props: 'nps "copy.props --watch --safe --skip-initial-copy"',
test: 'nps "copy.test --watch --safe --skip-initial-copy"',
Expand All @@ -64,10 +64,15 @@ const getScripts = (options) => {
styles: {
default: 'concurrently "nps watch.styles.themes" "nps watch.styles.components"',
themes: 'nps "build.styles.themes -w"',
components: 'nps "build.styles.components -w"',
components: {
default: 'concurrently "nps watch.styles.components.existingFiles" "nps watch.styles.components.newFiles"',
existingFiles: `nps "build.styles.components -w"`,
newFiles: `node "${LIB}/postcss-new-files/index.js" --srcFiles="src/themes/*.css"`,
},
},
templates: "chokidar \"src/**/*.hbs\" -c \"nps build.templates\"",
samples: "chokidar \"test/**/*.sample.html\" -c \"nps build.samples\"",
templates: 'chokidar "src/**/*.hbs" -c "nps build.templates"',
samples: 'chokidar "test/**/*.sample.html" -c "nps build.samples"',
i18n: 'chokidar "src/i18n/messagebundle.properties" -c "nps build.i18n.defaultsjs"'
},
dev: {
default: 'concurrently "nps serve" "nps watch"',
Expand Down
36 changes: 36 additions & 0 deletions packages/tools/lib/postcss-new-files/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const chokidar = require("chokidar");
const commandLineArgs = require("command-line-args");
const { exec } = require("child_process");

const options = commandLineArgs([
{ name: "srcFiles", type: String },
]);

const runPostcss = path => {
let command = `postcss ${path} --config config/postcss.components --base src --dir dist/css/`;
console.log(`Executing: ${command}`);
exec(command, (err, stdout, stderr) => {
if (err) {
console.log(`Could not run postcss for ${path}. Error: ${err}`);
}
});

command = `${command} -w`;
console.log(`Executing: ${command}`);
exec(command, (err, stdout, stderr) => {
if (err) {
console.log(`Could not run postcss in watch mode for ${path}. Error: ${err}`);
}
});
};

let ready = false; // Do nothing until the ready event has been fired (we don't want to recompile all files initially)
const watcher = chokidar.watch(options.srcFiles);
watcher.on("ready", () => {
ready = true; // Initial scan is over -> waiting for new files
});
watcher.on("add", path => {
if (ready) {
runPostcss(path);
}
});
2 changes: 2 additions & 0 deletions packages/tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"@wdio/sync": "^7.2.0",
"@webcomponents/webcomponentsjs": "^2.5.0",
"chai": "^4.3.4",
"child_process": "^1.0.2",
"chokidar": "^3.5.1",
"chokidar-cli": "^2.1.0",
"clean-css": "^5.1.1",
"colors": "^1.4.0",
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2497,6 +2497,11 @@ check-error@^1.0.2:
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
integrity sha1-V00xLt2Iu13YkS6Sht1sCu1KrII=

child_process@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/child_process/-/child_process-1.0.2.tgz#b1f7e7fc73d25e7fd1d455adc94e143830182b5a"
integrity sha1-sffn/HPSXn/R1FWtyU4UODAYK1o=

chokidar-cli@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/chokidar-cli/-/chokidar-cli-2.1.0.tgz#2491df133bd62cd145227b1746fbd94f2733e1bc"
Expand All @@ -2522,7 +2527,7 @@ chokidar@3.4.1:
optionalDependencies:
fsevents "~2.1.2"

chokidar@3.5.1, chokidar@^3.0.0, chokidar@^3.2.3, chokidar@^3.3.0, chokidar@^3.4.0, chokidar@^3.5.0:
chokidar@3.5.1, chokidar@^3.0.0, chokidar@^3.2.3, chokidar@^3.3.0, chokidar@^3.4.0, chokidar@^3.5.0, chokidar@^3.5.1:
version "3.5.1"
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.1.tgz#ee9ce7bbebd2b79f49f304799d5468e31e14e68a"
integrity sha512-9+s+Od+W0VJJzawDma/gvBNQqkTiqYTWLuZoyAsivsI4AaWTCzHG06/TMjsf1cYe9Cb97UCEhjz7HvnPk2p/tw==
Expand Down

0 comments on commit 8048c9f

Please sign in to comment.