5

I have the following task that build my application:

const app = new Metalsmith(config.styleguide.path.root);
app.use(
       msDefine({
           production: false,
           rootPath: '/'
       })
   );

app.use(
    msIf(
        gutil.env.config === 'release',
        msDefine({
            production: true,
            rootPath: '/styleguide/'
        })
    )
);
app.build(...);

I need to access the rootPath from within the application, eg:

import stuff from 'stuff';
export class IconCtrl ...
   ...
   _getIconPath(name: string, size: string): string {

      switch (this.version) {
        case 'current':
            return `${stuff.rootPath()}/current/icn-${name}-${size}.svg`;
        default:
            return `${stuff.rootPath()}/legacy/${name}.svg`;
      }
   }
   ...

I haven't found a clean way to do it so far. I am not sure how to access the application configuration at build time from within the app.

mickl
  • 48,568
  • 9
  • 60
  • 89
Alex C
  • 1,334
  • 2
  • 18
  • 41
  • 1
    Please dont do it. Set an app bootstrap provider constant separately or create a json file that can be accessed by both – Gary Jan 02 '18 at 16:32
  • Have you considered changing the basepath instead?https://www.w3schools.com/tags/tag_base.asp – jornare Jan 07 '18 at 16:21

1 Answers1

2

you could use something like gulp-inject-scripts. https://www.npmjs.com/package/gulp-inject-scripts

Example

var gulp = require('gulp');
var injectScripts = require('gulp-inject-scripts');

gulp.task('inject:script', function(){
  return gulp.src('./demo/src/*.html')
    .pipe(injectScripts({
      baseDir "./demo/dist" // SET BASE DIRECTORY 
    }))
    .pipe(gulp.dest('./demo/dist'));
});
racamp101
  • 506
  • 2
  • 12