【发布时间】:2020-12-15 09:10:13
【问题描述】:
完成开发后,我运行npm run build 来构建生产应用程序。但我会得到:
Uncaught SyntaxError: Unexpected token '
来自bundle.js。
我在svelte 3.0.0 上运行。我该如何解决这个问题?
更新1
下面是我的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 preprocess from 'svelte-preprocess';
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.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
dev: !production,
preprocess: preprocess(),
css: css => {
css.write('bundle.css');
}
}),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
!production && serve(),
!production && livereload('public'),
production && terser()
],
watch: {
clearScreen: false
}
};
我不太记得了,但我相信我所做的唯一更改是与 css 部分的预处理有关。
更新2
我只是用新的svelte 项目进行测试,没有碰任何东西。
我几乎按照网站上的说明进行操作:
npx degit sveltejs/template my-svelte-project
cd my-svelte-project
npm install
npm run build
唯一不同的是我运行build 而不是dev。并立即得到了同样的错误:
bundle.js:1 Uncaught SyntaxError: Unexpected token '
下面是rollup.config.js:
import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import css from 'rollup-plugin-css-only';
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.js',
output: {
sourcemap: true,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
compilerOptions: {
// enable run-time checks when not in production
dev: !production
}
}),
css({ output: 'bundle.css' }),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
!production && serve(),
!production && livereload('public'),
production && terser()
],
watch: {
clearScreen: false
}
};
除了取出cmets外,所有代码都是下载的全新代码。
我该如何解决这个问题?
【问题讨论】:
-
您是否正在尝试导入特殊文件,例如
.json、图像或 typescript/es6? -
@johannchopin 我在开发过程中可能会遇到。我确实在 index.html 中导入了一些 js 包。有问题吗?
-
请分享您的汇总配置以及有关错误的更多详细信息。
-
@johannchopin 我添加了我的汇总配置。问题是,我尝试使用全新的下载项目并构建它,已经从那里得到了问题。我想知道这是否只是我的问题。
-
@sooon 你运行的是什么版本的 node 和 npm?推荐使用 LTS 版本(14.15.2)
标签: javascript svelte