chucklai
chucklai

Reputation: 844

why module build error happens when run webpack?

I'm writting a loader while this error happens:

ERROR in ./src/components/notFound.vue
Module build failed (from ./loader/cssExchangeLoader.js):
/Users/laiyinan/Project/前端开发/blog/loader/cssExchangeLoader.js:3
export default function(source){
^^^^^^

SyntaxError: Unexpected token export

loader(ALL):

var loaderUtils = require('loader-utils');

export default function(source){
    let options = loaderUtils.getOptions(this);
    console.log(options);
    console.log(source.substr(0,10));
    return `export default ${JSON.stringify(source)}`;
} 

webpack.config(Part of):

{
                test: /(\.css$)|(\.scss$)|(\.vue$)/,
                use: [
                    {
                        loader: path.resolve(root,'loader/cssExchangeLoader.js'),
                        options: {
                            target: 'red',
                            alternative: 'green'
                        }
                    }
                ],
                exclude: [/node_modules/,/loader/]
            }

why module build error happens when run webpack? And how to fix it? Thanks in advance!

Upvotes: 0

Views: 39

Answers (1)

szatkus
szatkus

Reputation: 1385

Webpack by default (well, actually not Webpack by itself, but Node.js) supports only CJS imports. This code would work:

var loaderUtils = require('loader-utils');

module.exports = function(source){
    let options = loaderUtils.getOptions(this);
    console.log(options);
    console.log(source.substr(0,10));
    return `export default ${JSON.stringify(source)}`;
} 

Upvotes: 1

Related Questions