Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

explicit requires should override loader pattern #1747

Closed
geddski opened this issue Dec 11, 2015 · 12 comments
Closed

explicit requires should override loader pattern #1747

geddski opened this issue Dec 11, 2015 · 12 comments
Labels

Comments

@geddski
Copy link

geddski commented Dec 11, 2015

Shouldn't explicit loaders override the webpack config?

For example if my webpack config has a pattern for loading all html files with one loader:

{ test: /\.html$/, loader: "ngtemplate"}
// this works
var template = require('mytemplate.html');

But if somewhere in the app the html file is loaded explicitly with a different loader:

var template = require('text!mytemplate.html');

The result is now the output of the ngtemplate loader, instead of the plain text of the file. Is this a bug?

@sokra
Copy link
Member

sokra commented Dec 14, 2015

var template = require('!text!mytemplate.html');

@denis-sokolov
Copy link

Thanks, @sokra! This information is useful and was notably missing from the documentation. I have updated the documentation, let me know if I’m mistaken.

And it seems like the issue may be closed now. @geddski, does the answer cover your use case?

@geddski
Copy link
Author

geddski commented Feb 26, 2016

the leading ! didn't really work for my case, because while migrating Domo to webpack I had to keep things backwards compatible with requirejs for a time. I found another way to do what I needed. But I agree that this info is very useful. But I still think the local require is more specific and should override the loader patterns without the extra !. Seems a more sensible default IMO.

@silvenon
Copy link

If you chain loaders like this:

loaders: [{
  test: /\.s?css$/,
  loader: 'style!css'
}, {
  test: /\.scss$/,
  loader: 'sass'
}]

your SCSS files will be compiled with style!css!sass, so adding different behavior to the require() call could be confusing. I would rather that the leading ! feature was added to configuration as well:

loaders: [{
  test: /\.s?css$/,
  loader: 'style!css'
}, {
  test: /\.scss$/,
  loader: '!style!css!postcss!sass'
}]

because this doesn't work 😕

@jamesmfriedman
Copy link

@geddski, found this trying to fix the same problem. Ended up working for me.

bnhovde added a commit to bnhovde/react-boilerplate that referenced this issue Jun 21, 2016
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
mxstbr pushed a commit to react-boilerplate/react-boilerplate that referenced this issue Jun 21, 2016
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
@Delagen
Copy link

Delagen commented Nov 14, 2016

@sokra I have loader before typescript that replace in source path to html with require call with !raw-loader but it still use file-loader that is defined in global config.

@saawsan
Copy link

saawsan commented Mar 15, 2017

@sokra answer works perfectly. Thanks.

However, I think it's worth mentioning that ESLint forbid the use of inline loaders. I had to rule this config out.
https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-webpack-loader-syntax.md

@gejgalis
Copy link

gejgalis commented May 8, 2017

I confirm the issue. I wanted to override configuration with inline require('raw-loader!./foo.tpl'), but webpack is still processing my configuration with test: /\.tpl$/

I thought I could have 2 separate configurations:

  1. test: /\.tpl\?raw$/, loader: 'raw-loader'
  2. test: /\.tpl$/, loader: 'vue-template-loader'

and use it by:

  1. require('./foo.tpl?raw')
  2. require('./foo.tpl')

but unfortunally webpack drops everything after file path before executing the tests :(

I can't migrate to webpack 2 due to this issue... :(

marvellous122 added a commit to marvellous122/react-redux-base that referenced this issue Jan 19, 2018
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
@sberney
Copy link

sberney commented Dec 3, 2018

Yeah, I can't realistically use !raw-loader!./path-to-file.html in place of raw-loader!./path-to-file.html, because the code generated by my webpack project will be loaded by runtime things like requirejs and systemjs. They don't support !raw-loader! syntax.

I also think that raw-loader!./path-to-file.html ought to override the default plugin configuration.

@silvenon
Copy link

silvenon commented Dec 6, 2018

Could you give more details about your setup? Why does your project contain webpack-specific loader paths if they aren't being consumed by webpack?

That behavior is not a bug, so introducing such a breaking change to webpack would have big consequences.

shaun554 added a commit to shaun554/react-boilerplate that referenced this issue Mar 21, 2019
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
@vankop
Copy link
Member

vankop commented Aug 9, 2020

Answer above #1747 (comment)

Closing because of inactivity

@vankop vankop closed this as completed Aug 9, 2020
@sberney
Copy link

sberney commented Aug 18, 2020

I still don't understand why it doesn't work the way I expected (and the way other bundlers work), but I am not having this problem anymore. Like others, part of the problem was legacy code used in transition for years. Unlike others, I'm never going to fully transition to an entirely webpack-based runtime. I want to partially compile the requires, and partially "pre-compile" them so that requirejs and other programs can finish the job.

I realize now that webpack needs more than the band-aid implied by fixing this to support the workflow I want in the places I want it. I have off-and-on thought about building my own tooling to perform the specifics tasks of preparing files to be consumed by the specific types of things I am using (like requirejs and systemjs) at runtime.

mauricewells pushed a commit to mauricewells/react-boilerplate that referenced this issue Jan 21, 2022
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
kazuma512 added a commit to kazuma512/react-boilerplate-typescript that referenced this issue Jun 2, 2023
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
AIDevMonster added a commit to AIDevMonster/react-boilerplate that referenced this issue Jun 21, 2023
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
whiteghostDev added a commit to whiteghostDev/react-boilerplate that referenced this issue Aug 6, 2023
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
muscliary pushed a commit to muscliary/react-boilerplate that referenced this issue Sep 12, 2023
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
wesleywang4766 added a commit to wesleywang4766/lodash that referenced this issue Mar 8, 2024
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
winder9 pushed a commit to M-Killer-dev/React-project that referenced this issue Jun 16, 2024
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
JackMJones pushed a commit to JackMJones/Jack1213 that referenced this issue Aug 17, 2024
Changed the loader for manifest.json from json-loader to file-loader
to prevent it being wrapped with module.exports

Used approach as described here:
webpack/webpack#1747 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests