[TW5] How to deal with window is not defined in the plugin

42 views
Skip to first unread message

LinOnetwo

unread,
May 1, 2019, 12:36:07 PM5/1/19
to tiddly...@googlegroups.com
if ($tw && $tw.browser) exports.adaptorClass = SoLiDTiddlyWikiSyncAdaptor

But when starting tiddlywiki from command line, it still throws

ReferenceError: window is not defined

LinOnetwo

unread,
May 1, 2019, 12:50:09 PM5/1/19
to TiddlyWikiDev
How can I prevent tiddlywiki run this plugin on nodejs?

在 2019年5月2日星期四 UTC+8上午12:36:07,LinOnetwo写道:

LinOnetwo

unread,
May 1, 2019, 1:02:22 PM5/1/19
to TiddlyWikiDev
This is a typical SSR issue, use following webpack config solves it:

output: {
globalObject: 'this',
},

在 2019年5月2日星期四 UTC+8上午12:36:07,LinOnetwo写道:
if ($tw && $tw.browser) exports.adaptorClass = SoLiDTiddlyWikiSyncAdaptor

LinOnetwo

unread,
May 2, 2019, 2:39:33 AM5/2/19
to TiddlyWikiDev
I finally use this webpack config:

const path = require('path');
require('dotenv').config();

module.exports = {
  devtool
: process.env.NODE_ENV === 'development' ? 'inline-source-map' : false,
  entry
: './src/index.js',
  output
: {
    filename
: 'SyncAdaptor.js',
    path
: path.resolve(__dirname, 'dist'),
    libraryTarget
: 'commonjs',
 
},
 
module: {
    rules
: [
     
{
        test
: /\.js$/,
        exclude
: /node_modules/,
       
use: {
          loader
: 'babel-loader',
       
},
     
},
   
],
 
},
  optimization
: {
    minimize
: process.env.NODE_ENV !== 'development',
 
},
  externals
: [],
};
在此输入代码...


And use a index.js to guard if it's browser:

// prevent execution on server side, or there will be ReferenceError: window is not defined raised from solid-auth-client
if (typeof window !== 'undefined') {
 
require('./SyncAdaptor');
}

Then it's fine to import browser only npm packages.



BJ

unread,
May 2, 2019, 5:56:07 AM5/2/19
to tiddly...@googlegroups.com
maybe you can put the condition around all the code:

if ($tw && $tw.browser){

function .....


etc




exports
.adaptorClass = SoLiDTiddlyWikiSyncAdaptor
}


Reply all
Reply to author
Forward
0 new messages