Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import default export of markedjs #99

Closed
1 of 9 tasks
RebeccaStevens opened this issue Feb 6, 2019 · 5 comments
Closed
1 of 9 tasks

Import default export of markedjs #99

RebeccaStevens opened this issue Feb 6, 2019 · 5 comments

Comments

@RebeccaStevens
Copy link

RebeccaStevens commented Feb 6, 2019

Description

My build breaks when I try to build my app as the markedjs library isn't being imported correctly.

If I change this line:

import * as marked from 'marked/lib/marked.js';

to this:

import marked from 'marked/lib/marked.js';

my build works successfully.

Expected outcome

console.log(window.marked);
// ƒ marked(src,opt,callback){ ...

Actual outcome

console.log(window.marked);
// {default: ƒ, __moduleExports: ƒ}

Browsers Affected

I assume all but I've only tested on Chromium.

  • Chromium
  • Chrome
  • Firefox
  • Safari 9
  • Safari 8
  • Safari 7
  • Edge
  • IE 11
  • IE 10
@e111077
Copy link
Contributor

e111077 commented Mar 12, 2019

Hi, unfortunately the marked library does not have an es6 default export (export default marked). This means that changing this to the default import breaks vanilla ES6 javascript usage of marked-element. This works fine with commonjs and marked uses commonjs exports as well which is what you may be seeing here.

Can you give more information about your build?

@RebeccaStevens
Copy link
Author

Basically I'm just trying to use the iron-component-page.
My main script file looks like this:

if (window.WebComponents === undefined) {
  console.error(new Error('WebComponents not defined.'));
} else {
  window.WebComponents.waitFor(async () => import('@polymer/iron-component-page'));
}

This is then passed to both rollup (to create an esm) and webpack (to create an iife). (I wasn't able to use rollup for both as rollup has issues creating an iife if the source has a dynamic import).

My rollup config looks something like this:

{
  input: joinPaths(config.packageRoot, config.docs.templateFiles.entrypoint),

  output: {
    dir: config.docs.path,
    entryFileNames: '[name].min.mjs',
    chunkFileNames: 'common/[hash].min.mjs',
    format: 'esm',
    sourcemap: false
  },

  treeshake: {
    pureExternalModules: true,
    propertyReadSideEffects: false,
    annotations: true
  },

  plugins: [
    rollupPluginNodeResolve(),
    rollupPluginCommonjs(),
    rollupPluginTypescript({
      tsconfig: joinPaths(config.packageRoot, config.docs.templateFiles.tsconfig)
    }),
    rollupPluginBabel({
      babelrc: false,
      extensions: ['.js', '.mjs', '.ts'],
      ...getBabelConfigModule()
    }),
    rollupPluginTerser(minModule)
  ]
}

Not sure if it's relevant or not but my source code is written in TypeScript.

@e111077
Copy link
Contributor

e111077 commented Mar 12, 2019

hmm is it possible to exclude the marked js library as a commonjs module? My guess is that your build is treating it as such when we are using it as an es module as marked was written to support all cases.

@RebeccaStevens
Copy link
Author

That was it. Adding this fixed my issue:

rollupPluginCommonjs({
  exclude: ['node_modules/marked/**']
})

Thanks for your help.

@e111077
Copy link
Contributor

e111077 commented Mar 12, 2019

Really good to hear, cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants