-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
"extract-text-webpack-plugin" loader is used without the corresponding plugin #579
Comments
Wow I am really impressed that you found out how to use this plugin for server side rendering. There is no documentation at all for this feature (although it was developed over a year ago). There are/were several webpack bugs which didn't get enough priority by the webpack core team:
As I always thought of this feature as a nice to have I stopped working on it. |
Well it has nothing todo with SSR. I even dont use it in case of SSR because another plugin handles the generation of the index.html . Only use it for providing the index.html in dev Mode. So the problem is somewhere else... |
@digitalkaoz Can you set up a standalone project to study? |
@digitalkaoz Instead of |
is rendered at build time not in browser - so this is server side rendering ;) @bebraw thanks for picking this up 👍 |
We could establish a new term for this: CTR (Compile-Time Rendering) 😉 |
@sokra Haha unfortunately ctr is already used :) This feature could boost the initial perceived load time of many react apps a lot without much work for the developer. @markdalgleish built a plugin which also uses webpack to render at buit time https://github.com/markdalgleish/static-site-generator-webpack-plugin I added an example for this a while back: https://github.com/jantimon/html-webpack-plugin/tree/master/examples/javascript To get the same error like @digitalkaoz just add a require statement to a css file in https://github.com/jantimon/html-webpack-plugin/blob/master/examples/javascript/universial.js @pirelenito also created a unit test for the extract-text-webpack-plugin in pr #201 (1 year ago for webpack 1) |
BTR: Build-Time Rendering? |
Hi there! I have the same issue.
index.ejs:
If I remove |
@mdnsk Yeah, after removing |
@jantimon but there was no resolution #201 here is the example project which causes this bug: https://github.com/digitalkaoz/digitalkaoz.github.io/tree/html_webpack but as @jantimon stated, just require a css file in your JS to trigger the bug... im a bit confused as it looks like it works in the provided examples in this repo (but its using webpack1)?! |
I mean the same question. I need it to use in my partial ejs templates. For example, components/header/header.ejs:
|
@mdnsk Yup. That was more of a question which we should study. I have to get a little demo up and running to say anything more. |
The problem is require to styles in a html-plugin template. What you want it probably to specify other One possible solution is the add a rule condition for compilation name. {
test: /\.css$/,
oneOf: [
{ compilation: /html-webpack-plugin/, use: "null-loader" },
{ use: ETP. ... }
]
} |
@bebraw you can use the examples in this repo:
|
The root problem is that the extract text plugin uses kind of a hack to communicate between loader and plugin. (See here https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/master/index.js#L216-L227 and here https://github.com/webpack-contrib/extract-text-webpack-plugin/blob/master/loader.js#L26) The null loader seems to be a work a round - imho we should add a feature request for the extract-text-plugin to support child compilations. This might add a feature request for the webpack core (easier communication between loaders and plugins) @digitalkaoz thanks - we understood that - #201 shows the problem but has no solution to the problem as it can't be solved in the |
as @bebraw seems active in both projects he seems to be the right person to investigate :) |
@digitalkaoz Tobias likely knows this area the best. By the sound of it, it's going to require a bigger change to pull this off properly. The way extract-text-plugin handles communication is the problem and that would have to change in order to fix this. Perhaps a plugin/loader should know something about its compilation context so it doesn't default to the root one? |
@sokra the can you provide a correct example to me? |
@digitalkaoz That was a potential solution that needs to be implemented still. |
Maybe we can move this part of the discussion to the extract-text-webpack-plugin: |
ok, i have it working somehow: everything depends on this patch: webpack-contrib/extract-text-webpack-plugin#390 thanks @sokra for the hint on Rules!
{
module: {
rules: [
{
test: /\.scss$/,
oneOf: [
{test: /html-webpack-plugin/, use: "null-loader"},
{
use: ExtractTextPlugin.extract({
fallbackLoader: 'style-loader',
loader: ['css-loader', 'sass-loader']
})
}
]
},
],
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './js/index.jsx',
}),
new ExtractTextPlugin({filename: '[name].css'}),
]
}
import React from 'react';
import Document from './components/Document.jsx';
const styles = [];
const scripts = [];
// Client Rendering
if (typeof document !== 'undefined') {
const Dom = require('react-dom');
const App = require('./components/App.jsx').default; //lazy because of window dep
Dom.render(<App />, document.getElementById('app'));
}
export default (locals, callback) => {
const Server = require('react-dom/server');
// Serverside Rendering
if ('function' === typeof callback) {
const App = require('./components/App.jsx').default; //render the full app
callback(null, '<!DOCTYPE html>'+ Server.renderToString(<Document app={<App />} scripts={ scripts } styles={ styles }/>));
} else {
// Build-Time Rendering
return '<!DOCTYPE html>'+ Server.renderToStaticMarkup(<Document />);
}
};
import React from 'react';
export default class extends React.Component {
static defaultProps = {
scripts: [],
styles: [],
app: null
};
render() {
return <html lang="en">
<head>
{ this.props.styles.map((s) =>{ return <link rel="stylesheet" key={s} href={s} type="text/css" /> })}
</head>
<body>
<div id="app">{ this.props.app }</div>
{ this.props.scripts.map((s) =>{ return <script key={s} src={s}></script> })}
</body>
</html>
}
} |
Thank you @digitalkaoz and @sokra ! I have it working now. entry: {
'bundle': './index.js',
'__page-index': './pages/index.ejs',
'__page-contacts': './pages/contacts.ejs'
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist')
},
...
plugins: [
new HtmlWebpackPlugin({
title: 'Index Page',
filename: 'index.html',
template: './pages/index.ejs'
}),
new HtmlWebpackPlugin({
title: 'Contacts Page',
filename: 'contacts.html',
template: './pages/contacts.ejs'
}),
new ExtractTextPlugin('style.css')
] |
@mdnsk ExtractTextPlugin works when adding templates as entry.However,it seems the |
It still doesn't work in my project @mdnsk |
After struggeling for 3 days with this issue, I've found out, that I only get it when using the terminal plugin in my Atom editor. When running "npm run dev" from the windows cmd app, it works fine. Hope this is of any help for others. |
@kirschkern Can you provide more information? Do you understand why Atom breaks it? |
Sorry, I have no idea why it doesn't work from inside the terminal plugin. I can only offer this "workaround" so far. |
the same issue. This is my environment: When i added extract-text-webpack-plugin, It's fine
|
I couldn't find the solution for using this plugin alongside extractTextPlugin. Adding the template to entries doesn't seem like a good solution for me. |
|
@mastilver thank you for your response. |
btw. you can configure different loaders depending on compilation: rules: [
{
test: /\.css$/,
compiler: { not: /^html-webpack-plugin/ },
use: ExtractTextPlugin.extract(...)
}
] |
@sokra This throws an error for me seems like
And I use this rule test: /\.styl$/,
compiler: {
not: /^html-webpack-plugin/
},
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
modules: true,
localIdentName: '[emoji]_[path]_[name]_[local]_[hash:base64:3]'
}
},
{ loader: 'stylus-loader' }
] |
share a correct way~ module: {
rules: [
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [{
loader: 'css-loader',
options: {
importLoaders: 1,
}
},{
loader: 'postcss-loader',
options: {
plugins: (loader) => [precss, autoprefixer({
browsers: [
"> 1%",
"last 5 versions",
"ie 6"
]
})]
}
}
]
})
}
]
},
plugins:[
// ...
new ExtractTextPlugin('[name]/styles.[contenthash].css')
], |
Finally what is the right answer? |
I've never seen more counter-intuitive and error-prone thing then webpack.config... cannot do single bit without running into mysterious issues and googling around for hours. There is no plugin once you just added the plugin... enough for today |
I am still waiting for this to be fixed :( |
So....what is the right answer? @digitalkaoz |
It's the same as webpack-contrib/mini-css-extract-plugin#219 The html-webpack-plugin picks up the main compiler configuration but the mini-css-extract-plugin and the extract-text-plugin loaders get confused somehow. It's probably because of the way the loader and plugin communicate using the |
@jantimon do u have some ways to filter html-webpack-plugin not to handle with mini-css-extract-plugin loader ? I have tried some ways from above but it was still not work well . |
https://blog.csdn.net/simplehouse/article/details/78408679 |
anyone searching for a solution, this is the right one: drop this plugin and do it with the extract-loader + file-loader: |
@digitalkaoz Thank you for the workaround. Using extract-loader + file-loader seems to prevent the error and a CSS files is generated, but it looks like this file isn't added automatically to my |
finally i am ok!when i add
|
This issue had no activity for at least half a year. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Again, this error popped up (node-version:
v7.4.0
)my webpack config
my template
if i exclude the
ExtractTextPlugin
the rendering works fine...any ideas?
The text was updated successfully, but these errors were encountered: