GithubHelp home page GithubHelp logo

Support extract jade ? about extract-loader HOT 8 OPEN

woohling avatar woohling commented on August 12, 2024
Support extract jade ?

from extract-loader.

Comments (8)

battlesnake avatar battlesnake commented on August 12, 2024 2

@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.

jhnns avatar jhnns commented on August 12, 2024

That should work – provided that you included the index.html anywhere in your bundle.

from extract-loader.

jpommerening avatar jpommerening commented on August 12, 2024

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.

jhnns avatar jhnns commented on August 12, 2024

@jpommerening Wow, that's genius. Would you be open to add a test/example for that to the extract-loader? :D

from extract-loader.

markbrouch avatar markbrouch commented on August 12, 2024

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.

zxdong262 avatar zxdong262 commented on August 12, 2024

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.

zxdong262 avatar zxdong262 commented on August 12, 2024

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.

battlesnake avatar battlesnake commented on August 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.