Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Source map clean-up with Cordova

Posted on 05.04.2016 by Smart Mobile Studio Team Posted in Developers log, News and articles

Source maps are a very powerful way to support the debug process of HTML5-based web-apps. It actually maps the JS code to the higher level language (in this case Object Pascal). While this is very handy, especially in combination with obfuscation, it also reveals how the software is build internally. Thus you probably don’t want it to be shipped to customers.

Unfortunately, Smart Mobile Studio only create source maps, but never deletes them, if not used anymore. At the same time Cordova copies the entire content of the www folder (including the source map) to the assets and thus inherently ships these files to the customers when publishing the app. Once available it’s very easy to extract the files and reconstruct the entire source code of your application.

Typically you would handle the deletion of this in your build tool. However, since Cordova already contains a build tool, you might decide to use this instead of adding another tool to your toolchain.

With Cordova you can easily add hooks for many different commands. The command you’re probably looking for in this case is ‘after_prepare’. To add your script for this command, just create a folder named ‘hooks’ in the root of your project. Then add the ‘after_prepare’ folder to this ‘hooks’ folder. Now when either

  • cordova prepare
  • cordova platform add
  • cordova build
  • cordova run

has been executed all scripts in this folder are executed.

When we talk about scripts in the context of Cordova this obviously means JS. While you could copy & hack the script of choice from several places, you could also use Smart Mobile Studio to write your own script within minutes – in the language you love!

Custom Node.js hook script

To do so just start a new Node.JS project. You can delete everything you find in the template as it is not needed. Instead you might want to start with something like this:

uses
  NodeJS.Core, NodeJS.fs;

var fs := Jfs_Exports(RequireModule('fs'));

This makes the file system module available in the script. It is accessibly like an object by the variable ‘fs’.

Now deleting a folder could be as easy as:

fs.rmdirSync(Path);

but care has to be taken as you need to do this recursively. Also you must first unlink the directory as on some platforms (*nix based) it might not be a copy, but a (hard) link (and you might not want to delete the source maps all together, but only in the Cordova output).

Taking this into account, the recursive deletion of a folder can look like:

procedure DeletePathRecursive(Path: String);
begin
  if fs.existsSync(Path) then
  begin
    for var Dir in fs.readdirSync(Path) do
      if fs.lstatSync(Dir).isDirectory then
        deleteFolderRecursive(Dir)
      else
        fs.unlinkSync(Dir);

      fs.rmdirSync(Path);
  end;
end;

Now we only need to specify the paths and call this procedure with

var Paths = ['platforms/ios/www/debug', 'platforms/android/assets/www/debug'];

for var Path in Paths do
  DeletePathRecursive(Path);

In the end the entire source code should be something like this:

uses
  NodeJS.Core, NodeJS.fs;

var fs := Jfs_Exports(RequireModule('fs'));

procedure DeletePathRecursive(Path: String);
begin
  if fs.existsSync(Path) then
  begin
    for var Dir in fs.readdirSync(Path) do
      if fs.lstatSync(Dir).isDirectory then
        deleteFolderRecursive(Dir)
      else
        fs.unlinkSync(Dir);

    fs.rmdirSync(Path);
  end;
end;

var Paths = ['platforms/ios/www/debug', 'platforms/android/assets/www/debug'];
for var Path in Paths do
  DeletePathRecursive(Path);

If you compile this you should end up with a 4kB small JS script. Eventually you must add the

#! /usr/bin/env node

to the script in order to get it work. Alternatively you can specify a template in Smart Mobile Studio to add this at link time.

The JS script now only needs to be copied to the hooks/after_prepare folder. It will then be executed every time you call ‘cordova build’.

build script clean-up Cordova node.js Source maps
« Smart Mobile Studio 2.2 Release Candidate 2
It’s finally here – Smart Mobile Studio 2.2 »

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS