Skip to content
Permalink
Browse files
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
```
  • Loading branch information
raphinesse committed Jul 22, 2019
1 parent 498d532 commit a6413ddcae1713f498b7bc2d942cb5db07f0cb57
Showing 5 changed files with 8 additions and 22 deletions.
@@ -43,9 +43,9 @@ const bundleTemplate = ({
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 @@ ${modules}
window.cordova = require('cordova');
${includeScript('bootstrap')}
})();
`.trim();
`.trimLeft();
@@ -45,15 +45,17 @@ module.exports = {

// 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 });
},

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 }))
@@ -29,12 +29,6 @@ const {
} = 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 @@ function collectCommonModules () {
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;
}
@@ -47,14 +47,9 @@ function scriptPipeline (config) {
.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 }), {});
@@ -35,11 +35,6 @@ function collectTestBuildModules () {
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;
});

0 comments on commit a6413dd

Please sign in to comment.