aMarCruz / rollup-plugin-pug

Rollup plugin that transforms pug templates in es6 modules.
MIT License
27 stars 11 forks source link

Importing modules #6

Closed noonien closed 7 years ago

noonien commented 7 years ago

How does one import modules?

- import 'something';
p= something('something')

errors out with: Error: 'import' and 'export' may only appear at the top level (2:0)

aMarCruz commented 7 years ago

@noonien , this plugin transforms pug templates to ES6 modules, but pug byself is an ES5 tool and does not supports modules.

To add support for ES6 modules to pug, you need to use a filter like this one

I've never used pug filters and can't help on this but I'm sure the pug site can. Closing now...

ADDED: If your tool-chain supports require you can use it without problems, I do this all the time with my pugjs-brunch plugin. If I have the time, a future version of rollup-plugin-pug will include support to imports.

noonien commented 7 years ago

From what I can gather, the JS code blocks (code after the -) are copied verbatim. I think the issue is that the said code is inside the compiled function that pug returns.

You already seem to keep track of pug dependencies(include, extends and mixins) and import them.

However, the problem appears to be that pug is not aware of the import JS statement and just copies it inside the compiled function, which is invalid ES6.

I don't believe filters can solve this, because all they do is apply transformation to the strings, not move code outside the function.

As far as I can tell, these are the available solutions:

I have also created an issue on the pug issue tracker @ pugjs/pug#2804 to get the pug developers opinions.

aMarCruz commented 7 years ago

@noonien , I think the rollup filter must translate imports to require inside the pug funcion, but really I don't know how it works.

And yes, the desired solution would be rollup-plugin-pug to move them out of the pug function. I hope to find the time to implement this, maybe the next week.

aMarCruz commented 7 years ago

@noonien , I made a commit in the branch "master" supporting import in ole-line code (each line containing import must start with a dash, without comments before the import clause).

Please test.

noonien commented 7 years ago

Importing now works just fine.

Awesome! Thanks!

aMarCruz commented 7 years ago

@noonien thank you. I will publish this version next week.