Is there any way to use #next/env in codegen.yaml? - next.js

I was wondering if there was a way to use #next/env to replace variables in codegen.yaml when i run yarn codegen:
graphql-codegen -r dotenv/config --config codegen.yml - this is codegen example to load dot env
graphql-codegen -r #next/env --config codegen.yml - this is what i want to achieve
When i call it, i get
${SERVER_API_URL}/graphql goes as undefined/graphql
My usecase is:
# .env
HOST=https
API_DOMAIN=example.com
SERVER_API_URL=$HOST://$API_DOMAIN
# codegen.yaml
schema: ${SERVER_API_URL}/graphql
Why use #next/env? it replaces variables in .env, i'm attaching documentation sample from next.js documentation:
Note: Next.js will automatically expand variables ($VAR) inside of
your .env* files. This allows you to reference other secrets, like so:
# .env
HOSTNAME=localhost
PORT=8080
HOST=http://$HOSTNAME:$PORT
If you are trying to use a variable with a $ in the actual value, it needs to be escaped like so: \$.
For example:
# .env
A=abc
# becomes "preabc"
WRONG=pre$A
# becomes "pre$A"
CORRECT=pre\$A
source: https://nextjs.org/docs/basic-features/environment-variables#loading-environment-variables

You can't use #next/env directly via -r flag. However you still have couple of alternatives. For example, you can address the required .env file directly:
DOTENV_CONFIG_PATH=./.env.local graphql-codegen --config codegen.yml -r dotenv/config
If you still want to use #next/env, you can convert your codegen.yml to codegen.js and import #next/env. For example, my original YML:
overwrite: true
schema:
- https://graphql.fauna.com/graphql:
headers:
Authorization: 'Bearer ${FAUNA_ADMIN_KEY}'
documents: null
generates:
src/generated/graphql.ts:
plugins:
- typescript
- typescript-operations
- typescript-graphql-request
./graphql.schema.json:
plugins:
- 'introspection'
I re-wrote it as JS:
const { loadEnvConfig } = require('#next/env')
loadEnvConfig(process.cwd())
module.exports = {
overwrite: true,
schema: {
'https://graphql.fauna.com/graphql': {
headers: {
Authorization: `Bearer ${process.env.FAUNA_ADMIN_KEY}`,
},
},
},
documents: null,
generates: {
'src/generated/graphql.ts': {
plugins: [
'typescript',
'typescript-operations',
'typescript-graphql-request',
],
},
'./graphql.schema.json': { plugins: ['introspection'] },
},
}
and then generated the code using graphql-codegen --config codegen.js.
Here is an issue on Github that helped me.

Related

how to exclude files with postcss 8

