Mathieu
Mathieu

Reputation: 4787

Webpack - import a file with only global constants

I have a file with global constants that i am trying to import in another js file so that I can use the constants in its code.

globalConstant.js

global.RoutesOffersPage =  {
  routes: [
    { url: '/fr',
      title: "page daccueil france"
    },
    { url:'/fr/dothis',
      title: "trop sympa"
    }

  ]
}
global.RoutesActionPage =  {
  routes: [    
    { url: '/fr/action',
      title: 'page d'action'
    }
  ]
}
export { global.RoutesOffersPage }; 
export { global.RoutesActionPage };

And I would like to use those global variables in my webpack.config.js

webpack.config.js

import { global.RoutesOffersPage } from './globalConstant.js'
import { global.RoutesActionPage } from './globalConstant.js'

var templateOffersPage = ejs.compile(fs.readFileSync(__dirname + '/offersPageTemplate.ejs', 'utf-8'))
var paths = global.RoutesOffersPage.routes.map(r => r.url);
var paths2 = global.RoutesActionPage.routes.map(r => r.url);
//do stuff

But it the build gives me the error:

SyntaxError: Unexpected token import

I usually know how to import when there's a function but here it's just constant 'global.constants'. I'm lost.

Upvotes: 2

Views: 1208

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

You can use import thanks to webpack, but as webpack does not recursively bundle its own settings, you cannot use it in the webpack config. You have to go back to require there:

require('./globalConstant.js');

global.RoutesOfferPage/*...*/

Upvotes: 2

Related Questions