Javascript 在我的苗条web应用程序中使用动态导入和加密js时,我无法正确编译汇总
最近,我尝试对每个页面进行代码拆分,但在使用crypto js包时,无法使其正常工作。如果我取下这个包,一切正常。js使用行Javascript 在我的苗条web应用程序中使用动态导入和加密js时,我无法正确编译汇总,javascript,node.js,svelte,bundling-and-minification,rollup,Javascript,Node.js,Svelte,Bundling And Minification,Rollup,最近,我尝试对每个页面进行代码拆分,但在使用crypto js包时,无法使其正常工作。如果我取下这个包,一切正常。js使用行import“crypto”进行编译,这会导致浏览器出错而无法工作 rollup.config.js import resolve from '@rollup/plugin-node-resolve'; import commonjs from '@rollup/plugin-commonjs'; import livereload from 'rollup-plugin-
import“crypto”
进行编译,这会导致浏览器出错而无法工作
rollup.config.js
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import json from '@rollup/plugin-json';
import { config } from "dotenv"
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
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);
}
};
}
let envVar = {}
Object.entries(config().parsed).map(([prop, value]) => {
if (prop.startsWith("APP")) envVar[prop] = value
});
export default {
input: 'src/main.js',
output: {
sourcemap: !production,
format: 'esm',
name: 'app',
dir: 'public/build',
},
plugins: [
json(),
replace({
__myapp: JSON.stringify({
env: envVar
}),
}),
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('bundle.css');
}
}),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
commonjs({
transformMixedEsModules:true,
sourceMap: !production,
}),
resolve({
browser: true,
dedupe: ['svelte'],
preferBuiltins: false
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};
(function(l, r) { if (l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (window.location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.getElementsByTagName('head')[0].appendChild(r) })(window.document);
export { ao as default } from './main-d2838af7.js';
import 'crypto';
//# sourceMappingURL=main.js.map
浏览器错误
未捕获类型错误:解析模块说明符“crypto”失败。相对引用必须以“/”、“/”或“./”开头
main.js
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import livereload from 'rollup-plugin-livereload';
import json from '@rollup/plugin-json';
import { config } from "dotenv"
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
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);
}
};
}
let envVar = {}
Object.entries(config().parsed).map(([prop, value]) => {
if (prop.startsWith("APP")) envVar[prop] = value
});
export default {
input: 'src/main.js',
output: {
sourcemap: !production,
format: 'esm',
name: 'app',
dir: 'public/build',
},
plugins: [
json(),
replace({
__myapp: JSON.stringify({
env: envVar
}),
}),
svelte({
// enable run-time checks when not in production
dev: !production,
// we'll extract any component CSS out into
// a separate file - better for performance
css: css => {
css.write('bundle.css');
}
}),
// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
commonjs({
transformMixedEsModules:true,
sourceMap: !production,
}),
resolve({
browser: true,
dedupe: ['svelte'],
preferBuiltins: false
}),
// In dev mode, call `npm run start` once
// the bundle has been generated
!production && serve(),
// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload('public'),
// If we're building for production (npm run build
// instead of npm run dev), minify
production && terser()
],
watch: {
clearScreen: false
}
};
(function(l, r) { if (l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (window.location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.getElementsByTagName('head')[0].appendChild(r) })(window.document);
export { ao as default } from './main-d2838af7.js';
import 'crypto';
//# sourceMappingURL=main.js.map
您能否共享使用加密包的文件的代码(仅文件的导入和扩展就足够了)?顺便注意:您共享的编译代码是一个dev构建,而不是prod,所以这就是我们将在这里调试的内容。您知道这一点吗?更新后我也有同样的问题