Can't write webpack loader using es6 imports
Asked Answered
H

0

6

I wrote this webpack loader

module.exports = function(source) {
    return `export default 'hello'`;
}

that I want to rewrite using es6 imports

export default function loader(source) {
    return `export default 'hello'`;
}

without success

SyntaxError: Unexpected token export


My webpack configuration is:

const path = require('path')

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },,
    resolveLoader: {
        modules: [
            'node_modules',
            path.resolve(__dirname, 'loaders')
        ]
    },
    module: {
        rules: [{
            test: /\.m?js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['@babel/preset-env']
                }
            }
        }, {
            test: /\.txt$/,
            use: {
                loader: 'my-loader'
            }
        }]
    }
};

How can I do this?

Humanly answered 7/11, 2018 at 16:57 Comment(5)
you can't because webpack will work on node js, which uses common js, es modules are not allowed on node yet.Geometrid
So this example where the loader eses es6 imports can't work without pre-compiling it?Humanly
Nop, it won't work unless compiled.Geometrid
github.com/webpack-contrib/uglifyjs-webpack-plugin as example... build using es6 but has a build step.Geometrid
That's clear, thank you!Humanly

© 2022 - 2024 — McMap. All rights reserved.