Integrate ECMAScript-6 into existing AngularJS pro

2019-09-05 01:11发布

问题:

I'm trying to integrate ECMA6 into existing Angular project. And I'm looking for a best practise for some issues we have. All components (and we have a lot) have following file structure:

-app/  
  --components/
    --somemodul/
     --somemodul.mdl.js
     --somemodul.drv.js
     --somemodul.ctrl.js
     --somemodul.srv.js
     --somemodul.tmpl.html
-app.js

//somemodul.mdl.js
(function () {
    angular.module('somemodul', []); //initiate module
})()

//somemodul.drv.js
(function () {
    angular.module('somemodul')
        .directive('someModuleDrv', someModuleDrv); //add to module

    function someModuleDrv() {
        // CODE
    }
})()

//somemodul.ctrl.js
(function () {
    angular.module('somemodul')
        .controller('someModuleCtrl', someModuleCtrl); //add to module

    function someModuleCtrl() {
        // CODE
    }
})()

//somemodul.srv.js
(function () {
    angular.module('somemodul')
        .service('someModuleSrv', someModuleSrv); //add to module

    function someModuleSrv() {
        // CODE
    }
})();

On build, Gulp do concat and everything works fine.

With ECMA6 We need to import all these modules into app.js. And now I see two options:

1) To concate myself all 'somemodul' files into single file:

//somemodul.js
export default angular.module('somemodul', [])
        .directive('someModuleDrv', someModuleDrv)
        .service('someModuleSrv', someModuleSrv)
        .controller('someModuleCtrl', someModuleCtrl);


    function someModuleDrv() { };
    function someModuleSrv() {  };
    function someModuleCtrl() {  };

2) Import all submodules into 'somemodule.mdl.js':

//somemodul.mdl.js
import someModuleDrv from './somemodul.drv.js'
import someModuleSrv from './somemodul.srv.js'
import someModuleCtrl from './somemodul.ctrl.js'

export default angular.module('somemodul', [
    someModuleDrv.name,
    someModuleSrv.name,
    someModuleCtrl.name
]);


//somemodul.drv.js
export default angular.module('somemodul.drv', [])
    .directive('someModuleDrv', someModuleDrv);

function someModuleDrv() {
    // CODE
}


//somemodul.srv.js
export default angular.module('somemodul.srv', [])
    .directive('someModuleSrv', someModuleSrv);

function someModuleSrv() {
    // CODE
}


//somemodul.ctrl.js
export default angular.module('somemodul.ctrl', [])
    .directive('someModuleCtrl', someModuleCtrl);

function someModuleCtrl() {
    // CODE
}

Both options works. Unfortunately both options requires a lot of hands-on work. So I'm looking for possible other solution.....

Thanks.

回答1:

Found solution is using require.context().

In my case it will looks like this:

//somemodul.js
export default angular.module('somemodul', [])
/**
 * Requires all directives from subdirectories.
 * @param requireContext
 * @returns {*}
 */
function requireAll(requireContext) {
    return requireContext.keys().map(requireContext);
}
requireAll(require.context(".", true, /^\.\/.*\.js$/));

I tried do not use require and stay only with (export/import), but looks like it's only one way.