json - @rollup/plugin-typescript 找不到 JSON 文件
问题描述
我已经设置了一个 Svelte 项目并希望使用 JSON 文件来使用 Svelte i18n 包。不幸的是,我无法导入 JSON 文件。它所需的软件包已安装,但我不知道为什么它的 Typescript 方面不接受它。
我在这里有一个最小的复制品。
import App from './App.svelte';
// src/index.js
import pkg from './package.json';
console.log(`running version ${pkg.version}`);
const app = new App({
target: document.body,
props: {
name: "hello"
}
});
export default app;
导入 JSON 文件会导致以下错误消息:
(!) Plugin typescript: @rollup/plugin-typescript TS2732: Cannot find module './locale.de.json'. Consider using '--resolveJsonModule' to import module with '.json' extension
//rollup.config.js
import svelte from 'rollup-plugin-svelte';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import sveltePreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import json from '@rollup/plugin-json';
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
process.on('SIGTERM', toExit);
process.on('exit', toExit);
}
};
}
export default {
input: 'src/main.ts',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
json({
compact: true
}),
svelte({
dev: !production,
css: css => {
css.write('bundle.css');
},
preprocess: sveltePreprocess(),
}),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
typescript({
sourceMap: !production,
inlineSources: !production
}),
!production && serve(),
!production && livereload('public'),
production && terser()
],
watch: {
clearScreen: false
}
};
//tsconfig.json
{
"resolveJsonModule": true,
"module": "esnext",
"sourceMap": true,
"extends": "@tsconfig/svelte/tsconfig.json",
"include": ["src/**/*"],
"exclude": ["node_modules/*", "__sapper__/*", "public/*"]
}
解决方案
我有同样的问题。resolveJsonModule: true
在每个打字稿调用中添加rollup.config.js
可以解决问题。
这使我想到汇总插件不会以预期的方式将 tsconfig 与覆盖合并。
rollup.config.js
typescript({
sourceMap: dev,
inlineSources: dev,
resolveJsonModule: true
})
tsconfig.js
{
"extends": "@tsconfig/svelte/tsconfig.json",
"compilerOptions": {
"lib": ["DOM", "ES2017", "WebWorker"]
},
"esModuleInterop": true,
"resolveJsonModule": true,
"include": ["src/**/*", "src/node_modules/**/*"],
"exclude": ["node_modules/*", "__sapper__/*", "", "static/*"],
"types": ["svelte", "@sapper"]
}
顺便提一句。我也在用 i18n 进行测试;)。
推荐阅读
- angular - 删除 Fomantic UI(语义 UI 分支)中未使用的 CSS/JS
- javascript - 即使更改了基础层,如何在前面保持覆盖层
- sql - 获取 main 中每个子组的计数
- reactjs - 未定义函数(多个函数调用) - React
- javascript - 当提供者组件父状态发生变化时,为什么不能更新子组件中的值(带有反应上下文)?
- android - 使用数据绑定从 ViewModel 显示/隐藏 ProgessBar - MVVM
- python - 如何更改 django 模板中的现有变量
- mysql - 如何在 SQL 查询中获取唯一数据?
- sql - 试图找到至少 1 件交易金额 <= 10 美元的交易
- r - 如何知道 ROC 曲线上 X 的值,特别是截止值