-
Starting Node.js 17.1.x, the
The problem is that after this error is thrown, no further linting is performed, ESLint doesn't analyze any further line of code and bearing in mind that usually loading of JSON modules defined somewhere at the beginning of a file, ESLint doesn't scan the entire file. Currently, the Import Assertions is on the stage 3 and I know that ESLint accepts new JS features starting the stage 4, but bearing in mind a fact that without a support of |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 34 replies
-
We have the policy to wait until stage four because We don’t know which ECMA version it will end up in. Once import assertions have landed in an ECMAScript version, we can look at adding support in ESLint. In the meantime, you’ll need to use some thing like the Babel ESLint parser to get the support. |
Beta Was this translation helpful? Give feedback.
-
I know it is far from ideal and only works on a node environment, but this workaround worked for me in the meanwhile and I thought it might help others reading this in the future: import { readFileSync } from "fs";
const packageJSON = JSON.parse(readFileSync("./package.json")); |
Beta Was this translation helpful? Give feedback.
-
BIG thanks to @msurdi 💯 😍 !!! I now use this as my rollup.config.mjs thanks to you:import { copy } from '@guanghechen/rollup-plugin-copy'
import sourcemaps from 'rollup-plugin-sourcemaps'
import { terser } from 'rollup-plugin-terser'
/**
* Work-around for eslint's failure for import-assert !
* @todo Until eslint supports this, we need this work-around !
* @see {@link https://github.com/eslint/eslint/discussions/15305#discussioncomment-2400923 solutions attribution}
*/
// import pkg from './package.json' assert {type: 'json'};
import { readFileSync } from 'fs'
function addCopyrights(minified){
return [
'/**'
,(minified ? ' * minified version\n' : '')
+ ' * @copyright See the `/LICENSE.*` file for details.'
,' * @license CC-BY-NC-SA-4.0'
,' */'
].join('\n')
}
const
buildDir = 'public'
,sourcesDir = 'src'
,_buildStamp = new Date().toISOString()
/* eslint-disable comma-style */
,/**
* Work-around for eslint's failure for import-assert !
* @todo Until eslint supports this, we need this work-around !
* @see {@link https://github.com/eslint/eslint/discussions/15305#discussioncomment-2400923 solutions attribution}
*/
pkg = JSON.parse(readFileSync('./package.json'))
/* eslint-enable comma-style */
,commonOutputOptions = {
dir: buildDir
,entryFileNames: '[name].mjs'
,banner:
addCopyrights(false)
// Add _BUILDSTAMP_ here so it will be optimized also.
+ `\nconst _BUILDSTAMP_ = "${pkg.version}@${_buildStamp}";`
,preserveModules: false
,preserveModulesRoot: sourcesDir
,sourcemap: true
,sourcemapExcludeSources: true
}
,terserOptions = {
ecma: '2015'
,format: {
preamble: addCopyrights(true)
// Don't preserve ANY comments.
,comments: false
}
/**
* Required for our logger to show meaningfull stacktraces !
*/
// eslint-disable-next-line camelcase
,keep_fnames: true // spell-checker: disable-line
// eslint-disable-next-line camelcase
,keep_classnames: true
,module: true
,compress: false
// ,compress: {
// passes: 2 // More as 2 has no effect atm...
// }
}
,copyStaticFilesOptions = {
verbose: true
,targets: [
// Unaltered
{
src: [
'LICENSE.md'
,'README.html'
,`${sourcesDir}/*.{css,svg,png}`
]
,dest: buildDir
// ,copyOnce: true
}
// Using non-minimized js
,{
src: `${sourcesDir}/*.html`
,dest: buildDir
,rename: name => name + '.nonmin.html'
}
// Using minimized js
,{
src: `${sourcesDir}/*.html`
,dest: buildDir
// ,copyOnce: true
,transform: contents => contents
.toString()
.replace('app.mjs' ,'app.min.mjs')
}
]
}
export default {
input: [
`${sourcesDir}/app.mjs`
,`${sourcesDir}/ThreeD-app.mjs`
]
,output: [
{
...commonOutputOptions
}
,{
...commonOutputOptions
,entryFileNames: '[name].min.mjs'
,plugins: [
terser(terserOptions)
]
}
]
,plugins: [
sourcemaps()
,copy(copyStaticFilesOptions)
]
,watch: {
clearScreen: false
}
// ,perf: true
} |
Beta Was this translation helpful? Give feedback.
-
Try this solution: yarn add -D @babel/eslint-parser @babel/plugin-syntax-import-assertions // .eslintrc.js
parser: "@babel/eslint-parser",
parserOptions: {
requireConfigFile: false,
babelOptions: {
plugins: [
'@babel/plugin-syntax-import-assertions'
],
},
}, It works for me. |
Beta Was this translation helpful? Give feedback.
-
If you control the launching of the node process (e.g. a test suite) you can avoid the import assertion completely with a custom loader. You do get some annoying warnings though.
main.js import data from './data.json'
console.log(data); data.json {
"foo": "bar"
} loader.js import path from 'path';
import { promises as fs } from 'fs';
import urlModule from 'url';
export async function load(url, context, defaultLoad) {
const filename = urlModule.fileURLToPath(url);
const parts = path.parse(filename);
if (parts.ext === ".json") {
const source = await fs.readFile(filename);
return { source, format: 'json', shortCircuit: true };
}
return await defaultLoad(url, context);
} |
Beta Was this translation helpful? Give feedback.
-
I use this low-tech solution where I bundle all JSON imports in 1 file, and then re-export them as named exports. /*
* Eslint does not support import assertions because they only
* support stage-4 language features.
*
* It's annoying and can't be disabled. So instead this file
* will import all JSON and you can then import it from here.
*
* This way, we just ignore this file in eslint and voila.
*/
import dockerAllContainersExample from './docker-all-containers.json' with { type: 'json' }
import dockerRunningContainersExample from './docker-running-containers.json' with { type: 'json' }
import dockerDfExample from './docker-df.json' with { type: 'json' }
import dockerInfoExample from './docker-info.json' with { type: 'json' }
import dockerImagesExample from './docker-images.json' with { type: 'json' }
import dockerNetworksExample from './docker-networks.json' with { type: 'json' }
export {
dockerAllContainersExample,
dockerRunningContainersExample,
dockerDfExample,
dockerInfoExample,
dockerImagesExample,
dockerNetworksExample,
} |
Beta Was this translation helpful? Give feedback.
-
The day we've all been waiting for (😄 ), Import Attributes have now finally advanced to Stage 4 at TC39! 🎉
Not sure if we should introduce an issue for it now formally or if there is another method the team would desire for tracking this change? |
Beta Was this translation helpful? Give feedback.
We have the policy to wait until stage four because We don’t know which ECMA version it will end up in. Once import assertions have landed in an ECMAScript version, we can look at adding support in ESLint. In the meantime, you’ll need to use some thing like the Babel ESLint parser to get the support.