Make built JS output a bit prettier (#208)
In #205 I took great care to preserve the _exact_ format of the built
JS code, so the build refactoring could be verified by diffing the
build output. However, some of the existing formatting is sub-optimal
so this commit optimizes it a little.
Most notably, this commit will change file path comments to be relative
paths, not absolute ones. An example for this:
```diff
-// file: /home/raphinesse/code/cordova-android/cordova-js-src/exec.js
+// file: ../cordova-android/cordova-js-src/exec.js
```
diff --git a/build-tools/bundle.js b/build-tools/bundle.js
index 2090587..3a7b97a 100644
--- a/build-tools/bundle.js
+++ b/build-tools/bundle.js
@@ -43,9 +43,9 @@
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
-\x20
+
http://www.apache.org/licenses/LICENSE-2.0
-\x20
+
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -60,4 +60,4 @@
window.cordova = require('cordova');
${includeScript('bootstrap')}
})();
-`.trim();
+`.trimLeft();
diff --git a/build-tools/common.js b/build-tools/common.js
index 784881c..18af399 100644
--- a/build-tools/common.js
+++ b/build-tools/common.js
@@ -45,7 +45,9 @@
// TODO format path relative to pkg.json
prependFileComment (f) {
- const comment = `// file: ${f.path}`;
+ const relativePath = path.relative(pkgRoot, f.path);
+ const normalizedPath = path.posix.normalize(relativePath);
+ const comment = `// file: ${normalizedPath}`;
const contents = [comment, f.contents].join('\n');
return Object.assign({}, f, { contents });
},
@@ -53,7 +55,7 @@
collectModules (dir) {
return globby.sync(['**/*.js'], { cwd: dir })
.map(fileName => ({
- path: path.relative(pkgRoot, path.join(dir, fileName)),
+ path: path.join(dir, fileName),
moduleId: fileName.slice(0, -3)
}))
.map(file => ({ [file.moduleId]: file }))
diff --git a/build-tools/modules.js b/build-tools/modules.js
index 4ae28b8..b896055 100644
--- a/build-tools/modules.js
+++ b/build-tools/modules.js
@@ -29,12 +29,6 @@
} = require('./common');
module.exports = function modules (config) {
- for (const m of values(config.extraModules)) {
- if (m.path.startsWith('../')) {
- m.path = path.resolve(m.path);
- }
- }
-
const commonModules = collectCommonModules();
const modules = values(Object.assign(commonModules, config.extraModules));
modules.sort((a, b) => a.moduleId.localeCompare(b.moduleId));
@@ -45,7 +39,7 @@
const modules = collectModules(path.join(pkgRoot, 'src/common'));
modules[''] = {
moduleId: '',
- path: path.relative(pkgRoot, path.join(pkgRoot, 'src/cordova.js'))
+ path: path.join(pkgRoot, 'src/cordova.js')
};
return modules;
}
diff --git a/build-tools/scripts.js b/build-tools/scripts.js
index 4fec388..ff0573a 100644
--- a/build-tools/scripts.js
+++ b/build-tools/scripts.js
@@ -47,14 +47,9 @@
.then(readContents)
.then(config.preprocess)
.then(stripLicenseHeader)
- .then(prependEmptyLine)
.then(prependFileComment);
}
-function prependEmptyLine (f) {
- return Object.assign({}, f, { contents: '\n' + f.contents });
-}
-
function indexByModuleId (files) {
return files
.reduce((acc, f) => Object.assign(acc, { [f.moduleId]: f }), {});
diff --git a/test/build.js b/test/build.js
index 5c2a007..b94ada6 100755
--- a/test/build.js
+++ b/test/build.js
@@ -35,11 +35,6 @@
const moduleId = path.posix.join(platform, 'exec');
modules[moduleId] = Object.assign({}, modules.exec, { moduleId });
- // Remove plugin/* modules to minimize diff to old build
- Object.keys(modules)
- .filter(k => k.startsWith('plugin/'))
- .forEach(k => delete modules[k]);
-
return modules;
});