Skip to content

How can I get a html text by require() #156

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

Closed
xiaogan18 opened this issue Dec 9, 2017 · 5 comments
Closed

How can I get a html text by require() #156

xiaogan18 opened this issue Dec 9, 2017 · 5 comments

Comments

@xiaogan18
Copy link

the code:
var rr=require('html-loader?./1.html')
console.info(rr)
console.info(typeof(rr))
console log:
export default "

testing
";
string

I want get a html string of 1.html,like"

testing
" without "export default".

@michael-ciniawsky
Copy link
Member

const html = require('html-loader?./1.html').default

@xiaogan18
Copy link
Author

it output 'undefined'

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Dec 9, 2017

Try ! instead of ? in the file path please

const html = require('html-loader!./1.html') // should normally work
const html = require('html-loader!./1.html').default

Otherwise show your full webpack.config.js and try to use the loader within the config file

webpack.config.js

{
  test: /\.html$/,
  use: [ 'html-loader' ]
}

@xiaogan18
Copy link
Author

xiaogan18 commented Dec 9, 2017

'use strict'

const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf')

function resolve (dir) {
  return path.join(__dirname, '..', dir)
}

const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

module.exports = {
  context: path.resolve(__dirname, '../'),
  entry: {
    app: './src/main.js'
  },
  output: {
    path: config.build.assetsRoot,
    filename: '[name].js',
    publicPath: process.env.NODE_ENV === 'production'
      ? config.build.assetsPublicPath
      : config.dev.assetsPublicPath
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src'),
    }
  },
  module: {
    rules: [
      ...(config.dev.useEslint ? [createLintingRule()] : []),
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: [resolve('src'), resolve('test')]
      },
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('img/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('media/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
        loader: 'url-loader',
        options: {
          limit: 10000,
          name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
        }
      },
      {
        test: /\.html$/,
        loader: 'html-loader',
        options: {
          minimize:true
        }
      }
    ]
  },
  node: {
    // prevent webpack from injecting useless setImmediate polyfill because Vue
    // source contains it (although only uses it if it's native).
    setImmediate: false,
    // prevent webpack from injecting mocks to Node native modules
    // that does not make sense for the client
    dgram: 'empty',
    fs: 'empty',
    net: 'empty',
    tls: 'empty',
    child_process: 'empty'
  }
}

@michael-ciniawsky
Copy link
Member

Did you remove the html-loader! prefix from the import/require when setting html-loader in webpack.config.js ? This might also be related to vue && vue-loader which loads HTML on it's own => vue-html-loader, so please consider opening an issue in vue-loader instead aswell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants