Init module
Init module is the main module file that is started when mounting the module.
The index.js
file is responsible for initiating the module and properly load the module
More about init modules file can be found here.
The mechanism can be useful when you want to load some libraries or by plugging some mechanism under a specific module.
Example:
Using the @nuxtjs/style-resources
library in a single module.
Load module
npm i @nuxtjs/style-resources
nuxt.config.js
export default {
modules: {
'@nuxtjs/style-resources',
}
}
Use
index.js
import {
join,
} from 'path';
import configuration from './config';
export default async function ({
allModules,
}) {
const {
name,
styleResources = {},
} = configuration;
if (!this.options.styleResources) {
this.options.styleResources = {};
}
Object.keys(styleResources).forEach((resource) => {
this.options.styleResources[resource] = join(
allModules.find(m => m.name === name).path,
styleResources[resource],
).replace(/\/$/g, '');
});
}
config/index.js
export default {
name: '@ergonode/ui',
order: 5,
aliases: {
'@UI': '/',
},
css: [
'./assets/scss/reset.scss',
'./assets/scss/font-inter-ui.scss',
],
styleResources: {
scss: './assets/scss/main.scss',
},
};