Open k-fujishiro opened 5 months ago
I had a simmilar issue while trying to import jsonata
.
Good news is that I managed to make it work. Sad news is it looks horrible.
I did the following additional steps:
installing rollup/plugin-commonjs
and rollup/plugin-node-resolve
via npm.
rollup.config.mjs
[...]
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
[...]
export default [
{
input: 'src/Code.ts',
output: {
dir: 'dist',
format: 'esm',
},
plugins: [
cleanup({ comments: 'none', extensions: ['.ts'] }),
license({
banner: {
content: {
file: fileURLToPath(new URL('license-header.txt', import.meta.url)),
},
},
}),
resolve({
exportConditions: ['import'],
}),
typescript(),
commonjs(),
prettier({ parser: 'typescript' }),
],
context: 'this',
},
[...]
Code.ts:
import jsonata from 'jsonata';
[...]
package.json:
"dependencies": {
"jsonata": "^2.0.5",
"@rollup/plugin-node-resolve": "^15.2.3",
}
Unfortunately jsonata
has a const utils = require("./utils");
line. I believe this is the reasion for commonjs. However this also means that the output Code.js
file contains 9k+ lines.
If someone would be able to help and find a way to move the dependency modules to a separate file, that would be amazing.
@ocsi01
Here's something I managed to get to work: Create two outputs with Rollup, one for your Code.ts and one for an "external" jsonata lib. Since AppsScript doesn't know modules, we'll have to use some trickery.
jsonata-lib.ts
import 'jsonata';
Code.ts (example)
// import the jsonata type
import type jsonataNS from 'jsonata';
// declare jsonata as a global variable
declare global {
const jsonata: typeof jsonataNS;
}
async function run() {
const data = {
example: [{ value: 4 }, { value: 7 }, { value: 13 }],
};
// use jsonata
const expression = jsonata('$sum(example.value)');
const result = await expression.evaluate(data); // returns 24
console.log(result);
}
rollup.config.mjs
import cleanup from 'rollup-plugin-cleanup';
import license from 'rollup-plugin-license';
import prettier from 'rollup-plugin-prettier';
import typescript from 'rollup-plugin-typescript2';
import nodeResolve from '@rollup/plugin-node-resolve';
import { fileURLToPath } from 'url';
export default [
{
input: 'src/jsonata-lib.ts',
output: {
dir: 'dist',
format: 'es',
},
plugins: [
cleanup({ comments: 'none', extensions: ['.ts'] }),
nodeResolve(),
typescript(),
],
},
{
input: 'src/Code.ts',
output: {
dir: 'dist',
format: 'es',
},
plugins: [
cleanup({ comments: 'none', extensions: ['.ts'] }),
license({
banner: {
content: {
file: fileURLToPath(new URL('license-header.txt', import.meta.url)),
},
},
}),
typescript(),
prettier({ parser: 'typescript' }),
],
context: 'this',
},
];
@k-fujishiro
As @ocsi01 stated, installing and using @rollup/plugin-node-resolve
and @rollup/plugin-commonjs
should fix this issue. The first will include imported node modules in your bundle, the second will make sure that rollup can handle CommonJS imports.
I am new to make scripts in Node.js environment. I started to develope GAS application in the environment created by Apps Script in IDE (ASIDE). As I need to use the library Dayjs, I installed that and make scripts according to the official documentation. Then I got the error message "SyntaxError: Cannot use import statement outside a module". If anyone has any information that can help solve this please let me know. More information is below:
index.ts:
appsscript.json:
package.json:
tsconfig.json: