简体   繁体   English

无法使用webpack 4转换.marko文件

[英]cannot babel transform .marko files with webpack 4

i have a working marko setup for my widget. 我为我的小部件设置了有效的marko设置。 Im using webpack 4 and babel 7. When i add babel-loader to .marko files, the webpack compiler throws because it couldn't recognize marko's syntax as valid javascript. 我正在使用webpack 4和babel7。当我将babel-loader添加到.marko文件时,webpack编译器将抛出异常,因为它无法将marko的语法识别为有效的javascript。 However the loader should work after the marko transpilation. 但是,加载器应在标记拼写之后工作。

Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Volumes/Workspace/product-curator-widget/src/index.marko: A class name is required (1:6)

> 1 | class {
    |       ^
  2 |   onCreate () {

index.marko index.marko

class {
  onCreate () {
    this.state = {
      items: [ {label: 'foo'}, {label: 'bar'} ] 
    }
    const pr = new Promise((resolve) => resolve()) //trying to transpile this arrow function
  }
}


<paper>
  <chip for (item in state.items) label="${item.label}" value="${item.value}" />
</paper>

webpack.config.js webpack.config.js

'use strict'
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');

module.exports = () => ({
  mode: 'development',
  devtool: 'cheap-source-map',
  entry: [
    'core-js',
    './src/index.js'
  ],
  resolve: {
    extensions: ['.js', '.marko'],
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: [/node_modules/, /\.test\.js$/],
        use: ['babel-loader'],
      },
      {
        test: /\.marko$/,
        exclude: [/node_modules/, /\.test\.js$/],
        use: ['@marko/webpack/loader', 'babel-loader'],
      },
      {
        test: /\.scss$/,
        exclude: [/node_modules/],
        use: ['style-loader', 'css-loader', 'sass-loader'],
      }
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject: true,
      template: './src/index.html'
    }),
    new CleanWebpackPlugin()
  ],
})

babel.config.js babel.config.js

module.exports = function (api) {
  api.cache(true)

  return {
    "plugins": [
      "@babel/plugin-proposal-object-rest-spread",
      "@babel/plugin-transform-async-to-generator",
      "@babel/plugin-transform-regenerator",
    ],
    "presets": [
      [
        "@babel/preset-env",
        {
          "targets": {
            "ie": "10"
          }
        }
      ]
    ]
  }
}

Loaders in webpack are evaluated from right to left . webpack中的加载程序从右到左进行评估。 In this case, you'll want @marko/webpack/loader to be the first loader to run (put it last in the array), so by the time babel-loader is called, the .marko file has been compiled to JS. 在这种情况下,您将希望@marko/webpack/loader是第一个运行的加载器(将其放在数组的最后),因此,在调用babel-loader.marko文件已编译为JS。

Side note: if you're using Marko components that have been published to npm, you don't want to ignore node_modules . 旁注:如果您使用的是已发布到npm的Marko组件,则不想忽略node_modules Marko recommends publishing the source .marko files because the compiler produces different output for the server vs the browser. Marko建议发布源.marko文件,因为编译器为服务器和浏览器生成不同的输出。 Additionally, the compilation output can be different depending on the version of Marko your app is using. 此外,编译输出可能会有所不同,具体取决于您的应用程序所使用的Marko版本。

      {
        test: /\.marko$/,
        use: ['babel-loader', '@marko/webpack/loader'],
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM