Parcel: Import markdown file as a multi-line js string.

Created on 9 Mar 2018  路  10Comments  路  Source: parcel-bundler/parcel

馃檵 feature request

馃帥 Configuration (.babelrc, package.json, cli command)

// .babelrc
{
  "presets": [
    "env",
    "react"
  ],
  "plugins": [
    "transform-object-rest-spread",
    ["transform-class-properties", { "spec": true }]
  ]
}

馃 Expected Behavior

import mardownContent from '../contents/help.md';
console.log(mardownContent); // `# Title \n Paragraph`

馃槸 Current Behavior

import mardownContent from '../contents/help.md';
console.log(mardownContent); // 12d7fdaf521be8a4e6ac363931673970.md

馃拋 Possible Solution

Allow this natively or make it a plugin?

馃敠 Context

Isolating content from code seems like a fair idea.

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | ^1.4.1 |
| Node | v8.0.0 |
| npm/Yarn | 5.0.0 |
| Operating System | Linux |

Good First Issue Help Wanted Feature

Most helpful comment

@augnustin Parcel will parse readFileSync at build time and replace it with the contents of your file. You won't have any fs calls in the final bundle/in the browser.

All 10 comments

I think this should be part of the Parcel core.

This is a great issue for for anyone new to Parcel looking for contribute. Just create a new MarkdownAsset that spits out the markdown without any transforms.

Docs:
Basic Overview
More In-Depth Explanation

Hi David,

Thanks for reaching. I could have a look at this eventually. Let's clarify a bit first:

  • Doesn't this behavior exist for regular text files?
  • Doesn't this have to do with ES6 specifications? Shouldn't we be compliant?

Best

You can also natively read the raw content of any file using fs.readFileSync, Parcel will parse it. This keeps your code unopinionated and compatible with Node.js :

import {readFileSync} from 'fs'

const mardownContent = readFileSync(__dirname + '../contents/help.md', 'utf-8')

@fathyb I hardly see how this can work. We are talking about importing it at build time in the bundle. Once bundled, readFileSync won't be available in the browser.

Or did I miss something?

@augnustin Parcel will parse readFileSync at build time and replace it with the contents of your file. You won't have any fs calls in the final bundle/in the browser.

@fathyb That's a cool idea and it's awesome that it works in Parcel. Is there any way to make this work in rollup as well?

Is this still considered an issue? Looks to me like parcel already supports the use-case, albeit maybe missing a clear documentation?

Closing this, feel free to reopen if needed

Was this page helpful?
0 / 5 - 0 ratings