【问题标题】:webpack -p disables es2015 features in my ejs templateswebpack -p 在我的 ejs 模板中禁用 es2015 功能
【发布时间】:2016-09-08 03:44:51
【问题描述】:

我的项目有一个简单的 webpack 设置,它在前端使用 ejs 模板:

<ul>
  <% for (let station of stations) { %>
    <li>
      <strong><%= station.name %></strong>
      <br/>
      Coach Code: <%= station.nationalcoachcode %><br/>
      Distance:  <%= (station.distance / 1000 ).toFixed(1) %> km
    </li>
  <% } %>
</ul>

当我使用 webpack-dev-server 提供它或使用 webpack 构建它时,这非常有效,但是当我尝试使用 webpack -p 获得一个缩小的构建时,它无法再处理 for-of 循​​环并给我以下错误:

ERROR in app.js from UglifyJs SyntaxError: Unexpected token name «station», expected punc «;» [./~/ejs-loader!./src/app/stations.tmpl.ejs:7,0]

这是我的webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' );
const webpack = require ( 'webpack' );

const PATHS = {
  app: path.join( __dirname , 'src' ),
  build : path.join( __dirname , 'build' )
};

module.exports = {

  entry: {
    app: PATHS.app
  },

  output: {
    path: PATHS.build,
    filename : '[name].js'
  },

  devtool: 'source-map',

  module: {
    loaders: [{
      test: /\.js$/,
      loader: 'babel-loader',
      query: {
        presets: ['es2015']
      }
    }, {
      test: /\.ejs$/,
      loader: 'ejs-loader'
    }]
  },

  resolve: {
    extensions: ['', '.js', '.json', 'ejs']
  },

  plugins: [
    new HtmlWebpackPlugin({
      template: path.join(PATHS.app, 'index.ejs')
    }),
    new webpack.ProvidePlugin({
      _: "lodash"
    })
  ]

};

[edit]:我已经尝试过先通过 babel 运行 ejs 模板,但没有解决问题。

【问题讨论】:

    标签: javascript ecmascript-6 webpack ejs


    【解决方案1】:

    我的英文不好:(看代码就行了:

    module: {
      preLoaders: [
        {
          test: /\.ejs$/, loader: 'ejs-compiled'
        }
      ],
      loaders: [
        {
          test: /\.e?js$/, // <-- look!
          exclude: /node_modules/,
          loader: "babel-loader",
          query: {
            presets: ['es2015']
          }
        }
      ]
    },
    'ejs-compiled-loader': {
      htmlmin: true,
      htmlminOptions: {
        removeComments: true
      },
      _with: false // <-- important!!!
    }
    

    在 EJS 中

    <% var {station} = locals; %>
    <ul>
      <% for (let station of stations) { %>
        <li>
          <strong><%= station.name %></strong>
          <br/>
          Coach Code: <%= station.nationalcoachcode %><br/>
          Distance:  <%= (station.distance / 1000 ).toFixed(1) %> km
        </li>
      <% } %>
    </ul>
    

    希望对你有帮助……

    【讨论】:

    • 谢谢,但这对我也不起作用。有了这个配置,它现在给我一个错误ReferenceError: htmlWebpackPlugin is not defined
    • 另外,是否有可能在 .ejs 文件中您需要从本地获取 stations,而不是 station
    • 如果你禁用with (_with: false) 那么station 是未定义的。 var station = locals.station
    • 没关系,不用麻烦了。对于太少的回报来说,这太老套了。我会坚持在我的模板中使用 VanillaJS。不过还是谢谢!!
    • 嗯......也许......你会在插件中犯错吗?我已经赢得了我的版本
    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多