4
votes

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?

you can't because webpack will work on node js, which uses common js, es modules are not allowed on node yet.PlayMa256
So this example where the loader eses es6 imports can't work without pre-compiling it?Devid Farinelli
Nop, it won't work unless compiled.PlayMa256
github.com/webpack-contrib/uglifyjs-webpack-plugin as example... build using es6 but has a build step.PlayMa256
That's clear, thank you!Devid Farinelli