【发布时间】:2020-11-27 06:17:25
【问题描述】:
我正在尝试将我的 Svelte 应用程序打包到单个 Html 文件输出中。 我已经设法通过基于该答案的配置获得所需的输出: Output Single HTML File from Svelte Project
使用“npm run dev”,第一次构建一切正常,但我在(实时重载)构建后遇到问题:bundle['bundle.css'] 未填写在我的 inlineSvelte 的 generateBundle 函数中。
我没有设法将rollup-plugin-css-only 更改为rollup-plugin-embed-css,这似乎有一个适合我需要的名称。
这是我的rollup.config.js:
import svelte from 'rollup-plugin-svelte';
import livereload from 'rollup-plugin-livereload';
import css from 'rollup-plugin-css-only';
...
function inlineSvelte(templatePath, dest) {
return {
name: 'Svelte Inliner',
generateBundle(opts, bundle) {
const file = path.parse(opts.file).base;
const jsCode = bundle[file].code;
const cssCode = bundle['bundle.css'].source;
const template = fs.readFileSync(templatePath, 'utf-8');
bundle[file].code = template
.replace('%%script%%', jsCode)
.replace('%%style%%', cssCode);
}
}
}
export default {
input: 'src/main.js',
output: {
format: 'es',
file: outputDir + 'index.html',
name: 'app'
},
plugins: [
svelte({
compilerOptions: {
dev: !production
}
}),
css({ output: 'bundle.css' }),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
!production && livereload(outputDir),
inlineSvelte('./src/template.html')
],
watch: {
clearScreen: false
}
};
【问题讨论】:
标签: javascript html svelte rollup livereload