wwilsman / js-yaml-loader Goto Github PK
View Code? Open in Web Editor NEWJS-YAML loader for webpack
JS-YAML loader for webpack
Hi, I tried to use this library for the first time and immediately hit a very cryptic error:
ERROR in ./src/test.yaml (./node_modules/js-yaml-loader!./src/test.yaml)
Module Error (from ./node_modules/js-yaml-loader/index.js):
end of the stream or a document separator is expected at line 2, column 43:
... orts = doc.length <= 1 ? doc[0] : doc;
^
@ ./src/index.js 1:0-46 3:12-16
Then webpack quits because js-yaml-loader didn't export a Buffer or String (yeah, since it failed.)
I've made a (hopefully) minimal test case and put it on this repo: https://github.com/unleashy/js-yaml-error-demo
As you can see in src/test.yaml, the file is empty. It doesn't seem to matter what you put in the yaml file, so I'm guessing it's some issue in the library itself; weirdly, "end of the stream or a document separator is expected" is a JS-YAML error message, so it's like JS-YAML is trying to parse the JS code... somehow? I don't know.
In any case, thanks!
When trying to load a file with multiple documents, I get the following error:
Module Error (from ./node_modules/js-yaml-loader/index.js): expected a single document in the stream, but found more
The file looks like something like this:
---
kind: example
apiVersion: v1
name: test
---
kind: example2
apiVersion: v1
name: test2
---
kind: example3
apiVersion: v1
name: test3
Ideally, I'd add a checker to count the documents, and if length > 1, then I'd invoke the safeLoadAll
method instead of safeLoad
. But since js-yaml
doesn't export this counter function, would it be possible to replace safeLoad
to safeLoadAll
? I've tested and it would load both single and multiple documents in a file.
const uneval = require('un-eval');
const { getOptions } = require('loader-utils');
const yaml = require('js-yaml');
module.exports = function(source) {
this.cacheable && this.cacheable();
try {
const options = getOptions(this) || {};
const safe = options.safe !== false;
const res = safe ? yaml.safeLoadAll(source) : yaml.loadAll(source);
return `module.exports = ${uneval(res)};`;
} catch (err) {
this.emitError(err);
return null;
}
};
When trying to import a date from a YAML file, like this:
education:
- name: University of Oklahoma
instAbbr: UO
location: Oklahoma City, OK
degree: Bachelors of Information Technology
degAbbr: BIT
startDate: 2011-06-01
endDate: 2014-01-01
curriculum:
- DB1101 - Basic SQL
- CS2011 - Java Introduction
Webpack spits out the error:
ERROR in ./import/EXAMPLE.yaml
Module parse failed: Invalid number (42:23)
You may need an appropriate loader to handle this file type.
| degree: 'Bachelors of Information Technology',
| degAbbr: 'BIT',
| startDate: 2011-06-01T00:00:00.000Z,
| endDate: 2014-01-01T00:00:00.000Z,
| curriculum: [ 'DB1101 - Basic SQL', 'CS2011 - Java Introduction' ] } ],
@ ./src/index.js 27:11-44
@ multi (webpack)-dev-server/client?http://localhost:4000 ./src/index.js
Wrapping the dates in a quotes (to turn them into strings) works perfectly fine.
Attempting the same file with yaml-loader returns no issues, and imports the date as expected (i.e., as a DateTime object in the js object).
I am trying to read this yaml file:
function: !!js/function >
function foobar() {
return 'Wow! JS-YAML Rocks!';
}
My _webpack.config.js:
{
test: /\.yaml$/,
use: [{
loader: 'js-yaml-loader',
options: {
safe: false
}
}]
}
But i'm getting this error:
Module build failed: Error: Final loader didn't return a Buffer or String
I am used following way to read yaml file in React js
import React from 'react';
import logo from './logo.svg';
import './App.css';
import data from "./data.yml";
const title = data;
function App() {
return (
this is yml{data.version}{title}
Hey there! First of all, thank you for this great module!
Basically, everything works fine when I run yarn dev
, but when I try to build the project with yarn build
, the following error came:
yarn run v1.22.5
warning ..\..\..\package.json: No license field
$ next build
Failed to compile.
./core/components/Noun/words.ts:1:21
Type error: Cannot find module '../../content.yaml' or its corresponding type declarations.
> 1 | import content from "../../content.yaml";
| ^
3 |
4 | const words = (content as {
5 | words: string[],
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
package.json
{
/* ... */
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"js-yaml-loader": "^1.2.2",
"mobx": "^6.1.7",
"next": "10.0.6",
"node": "^15.8.0",
"react": "17.0.1",
"react-dom": "17.0.1",
"react-router-dom": "5.2.0",
"styled-components": "5.2.1"
},
"devDependencies": {
"@types/js-yaml": "^4.0.0",
"@types/node": "^14.14.31",
"@types/react": "17.0.1",
"@types/styled-components": "^5.1.7",
"typescript": "4.1.5"
}
}
webpack.config.js
module: {
rules: [{
test: /\.ya?ml$/,
use: 'js-yaml-loader',
}]
}
next.config.js
module.exports = {
webpack: function (config) {
config.module.rules.push(
{
test: /\.ya?ml$/,
use: 'js-yaml-loader',
},
)
return config
}
}
next-env.d.ts
/// <reference types="next" />
/// <reference types="next/types/global" />
Version 1.0.0 throws an error when trying to configure the loader without explicitly defining options
with Webpack.
Fails with error "Cannot read property 'safe' of null".
Failing configuration:
rules: [
{
test: /\.yaml$/,
use: 'js-yaml-loader'
}
]
Working configuration:
rules: [
{
test: /\.yaml$/,
use: [{
loader: 'js-yaml-loader',
options: {}
}]
}
]
Your package is vulnerable. could you apply a fix?
$ npm audit
json5 <2.2.2
Severity: high
Prototype Pollution in JSON5 via Parse Method - https://github.com/advisories/GHSA-9c47-m6qq-7p4h
node_modules/json5
loader-utils <=1.4.2
Depends on vulnerable versions of json5
node_modules/loader-utils
js-yaml-loader >=1.0.0
Depends on vulnerable versions of loader-utils
node_modules/js-yaml-loader
Upgrading https://www.npmjs.com/package/loader-utils should fix the issue:
npm install loader-utils@latest
I'm currently using js-yaml-loader
to load my credentials from /config/secrets.yml
but when I'm testing it using Jest.
image_handler.js
import placeholder from "../assets/images/placeholder.jpg";
import secrets from 'js-yaml-loader!../config/secrets.yml';
const config = secrets[process.env.NODE_ENV];
const parseImages = (images) => {
var poiImages = [];
if (images && images.length > 0 && images[0] != null) {
for (let image of images) {
var url = image.url || `${config.BASE_URL}/media/v1/download/uuid/${image.uuid}?apikey=${config.TIH_KEY}`;
poiImages.push(url);
}
} else {
poiImages.push(placeholder);
}
return poiImages;
}
export default {
parseImages
};
I'm getting Cannot find module 'js-yaml-loader!../config/secrets.yml' from 'image_handler.js'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.