Jelajahi Sumber

devkit: amberc -l / libraries works via r.js

-L / library_dirs is out

options for adding mappings / mappingfile are needed;
atm only libs from amber_core can actually be used
Herbert Vojčík 8 tahun lalu
induk
melakukan
ce768d6162

+ 0 - 10
external/amber-cli/support/amberc-cli.js

@@ -38,9 +38,6 @@ function handle_options(optionsArray) {
             case '-l':
                 defaults.load.push.apply(defaults.load, optionsArray.shift().split(','));
                 break;
-            case '-L':
-                defaults.jsLibraryDirs.push.apply(defaults.jsLibraryDirs, optionsArray.shift().split(','));
-                break;
             case '-g':
                 defaults.jsGlobals.push.apply(defaults.jsGlobals, optionsArray.shift().split(','));
                 break;
@@ -95,13 +92,6 @@ function print_usage_and_exit() {
         '     Add listed JavaScript libraries in listed order.',
         '     Libraries are not separated by spaces or end with .js.',
         '',
-        '  -L directory1,directory2',
-        '     Add listed directories to the library search path.',
-        '     The order of processing is:',
-        '     1. current directory',
-        '     2. directories specified by -L',
-        '     3. $AMBER',
-        '',
         '  -g jsGlobal1,jsGlobal2',
         '     Comma separated list of JS global variable names.',
         '     The names are added to a list containing "window", "document" and others.',

+ 11 - 74
external/amber-dev/lib/amberc.js

@@ -28,7 +28,9 @@ function AmberCompiler(amber_dir) {
         nodeRequire: require,
         paths: {
             'amber': path.join(amber_dir, 'support'),
-            'amber_core': path.join(amber_dir, 'src')
+            'amber_core': path.join(amber_dir, 'src'),
+            'text': require.resolve('requirejs-text').replace(/\.js$/, ""),
+            'amber/without-imports': path.join(__dirname, 'without-imports')
         }
     });
     // Important: in next list, boot MUST be first
@@ -54,7 +56,6 @@ var createDefaultConfiguration = function () {
         jsGlobals: [],
         amdNamespace: 'amber_core',
         libraries: [],
-        jsLibraryDirs: [],
         compile: [],
         compiled: [],
         outputDir: undefined,
@@ -75,11 +76,6 @@ AmberCompiler.prototype.main = function (configuration, finished_callback) {
         configuration.amdNamespace = 'amber_core';
     }
 
-    if (configuration.jsLibraryDirs != null) {
-        configuration.jsLibraryDirs.push(path.join(this.amber_dir, 'src'));
-        configuration.jsLibraryDirs.push(path.join(this.amber_dir, 'support'));
-    }
-
     console.ambercLog = console.log;
     if (false === configuration.verbose) {
         console.log = function () {
@@ -95,7 +91,6 @@ AmberCompiler.prototype.main = function (configuration, finished_callback) {
 
     check_configuration(configuration)
         .then(collect_st_files)
-        .then(resolve_kernel)
         .then(create_compiler)
         .then(compile)
         .then(category_export)
@@ -131,23 +126,6 @@ function check_configuration(configuration) {
 }
 
 
-/**
- * Check if the file given as parameter exists in any of the following directories:
- *  1. current local directory
- *  2. configuration.jsLibraryDirs
- *  3. $AMBER/src/
- *  3. $AMBER/support/
- *
- * @param filename name of a file without '.js' prefix
- * @param configuration the main amberc configuration object
- */
-function resolve_js(filename, configuration) {
-    var baseName = path.basename(filename, '.js');
-    var jsFile = baseName + '.js';
-    return resolve_file(jsFile, configuration.jsLibraryDirs);
-}
-
-
 /**
  * Check if the file given as parameter exists in any of the following directories:
  *  1. current local directory
@@ -207,40 +185,6 @@ function collect_st_files(configuration) {
 }
 
 
-/**
- * Resolve .js files needed by kernel.
- * Returns a Promise which resolves into the configuration object.
- */
-function resolve_kernel(configuration) {
-    var kernel_files = configuration.kernel_libraries.concat(configuration.load);
-    return Promise.all(
-        kernel_files.map(function (file) {
-            return resolve_js(file, configuration);
-        })
-    )
-        .then(function (data) {
-            // boot.js and Kernel files need to be used first
-            // otherwise the global objects 'core' and 'globals' are undefined
-            configuration.libraries = data.concat(configuration.libraries);
-            return configuration;
-        });
-}
-
-
-function withImportsExcluded(data) {
-    var srcLines = data.split(/\r\n|\r|\n/), dstLines = [], doCopy = true;
-    srcLines.forEach(function (line) {
-        if (line.replace(/\s/g, '') === '//>>excludeStart("imports",pragmas.excludeImports);') {
-            doCopy = false;
-        } else if (line.replace(/\s/g, '') === '//>>excludeEnd("imports");') {
-            doCopy = true;
-        } else if (doCopy) {
-            dstLines.push(line);
-        }
-    });
-    return dstLines.join('\n');
-}
-
 /**
  * Resolve .js files needed by compiler, read and eval() them.
  * The finished Compiler gets stored in configuration.{core,globals}.
@@ -249,24 +193,17 @@ function withImportsExcluded(data) {
 function create_compiler(configuration) {
     var compiler_files = configuration.compiler_libraries;
     var include_files = configuration.load;
-    return new Promise(function (resolve, reject) {
-        requirejs(compiler_files, function (boot) {
+    return new Promise(requirejs.bind(null, compiler_files))
+        .then(function (boot) {
+            boot.api.initialize();
             configuration.core = boot.api;
             configuration.globals = boot.globals;
-            resolve(configuration);
-        }, function (err) {
-            reject(err);
-        });
-    })
-        .then(function (configuration) {
-            var files = []; // TODO load -L libraries while filtering their imports
-            files.forEach(function (data) {
-                data = data + '';
-                data = withImportsExcluded(data);
-                //builder.add(data);
+            var pluginPrefixedLibraries = include_files.map(function (each) {
+                return 'amber/without-imports!' + each;
             });
-
-            configuration.core.initialize();
+            return new Promise(requirejs.bind(null, pluginPrefixedLibraries));
+        })
+        .then(function () {
             console.log('Compiler loaded');
 
             configuration.globals.ErrorHandler._register_(configuration.globals.RethrowErrorHandler._new());

+ 22 - 0
external/amber-dev/lib/without-imports.js

@@ -0,0 +1,22 @@
+function withImportsExcluded(data) {
+    var srcLines = data.split(/\r\n|\r|\n/), dstLines = [], doCopy = true;
+    srcLines.forEach(function (line) {
+        if (line.replace(/\s/g, '') === '//>>excludeStart("imports",pragmas.excludeImports);') {
+            doCopy = false;
+        } else if (line.replace(/\s/g, '') === '//>>excludeEnd("imports");') {
+            doCopy = true;
+        } else if (doCopy) {
+            dstLines.push(line);
+        }
+    });
+    return dstLines.join('\n');
+}
+
+define({
+    load: function (name, req, onload, config) {
+        req(['text!' + name + '.js'], function (text) {
+            text = withImportsExcluded(text);
+            onload.fromText(text);
+        });
+    }
+});

+ 2 - 1
external/amber-dev/package.json

@@ -18,6 +18,7 @@
     "amd-config-builder": "^0.2.0",
     "amdefine": ">=0.1.1",
     "es6-promise": "^2.0.0",
-    "requirejs": "^2.1.19"
+    "requirejs": "^2.1.19",
+    "requirejs-text": "^2.0.12"
   }
 }

+ 0 - 5
external/amber-dev/tasks/grunt-amberc.js

@@ -14,14 +14,12 @@ module.exports = function (grunt) {
      amberc: {
        options: {
          amber_dir: process.cwd(),                // REQUIRED
-         library_dirs: ['dir1', '/usr/local/js'], // optional
          verbose: true                            // optional
        },
        helloWorld: {
          // this 'options' object is optional as well as all parameters inside it
          // they can be used to override the global 'options'
          options: {
-           library_dirs: ['dir1', '/usr/local/js'], // optional
            verbose: true
          },
          src: ['projects/HelloWorld/src/HelloWorld.st'], // REQUIRED
@@ -73,9 +71,6 @@ module.exports = function (grunt) {
         if (data.libraries != null) {
             configuration.load = data.libraries;
         }
-        if (data.library_dirs != null) {
-            configuration.jsLibraryDirs = data.library_dirs;
-        }
         if (sourceFiles != null) {
             configuration.stFiles = sourceFiles;
         }