汇总:如何要求 json 但不将其包含在捆绑包中
rollup: how to require json but not include it in bundle
我的 node
脚本中某处需要一个 json
文件,但我 不想 将其捆绑 [=16] =].我希望它被视为 "external"。每次 json
文件更改时,脚本应该能够获取更改,而无需通过汇总重建包。目前,它在捆绑包中 "hard coded":
// ./src/index.js
const json = require('../example.json');
与 ./example.json
为:
{
"exampleValue": "X"
}
returns 捆绑包包括:
// ...
var exampleValue = "X";
var example = {
exampleValue: exampleValue
};
var example = /*#__PURE__*/Object.freeze({
exampleValue: exampleValue,
default: example
});
// ...
我试图用 rollup-plugin-json
排除它,但正如我所料,它抛出:
[!] Error: Unexpected token (Note that you need rollup-plugin-json to import JSON files)
是否可以将 json 文件标记为外部文件,就像您可以通过汇总配置文件中的 external
和 output.paths
属性标记外部包一样?
json plugin 实际上为我完成了这项工作。这是我的配置文件 rollup-config.js
:
// import json from 'rollup-plugin-json'; Deprecated
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import pkg from './package.json';
export default [
// browser-friendly UMD build
{
input: './index.js',
output: {
file: '../index.js',
name: 'Hy52fWS8r', // The global (never used) name for the factory of this UMD
format: 'umd'
},
plugins: [
resolve(),
commonjs(),
json({
compact: true
})
]
}
];
源代码:
// test.json
[1, 2, 3]
// index.js
const test = require('./test.json')
console.log(test) // Array(3) [ 1, 2, 3 ]
rollup
的输出如下所示:
var test = [1,2,3];
var test = /*#__PURE__*/Object.freeze({
'default': test
});
var test = getCjsExportFromNamespace(test);
我还不知道这背后的原理是什么。您可以忽略 test
和 test
变量。
我的 node
脚本中某处需要一个 json
文件,但我 不想 将其捆绑 [=16] =].我希望它被视为 "external"。每次 json
文件更改时,脚本应该能够获取更改,而无需通过汇总重建包。目前,它在捆绑包中 "hard coded":
// ./src/index.js
const json = require('../example.json');
与 ./example.json
为:
{
"exampleValue": "X"
}
returns 捆绑包包括:
// ...
var exampleValue = "X";
var example = {
exampleValue: exampleValue
};
var example = /*#__PURE__*/Object.freeze({
exampleValue: exampleValue,
default: example
});
// ...
我试图用 rollup-plugin-json
排除它,但正如我所料,它抛出:
[!] Error: Unexpected token (Note that you need rollup-plugin-json to import JSON files)
是否可以将 json 文件标记为外部文件,就像您可以通过汇总配置文件中的 external
和 output.paths
属性标记外部包一样?
json plugin 实际上为我完成了这项工作。这是我的配置文件 rollup-config.js
:
// import json from 'rollup-plugin-json'; Deprecated
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import pkg from './package.json';
export default [
// browser-friendly UMD build
{
input: './index.js',
output: {
file: '../index.js',
name: 'Hy52fWS8r', // The global (never used) name for the factory of this UMD
format: 'umd'
},
plugins: [
resolve(),
commonjs(),
json({
compact: true
})
]
}
];
源代码:
// test.json
[1, 2, 3]
// index.js
const test = require('./test.json')
console.log(test) // Array(3) [ 1, 2, 3 ]
rollup
的输出如下所示:
var test = [1,2,3];
var test = /*#__PURE__*/Object.freeze({
'default': test
});
var test = getCjsExportFromNamespace(test);
我还不知道这背后的原理是什么。您可以忽略 test
和 test
变量。