Comments (8)
@markbrouch : I have the same issue (or similar at least), did you ever resolve it?
Pug rule:
{
test: /\.pug$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name].html'
}
},
{
loader: 'extract-loader'
},
{
loader: 'html-loader',
options: {
basedir: resolve('src'),
attrs: [
'img:src',
'a:href',
'script:src',
'link:href'
],
root: resolve('src')
}
},
{
loader: 'apply-loader',
options: {
obj: {
pages: require('./src/page'),
products: require('./src/product'),
}
}
},
{
loader: 'pug-loader',
options: {
basedir: resolve('src'),
pretty: true
}
}
]
},
Other stuff:
const resolve = (...paths) => path.resolve(__dirname, ...paths);
const entry = glob.sync('/**/*.@(pug|png|svg)',
{
root: resolve('src'),
nomount: true,
nosort: true
})
.map(x => x.replace(/^\/+/, './'));
module.exports = {
entry: entry,
context: resolve('src'),
output: {
path: resolve('dist'),
filename: 'bundle.js',
publicPath: '/'
},
resolve: {
extensions: [
'.pug',
'.ts'
],
mainFiles: [
'index.pug',
'index.ts'
],
alias: {
'@': 'src/'
},
modules: [
resolve('node_modules')
]
},
...
Resulting "HTML" file:
var req = require("!!/home/path-to-project/node_modules/pug-loader/index.js??ref--5-4!/home/path-to-project/src/index.pug");
module.exports = (req['default'] || req).apply(req, .....
from extract-loader.
That should work – provided that you included the index.html
anywhere in your bundle.
from extract-loader.
Late to the party, but: The jade-loader
(as well as the pug-loader
that replaced it) returns a template function that you have to call with the template substitutions you want to make. (See above links.)
The HTML loader expects a string of HTML as input, so it's not able to process the result (which is JavaScript code).
You can use the apply-loader
to call the template function and export the HTML result.
Also, you need to test for the input file (is index.html
or index.jade
?). I'm not really sure about the absolute path (__dirname
), you might just want to use a regex like /\.jade$/
or /\.html$/
.
Example:
// webpack config module.loaders:
{
test: /\.jade$/,
loaders: [ "file?name=[name].html", "extract", "html", "apply", "jade" ]
}
// your code:
const htmlUrl = require( './views/index.jade' );
from extract-loader.
@jpommerening Wow, that's genius. Would you be open to add a test/example for that to the extract-loader? :D
from extract-loader.
Very late to the party 😄
@jpommerening This was the solution I came up with as well, but it doesn't seem to be working. I was wondering if you could take a look to see if I'm missing something obvious.
My webpack config module.loaders:
{
test: /\.pug$/,
loaders: [
{
loader: 'file-loader',
options: { name: '[path][name].html' }
},
'extract-loader',
'html-loader',
'apply-loader',
'pug-loader'
]
}
This generates the correct html files, but their contents are off. For instance, each html file will be something like:
var req = require("!!<absolute-path-to-project>/node_modules/pug-loader/index.js!<absolute-path-to-pug-file>/a.pug");
module.exports = (req['default'] || req).apply(req, [])
...instead of the expected HTML.
I've tried a similar setup with extract-text-webpack-plugin
in conjunction with pug-loader
and apply-loader
and that worked except for how the plugin combines all files into one (I want them separate for pug). It seems like the issue is with extract-loader
not running the apply-loader
's exported function (which should be the html from the pug template function).
Removing html-loader
from the loader pipeline results in a webpack error:
ERROR in ./index.pug
Module build failed: <absolute-path-to-pug-file>/index.pug:2
module.exports = (req['default'] || req).apply(req, [])
^
TypeError: (req.default || req).apply is not a function
Greatly appreciate some help!
from extract-loader.
I've tried a similar setup with extract-text-webpack-plugin in conjunction with pug-loader and apply-loader and that worked
@markbrouch will you please share this config?
from extract-loader.
this one works
const pugOpts = {
loader: 'pug-html-loader',
options: {
data: {
version,
_global: {}
}
}
}
{
test: /\.pug$/,
use: [
'file-loader?name=index.html',
{
loader: 'extract-loader',
options: {
publicPath: ''
}
},
'html-loader',
pugOpts
]
}
from extract-loader.
I found that my problem wasn't due to configuration, it was a bug regarding circular dependencies which I've logged separately with an example #39 . I found that after switching from pug-loader
+extract-loader
to pug-html-loader
.
from extract-loader.
Related Issues (20)
- SyntaxError: unknown: Unexpected token (import) HOT 12
- Is this loader maintained? HOT 4
- webpack5 use extract-loader to extract html file,but extract-loader always report error. HOT 3
- No dependencies to successor loader
- Reporting a vulnerability
- Archive this package
- loaderContext.loadModule API throw error when using with css-loader
- Missing Babel dependencies in 5.0
- Shouldn't sourceMaps be ignored if the devtool option is set to false?
- Import error with ES6 js script embedded in html body HOT 1
- working incorrect with [email protected] HOT 1
- Doesn't work with aliases properly HOT 3
- undefined image src if image used twice in html HOT 1
- v5.2.0 breaks parts of code loaded with ref-loader HOT 1
- Update dependencies to babel7.
- style="background-image: url('file');" not parsed
- could there be boolean option to ignore .js files?
- Webpack 5.1.3 and PublicPath auto HOT 3
- Generating "[object Object]" for placeholders... HOT 4
- Extract loader doesn't work with css-loader 5.x HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from extract-loader.