javascript - Webpack使用html文件中的html文字提取js

标签 javascript webpack

我有一个这样的js文件

const link = () => {
   return '<p>my text </p>'
}

我想使用 webpack 返回一个 html 文件:

<p>my text </p>

我需要使用哪个加载器?你有一个 webpack 配置示例吗?

谢谢

最佳答案

听起来您想从 js 文件创建 HTML 文件。

如果要寻找与此类似的“静态站点生成器”:

如果您没有使用 .像 React 或 Angular 这样的框架,您可能必须使用模板语言,例如 Handlebars 或 ejs .

以下是一些如何开始构建网站的示例:

您的index.js文件可能看起来更像:

module.exports = function(locals) {
  return locals.template({ html: '<h1>' + locals.path + '</h1>' });
};

它将与 template.ejs 文件结合使用,例如:

<html>
  <body>
    <%- html %>
  </body>
</html>

和一个 wabpack 配置文件,例如:

var StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
var StatsWriterPlugin = require("webpack-stats-plugin").StatsWriterPlugin;
var ejs = require('ejs');
var fs = require('fs');

var template = ejs.compile(fs.readFileSync(__dirname + '/template.ejs', 'utf-8'))

var paths = [
  '/',
  '/foo',
  '/foo/bar'
];

module.exports = {
  entry: __dirname + '/index.js',

  output: {
    filename: 'index.js',
    path: __dirname + '/actual-output',
    libraryTarget: 'umd'
  },

  plugins: [
    new StaticSiteGeneratorPlugin({
      paths: paths,
      locals: {
        template: template
      }
    }),
    new StatsWriterPlugin() // Causes the asset's `size` method to be called
  ]
};

关于javascript - Webpack使用html文件中的html文字提取js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47927507/

相关文章:

javascript - 文本换行到列

javascript - 疯狂的 javascript 问题 : undefined actually ! == 未定义!

javascript - js 时区 - 日期获取不正确

angularjs - 带有 Angular 1 和 Bower 的 Webpack

.net - Azure 部署 .NET 应用部署脚本

javascript - 使 webpack 库输出与 babel 6 兼容

mocha.js - 使用Babel和Webpack的Mocha测试失败

javascript - Dockerode 可与 Node 配合使用,但不能通过 Webpack 配合使用

javascript - 检测原始数据的变化

javascript - 在 JavaScript 中将方法用作(curried)函数