Pārlūkot izejas kodu

Merge pull request #1237 from transloadit/chore/fast-build-lib

Only rebuild changed files with `npm run build:lib`
Artur Paikin 6 gadi atpakaļ
vecāks
revīzija
2713eb4450
3 mainītis faili ar 33 papildinājumiem un 4 dzēšanām
  1. 30 1
      bin/build-lib.js
  2. 1 1
      bin/release
  3. 2 2
      package.json

+ 30 - 1
bin/build-lib.js

@@ -8,17 +8,45 @@ const path = require('path')
 
 const transformFile = promisify(babel.transformFile)
 const writeFile = promisify(fs.writeFile)
+const stat = promisify(fs.stat)
 
 const SOURCE = 'packages/{*,@uppy/*}/src/**/*.js'
 // Files not to build (such as tests)
 const IGNORE = /\.test\.js$|__mocks__|companion\//
+// Files that should trigger a rebuild of everything on change
+const META_FILES = [
+  '.babelrc',
+  'package.json',
+  'package-lock.json',
+  'bin/build-lib.js'
+]
+
+function lastModified (file) {
+  return stat(file).then((s) => s.mtime)
+}
 
 async function buildLib () {
+  const metaMtimes = await Promise.all(META_FILES.map((filename) =>
+    lastModified(path.join(__dirname, '..', filename))
+  ))
+  const metaMtime = Math.max(...metaMtimes)
+
   const files = await glob(SOURCE)
   for (const file of files) {
     if (IGNORE.test(file)) continue
-
     const libFile = file.replace('/src/', '/lib/')
+
+    // on a fresh build, rebuild everything.
+    if (!process.env.FRESH) {
+      const srcMtime = await lastModified(file)
+      const libMtime = await lastModified(libFile)
+        .catch(() => 0) // probably doesn't exist
+      // Skip files that haven't changed
+      if (srcMtime < libMtime && metaMtime < libMtime) {
+        continue
+      }
+    }
+
     const { code, map } = await transformFile(file, {})
     await mkdirp(path.dirname(libFile))
     await Promise.all([
@@ -29,6 +57,7 @@ async function buildLib () {
   }
 }
 
+console.log('Using Babel version:', require('babel-core/package.json').version)
 buildLib().catch((err) => {
   console.error(err.stack)
   process.exit(1)

+ 1 - 1
bin/release

@@ -59,7 +59,7 @@ git add README.md
 cp README.md packages/uppy/README.md
 
 npm run clean
-npm run build
+FRESH=1 npm run build
 
 git commit -m "Release"
 lerna version --amend --no-push --exact

+ 2 - 2
package.json

@@ -85,7 +85,7 @@
     "build:gzip": "node ./bin/gzip.js",
     "size": "echo 'JS Bundle mingz:' && cat ./packages/uppy/dist/uppy.min.js | gzip | wc -c && echo 'CSS Bundle mingz:' && cat ./packages/uppy/dist/uppy.min.css | gzip | wc -c",
     "build:js": "npm-run-all build:lib build:bundle",
-    "build:lib": "babel --version && node ./bin/build-lib.js",
+    "build:lib": "node ./bin/build-lib.js",
     "build": "npm-run-all --parallel build:js build:css build:companion --serial build:gzip size",
     "clean": "rm -rf packages/*/lib packages/@uppy/*/lib && rm -rf packages/uppy/dist",
     "lint:fix": "npm run lint -- --fix",
@@ -101,7 +101,7 @@
     "test:prepare-ci": "npm-run-all --parallel --race test:registry test:build-ci",
     "test:acceptance": "npm run test:prepare-ci && wdio test/endtoend/wdio.remote.conf.js",
     "test:acceptance:local": "npm run test:build && wdio test/endtoend/wdio.local.conf.js",
-    "test:unit": "jest",
+    "test:unit": "npm run build:lib && jest",
     "test:companion": "cd ./packages/@uppy/companion && npm run test",
     "test:type": "tsc -p .",
     "test": "npm run lint && npm run test:unit && npm run test:type && npm run test:companion",