I have a json
file that is required somewhere in my node
script, but I do not want to have it bundled by rollup
. I want it to be treated as an "external". Every time the json
file changes, the script should be able to pick up the changes without rebuilding the bundle by rollup. Currently, it is "hard coded" in the bundle:
// ./src/index.js
const json = require('../example.json');
with ./example.json
as:
{
"exampleValue": "X"
}
returns a bundle that includes:
// ...
var exampleValue = "X";
var example = {
exampleValue: exampleValue
};
var example$1 = /*#__PURE__*/Object.freeze({
exampleValue: exampleValue,
default: example
});
// ...
I've tried to exclude it with rollup-plugin-json
, but as I could have expected, it throws:
[!] Error: Unexpected token (Note that you need rollup-plugin-json to import JSON files)
Is it possible to mark a json file as external, the same way you can mark external packages through the external
and output.paths
proeprties in the rollup configuration file?
The json plugin actually does the job for me. Here is my configuration file 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
})
]
}
];
Source code:
// test.json
[1, 2, 3]
// index.js
const test = require('./test.json')
console.log(test) // Array(3) [ 1, 2, 3 ]
The rollup
's output looks as follows:
var test = [1,2,3];
var test$1 = /*#__PURE__*/Object.freeze({
'default': test
});
var test$2 = getCjsExportFromNamespace(test$1);
I don't know yet what is the rationale behind this. You can just ignore the test$1
and test$2
variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With