martonlederer / esbuild-plugin-postcss2

Use postcss with esbuild
MIT License
33 stars 19 forks source link

Doesn't run as expected in Readme #10

Closed DanielTate closed 3 years ago

DanielTate commented 3 years ago

npm install -D esbuild esbuild-plugin-postcss2

create build.js

    const esbuild = require('esbuild')
    const postCssPlugin = require('esbuild-plugin-postcss2')

    // Build CSS
    esbuild.build({
        entryPoints: ['src/css/app.css'],
        plugins: [
            postCssPlugin()
        ],
        outfile: 'dist/css/app.css'
    })

run node build.js

Error: postCssPlugin() is not a function

DanielTate commented 3 years ago

I also tested with Yarn yielding the same result. I noticed in your package you are exporting postCSSPlugin not postCssPlugin I did try both but didn't seem to get anywhere. Any insight would be great.

martonlederer commented 3 years ago

If you use ES modules, the default import should work. If not, just use it like this: https://github.com/th8ta/ArConnect/blob/3aa0ee9108ec430dd07303076517fb6df8dc3770/esbuild.js#L36

DanielTate commented 3 years ago

@martonlederer Thank you for your reply. If I could suggest you add that as a small note for people that don't know about ESM modules that would be helpful. Also in your example you're using require('esbuild') which suggests you're not using esbuild in this case?

After reading a little bit my understanding was adding "type":"module" to my package would let me use import statements in node. Would this make my script a "module"? In any case this doesn't change the example code provided. I still get the same errors weather I use this approach or not.

Can you give an example of using ESM and not using ESM?

For any search engine people, this worked for me:

esbuild.build({
    ...
    plugins: [
        postCssPlugin.default({
            plugins: [ ... ]
        })
    ],
    ...
})