Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Is there a way to assign the content of a file to a specific variable using webpack?

Is there a way to assign the file contents directly to a variable/method without webpack generating an additional variable?

Currently webpack does something like this:

index.js

import scss from './styles.scss';
console.log(scss);

output.js (actual)

const styles = '...';
console.log(styles);

I want webpack to return the content of the styles.scss file directly to the console.log() method in the output without adding styles variable.

output.js (expected)

console.log('...');

I'm currently using my custom loader for reading .scss files:

css-loader.js

const sass = require('sass');

module.exports = function(source) {
    const result = sass.compile(this.resourcePath);

    return 'export default `\n' + result.css + '\n`';
};

I know there is a way to do this using the require() function but it generates a lot of extra code coming from webpack. Is there any other better way to do it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So it's really a scoping issue. So there's no real requirement to not wanting webpacks default behaviour.

Webpack has a few ways how to do that. See this other answer for how to define a global variable, which is Webpacks way of doing what you want to do.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!