admin管理员组文章数量:1387455
When I use only const Example1 = require('./example1.js)
statement then code inside example1.js file is getting included in the bundle. And if I use only import Example2 from './example2.js'
then also code inside example2.js is getting included in the bundle. But if I use both the statements only import
is working and require
is not working.
I am using rollup for bundling.
My rollup configuration looks like this
import babel from 'rollup-plugin-babel'
import monjs from 'rollup-plugin-monjs'
import external from 'rollup-plugin-peer-deps-external'
import postcss from 'rollup-plugin-postcss'
import resolve from 'rollup-plugin-node-resolve'
import url from 'rollup-plugin-url'
import svg from 'rollup-plugin-svg'
import json from 'rollup-plugin-json';
import { terser } from 'rollup-plugin-terser'
export default {
input: 'src/sdk/test.js',
output: [
{
file: "src/sdk/sdk.js",
format: 'cjs'
},
{
file: "src/sdk/sdk.es.js",
format: 'es'
},
{
file: "src/sdk/sdk.iife.js",
format: 'iife'
}
],
plugins: [
resolve({
browser: true,
}),
monjs(),
external(),
postcss({
modules: true
}),
url({
limit: 100 * 1024,
emitFiles: false
}),
svg(),
babel({
exclude: 'node_modules/**',
"plugins": ["@babel/plugin-proposal-class-properties"]
}),
terser(),
json()
]
}
When I use only const Example1 = require('./example1.js)
statement then code inside example1.js file is getting included in the bundle. And if I use only import Example2 from './example2.js'
then also code inside example2.js is getting included in the bundle. But if I use both the statements only import
is working and require
is not working.
I am using rollup for bundling.
My rollup configuration looks like this
import babel from 'rollup-plugin-babel'
import monjs from 'rollup-plugin-monjs'
import external from 'rollup-plugin-peer-deps-external'
import postcss from 'rollup-plugin-postcss'
import resolve from 'rollup-plugin-node-resolve'
import url from 'rollup-plugin-url'
import svg from 'rollup-plugin-svg'
import json from 'rollup-plugin-json';
import { terser } from 'rollup-plugin-terser'
export default {
input: 'src/sdk/test.js',
output: [
{
file: "src/sdk/sdk.js",
format: 'cjs'
},
{
file: "src/sdk/sdk.es.js",
format: 'es'
},
{
file: "src/sdk/sdk.iife.js",
format: 'iife'
}
],
plugins: [
resolve({
browser: true,
}),
monjs(),
external(),
postcss({
modules: true
}),
url({
limit: 100 * 1024,
emitFiles: false
}),
svg(),
babel({
exclude: 'node_modules/**',
"plugins": ["@babel/plugin-proposal-class-properties"]
}),
terser(),
json()
]
}
Share
Improve this question
asked Jul 7, 2020 at 8:15
Vikram HegdeVikram Hegde
6502 gold badges7 silver badges14 bronze badges
2
- Well, no, just don't mix the two module syntaxes. – Bergi Commented Jul 26, 2020 at 15:33
- I wish I could @Bergi – Vikram Hegde Commented Jul 27, 2020 at 12:52
1 Answer
Reset to default 3By default, rollup supports 'esm' modules as entry. If you like to include monjs files in the project, you can include '@rollup/plugin-monjs' into plugins field, it usually works.
Maybe the following config will help, I tried with very simple example:
monjs({transformMixedEsModules:true})
transformMixedEsModules Instructs the plugin whether or not to enable mixed module transformations. This is useful in scenarios with mixed ES and CommonJS modules. Set to true if it's known that require calls should be transformed, or false if the code contains env detection and the require should survive a transformation.
https://github./rollup/plugins/tree/master/packages/monjs
本文标签: javascriptHow to include both import and require statements in the bundle using rollupStack Overflow
版权声明:本文标题:javascript - How to include both import and require statements in the bundle using rollup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744559089a2612659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论