I am using tailwindcss with postcss8 (it comes with tailwind) and there is a problem trying to parse a css from a node_modules package.
is there a way to exclude these css files from being processed by postcss?
this is the error I get:
ERROR in ./node_modules/#fullcalendar/common/main.css (./node_modules/#nuxt/postcss8/node_modules/css-loader/dist/cjs.js??ref--3-oneOf-1-1!./node_modules/#nuxt/postcss8/node_modules/postcss-loader/dist/cjs.js??ref--3-oneOf-1-2!./node_modules/#fullcalendar/common/main.css)
Module build failed (from ./node_modules/#nuxt/postcss8/node_modules/postcss-loader/dist/cjs.js):
ParserError: Syntax Error at line: 1, column 45
at /Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#fullcalendar/common/main.css:633:3
at Parser.error (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-values-parser/lib/parser.js:127:11)
at Parser.operator (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-values-parser/lib/parser.js:162:20)
at Parser.parseTokens (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-values-parser/lib/parser.js:245:14)
at Parser.loop (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-values-parser/lib/parser.js:132:12)
at Parser.parse (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-values-parser/lib/parser.js:51:17)
at parse (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-custom-properties/index.cjs.js:47:30)
at /Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-custom-properties/index.cjs.js:333:24
at /Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:72:18
at /Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:55:18
at Rule.each (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:41:16)
at Rule.walk (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:52:17)
at /Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:60:24
at Root.each (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:41:16)
at Root.walk (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:52:17)
at Root.walkDecls (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/#nuxt/postcss8/node_modules/postcss/lib/container.js:70:19)
at transformProperties (/Users/ctw/Sites/github/escience/WoSSS/website/node_modules/postcss-custom-properties/index.cjs.js:330:8)
# ./node_modules/#fullcalendar/common/main.css 4:14-191
# ./node_modules/#fullcalendar/common/main.js
# ./node_modules/#fullcalendar/timegrid/main.js
# ./node_modules/babel-loader/lib??ref--2-0!./node_modules/#nuxt/components/dist/loader.js??ref--0-0!./node_modules/vue-loader/lib??vue-loader-options!./components/global/Schedule.vue?vue&type=script&lang=js&
# ./components/global/Schedule.vue?vue&type=script&lang=js&
# ./components/global/Schedule.vue
# ./node_modules/.cache/nuxt/components/index.js
# ./node_modules/.cache/nuxt/components/plugin.js
# ./node_modules/.cache/nuxt/index.js
# ./node_modules/.cache/nuxt/client.js
# multi ./node_modules/#nuxt/components/lib/installComponents.js ./node_modules/.cache/nuxt/client.js
I also had this issue. However, this is how I solve it.
Add the following to your nuxt.config.js file.
build: {
postcss: {
plugins: {
/**
* do not remove the cssnano key.
* (https://github.com/fullcalendar/fullcalendar/issues/5503)
*/
cssnano: {
preset: [
'default',
{
calc: false,
},
],
},
},
},
transpile: [
/#fullcalendar.*/, // always need for fullcalenar
],
},
To learn more about it, read this processing css with postcss, calc/var problems.
I hope this helps you. If you have solved this, please share your solution so that the community can learn from it.

grunt rpm create a symlink

I have this grunt file that creates a rpm package for me, how can I create a symlink like this for an example:
link("/usr/local/bin/tams-cli", "/opt/tams-cli/tams-cli.js")
Have not been able to to find that, here below is my source code.
grunt.initConfig({
pkg: grunt.file.readJSON('./package.json'),
easy_rpm: {
options: {
buildArch,
rpmDestination: './built/',
},
release: {
files: [
{
src: ['node_modules/**/*',
'js/**/*',
'cfg/*',
'package.json',
'readme.md',
],
dest: '/opt/tams-cli',
},
{
src: 'tams-cli.js',
dest: '/opt/tams-cli',
mode: 0550,
}
],
excludeFiles: [
'tmp-*',
'./built',
],
},
},
To create the symlink after the rpm package is installed utilize the postInstallScript option in your easy_rpm task. The description for the postInstallScript reads:
postInstallScript
Array<String>
An array of commands to be executed after the installation. Each element in the array represents a command.
In the Gruntfile.js excerpt below it utilizes the ln command to create the symlink using the additional two options:
-s to make a symbolic link instead of a hard link.
-f to remove existing destination files if they exist already.
Gruntfile.js
grunt.initConfig({
// ...
easy_rpm: {
options: {
postInstallScript: ['ln -s -f /opt/tams-cli/tams-cli.js /usr/local/bin/tams-cli'],
// ..
},
// ...
},
// ...
});

Grunt less source maps change path prefix

My config
server: {
options: {
sourceMap: true,
sourceMapFilename: '.tmp/styles/main.css.map',
sourceMapURL: '/styles/main.css.map'
},
files: {
'.tmp/styles/main.css':
'src/app/views/styles/application.less'
}
},
My structure
.tmp
src
Gruntfile.js
so after calling grunt less:server
I am getting .tmp/styles/main.css.map
with attr "sources" everywhere src/ prefix
but I want without src/ because server starts from src/*
How can I change it ?
Since version 1.0.0. grunt-contrib-less accepts the same options as the command line compiler does. You can get a list of these options by running lessc wihtout any argument on your command line:
--source-map-rootpath=X Adds this path onto the sourcemap filename and less file paths.
So you should use:
options: {
sourceMap: true,
sourceMapFilename: '.tmp/styles/main.css.map',
sourceMapURL: '/styles/main.css.map',
sourceMapRootpath: "/app/views/styles/"
}

how to use multiple mochacov reporters in single Gruntfile and get the coverage report

Currently am switching between html-cov and json-cov in my gruntfile and running the command
grunt mochacov:all > coverage.html
(or)
grunt mochacov:all > coverage.json
Here is the json portion of config from my Gruntfile.js :
mochacov : {
options : {
reporter : 'json-cov',
//reporter : 'travis-cov',
require : ['should'],
timeout: 250000,
//quiet : true,
output : 'coverage.json',
},
all: ['test/test-*.js']
},
Is there anyway using which i can get both coverage.html and coverage.json in the output by default by specifying both html-cov and json-cov somehow?
Sadly, looks like by default grunt-mocha-cov doesn't support this, as the reporter only accepts a 'string' (per the docs).
Theoretically this should be possible with a grunt multitask.
I have found out the answer for the same :-
This is what we need to have in our Gruntfile.js
mochacov: {
jsoncoverage: {
options: {
reporter: 'json-cov',
//reporter : 'travis-cov',
require: ['should'],
timeout: 250000,
//quiet : true,
output: 'reports/coverage.json'
},
all: ['test/test-*.js']
},
htmlcoverage: {
options: {
reporter: 'html-cov',
//reporter : 'travis-cov',
require: ['should'],
timeout: 250000,
//quiet : true,
output: 'reports/coverage.html'
},
all: ['test/test-*.js']
}
}
//and then the following line needs to be there :
grunt.registerTask('mochacoverage', ['mochacov:jsoncoverage', 'mochacov:htmlcoverage']);
Then we can run it using grunt mochacoverage and it will do both html and json

Requirejs optimization with grunt

I am trying to create a requirejs optimization config with grunt and almond. Here's the config:
requirejs:
build:
options:
almond: true
dir: 'build'
appDir: ''
baseUrl: '../client'
wrap: true
include: '../client/main'
keepBuildDir: true
paths:
underscore: 'client/vendor/underscore'
jquery : 'client/vendor/jquery'
backbone : 'client/vendor/backbone'
Folder Structure:
Error:
C:\Users\User\Documents\Source\Project>grunt requirejs
Running "requirejs:build" (requirejs) task
>> Error: ENOENT, no such file or directory
>> 'C:\Users\User\Documents\Source\Project\build\models\MenuItem.js'
>> In module tree:
>> ../client/main
Warning: RequireJS failed. Use --force to continue.
Main.js code (written in coffeescript)
requirejs.config(
baseUrl: "client"
shim:
'backbone':
deps: ['jquery']
exports: 'Backbone'
'jquery':
exports: '$'
'underscore':
exports: '_'
paths:
jquery: 'vendor/jquery-1.11.0'
underscore: 'vendor/underscore'
backbone: 'vendor/backbone'
)
define 'start', ()->
window.types =
Models: {}
Collections: {}
Views: {}
null
require ['models/MenuItem', 'views/MenuItem'], (MenuItemModel, MenuItemView)->
view = new MenuItemView(
new MenuItemModel(),
"#app",
'first'
)
view.render();
null
I want to compile my entire project spread across multiple js files into a single file in a way that requirejs would not be needed. I am trying to do this using almond js but the build task does not look for referenced files relative to the path of referring file. Please help me with the correct configuration.

Resources