Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get webpack entry point name and reuse it in loader

I'm trying to pass some data to SCSS, based on entry point. I will have two themes and I have to know what theme Im currently bundling.

Webpack entry points :

webpack: {
    entry: {
        toyota: path.resolve(__dirname, '../entries/theme.toyota.js'),
        lexus: path.resolve(__dirname, '../entries/theme.lexus.js'),
    },

Then, I would like to recognise the entry point, and pass that as variable to SCSS:

  test: /\.s?css$/,
    exclude: /node_modules/,
    use: [{
            loader: 'sass-loader',
            options: {
                data: '$brand: "' + entryPointName +'";',
                sourceMap: true
            }
        },
    ]

I already tried [name] but no luck...

I could achieve that by running build again with different ENV variable, but i was hoping to achieve that with one task (for faster build time with watch).

Is there any way to achieve that?

like image 214
SimonRabbit Avatar asked Nov 07 '22 10:11

SimonRabbit


1 Answers

Finally, solution is webpack layers...

webpack: {
    entry: {
        toyota: { import: path.resolve(__dirname, '../entries/theme.toyota.js'), layer: 'toyota' },
        lexus: { import: path.resolve(__dirname, '../entries/theme.lexus.js'), layer: 'lexus' },
    },
    experiments: {
        layers: true,
    },
test: /\.s?css$/,
exclude: /node_modules/,
oneOf: [
    {
        issuerLayer: 'toyota',
        use: [{
                loader: 'sass-loader',
                options: {
                    data: '$brand: "toyota";',
                    sourceMap: true
                }
            },
        ]
    },
    {
        issuerLayer: 'lexus',
        use: [{
                loader: 'sass-loader',
                options: {
                    data: '$brand: "lexus";',
                    sourceMap: true
                }
            },
        ]
    },
]
like image 66
mikep Avatar answered Nov 12 '22 11:11

mikep